Chapter 8: Robot Control with Distance Detection
· Page 281
The left servo is different because Kp for that system is -35
pulseLeft = 2 - distanceLeft * (-35) + 750
Since the values -35, 35, 2, and 750 all have names, it’s definitely a good place for some
constant declarations.
Kpl CON -35
Kpr CON 35
SetPoint CON 2
CenterPulse CON 750
With these constant declarations in the program, you can use the name
Kpl
in place of -
35,
Kpr
in place of 35,
SetPoint
in place of 2, and
CenterPulse
in place of 750. After
these constant declarations, the proportional control calculations now look like this:
pulseLeft = SetPoint - distanceLeft * Kpl + CenterPulse
pulseRight = SetPoint - distanceRight * Kpr + CenterPulse
The convenient thing about declaring constants for these values is that you can change
them in one place, at the beginning of the program. The changes you make at the
beginning of the program will be reflected everywhere these constants are used. For
example, by changing the
Kpl CON
directive from -35 to -40, every instance of
Kpl
in the
entire program changes from -35 to -40. This is exceedingly useful for experimenting
with and tuning the right and left proportional control loops.
Example Program – FollowingBoeBot.bs2
FollowingBoeBot.bs2 repeats the proportional control loop just discussed with every
servo pulse. In other words, before each pulse, the distance is measured and the error
signal is determined. Then the error is multiplied by Kp, and the resulting value is
added/subtracted to/from the pulse widths that are sent to the left/right servos.
√
Enter, save, and run FollowingBoeBot.bs2.
√
Point the Boe-Bot at an 8 ½
×
11” sheet of paper held in front of it as though it’s
a wall-obstacle. The Boe-Bot should maintain a fixed distance between itself
and the sheet of paper.
√
Try rotating the sheet of paper slightly. The Boe-Bot should rotate with it.
√
Try using the sheet of paper to lead the Boe-Bot around. The Boe-Bot should
follow it.