user4478: 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.
e.g.
(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 left and 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?

user-1: If you want to take it to to the extreme, I always use an algorithm called Diamond Coordinates. It is described in this PDF.  It performs perfectly, and I keep a C# and Java implementation handy. I'll probably be doing a Python one soon :)


4478: <p>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.</p>

<p>e.g.</p>

<pre><code>(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
</code></pre>

<p>I thought the equation to convert these to the literal motor signals was:</p>

<pre><code>float mag = sqrt(dirX*dirX + dirY*dirY);
float left = dirX - dirY;
float right = dirX + dirY;
</code></pre>

<p>This seems to work for the first 4 cases, but fails for the last two, where it results in an un-normalized vector.</p>

<p>One potential fix would be to simple wrap <code>left</code> and <code>right</code> 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?</p>
 -1: <p>If you want to take it to to the extreme, I always use an algorithm called Diamond Coordinates. It is described in <a href="http://www.rchordata.com/files/Using%20Diamond%20Coordinates%20to%20Power%20a%20Differential%20Drive.pdf" rel="nofollow noreferrer">this</a> PDF.  It performs perfectly, and I keep a C# and Java implementation handy. I'll probably be doing a Python one soon :)</p>

<p><img src="https://i.stack.imgur.com/u9Z1H.png" alt="enter image description here"></p>
