So I’ve got a Turtle under my command, but the commands are all in steps of the motor – such as “move fwd 100” or “turn acw 37”. I’d prefer the units to be meaningful approximations to reasonal units like millimeters or degrees, so that needs some basic maths.
The distance between the wheels is 160mm – let’s call it D. The diameter of the wheel is 82mm – let’s call it d. The motors performs 1 full rotation (360 degrees) in 200 steps.
So forward and backward are relatively simple math(s): distance travelled in millimeters, m = (n x π x d) / 200. So to travel m millimeters, we need n = m x 200 / (π x d) steps.
Turning on it’s central axis is slightly trickier: n steps travel a distance of (n x π x d) / 200 as above. But now this represents a partial arc of the circle the turtle is turning in – lets call that angle θ (theta). So (θ x π x D) / 360 = (n x π x d) / 200. Cancelling the π’s leads to (θ x D) / 360 = (n x d) / 200. Or for a given angle to turn, the number of steps is (θ x D x 200 / (d x 360)).
Finally, I also have a “sweep” action where one wheel is locked, and only the other moves. This effectively doubles D as now the radius of the circle is D rather than the diameter. So the steps swept to move through an angle theta is now (θ x 2 x D x 200 / (d x 360)).
Having got the number of steps, all that’s left to do is to convert them from floating point to integers allowing for rounding errors. I’ll post the updated code containing these changes, and changes related to the next post…