REQ: Arduino

Discussions relating to development and use of the OpenServo software/firmware.

Moderators: jharvey, Secondary Admin, Admins

Post Reply
FireBALL
Posts: 28
Joined: Fri Jul 24, 2009 4:31 am

REQ: Arduino

Post by FireBALL » Fri Jul 24, 2009 4:34 am

Hi,

I'm looking for a library for communicating with open servo through arduino.

I would highly appreciate if someone could point me to a link or code

thank you for your help and time.

cheers

kbb
Posts: 180
Joined: Fri Jun 01, 2007 1:47 am

Re: REQ: Arduino

Post by kbb » Sat Jul 25, 2009 1:33 am

FireBALL wrote:I'm looking for a library for communicating with open servo through arduino.

I would highly appreciate if someone could point me to a link or code
OpenServo uses I2C/TWI, this is apparently supported by (some versions of?) the Arduino (I don't have one). So it is theoretically relatively easy. You can read about using I2C/TWI with the Arduino in the articles found in the links section at the bottom of this page:

http://www.arduino.cc/playground/Learning/I2C

Kevin.

ginge
Site Admin
Posts: 1031
Joined: Sat Jan 14, 2006 2:34 pm
Location: Manchester, UK
Contact:

Post by ginge » Sat Jul 25, 2009 3:56 pm

Good link.

Below is a snippet of arduino code that with the correct I2C initialisation will start the servo moving


Wire.begin(); // join i2c bus (address optional for master)

Wire.beginTransmission(0x10);
Wire.send(SEEK_HI); // Select the seek register (0x10)

Wire.send(0x03); // Send position 0x300

Wire.send(0x00);
Wire.endTransmission(); // Finish

ahoeben
Posts: 20
Joined: Fri Jun 20, 2008 4:14 pm

Post by ahoeben » Sun Jul 26, 2009 8:27 am

Here's some code that I use to control openservos (pan and tilt)

Code: Select all

#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&#40;int i2c, byte reg, int data&#41; &#123;
  // write a 16 bit register
  _openservo_begin&#40;i2c, reg&#41;;
  
  Wire.send&#40;data >> 8&#41;;    //  high byte  
  Wire.send&#40;data & 0xff&#41;;  //  low byte

  Wire.endTransmission&#40;&#41;;

&#125;

unsigned int _openservo_read16&#40;int i2c, byte reg&#41; &#123;
  // read a 16 bit register  
  _openservo_begin&#40;i2c, reg&#41;;
  
  unsigned int data;
  Wire.requestFrom&#40;i2c, 2&#41;;
  if &#40;Wire.available&#40;&#41;&#41;
    data = Wire.receive&#40;&#41; << 8; // high byte  
  if &#40;Wire.available&#40;&#41;&#41;    
    data |= Wire.receive&#40;&#41;;     //  low byte
  
  Wire.endTransmission&#40;&#41;;
  
  return data;
&#125;

void _openservo_command8&#40;int i2c, byte reg&#41; &#123;
  // send an 8 bit command
  _openservo_begin&#40;i2c, reg&#41;;
  
  Wire.endTransmission&#40;&#41;;  
&#125;

void _openservo_begin&#40;int i2c, byte reg&#41; &#123;
  // init an i2c transmission
  Wire.beginTransmission&#40;i2c&#41;; 
  Wire.send&#40;reg&#41;;  
&#125;
To move a servo to a position:

Code: Select all

_openservo_write16&#40;SERVO_ADDRESS, OPENSERVO_SEEK, value&#41;;
To get the current position:

Code: Select all

unsigned int value = _openservo_read16&#40;SERVO_ADDRESS, OPENSERVO_POSITION&#41;;
To disable PWM:

Code: Select all

_openservo_command8&#40;SERVO_ADDRESS, OPENSERVO_PWM_DISABLE&#41;;
etc

ginge
Site Admin
Posts: 1031
Joined: Sat Jan 14, 2006 2:34 pm
Location: Manchester, UK
Contact:

Post by ginge » Sun Jul 26, 2009 10:07 am

Awesome. I will put that into the wiki. Thanks!

ahoeben
Posts: 20
Joined: Fri Jun 20, 2008 4:14 pm

Post by ahoeben » Fri Aug 07, 2009 9:21 am

Something I forgot to include in my code (or accompanying comments) is that you have to do a "Wire.begin()" in your setup() code (or somewhere else before you start taslking to the openservos), or it won't work...

FireBALL
Posts: 28
Joined: Fri Jul 24, 2009 4:31 am

Post by FireBALL » Tue Aug 11, 2009 5:14 pm

cheers!

you guys are big help

Adrian678
Posts: 1
Joined: Mon Oct 11, 2010 3:19 am

Post by Adrian678 » Mon Oct 11, 2010 3:29 am

I reallly agree to that,
And i really appreciate it guys,
"Thanks"

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests