Data Sheet
Copyright © Parallax Inc. Parallax Feedback 360° High-Speed Servo (#900-00360) v1.2 9/12/2017 Page 5 of 7
This code allows you to enter angles into the terminal, and displays the servo’s progress as it turns to its
target position.
while(1)
{
print("Enter angle: ");
scan("%d", &targetAngle);
print("\r");
while(abs(targetAngle - angle) > 4)
{
print("targetAngle = %d, angle = %d\r", targetAngle, angle);
pause(50);
}
}
P
osition detection
The code from the feedback360 function needs to be repeated rapidly, at least every ¼ turn, but
between every servo pulse (50 Hz) is recommended.
This requires high and low pulse measurements for a duty cycle calculation. If your code skips a pulse
between high and low, it also needs to check and make sure the cycle time is in the correct window.
Reason being, if the high and low pulses are sampled before and after a moving servo crosses the 359 to
0 degree boundary, it might have a long high pulse for a high angle, and an almost equally long low
pulse for a low angle, resulting in incorrect 180 degree calculation.
int tCycle = 0;
while(1) /
/ Repeat cycle time valid
{
tHigh = pulse_in(pinFeedback, 1); // Measure high pulse
tLow = pulse_in(pinFeedback, 0); // Measure low pulse
tCycle = tHigh + tLow; // Calculate cycle time
if((tCycle > 1000) && (tCycle < 1200)) break; // Cycle time valid? Break!
}
dc = (dutyScale * tHigh) / tCycle; // Calculate duty cycle
N
ext, calculate the angle theta. The unitsFC variable has a value like 360, for the number of units in
a full circle The dcMin and dcMax values are 29 and 971. This calculation returns 0 to 359 depending
on the measured duty cycle. Note that this uses (unitsFC - 1) - (the clockwise angle calculation). This
gives us a positive counterclockwise angle to correspond with clockwise rotation being positive
theta = (unitsFC
- 1) - ((dc - dcMin) * unitsFC) / (dcMax - dcMin + 1);
I
n case the pulse measurements are a little too large or small, let’s clamp the angle in the valid range.
if(theta
< 0) theta = 0;
else if(theta > (unitsFC - 1)) theta = unitsFC - 1;
S
ince we might be looking at positive or negative measurements above +/- 360 degrees, this keeps track
of the number of full turns. q2Min is an abbreviation for quadrant 2 minimum, which is 90 degrees, and
q3Max (quadrant 3 maximum) is 270 degrees. So, if the previous theta measurement was in the 1st
quadrant, and the current theta measurement is in the 4th quadrant, we know we the angle
transitioned from a high value to a low value, so increase the turns count. Conversely, if the previous