Servo Library  v0.8
Servo motor control library for educational and hobby applications
Functions
servo.h File Reference

Control servos in other cog. Currently up to 7, but more should be coming soon.

Currently supports LMM and CMM memory models. More...

#include "simpletools.h"

Go to the source code of this file.

Functions

int servo_angle (int pin, int degreeTenths)
 Set Parallax Standard Servo to angle from 0 to 180 in tenths of a degree.
int servo_speed (int pin, int speed)
 Set Parallax Continous Rotation Servo speed.
int servo_set (int pin, int time)
 Sets servo control signal to servo connected to a given pin.
int servo_setramp (int pin, int stepSize)
 Set the maximum change in control signal a servo will change in a given 20 ms time period.
int servo_get (int pin)
 Reports the number of microseconds of the pulse most recently sent to a given servo.
void servo_stop (void)
 Stops the servo process and frees a cog.
int servo_start (void)
 Starts the servo process and takes over a cog.

Detailed Description

Control servos in other cog. Currently up to 7, but more should be coming soon.

Currently supports LMM and CMM memory models.

Author
Andy Lindsay
Version
dev001

This library automatically delivers control pulses to the servos every 20 ms, all you have to do is use the set method to pick the pin the servo is connected to and the duration of the control pulses.

Parallax standard servos have a position range from approximately 0 to 180 degrees. For 0 degrees, use control pulses of

Please submit bug reports, suggestions, and improvements to this code to edito.nosp@m.r@pa.nosp@m.ralla.nosp@m.x.co.nosp@m.m.

Function Documentation

int servo_angle ( int  pin,
int  degreeTenths 
)

Set Parallax Standard Servo to angle from 0 to 180 in tenths of a degree.

Examples:

servoAngle(pin, 0); // for 0 degrees
servoAngle(pin, 900); // for 90 degrees
servoAngle(pin, 1800); // for 180 degrees

0 to 1800 corresponds to control pulses ranging from 500 to 2300 with 1400 at center (90 degrees), which is slightly different from the 1500 center convention, but it places the servo's range of motion neatly between its mechanical limits.

Parameters
pinNumber of the I/O pin connected to servo
degreeAngleTenths of a degree from 0 to 1800
Returns
1 if successful, 0 if no cogs or 1 if no locks.
int servo_get ( int  pin)

Reports the number of microseconds of the pulse most recently sent to a given servo.

Parameters
pinNumber of the I/O pin the servo is connected to
Returns
tp[i] The value stored by the array element that keeps duration of the most recent pulse sent to the servo. (Or -1 if an entry is not found for the pin argument.
int servo_set ( int  pin,
int  time 
)

Sets servo control signal to servo connected to a given pin.

This function directly controls the number of microseconds servo control pulses last.

Examples:

servoSet(pin, 500); // 0 degrees
servoSet(pin, 1400); // 90 degrees
servoSet(pin, 2300); // 180 degrees

500, 1400, and 2300 are the number of microseconds (us) the control pulses stay high. More generally, a microsecond above 500 corresponds to 1/10 of a degree clockwise of 0 degrees. So, for a given degree position, use:

degree position = 500 + (number of 10ths of a degree)

For setting continuous rotation servo speed, use:

  • Full speed clockwise -> servoSet(pin, 1400);
  • Stay still -> servoSet(pin, 1500);
  • Full speed counterclockwise -> servoSet(pin, 1600);

Values in the 1400 to 1600 range are (roughly) proportional to speed, but it's not nearly as precise as position control. Setting full speeds at 1300 and 1700 is a common practice to make sure both servos are going as fast as they possibly can.

Parameters
pinNumber of the I/O pin that sends signals to the servo
Returns
.
int servo_setramp ( int  pin,
int  stepSize 
)

Set the maximum change in control signal a servo will change in a given 20 ms time period.

This function allows you to make a Parallax Continuous Rotation servo ramp up to and down from whatever speed it was previously set to. It also allows you to set the rate at which a Parallax Standard Servo turns from its previous position to a new position.

Parameters
pinNumber of the I/O pin the servo is connected to
stepSizeThe number of microseconds per 20 ms of time that the servo's control signal is allowed to change. This equates to the number of tenths of a degree a Parallax Standard Servo is allowed to change, or the number of 100ths of full speed the a Parallax Continuous Rotation servo is allowed to change per 20 ms.
Returns
1 if successful, 0 if pin not found.
int servo_speed ( int  pin,
int  speed 
)

Set Parallax Continous Rotation Servo speed.

Examples:

servoSpeed(pin, 200); // Twice full speed counterclockwise
servoSpeed(pin, 100); // Full speed counterclockwise
servoSpeed(pin, 0); // Stay still
servoSpeed(pin, -100); // Full speed clockwise
servoSpeed(pin, -200); // Twice full speed clockwise

100 to -100 corresponds to the linear speed control range. For example, servoSpeed(pin, 75) will set the speed to roughly 75% of full speed counterclockwise. This is a very rough approximation.

Parameters
pinNumber of the I/O pin connected to servo
degreeAngleTenths of a degree from 0 to 1800
Returns
1 if successful, 0 if no cogs or 1 if no locks.
int servo_start ( void  )

Starts the servo process and takes over a cog.

You do not need to call this function from your code because the servoSet function calls it if it detects that the servo cog has not been started.

Returns
1..8 if successful. 0 if no available cogs, -1 if no avialable locks.