Product Introduction
But suppose for some reason you need or want the target heading to be designated as
+270 instead of -90. With the sensor pointing roughly forward, the initial heading could be
a low positive number, not converted by the above pseudocode, giving a very large error
value. Standard steering math would again sharply spin the robot around the wrong way
(clockwise). In this case it’s better to convert a low positive heading into a value greater
than 360 degrees. For example 5 degrees would become 365 degrees, and the steering
math would bring the robot around counterclockwise as desired.
So, when you use high positive values for CCW targets, here is adjustment pseudocode
for rotating CCW from zero (or any low value):
Obviously both of these methods must be adjusted if the steering might pass near or
through an absolute heading of 180 degrees. In these examples there’s nothing special
about “180”, it’s just an intermediate number to safely distinguish low positive headings
(above zero) from high positive headings (below 360), for the purpose of conversion.
Again, the programmer must know the intended driving plan. Do not rely blindly on one
method or “the math” to cover all situations. Manually check the key values.
4. Looping
Steering actions are typically placed inside a while loop, constantly retrieving the sensor's
heading values. When the loop's conditional expression is no longer true, the loop ends.
For turning, the loop's condition might be “sensor’s actual heading hasn't yet reached
target heading”.
How should you define “reached”? Use greater-than or less-than, as applicable. Look
for a threshold, not an exact match. Beginning programmers often loop “while
currentValue is not equal to ( !=) targetValue”. Robot sensors are famously jumpy and
cycle times are unpredictable, so the gyro sensor could easily skip over the exact target
value the loop continues, yielding a funny spinning robot for YouTube.
Here is simple pseudocode for a steering loop.