Tutorial 04: Drive a mecanum robot
In the five files, one drive(...) line in Robot.java gives you all of this: Robot.java. Build the drive by hand here with MecanumDrive to see what that line does inside.
You will build: a TeleOp that drives a mecanum robot, with slow mode, field-centric driving and reverse drive.
You need: a mecanum chassis with motors front_left, front_right, back_left, back_right, the
hub’s IMU (imu), and a block to lift the wheels off the ground.
Full program: Tut04MecanumDrive.java
1. How mecanum wheels move
Section titled “1. How mecanum wheels move”Each mecanum wheel has rollers at 45 degrees. The robot spins the four wheels in different
combinations to drive forward, sideways (“strafe”), turn, or all three at once. The library does the
math (in MecanumMixer). You only say what you want (Signs and units):
drive.drive(forward, strafeRight, turnClockwise); // each -1 to 12. Set up the drive
Section titled “2. Set up the drive”DriveConfig driveConfig = DriveConfig.mecanum();driveConfig.powerLimit = 0.6;MecanumDrive drive = new MecanumDrive(hardwareMap, driveConfig);DriveConfig holds the motor names, which motors are reversed, and the power limit. The defaults
(front_left … back_right, left side reversed) fit most goBILDA and REV chassis. For other names:
driveConfig.frontLeft = "fl";.
powerLimit = 0.6 means no wheel ever gets more than 60%. Keep it low until everything works.
3. Check the motor directions FIRST
Section titled “3. Check the motor directions FIRST”With one motor reversed wrongly, the robot spins or drifts, and nothing else makes sense. With the robot
on a block, run MotorDirectionCheck
and hold X, Y, A, B to spin one wheel each. Every wheel must roll the way that moves the robot
forward. Flip a wrong one in your config, for example driveConfig.backRightReversed = true;.
More: Motor Direction Check.
4. The basic drive loop
Section titled “4. The basic drive loop”double forward = -gamepad1.left_stick_y;double strafe = gamepad1.left_stick_x;double turn = gamepad1.right_stick_x;drive.drive(forward, strafe, turn);The minus on left_stick_y: the SDK says “up” is negative.
5. Make the sticks feel good
Section titled “5. Make the sticks feel good”double forward = StickShaping.shape(-gamepad1.left_stick_y, 0.05, 2);double strafe = StickShaping.shape(gamepad1.left_stick_x, 0.05, 2);double turn = StickShaping.shape(gamepad1.right_stick_x, 0.05, 2);StickShaping.shape(value, deadband, exponent) changes the raw stick number before it goes to the motors:
- Deadband 0.05: a stick at rest is often not at exactly 0. Without a deadband, the robot creeps.
- Exponent 2: half stick gives a quarter of the power. Small moves become precise, and a full push is still full power. Try 1 (straight) and 3 (very soft), and pick one.
6. Slow mode on a trigger
Section titled “6. Slow mode on a trigger”double speed = StickShaping.slowScale(gamepad1.left_trigger, 0.3);drive.drive(forward, strafe, turn, speed);Trigger let go = full speed. Pulled all the way = 30%. Use it to line up to score.
7. Field-centric driving
Section titled “7. Field-centric driving”With robot-centric driving, “stick up” means “where the robot faces”. When the robot faces the driver, everything feels backwards. With field-centric driving, “stick up” always means “away from the driver”. For that, the robot must know its heading (which way it faces, in degrees). The IMU, a turn sensor inside the Control Hub, measures it.
IMU imu = ImuSetup.get(hardwareMap, "imu", RevHubOrientationOnRobot.LogoFacingDirection.UP, RevHubOrientationOnRobot.UsbFacingDirection.FORWARD);...drive.driveFieldCentric(forward, strafe, turn, ImuSetup.headingDeg(imu), speed);The two directions must match how YOUR hub is mounted. Wrong values give a wrong heading, and field-centric driving goes the wrong way. How to read them and check them: How your Control Hub is mounted.
The heading is 0 where the robot pointed when the IMU started. So give the driver a button that says “forward is where the robot points now”. The driver points the robot away first:
.bindPress(Pad.G1, Btn.BACK, imu::resetYaw)imu::resetYaw is a short way to write () -> imu.resetYaw(). Yaw is the IMU’s word for heading.
In the program, Y toggles between the two styles, with a rumble to confirm.
8. Reverse drive
Section titled “8. Reverse drive”To load from the back, the driver may want the back to act as the front. ReverseDrive does that,
for forward and strafe only:
ReverseDrive reverse = new ReverseDrive();.bindPress(Pad.G1, Btn.L_STICK_BTN, () -> confirm.play(reverse.toggle()))...drive.drive(reverse.apply(forward), reverse.apply(strafe), turn, speed);reverse.toggle() flips the mode and returns the new state, so confirm.play(...) buzzes twice for ON
and once for OFF. In field-centric mode the robot’s front does not matter, so the program uses reverse
only in robot-centric mode. In finally, put drive.stop();.
Run it
Section titled “Run it”- With the wheels off the ground, push the stick up: all wheels roll forward. Then drive on the floor.
- Stick up = forward, stick right = slides right, right stick right = turns clockwise.
- Left trigger slows the robot down.
- Y (two buzzes): field-centric. Turn the robot 90 degrees and push stick up: it still goes away from you.
- Point the robot away and press BACK: it resets “forward”.
- Click the left stick (two buzzes): the back is now the front.
If something goes wrong
Section titled “If something goes wrong”| Problem | Fix |
|---|---|
| Spins when it should go straight | A motor direction is wrong. Section 3. |
| Slides diagonally when strafing | Two motor names are on the wrong wheels (for example front_left and back_left swapped). |
| Field-centric goes the wrong way, or circles | The IMU directions do not match the hub, or the driver did not reset “forward” with the robot facing away. |
| Robot creeps with sticks let go | Raise the deadband to 0.08. |
| “MecanumDrive needs a MECANUM DriveConfig” | You used DriveConfig.tank(). Use DriveConfig.mecanum() or TankDrive. |
Try this
Section titled “Try this”- Change the exponent to 3 for turning only. Check whether aiming is easier.
- Put field-centric ON by default.
- Make a “precision mode” toggle that sets a fixed
speed = 0.35, instead of the trigger.
Next: Tutorial 05: Drive a standard (tank) robot, or Tutorial 06: A fast, clear loop when you have no tank drive. Look it up: drive.md, hardware.md: ImuSetup, gamepad.md.
Something wrong on this page? Tell us with a short GitHub form.