# Programming Mecanum

Hi! I’m having trouble programming the mecanum wheels. I can make the robot move sideways and rotate but I cannot make it move forward and backward and I’m not sure why. I included the important parts of my code below.

``````Joystick joy1 = new Joystick(1);

RobotDrive drivetrain = new RobotDrive(1, 3, 2, 4);//Front Left - 1, Rear Left - 2, Front Right - 3, Rear Right - 4

double magnitude = joy1.getMagnitude();
double direction = joy1.getDirectionDegrees();
double rotation = joy1.getZ();

drivetrain.mecanumDrive_Polar(magnitude, direction, rotation);
``````

I know for certain that it isn’t an issue with the wiring. I tried using two joysticks instead of one but that didn’t work either. I’ve pretty much run out of ideas at this point and hope someone can help. Thank you!

Have you tried just using the X, Y, and Z axis instead of the Magnitude and Direction?

I have but it didn’t help.

*Put the bot up on blocks (wheels off the ground) and give it a pure forward command. Tell us what each of the four wheels do.

You could check to see if your drive motors need to be inverted. You could do this in code, or just swap red/black on one side of your robot.

Might not be the answer but another thing to try!

I think you instantiated your RobotDrive instance with the motors in the wrong order. Correct me if I’m wrong, but the order should be frontLeft, rearLeft, frontRight, rearRight. Try changing the instatiation to

``````RobotDrive drivetrain = new RobotDrive(1, 2, 3, 4);
``````

If that doesn’t work, try as others have suggested and mount the robot up on blocks to isolate the wheels. This helps tremendously in debugging a faulty drive train.

All the wheels spin inwards

That is how I instantiated it originally. When I did that, the wheels moved forward, backward and rotated but did not move side to side.

That’s why it won’t go forward of course.

What do the wheels do when you give a pure reverse command?

A pure strafe right command?

etc

When given the pure reverse command, I believe they all spin outward but the wheels work fine while shifting to the right, left and rotating so if I were to invert the motors it wouldn’t help much.

I’m pretty sure nyaculak is correct that you have the motors going into the RobotDrive object incorrectly. It should be frontLeft, rearLeft, frontRight, rearRight.

``````RobotDrive(int frontLeftMotor, int rearLeftMotor, int frontRightMotor, int rearRightMotor)
Constructor for RobotDrive with 4 motors specified with channel numbers.
``````

Code for doing Mecanum with joysticks looks like this:

``````myRobotDrive.mecanumDrive_Cartesian(joy1.getX(), joy1.getY(), joy1.getTwist());
``````

Optionally you can put the angle from a gyro in as the optional 4th parameter.

One possible problem is that some of the motors may need to be inverted depending on how they are configured. To determine that, put the robot on blocks and drive it with the joysticks and see what it is doing. You can call the

``````setInvertedMotor()
``````

method on the side that is going in the wrong direction.