WPILib resources:
Screensteps
API Reference (Java)
API Reference (C++)
Using the command based architecture, you need to first create a subsystem that the motor belongs to
Code:
public class MyMotorSubsystem extends Subsystem {
CANSomething myMotor;
public MyMotorSubsystem {
myMotor = new CANSomething(RobotMap.CAN_ID_MY_MOTOR);
}
public void setMotor(double value) {
myMotor.set(value);
}
}
Then you need a 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 run() {
myMotorSubsystem->setMotor(1.0);
}
void isFinished() {
return runMotorButton.get();
}
void end() {
myMotorSubsystem->setMotor(0.0);
}
void interrupted() {
myMotorSubsystem->setMotor(0.0);
}
}
Then you need to create an instance of the command
Code:
// somewhere in OI.java
new RunMotorCommand(new JoystickButton(myJoystick, something));