Page 280 · Robotics with the Boe-Bot
System
Error = -2
Kp X error
-35 X -2
Center pulse width
750
Output
adjust
+70
Left servo
output
820
Measured left
distance = 4
-
++
+
Figure 8-5
Proportional
Control Block
Diagram for
Left Servo and
IR LED and
Detector Pair
Programming the Boe-Bot Shadow Vehicle
Remember that the equation for the right servo’s output was:
Right servo output = (Right distance set point – Measured right distance)
×
Kp
+ Center pulse width
Here is an example of solving this same equation in PBASIC. The right distance set
point is 2, the measured distance is a variable named
distanceRight that will store the
IR distance measurement, Kp is 35, and the center pulse width is 750:
pulseRight = 2 - distanceRight * 35 + 750
Remember that in PBASIC math expressions are executed from left to right. First,
distanceRight is subtracted from 2. The result of this subtraction is then multiplied by
Kpr, and after that, the product is added to the center pulse width.
You can use parentheses to force a calculation that is further to the right in a line of
PBASIC code to be completed first. Recall this example: you can rewrite this line of
PBASIC code:
pulseRight = 2 - distanceRight * 35 + 750
like this:
pulseRight = 35 * (2 – distanceRight) + 750
In this expression, 35 is multiplied by the result of (2 – distanceRight), then the product is
added to 750.