Question about Mecanum code

I was looking at a few sets of mecanum code and was wondering about a few things.
Whats the best way to make sure there is no under/overflow in the code?
Is it necessary to have the wheels perfectly square?
i found some code that uses the distance to the XY center point of the axels to the center of the bot in the code, and that tells me that its only to tell where the axis of rotation is for Z.

Thanks for the help.


Most of the time the precise positioning of the wheels isn’t needed, but they should be reasonably close. In general if normal wheels would work then so will Mecanums.

To prevent overflow issues you can use a larger data type to store the intermediate calculation. For example if your inputs and outputs are in the range of -100 to 100, then if your variable can handle values from -300 to 300 you shouldn’t experience any overflows, and since the next step up from a char is a 16 bit integer, which holds -32768 to 32767, then you won’t experience any overflows unless you input data is incorrect.

You should also confirm that the output values are in an acceptable range.

As for exact code most of the time something like this is all you need to get a reasonable output. You can add additional logic, like a PID loop for encoders, but its not needed for a simple robot.

This code is from memory, but it should be correct.

//This is just some generic C-like syntax, but you should be able to
// use it for the old robot or the new controller assuming you use C/C++.
// If you use Labview you'll need to port the logic into that language.
Wheel names:
Front of robot
NW     NE
|       |
|       |
|       |
|       |
SW     SE

int NW, NE, SW, SE; //Variables which will hold the calculated values
int X, Y, Z; //X sideways movement, Y forward movement, Z rotation
NW = Y + X + Z;
NE = Y - X - Z;
SW = Y - X + Z;
SE = Y + X - Z;

//Check output are in a valid range here

//You can check the outputs are in range with somthing like this
//Assuming your output range is -100 to 100
if (NW < -100)
     NW = -100;
else if(NW > 100)
     NW = 100;
//Assign outputs to motors or motor control loop here