Tuesday 12 January 2010

Using Python for SD84 Servo/Robot controller

I've been playing with an SD84 board which uses 4 x PIC18F2520 chips to provide control for up to 84 servos (and/or analogue/digital input/output). I wanted to use Python on OSX but there are others out there who have used C (see the sd84lib). I've updated this article enough that I thought I repost it with a newer date.

The SD84 also uses a chip from FTDI which provides for the USB-serial interface. FTDI are a company based in Scotland that make a range of chips  e.g. FT232R used in Robot Electronic's SD84, and the Arduino.

Firstly you need to get the FTDI VCP drivers for OSX 10.5 (there are also drivers for Windows and Linux) - I found that the FTDIUSBSerialDriver_v2_2_9.dmg worked better than the latest 2.2.10 version (though there are now newer ones which may well work ok). Initially I installed the 2.2.10 version but firstly the kernel module didn't load when I plugged in the SD84, nor would it load manually (complaining of "can't add kernel extension /System/Library/Extensions/FTDIUSBSerialDriver.kext (not a bundle)") - I tried some suggestions here to no avail. I fixed it by uninstalling that version and cleaning up according to here - also removing my old pl2303 drivers, as mentioned here:
sudo rm -rf /System/Library/Extensions/FTDIUSBSerialDriver.kext \
/System/Library/Extensions.kextcache \
/System/Library/Caches/com.apple.kernelcaches/* \
/Library/Receipts/FTDIUSBSerialDriver.* /Library/Receipts/osx-pl2303-10.4.* \
/System/Library/Extensions/osx-pl2303.kext
I then did a clean install of the 2.2.9 package and rebooted then the drivers just loaded when I plugged in the SD84 - you should see a message about FTDI drivers loading in dmesg, and you should also see the driver instantiate the appropriate device nodes - e.g. /dev/tty.usbserial-XXXX.

There are also FTDI's D2XX drivers which are apparently supported by the Python pyusb module (not sure diff with this one) but I haven't tried them yet - though they potentially provide for better performance. It seems that these can also be used for JTAG based debugging.

I then used Python pyserial module (installed on OSX from ports using sudo port install py-serial, or you can download pyserial package and then run: sudo python setup.py install in the unpacked directory) to control an SD84 - the basics are (which will work on Linux as well, and windows if you replace'/dev/tty.usbserial-A2001mJE' with 'COM3') :
import serial
sync='\xAA\xA0\x55'
SET_SERVO='\x01'
SET_MODE='\x04'
GET_VERSION='\x0A'
# Open Serial port to FTDI usb serial tty
ser = serial.Serial('/dev/tty.usbserial-A2001mJE', baudrate=115200, byte
size=8, parity='N', stopbits=2,timeout=1)
# Send GET_VERSION command
ser.write(sync+GET_VERSION+'\x02\x00')
# Read the returned version and print
ver=ser.read(2)
print "VERSION:"+ver
# set SERVO mode
ser.write(sync+SET_MODE+'\x11\x01\x19')
#Read return code (should be \x00 if all is well)
ser.read(1)

There's also other places with useful info here on OSX and serial setup.
[written on 15/9/9 updated 23oct09, 12jan09]

No comments:

Post a Comment