Product Introduction

A shorter solution uses a do...while loop, which always executes at least once. Thus the
conversion code doesn't need to appear both outside and inside the loop.
6. Proportional Steering
As for the driving code itself, a simple method is proportional steering. Proportional means
that the amount of steering varies (linearly) in proportion to the amount of error:
1. calculate error (deviation from target)
2. use error and gain to adjust a preselected 'middle' power level
3. run the motors, each adjusted, to steer
Here is sample pseudocode for steering while driving forward. If you once learned about
line following in FIRST LEGO League, this might look familiar!
If midPower is zero, the robot can spin in place (if it’s capable) to the desired heading.
To get a sense of how this works, manually step through the code with sample values.
The beauty of proportional code is that it automatically steers left or right as needed. If the
error becomes negative (overshoot the target), the “positive” adjustment to leftPower
becomes negative, vice versa for rightPower, and the robot steers the other way. Magic!
Through testing, gain should be selected to use the full desired range of motor power, and
for steering response. A good choice of gain allows a driving robot to settle quickly into
the desired heading and maintain near-zero error. When you manually step through the
math for a few scenarios including the start, pick a rough initial value for gain, and fine-
tune it from there.
Gain is sometimes called a proportional constant or scaling factor, since it also serves to
“fit” the converted values to the available range of motor power. Some motor controllers
look for values between 0 and 1, other motor controllers want values between 0 and 100.