Log in

View Full Version : Mecanum drive code


JackOfAllTrade5
25-05-2014, 02:13
Hey guys our team just got mecanum wheels but I have no idea how to write the code for these... We use java and have two sets of joysticks we can use. One set has a z axis and the other doesn't so whatever is easier. Any help is appreciated! Thanks in advance.

NWChen
25-05-2014, 10:58
WPILibJ currently supports two mecanum control methods. They are methods of RobotDrive (http://team2168.org/javadoc/) (thanks to 2168 for the API mirror).

If you have a gyro on your robot, you can try field-centric (cartesian) control. This means that, regardless of orientation, your robot will move in the direction that your joystick points. For field-centric control, you can call the mecanumDrive_Cartesian method of RobotDrive:

RobotDrive drive;
Joystick joystick;
Gyro gyro;
...
//assign drive, joystick, and gyro to their respective ports
...
drive.mecanumDrive_Cartesian(joystick.getX(), joystick.getY(), joystick.getTwist(), gyro.getAngle());


Alternatively, if your robot has no gyro, you can use robot-centric (polar) control. This means that your robot will move in the direction that your joystick points, relative to itself. For robot-centric control, call the mecanumDrive_Polar method of RobotDrive:

drive.mecanumDrive_Polar(joystick.getMagnitude(), joystick.getDirectionDegrees(), joystick.getTwist());


I would recommend you look more at the above link to see what you can do for mecanum control. Note that both RobotDrive methods here rely upon having a joystick with a z-axis, where the z-axis controls the rotation/turning of the robot (and is used by the getTwist() method).