|
|
|||||||
| Register | Links | Tutorials | Data Center | Blogs | Gallery | FAQ | Members List | Social Groups | Calendar | Search | Today's Posts | Mark Forums Read |
| Home | Submit Tutorial | What's New | What's Popular | Search |
| Controlling AX-12 Servos |
|
|
|||||||
|
|||||||
| Difficulty: | Beginner | ||||||
|
Controlling AX-12 Servos
AX-12 Dynamixel servos, as found in the bioloid kit, are extremely useful and powerful servos. They sport a digital controller, serial bus, overload and overheat shutdown, and a great torque-to-cost ration. This tutorial will explain the basics of AX-12 servos, and show you how to control an AX-12 servo, using either your PC, or an Arduino. AX-12 Basics Controlling the AX-12 servos is a snap. They use a serial bus and a very simple packet structure:
We mentioned that the instructions we can carry out are mainly limited to Read/Write, so what are we reading and writing? The AX-12 uses a memory-mapped or register-based interface. Memory addresses (register addresses) correspond to particular parameters of operation. For instance, register #3 is the ID of the servo, if we write to this, the servo ID changes. The registers most of interest will be:
Python-Powered PC Control Since AX-12 servos use a serial bus, we can easily control them using our PC. Parts we will need:
Code:
import serial # we need to import the pySerial stuff to use
# important AX-12 constants
AX_WRITE_DATA = 3
AX_READ_DATA = 4
s = serial.Serial() # create a serial port object
s.baudrate = 1000000 # baud rate, in bits/second
s.port = "/dev/ttyUSB0" # this is whatever port your are using
s.open()
# set register values
def setReg(ID,reg,values):
length = 3 + len(values)
checksum = 255-((index+length+AX_WRITE_DATA+reg+sum(values))%256)
s.write(chr(0xFF)+chr(0xFF)+chr(ID)+chr(length)+chr(AX_WRITE_DATA)+chr(reg))
for val in values:
s.write(chr(val))
s.write(chr(checksum))
def getReg(index, regstart, rlength):
s.flushInput()
checksum = 255 - ((6 + index + regstart + rlength)%256)
s.write(chr(0xFF)+chr(0xFF)+chr(index)+chr(0x04)+chr(AX_READ_DATA)+chr(regstart)+chr(rlength)+chr(checksum))
vals = list()
s.read() # 0xff
s.read() # 0xff
s.read() # ID
length = ord(s.read()) - 1
s.read() # toss error
while length > 0:
vals.append(ord(s.read()))
length = length - 1
if rlength == 1:
return vals[0]
return vals
setReg(1,30,((512%256),(512>>8))) # this will move servo 1 to centered position (512)
print getReg(1,43,1) # get the temperature
Arduino Control The Arduino has a single serial port, which is somewhat problematic. We can send data out to the servos, but then we lose our serial port. Typically we need that to at least send debugging info back to our computer. For that reason, we will use the more powerful Sanguino. This can be programmed in the Arduino environment, but is a larger chip with 2 serial ports. The Arduino presents slightly more of a challenge. The AX-12 servo has 3 lines on the bus: Power, Ground, and Signal. How does it use only one signal? Only one device on the bus can talk at a time. This is known as half-duplex serial. This poses a challenge on our AVR, since the hardware serial UART has separate lines for the RX and TX. We have two possible solutions:
I've wrapped quite a bit of code into a bioloid library for the Sanguino. To use it, we need to do some setup:
Code:
// include the library itself
#include <ax12.h>
#include <BioloidController.h>
// now, create a connection called "bioloid" -- you should always call it bioloid
// so that our macros work. Our baud rate is 1Mbps = 1000000
BioloidController bioloid = BioloidController(1000000);
void setup(){
// this is a simple macro to move servo ID #1 to the center position (512)
SetPosition(1,512);
}
void loop(){
// nothing to do here...
}
You can download a ZIP file of the latest revision of my bioloid library off my SVN server. |
|||||||
| Tags: | None | ||||||
| Replies to Tutorial: Controlling AX-12 Servos |
|
#1
|
|||
|
|||
|
Hi Inxfergy,
Thanks for this very interesting tutorial The bioloid library is a dead link; please update the link, or let me have your library code by mail Greetings from Grenoble, France Yves |
|
#2
|
||||
|
||||
|
Re: Controlling AX-12 Servos
I've removed the bioloid library from my SVN repository, it's now hosted on google code under the arbotiX project:
http://arbotix.googlecode.com -Fergs |