Servo Software Interface
Introduction
The Servo Software Interface is a set of functions designed to easily control the hardware interface SER8 and SER16. Look at Servo Interface page for circuit details.
S.Ha.R.K. Directories and Options
Library Main Directory = shark/ports/servo
Library Include File = "servo.h" (shark/ports/servo/include/servo.h)
SHARKOPT for demo makefile = __SERVO__
Example Demo = demos/servo
Compiling Options
Edit shark/ports/servo/servo.c
#define SERVO_DEBUG
Enable the servo debug functions. All the sent/received bytes will be print to console.
#define SERVO_TIMEOUT_EVENT
Enable the timeout of servo commands. Without a timeout event, if the hardware interface is not present or powered, the control functions will cause a system block waiting the interface acknowledge. With the timeout events, if the acknowledge is not recevied until a specific time (SERVO_TIMEOUT), the servo command function will return an error. Usign this option the system will be more reliable, but all the servo functions will be slower.
Functions Description
int servo_open(int PORT,int SPEED)
Open the serial connection with interface circuit, usign a specific PORT and SPEED. Port value can be COM1,COM2,COM3,COM4. The default speed value is 19200 bps. If you want to change this value, the interface default speed must be set before with servo_set_RS232_baudrate
Return 0 if success.
int servo_close(int PORT)
Close the serial connection.
Return 0 if success
int servo_set_RS232_baudrate(int PORT, int BAUD)
Set the interface serial port speed. PORT is the serial port used to communicate to the interface, BAUD is the new value to use. If you change the interdace port speed, you must change the speed in the control computer port too.
Example
//Open the serial port with default speed
servo_open(COM1,19200);
//Set the new port speed
servo_set_RS232_baudrate(COM1,115200);
//Close the port
servo_close(COM1);
//Open with the new speed value
servo_open(COM1,115200);
Return 0 if success
int servo_get_RS232_baudrate(int PORT)
Get the actual port speed of the interface circuit. PORT is the serial port used to communicate to the interface
Return 0 if success
int servo_store_RS232_baudrate(int PORT)
Store the actual interface port speed inside the PIC EEPROM. After reset, the new value will be considered as default.
Return 0 if success
int servo_set_period(int PORT, int PERIOD)
Set the new period of servo control signal. As default the period is set to 20 ms, but for some servos this value should be changed. PERIOD is expressed in microseconds.
Return 0 if success
int servo_get_period(int PORT)
Get the actual period of servo control signal.
Return the period value in microseconds if success, -1 if the command fails
int servo_store_period(int PORT)
Store the actual period of servo control signal inside the PIC EEPROM. After reset, this value will be considered as the default.
Return 0 if success
int servo_get_setup_switch(int PORT)
Get the setup switch status in the interface circuit.
Return the setup switch status as LSB. -1 if the command fails
int servo_set_RC5_switch(int PORT, int DATA)
Set the RC5 pin of the pic as specified inside DATA LSB. This pin can be used as an optional control pin.
Return 0 if success.
int servo_turn_off(int PORT, int SERVO)
Turn off one servo. SERVO is the servo number (0-15 for SER16 or 0/7 for SER8).
Return 0 if success.
int servo_turn_on(int PORT, int SERVO)
Turn on one servo. SERVO is the servo number (0-15 for SER16 or 0/7 for SER8).
Return 0 if success.
int servo_turn_off_all(int PORT)
Turn off all the servos.
Return 0 if success.
int servo_turn_on_all(int PORT)
Turn on all the servos.
Return 0 if success.
int servo_set_levels(int PORT, int BANK, int MASK)
Set the turn off/on mask. BANK is the bank number. Bank 0 is the group of servo from 0 to 7, Bank 1 is the group from 8 to 15. MASK specifies what servo is enable or disable.
Example
//Enable servos 0,1,2,3 and disable servos 4,5,6,7
servo_set_levels(COM1, 0, 0x0F);
//Enable servos 14,15 and disable the others
servo_set_levels(COM1, 1, 0xC0);
Return 0 if success.
int servo_get_levels(int PORT, int BANK)
Get the mask of active servos. If BANK=0 get the mask for servos 0-7, if BANK=1 get the mask for servos 8-15.
Return 0 if success.
int servo_store_levels(int PORT)
Store the servos masks (BANK 0 and BANK 1) inside the PIC EEPROM. When the circuit will be reset, the default active servos will be the ones specified by the stored masks.
Return 0 if success.
int servo_set_max_angle(int PORT, int SERVO, int ANGLE_SEC)
Set the SERVO max angle (ANGLE_SEC). ANGLE_SEC is expressed in seconds. The default value is 324000, which means 90 degrees.
Return 0 if success.
int servo_set_min_angle(int PORT, int SERVO, int ANGLE_SEC)
Set the SERVO min angle (ANGLE_SEC). ANGLE_SEC is expressed in seconds. The default value is -324000, which means -90 degrees.
Return 0 if success.
int servo_set_servo_tick(int PORT, int SERVO, int ZERO_TICK, int DELTA_TICK)
Set the SERVO internal parameters. Ticks are the units used by PIC to generate the servo control signal. These are hardware parameters that usually should not be changed. ZERO_TICK is the default tick for servo zero position, DELTA_TICK is the number of ticks inside the signal period
The formula used to calculate the angle_tick from angle_sec is
angle_tick = (servo_table[port][servo].zero_tick + angle_sec * servo_table[port][servo].delta_tick / (servo_table[port][servo].max_angle_sec - servo_table[port][servo].min_angle_sec)) * 1000 / TICK_LEN;
Return 0 if success.
int servo_set_angle_sec(int PORT, int SERVO, int ANGLE_SEC)
Set the SERVO actual position. If the servo is enable, the servo will move to the new specified angle. ANGLE_SEC is the angle expressed in seconds. The macro
/* Convert angle (degree, minute, second -> second) */
#define ANGLE2SEC(deg,min,sec) ((deg)*3600 + (min)*60 + (sec))
can convert an angle value from (degree,minute,seconds) to seconds.
Examples
//Set the angle of servo 4 to 45 degrees
servo_set_angle_sec(COM1,4,ANGLE2SEC(45,0,0));
Return 0 if success.
int servo_get_angle_sec(int PORT, int SERVO)
Get the SERVO actual angle value.
Return the angle value in seconds, -1 if the command fails.
int servo_store_default_position(int PORT, int SERVO)
Store the actual servo angle as default value inside the PIC EEPROM.
Return 0 if success.
int servo_get_analog(int PORT, int AD_PORT)
Get the A/D sampled value from AP_PORT PIC pin. If the interface circuit is SER8 (PIC 16F876), AD_PORT can vary from 0 to 5. With SER16 (PIC 16F877), the AD_PORT value can vary from 0 to 7.
Return the sampled value, -1 if command fails.