View Single Post
  #7   Spotlight this post!  
Unread 01-15-2015, 11:03 AM
Joe Wilson Joe Wilson is offline
Mentor & FRC West Board Member
FRC #4627 (Manning Robotics)
Team Role: Mentor
 
Join Date: Dec 2014
Rookie Year: 2014
Location: Calgary, Alberta
Posts: 17
Joe Wilson is an unknown quantity at this point
Re: Accessing Joystick Axes in Command-Based Robot

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):

Code:
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:

Code:
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:

Code:
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:

Code:
motor1.set(-speed) //just reverse the direction by adding a -

Last edited by Joe Wilson : 01-15-2015 at 11:08 AM.
Reply With Quote