# How to drive mecanum wheels robot: code or algorithm

I have a robot with four mecanum wheels and I would like to make it move correctly by using a RC transmitter (my controller has two sticks and several buttons, each stick has a proportional range of [1000 - 1500 - 2000] where 1500 is the center position).

I've read different documents about how to control a robot with mecanum wheels, but I wasn't able to find any suitable example to use as starting point.

Basically, I would like to understand what kind of algorithm I need to use to set the velocity and the direction on each wheel depending on the stick position.

The motor controllers I'm using for my robot use a proprietary language, so I do not need any code, I just need to know how it should work and then I'll translate it to make it work with my electronics.

I hope you can help me!

EDIT: I was thinking something like this:

left stick UP = forward
left stick DOWN  = backward
left stick RIGHT [UP/DOWN=0]= SLIDE left
left stick RIGHT [UP/DOWN=0] = SLIDE right
left stick UP/RIGHT = diagonal and so on..
right stick RIGHT = turn on RIGHT
right stick LEFT = turn on left


my joystick has a left stick with two axes and a right stick with two other axis. Both stick are proportional.

EDIT2:

The figure shows the directions I would like to implements for my robot. In order to keep things simply, we can not consider the turning on rear axis and on a single wheel.

My motor controllers move each motor by using motor_command function which ranges from 0 to +/-1000, where 0 means motors stop and +1000 means motors at 100% forward while -1000 means motors at 100% backward. • first, you need to decide how the stick position should affect the bot ... or the inverse, which is how should the sticks be moved in order to achieve a particular result Jan 30, 2020 at 20:42
• I edit my first post by adding the stick behavior Jan 30, 2020 at 21:55
• next, add the motor direction to the list ... forget about the diagonals for now .... that should be enough information to write a working sketch to get you started with experiments Jan 30, 2020 at 23:00
• I added another edit on my first post with a figure and some other considerations. Is there any math consideration that I can use in order to control the robot both by the RC transmitter and by code parameters? Jan 31, 2020 at 16:45

I've built one and added the code here: https://github.com/charlch/MecanumBot/blob/master/MecanumBot.ino

The core of the maths is this:

Firstly turn the 1000 to 2000 input into -1.0 to 1.0

float norm(int in) {
return (float(in-1500.0)) / 500.0;
}

float forward = norm(forward_raw);
float strafe = norm(strafe_raw);
float turn = norm(turn_raw);


Then calculate the speed for each wheel and set the motor to that speed:

float m1 = (forward+strafe+turn)/3.0;
float m2 = (forward-strafe-turn)/3.0;
float m3 = (forward-strafe+turn)/3.0;
float m4 = (forward+strafe-turn)/3.0;

motor1.set_signal(m1);
motor2.set_signal(m2);
motor3.set_signal(m3);
motor4.set_signal(m4);