Thread: Java help
View Single Post
  #3   Spotlight this post!  
Unread 17-10-2016, 00:35
Lireal Lireal is offline
Registered User
AKA: Alex Colello
FRC #2141 (Spartonics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2013
Location: Concord, California
Posts: 99
Lireal has a spectacular aura aboutLireal has a spectacular aura aboutLireal has a spectacular aura about
Re: Java help

Here is what the above would look like in Java:

Code:
public class MyMotorSubsystem extends Subsystem {
  ControllerType myMotor;
  public MyMotorSubsystem () {
    myMotor = new ControllerType(RobotMap.ID_MY_MOTOR);
  }
  public void setMotor(double value) {
    myMotor.set(value);
  }
}
Command to run the motor.
Code:
public class CommandBase extends Command {
  public static MyMotorSubsystem myMotorSubsystem;
  public static void init() {
    myMotorSubsystem = new MyMotorSubsystem();
  }
}

public class RunMotorCommand extends CommandBase {
  JoystickButton runMotorButton;
  public RunMotorCommand(JoystickButton runMotorButton) {
    this.runMotorButton = runMotorButton;
    runMotorButton.whenPressed(this);
  }
  void initialize() {
    requires(myMotorSubsystem);
  }
  void execute() {
    myMotorSubsystem.setMotor(1.0);
  }
  boolean isFinished() {
    return !runMotorButton.get();
  }
  void end() {
    myMotorSubsystem.setMotor(0.0);
  }
  void interrupted() {
    myMotorSubsystem.setMotor(0.0);
  }
}
Create an instance of the command
Code:
// somewhere in OI.java
new RunMotorCommand(new JoystickButton(myJoystick, something));
My team does not use the CommandBase, and we usually instantiate subsystems in the Robot.java file, so I am not 100% sure this is right, but the basic idea should be apparent. Basically, create a command that in the execute will call a method from a subsystem that sets the motor to the value you want, which is triggered by a button and will end when that button isn't pressed.
Reply With Quote