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]

Friday, 8 January 2010

Accessing OSX install DVD from Linux/Ubuntu

Have you ever tried sticking your Mac [Snow] Leopard/Tiger OSX installation DVD/CD disks into a Linux box and just found a measly little windows partition there with the BootCamp stuff on it, and wondered where all the Mac stuff was hiding? Yeah so have I.

This is how to copy the disk using the dd command (you can do virtually the same thing on OSX - here's an article with some more info):
dd if=/dev/sr0 of=/DVDcopy.iso
For older versions of *NIX (which used to use '-if/-of'):
dd -if=/dev/sr0 -of=/DVDcopy.iso 

However whilst Ubuntu doesn't understand - it is all there just hiding... This handy site explained a way to get to it - directly on DVD. You can use your DVDcopy.iso in the same way. You'll probably need to install the tool (e.g. aptitude install kpartx). On your Linux box you do:
#sudo kpartx -av /DVDcopy.iso
add map loop0p1 (252:0): 0 60 linear /dev/loop0 4
add map loop0p2 (252:1): 0 2020420 linear /dev/loop0 64
add map loop0p3 (252:2): 0 13158216 linear /dev/loop0 2020488
add map loop0p4 (252:3): 0 12 linear /dev/loop0 15178704

Then use file to find out which partition contains the Mac stuff - run file on each one till you find one that says it's an Macintosh HFS e.g:
sudo file -s /dev/mapper/loop0p3
/dev/mapper/loop0p3: Macintosh HFS Extended version 4 data last....

The mount it and you're away:
sudo mount /dev/mapper/loop0p3 /my_mount

You should now be able to see the contents of the Mac (HFS+) partition in the directory /my_mount

Tuesday, 5 January 2010

Handy iPhone apps

Happy New Year to all! Well I thought I'd list a few apps I've found to be handy on the iPhone - all free apart from two:
  • Fring - Gives you a consolidated VoIP solution (SIP, Skype, and others) with offline notifications (some might say it's too consolidated - maybe Fring are run by the Spooks?!)
  • MobileRSS - The best free Google Reader with good offline and un/subscribing support
  • Twitterific - A nice Twitter app for keeping an eye on all that chattering with support for un/subscribing and stuff
  • OffMaps ($) - A very handy (and very reasonably priced) maps app that uses OpenStreetmap/cloudmade data with good offline support
  • Discover - Useful app for storing random stuff on the iPhone (works over wifi and acts as web server)
  • Touchterm ($) - A useful and cheapish ssh client
  • RjDj - A cool app that generates 'music' using 'patches' - based upon Pure-Data
  • MrMr - An OSC app for whacky control applications

Friday, 13 November 2009

Karmic IPv6 Global address problems

If you're running IPv6 on Ubuntu 9.10 with multiple interfaces and you're not seeing any global IPv6 addresses assigned to your interfaces, and you're seeing this kind message in /var/log/messages:
Nov 13 10:10:10 my_machine kernel: [ 1000.101010] eth0: IPv6 duplicate address detected!
Then it's obviously Friday 13th and you should put it down to ....Yeah maybe but yesterday it was the same last week....

It seems there's a kernel (2.6.31-15) bug which means IPv6 Duplicate Address Detection (DAD - see RFC4862) is generating false-positives and subsequently not permitting your global address to be assigned to an interface.

To work around this bug do (change eth0 to your iface name):
sudo sysctl net.ipv6.conf.eth0.accept_dad=0

Tuesday, 10 November 2009

Ubuntu 9.10 (Karmic) has dodgy input problem

I just upgraded to Karmic Koala and I'm NOT impressed - My mouse and keyboard have become unresponsive and sluggish - sometimes even losing keystrokes and control of the mouse for a few milliseconds at a time (I had to go back and correct this post due to missing keystrokes that I did make but didn't register).

I'm considering changing distro if this isn't fixed soon.

[update:9mar10]: See possible fixes here

Sunday, 25 October 2009

Our Solar system (planets) broken on Google Earth/Sky

I was trying to see the planets (our solar system) on google earth recently but there seems to be a problem/bug - they do not display - both in the dedicated app and on the google sky website. When you click on the planets in the app (with error reporting on) you get:
http://mw1.google.com/mw-earth-vectordb/sky/sky0/solarsearch/mars-en-currenthour.kmz 404 not found
...and for all the others (maybe it's something else but they're not working on either right now).

Wonder when it'll be fixed?!

I posted a question on the help forums but no response....

Friday, 2 October 2009

MacBook disc crash (and restore)

Well my MacBook's disc decided to die the other week but thanks to Time Machine I had most of my stuff safe (apart from my other two partitions on my triple boot setup ;) - But I hadn't booted into 'em in a while so there can't have had anything tooo serious lost there - I think I'm going to just use VMware/Parallels/VirtualBox from now on...).

Of course I'd been using my TC with encrypted backups so when I attempted a restore using a new Snow Leopard DVD it couldn't find the backups... Oh dang! Luckily there is another way - I upgraded the default Tiger install to shiney new Snow Leopard and then fired off the Migration Assistant (from the another Admin account on the machine - you should not create your original user account with the same name as the one in your backup as it will clash - so just use another [admin] login and once you've run the Migration Assistant it will recreate your old account).

Friday, 25 September 2009

Handy OSX commands

List USB devices (a bit like lsusb on Linux)
On standard installation:
ioreg -c IOUSBDevice -l -w 0
Or using the developer tools:
IORegistryExplorer

Airport Wifi Info
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport
There are various arguments you can run it with -h to get a listing but the most useful are -I to get current wireless status, and -s to do a wireless scan.

Mount and mess with disk images
Here's how to mount a dmg:
hdiutil attach your_disk_image.dmg

List process' network connections
sudo lsof -i -P

Tweaking startup apps
- To disable startup items - add/rm a file called .disabled in their directory (i.e. a subdir of /System/Library/StartupItems/ or /System/Library/StartupItems/)
- launchctl -- Interfaces with launchd - in /Library/LaunchDaemons and similar
- Various custom ones; e.g. sudo "/Library/Application Support/VMware Fusion/boot.sh" --stop

Search using Spotlight on the command line
mdfind your_search_term (or mdfind -name file_to_find to look for file names).
There's also a few other related utilities like mdls to list file attributes etc. For more info see this useful post. It is also possible to enable the unix style locate command but it seems like a waste of space on OSX since you've already got Spotlight - though locate does provide for searching all files (only by name). However one can tweak Spotlight to search these files too...

Playing sounds on the cmd line
/usr/bin/afplay
Also afinfo gives you info on the file.

Print out OSX version
/usr/bin/sw_vers | grep 'ProductVersion:' | grep -o '[0-9]*\.[0-9]*\.[0-9]*'
Or use system profiler (both suggestions from here):
system_profiler SPSoftwareDataType


There's also other pages with similarly useful info like here.

[updated: 23mar10]