Servo
Syntax
Servo Pin, Rotation Value
Overview
Control a remote control type servo motor.
Parameters
Pin is a Port.Pin constant that specifies the I/O pin for the attachment of the motor's control terminal. Rotation Value is a 16-bit (0-65535) constant or Word variable that dictates the position of the motor. A value of approx 500 being a rotation to the farthest position in a direction and approx 2500 being the farthest rotation in the opposite direction. A value of 1500 would normally centre the servo but this depends on the motor type.
Example
' Control a servo motor attached to pin 3 of PORTA
Device = 16F628
' We'll use a 14-bit core device
Declare Xtal = 20 ' Tell the compiler the device will be operating at 20MHz
Dim wPos as Word
' Servo Position
Symbol MyPin = PORTA.3
' Alias the servo pin
Cls
' Clear the LCD
wPos = 1500
' Centre the servo
PORTA = 0
' PORTA lines low to read buttons
TRISA = 0b00000111
' Enable the button pins as inputs
'
' Check any button pressed to move servo
'
Do
If PORTA.0 = 0 And wPos < 3000 Then wPos = wPos + 1 ' Move servo left
If PORTA.1 = 0 Then wPos = 1500
' Centre servo
If PORTA.2 = 0 And wPos > 0 Then wPos = wPos - 1 ' Move servo right
Servo MyPin, wPos
DelayMs 5
' Servo update rate
Print At 1, 1, "Position=", Dec wPos, " "
Loop
Notes
Servos of the sort used in radio-controlled models are finding increasing applications in this robotics age we live in. They simplify the job of moving objects in the real world by eliminating much of the mechanical design. For a given signal input, you get a predictable amount of motion as an output.
To enable a servo to move it must be connected to a 5 Volt power supply capable of delivering an ampere or more of peak current. It then needs to be supplied with a positioning signal. The signal is normally a 5 Volt, positive-going pulse between 1 and 2 milliseconds (ms) long, repeated approximately 50 times per second.
The width of the pulse determines the position of the servo. Since a servo's travel can vary from model to model, there is not a definite correspondence between a given pulse width and a particular servo angle, however most servos will move to the centre of their travel when receiving 1.5ms pulses. Servos are closed-loop devices. This means that they are constantly comparing their commanded position (proportional to the pulse width) to their actual position (proportional to the resistance of an internal potentiometer mechanically linked to the shaft). If there is more than a small difference between the two, the servo's electronics will turn on the motor to eliminate the error. In addition to moving in response to changing input signals, this active error correction means that servos will resist mechanical forces that try to move them away from a commanded position. When the servo is unpowered or not receiving positioning pulses, the output shaft may be easily turned by hand. However, when the servo is powered and receiving signals, it won't move from its position.
Driving servos with Positron8 is extremely easy. The Servo command generates a pulse in 1microsecond (µs) units, so the following code would command a servo to its centred position and hold it there: -
Do
Servo PORTA.0, 1500
DelayMs 20
Loop
The 20ms delay ensures that the program sends the pulse at the standard 50 pulse-per-second rate. However, this may be lengthened or shortened depending on individual motor characteristics.
The Servo command is oscillator independent and will always produce 1us pulses regardless of the crystal frequency used.
See also : PulseOut.