Product Introduction

2. Target Headings
Autonomous steering is typically done by comparing the current heading to a target
heading. You calculate the difference, or error, and command the motors accordingly.
When choosing a target heading, you must consider the type of heading value (basic vs.
signed). Suppose you are working with signed values, and you want the robot to turn 90
degrees to the left. This steering destination could be represented by either a target of
270 degrees or a target of -90 degrees. Which one to use? If the current heading near
zero is compared to +270, the error is very large, and standard steering math would
sharply spin the robot around the wrong way (clockwise). You’ll see that math later in
Section 6. So, here you would use -90 as the target.
If you are working with basic heading values, the same caution applies plus the math is
complicated by the “jump” that happens around zero. This is the topic of Section 3, which
can be skipped if you are not using basic values.
A lesson here is that the programmer must know the intended driving plan. Do not rely
blindly on one method or “the math” to cover all situations. Manually step through sample
scenarios, including the anticipated extremes and values near zero. This valuable
exercise is described later in Section 12.
3. Basic Heading Conversion
If you prefer using basic heading values, they may need to be converted before use. If
you are using signed heading values, skip this section.
As noted above, CCW rotation from zero immediately jumps to 359 degrees, then 358,
and so on. This complicates the math used to calculate deviation from a target heading.
A common solution is to convert this high angle to a small negative angle, by subtracting
360 degrees from large sensor headings. Thus 350 degrees, for example, becomes -10
degrees. This -10 degrees can flow seamlessly to, say, +10 degrees as the sensor
rotates CW, with no "jump" at zero. This works because the 360 is subtracted only from
high values, not low values.
When choosing a target heading, the conversion method must be considered. Look at the
example from Section 2. With the sensor pointing roughly forward, the initial heading
value could be a high positive number, converted to a low negative number. If compared
to +270, the error value is very large; standard steering math would sharply spin the robot
around the wrong way (clockwise).
So, when you use negative values for CCW targets, here is adjustment pseudocode for
rotating CW or CCW from zero (or any low value):