| View previous topic :: View next topic |
| Author |
Message |
Djago
Joined: 15 Sep 2007 Posts: 3
|
Posted: Sat Sep 15, 2007 1:21 am Post subject: Sample code |
|
|
Hi All,
I just started out with openservo and want to gradually replace normal servos with opensource types.
I purchased Openservo V3 boards from Jay and OSIF from Barry to get up to speed and familiar with things.
My application needs "ordinary" behaviour (like the std 1-2 ms servos) but in this case done with the openservos (modded HS 311).
However I cannot seem to find any sample code for master implementation (which is Atmega M128).
The links on the sample code page do not contain any links...
Any sourcecode either C, Basic or pseudo code would be helpful because from the TWI page I do not really get any wiser (I always get up in running in days with some sample code instead of a week or 2 and many forum posts when using data frame info from some datasheet/ openservo TWI page)..
Many thanks in advance.
Peter |
|
| Back to top |
|
 |
ginge Site Admin
Joined: 14 Jan 2006 Posts: 1027 Location: Manchester, UK
|
Posted: Sat Sep 15, 2007 6:29 pm Post subject: |
|
|
Hi Peter,
As the OpenServo is controlled through basic I2C, It should be fairly simple to send commands and get up and running.
I have a few question before I write some basic code for you though...
You have ordered an OSIF from me. I presume this is not going to be the main method of control, and you want to change it out to use an Atmega128?
In the simplest form you communicate over I2C using this procedure:-
I2C Read:-
Send Start Condition.
Wait for TW_START (Start sent)
Write Slave address
Wait for TW_MT_SLA_ACK (I2C Master Transmitter Slave Acknowledge)
Write register address
Wait for TW_MT_DATA_ACK (I2C Master Transmitter Data Acknowledge)
Send (repeat) start condition
Wait for TW_START or TW_REP_START (Start sent)
Send slave read (SLA+R)
Wait for TW_MR_SLA_ACK (I2C Master Receiver Slave Acknowledge)
Loop through data while slave ACK's getting data from TWDR
TW_MR_DATA_ACK (I2C Master Receiver Data Acknowledge)
Writing data is similar to the above
Start
Slave address
Register to write
Loop through data to write
There are a whole load of I2C error conditions that each of these stages is missing, but that is the general theme of things.
There are 2 ways to manipulate the I2C hardware on an Atmega... using interrupts to offload the handling (more complicated) or a simple top down approach.
Here is some code (without error handling) to write a new position to the OpenServo using an Atmega128 (no hardware init done in this example)
| Code: |
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* send start condition */
while ((TWCR & _BV(TWINT)) == 0) /* Wait for the Hardware to become ready */
if (TW_STATUS != TW_START || TW_STATUS != TW_REP_START)
{ return -1; /* Error! */ }
TWDR = (0x10 |TW_WRITE); /* Set the slave address 0x10 with SLA+W (write)*/
TWCR = _BV(TWINT) | _BV(TWEN); /* Clear interrupt to start */
while ((TWCR & _BV(TWINT)) == 0) /* Wait for the Hardware to become ready */
if (TW_STATUS != TW_MT_SLA_ACK) /* Wait for device ACK */
{ return -1; /* Error! */ }
TWDR = 0x10; /* select SEEK_HI */
TWCR = _BV(TWINT) | _BV(TWEN); /* Clear interrupt to start */
if (TW_STATUS != TW_MT_DATA_ACK) /* Wait for data ACK */
{ return -1; /* Error! */ }
TWDR = pos_high; /* Send the high byte of position
TWCR = _BV(TWINT) | _BV(TWEN); /* Clear interrupt to start */
if (TW_STATUS != TW_MT_DATA_ACK) /* Wait for data ACK */
{ return -1; /* Error! */ }
TWDR = pos_low; /* Send the low byte of position
TWCR = _BV(TWINT) | _BV(TWEN); /* Clear interrupt to start */
if (TW_STATUS != TW_MT_DATA_ACK) /* Wait for data ACK */
{ return -1; /* Error! */ }
TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN); /* send stop condition */
|
As mentioned, this does not handle errors gracefully or properly. It is also not tested!
I hope this makes sense, and helps in some way
Barry _________________ http://www.headfuzz.co.uk/
http://www.robotfuzz.co.uk/ |
|
| Back to top |
|
 |
Djago
Joined: 15 Sep 2007 Posts: 3
|
Posted: Sun Sep 16, 2007 1:11 am Post subject: |
|
|
Barry,
Thanks for the examples that will work fine for me.
The M128 is intended as the master in a project and the OSIF I will use to evaluate & test the servos and explore the openservo concept in general.
Peter |
|
| Back to top |
|
 |
|