Download C/C++ Programming Guide for the FIRST Robotics Competition

Transcript
Controlling Motors
Servo
Servo
The Servo class supports the Hitechnic servos supplied by FIRST. They have a 20ms update rate and are
controlled by PWM output signals.
The input values for the Servo range from 0.0 to 1.0 for full rotation in one direction to full rotation in the
opposite direction. There is also a method to set the servo angle based on the (currently) fixed minimum
and maximum angle values.
Example 1
The following code fragment rotates a servo through its full range in 10 steps:
Servo servo(3);
// create a servo on PWM port 3 on the first module
float servoRange = servo.GetMaxAngle() - servo.GetMinAngle();
for (float angle = servo.GetMinAngle();
angle < servo.GetMaxAngle();
angle += servoRange / 10.0)
{
servo.SetAngle(angle);
Wait(1.0);
}
// step through range of angles
// set servo to angle
// wait 1 second
Example 2
The following code fragment pans a servo back and forth every 3 seconds
#include “BaeUtilities.h”
panInit();
// optional parameters can adjust pan speed
bool targetFound = false;
while(!targetFound) {
panForTarget(servo, 0.0);
// code to identify target
}
Revision 0.5 – 28 December 2008
// sinStart from -1 to +1
63