EDIT:: I typed this out not realizing this was in the C++ forum, not Java, but it should be basically the same process, just with C++ syntax, etc. Sorry for the confusion!
Here’s how we do it:
in your OI class, create a couple public methods for accessing axes (I’m assuming you have created a Joystick object, named controller here):
public double getLeftStick() {
return controller.getRawAxis(1); //look up what the actual axis number is
}
and another one for the right axis.
In your subsystem class, you should have methods for controlling each motor given a value. These methods will be called by commands, so you don’t need to get the axis value here. These methods simply describe the kinds of things the subsystem is able to do. For example:
public void driveLeftMotor(double speed) {
motor1.set(speed);
}
And probably another almost identical method for driveRightMotor
Now, in the current version of the command-based robot template, the instance of OI, and the instances of each subsystem are created in the main Robot.java class. So make sure an instance of your subsystem is created here.
Almost there, now we just need a Command that actually calls these methods with the joystick axis values as parameters. Make a new Command class. Make sure it “requires(Robot.your_subsystem_instance)” (again, make sure Robot creates an instance, and you will need to import Robot.java).
Then edit execute() to:
public void execute() {
your_subsystem_name.driveLeftMotor(Robot.oi.getLeftStick());
your_subsystem_name.driveRightMotor(Robot.oi.getRightStick());
}
Now just set the default command in the subsystem class to be this new command and you’re all set!
**NOTE, if your motors are pointing in opposite directions, they will spin in opposite directions when given the same input from the joystick. You might need to change ONE of the subsystem methods to read:
motor1.set(-speed) //just reverse the direction by adding a -