Product Introduction
You might think the condition should always read “while current heading less than target
heading”. This works for CW rotation from zero towards a positive target. But what if the
target is -90 degrees? A current heading near zero is not less than -90, the conditional is
false, and the while loop will not run at all. A skipped loop can be hard to notice and
frustrating to debug.
So, pay attention to the choice of greater-than or less-than. It depends on your target
heading (e.g. -90 or +270) and your estimated current heading. Again, plan each intended
driving action, have some idea of its physical scenario, and test key values. Don’t blindly
copy-and-paste your steering code throughout the program.
You might have the bright idea of using the absolute-value function, to help ‘standardize’
your steering code. Certainly you can find examples of this online. But this is less
intuitive, complicates the debugging, and anyway will not be a true universal solution.
Stick with the actual numbers, and have a simple plan for each action.
5. Looping with Basic Headings
If you prefer using basic heading values, they should be converted before looping. If you
are using signed heading values, skip this section.
With looping, basic headings pose a special risk. If the sensor value hasn't yet been
converted, the conditional might already be false, and the while loop won't run at all. For
example when turning CW, the condition might be "heading less than 90". An near-zero
initial heading of 358 would be false, and the loop is skipped. Likewise for turning CCW,
the condition might be "heading greater than 270"; an initial heading of 2 would be false.
A very crude solution might be to first steer in the intended direction for a time duration
estimated to force the heading above 359 or below 0 as needed. A better solution is to
perform a one-time conversion before the loop, and have the loop test the converted
value.