Sample code for interfacing with the OpenServo

Dimax U2C

Interface code in Java Interface code in Ruby Interface code in C++ Interface code in C

Linux /dev/i2c

Communication with the I2C interface is very simple, as it is handled as a file for reading writing.

Simple method to confirm the OpenServo works.

//c code here

Basic Stamp

The Basic Stamp 12p is one of the easier stamps to interface with. It has Hardware I2C support built in, and can control an OpenServo very easily.

simple example

complex example

Windows & Linux parallel port interface

Windows:- hihihi interface code Linux:- as above

AVR Host controller

example using procyon AVR lib

Using the Arduine

Here's some code that I use to control openservos (pan and tilt) (provided by ahoeben http://www.openservo.com/forums/viewtopic.php?p=4347#4347) Code:

#include <Wire.h> 

// OpenServo registers & commands
#define OPENSERVO_POSITION    0x08
#define OPENSERVO_SEEK        0x10

#define OPENSERVO_PWM_ENABLE  0x82
#define OPENSERVO_PWM_DISABLE 0x83

// servo i2c addresses
#define SERVO_ADDRESS  0x20



/*
 * OpenServo utility handlers
 */

void _openservo_write16(int i2c, byte reg, int data) {
  // write a 16 bit register
  _openservo_begin(i2c, reg);
 
  Wire.send(data >> 8);    //  high byte 
  Wire.send(data & 0xff);  //  low byte

  Wire.endTransmission();

}

unsigned int _openservo_read16(int i2c, byte reg) {
  // read a 16 bit register 
  _openservo_begin(i2c, reg);
 
  unsigned int data;
  Wire.requestFrom(i2c, 2);
  if (Wire.available())
    data = Wire.receive() << 8; // high byte 
  if (Wire.available())   
    data |= Wire.receive();     //  low byte
 
  Wire.endTransmission();
 
  return data;
}

void _openservo_command8(int i2c, byte reg) {
  // send an 8 bit command
  _openservo_begin(i2c, reg);
 
  Wire.endTransmission(); 
}

void _openservo_begin(int i2c, byte reg) {
  // init an i2c transmission
  Wire.beginTransmission(i2c);
  Wire.send(reg); 
}

To move a servo to a position: Code: _openservo_write16(SERVO_ADDRESS, OPENSERVO_SEEK, value);

To get the current position: Code: unsigned int value = _openservo_read16(SERVO_ADDRESS, OPENSERVO_POSITION);

To disable PWM: Code: _openservo_command8(SERVO_ADDRESS, OPENSERVO_PWM_DISABLE);

etc

Microchip PIC

PicBasic PicC asm