WebbotLib AVR library
WebbotLib It just does it
  C++ documentation  C documentation

Devantech/SD21.h

The Devantech SD21 is a servo controller for driving up to 21 servos via I2C address 0xC2.
If you are only using a single battery for the servos and to power board then connect the battery to the servo supply terminals as shown and make sure the 2 pin jumper header is shorted together.
sd21-sup1.jpg
Alternatively you can power the board logic from a regulated 5V supply and provide a separate unregulated supply for powering the servo only. In this case the jumper should be left open.
sd21-sup2.jpg
To use this controller you will need to define the servos as follows:
#include "Servos/Devantech/SD21.h"
// Define two servos on the SD21 card.
// This is similar to MAKE_SERVO(inverted, iopin, center, range)
// Described in servos.h
// except that there is no iopin to attach the servo to
SERVO left = MAKE_REMOTE_SERVO(FALSE, 1500, 500);
SERVO right = MAKE_REMOTE_SERVO(TRUE , 1500, 500);
 
// Create a single list of the servos on the SD21
// The first entry is 'Servo 0' on the SD21 board
SERVO_LIST PROGMEM servos1[] = {&left,&right};
 
// Create a driver for the SD21 card
// and give it the list of servos
SERVO_DRIVER sd21 = MAKE_I2C_SERVO_DRIVER(0xC2,servos1);
 
// Create an I2C bus that just contains the SD21
static I2C_DEVICE_LIST PROGMEM i2c_list[] = { &sd21.i2cInfo };
I2C_HARDWARE_BUS i2c = MAKE_I2C_HARDWARE_BUS(i2c_list);
In appInitHardware you will need to initialise the i2c bus and SD21 driver:- 
// Initialise the i2c bus
i2cBusInit(&i2c);
// Initialise the servo controller
sd21Init(&sd21);
You may then move the servos using the functions in actuators.h
For example - the following code will continuously move the servos backwards and forwards:-
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
  TICK_COUNT ms = loopStart / 1000; // Get current time in ms
  int16_t now = ms % (TICK_COUNT)10000; // 10 sec for a full swing
  if(now >= (int16_t)5000){ // Goes from 0ms up to 5000ms
    now = (int16_t)10000 - now; // then 5000ms down to 0ms
  }
  // Map the 0 - 5000 value into DRIVE_SPEED range
  DRIVE_SPEED speed = interpolate(now, 0, 5000, DRIVE_SPEED_MIN, DRIVE_SPEED_MAX);
  // Set speed for all motors/servos
  act_setSpeed(&left,speed);
  act_setSpeed(&right,speed);
  return 0;
}

Valid XHTML 1.0 Transitional