I'm trying to control an Arduino-powered motorized toy with a differential drive. However, I want to code it the motor movements with a direction vector.
(1,0) = forward (-1,0) = backward (0,1) = turn left, pivoting on center axis (0,-1) = turn right, pivoting on center axis (1,1) = forward-left, pivoting on left wheel (1,-1) = forward-left, pivoting on right wheel
I thought the equation to convert these to the literal motor signals was:
float mag = sqrt(dirX*dirX + dirY*dirY); float left = dirX - dirY; float right = dirX + dirY;
This seems to work for the first 4 cases, but fails for the last two, where it results in an un-normalized vector.
One potential fix would be to simple wrap
right in min() to stop it from going over 1.0, but that feels like a hack, and would possibly lose resolution. Is there a more elegant fix?