|
GPS Code |
2008-06-25 |
|
As promised, here is source code for reading GPS data from the Streets and Trips USB GPS on OS X. This is the most C code I have written in a while, so don't go putting it into production! Here's how it works: nmea.h/.c - Structs representing nmea data, and code for reading data from the serial port, and parsing this out of a comma separated string. I have restricted myself to only parsing bits of the gga sentence, but it would be straightforward to extend this to other data and sentences. serial.h/.c - Code for opening and closing the serial port. main.c - A small main method that reads the nmea data and prints the lat, long and altitude to the console. If you want to try it out, don't forget to install the serial port driver as described in my previous post. Have fun with it! Tags:
programming |
|
|
GPS on Leopard |
2008-06-09 |
|
A while back I bought Microsoft Streets and Trips 2007 from the company store, and got a nice, small USB GPS with it. I messed around programming the GPS on Windows (post) and hooked it into my blog, but then I moved over to a Mac running Leopard as my daily machine and lost my neat GPS recording capability. No more! On Sunday I hacked together some code to pull data from the GPS on OS X.
I found a few posts from people trying to do the same thing, so I am going to outline the steps involved now, and show some code in a later post. 1. USB to Serial - The GPS device uses the RS232 serial standard over USB. This won't appear as a serial device without the right driver, so download the BF-810 USB to Serial adapter from here and install it. 2. Vendor and Product ID - The driver installed in step 1 needs some tweaks before it will work, and to make those tweaks you need to know two numbers: The vendor and product IDs. Plug in the GPS and fire up IORegistryExplorer, and then look under IOUSB group (command-6) for the GPS. It will be the entry with USB Vendor Name = Prolific Technology Inc. If you are lazy like me you can also find it by yanking the GPS out of the machine and watching it go red in the explorer. Note that the values here are in hex, so it is a great opportunity to dust off your hex to decimal skills. 3. Edit kext - Plug the vendor and product IDs for your device into the kernel extension Info.plist file /System/Library/Extensions/ProlificUsbSerial.kext/Contents/Info.plist. This is just a plain old xml file, so sudo fire up your editor of choice and replace whatever values are there under idProduct and idVendor with your own. 4. Reload kext - With our product and vendor IDs in place, it is now time to reload the driver. First up, run "sudo kextunload -b com.prolific.driver.PL2303". It is ok if this unload command fails. Now run "sudo kextload -b com.prolific.driver.PL2303". Provided the device is plugged in, you should now be able to see a device file called cu.usbserial in the /dev directory on your file system (ls /dev/cu*). 5. Time to code - Apple has a useful serial programming in C tutorial here. The demo accesses a modem, but the code only needs a few tweaks to pull NMEA data off the GPS. I started by stealing swathes of the demo and hacking on them to re-target it at the GPS. The important changes are: 5.1 Discovery - There is no need to discover the device dynamically, just hard code the device path and get rid of a couple of hundred lines of code right away: char* gpsPath = "/dev/cu.usbserial"; fileDescriptor = OpenSerialPort(gpsPath); 5.2 Config - The device is configured using the termios structure, and you'll need this to get the baud, parity, stop bits and word size right: cfsetspeed(&options, B4800); options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; 5.3 Reading data and NMEA parsing - Data isn't read back from the device in a predictable sequence, so you will need to scan the input as it is read and work out when the sequence starts. Luckily the NMEA data structure is simple to parse: every line starts with a $, holds some comma separated values, and ends with a checksum. Parse away! Tags:
programming |
|
|
Bug |
2008-05-24 |
|
This is a short post that isn't about programming, but is the latest in an irregular series called "things that bug me". I can't remember when I first heard the story about how NASA's x million dollar investment in space pen technology was trumped by the Soviets' clever use of pencils. The story crops up sometimes as an example of how pragmatism beats... who knows? Pen-mad inventors? At any rate, the story is a load of nonsense, and obviously so. Beyond that, it bugs me for reasons including but not limited to its gleeful anti-americanism and its underestimation of the people who work in the space program (likely v. smart). Anyway, read all about it at snopes. And arm yourself with the facts about inuit words for snow while you are at it. Note that I favour pencils myself, and use a fine Lamy propelling pencil as my daily writer. Tags:
things that bug me |
|
|
Kareem |
2008-03-15 |
|
I once saw Kareem Abdul-Jabbar play. It was some time around 1989 or 1990, in my native Brisbane, Australia. Kareem was making a valedictory tour with a team of old guys (Bob McAdoo!) playing against some Aussies including a young Lobo called Luc Longley. Just recently Kareem has started blogging on the latimes site. The dude is deep! I was blown away by this post, which shows a young Kareem, with hair, and his head up around the rim. The man was once young. |
|
|
Stonehenge |
2008-03-12 |
|
|
|
KML |
2008-03-12 |
|
I have done a little GPS hacking in the last year and am part-time interested in location, so as part of my Ruby blog re-write I implemented a KML feed. This lets me tag my blog posts with location coordinates. If this excites you then read on! If you aren't sure if you should be excited or not, then reading this introduction to KML will help you decide. You can see my feed here here. Now check out this map overlay here. Sweet! Here are my three step guide to rolling your own KML feed in Rails. I did this as part of my own blogging software, but these steps are generally applicable. First up, get some location data. I did this by putting an extra string field onto my blog entry. Then fill this field with data of the format "{longitude}, {latitude}". Next, write some view code to spit out data as KML. My .rhtml looks like this: <?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://earth.google.com/kml/2.1"> <Document> <% for thing in @things if thing and thing.location and thing.location.length > 0 then %> <Placemark> <name><%=thing.title -%></name> <description> <![CDATA[ <%= thing.content%> ]]> </description> <Point> <coordinates> <%= thing.location -%> </coordinates> </Point> </Placemark> <% end end %> </Document> </kml> Lastly, set the HTTP content type header like so: response.headers["type"] = "application/vnd.google-earth.kml-xml" Easy! Tags:
programming |
|
|
Sync |
2008-02-10 |
|
Last night I wrote a script to syncronise my Google Calendar and my Motorola Razr cell phone. I didn't really set out to do this, but it seemed like a useful thing to do and like all fun problems it got its teeth into me and I couldn't resist. It all started when my wife tried to send me a photo from her phone via bluetooth. In the course of trying to get the photo I paired up my own cell phone and my new laptop. Yes, new laptop. I bought in a new MacBook last week. It is a beaut. I am still setting it up and getting comfortable in the mac environment, so this was a good way to learn some things. Google Calendar to iCalSo now I have my phone paired with my laptop, and iSync is syncing my calendar from iCal to the phone. Neat. Only problem is that I don't keep my schedule in iCal, I keep it in Google Calendars. Getting a schedule from Google Calendar to iCal is pretty easy. See this article. The only caveat is that if you are a Google Apps For Your Domain customer, like me, then you might not see a "Share all information on this calendar" option for your calendar. By default this is switched off for the entire domain, so you will have to visit partnerpage.google.com/your_domain and click in the link on the top right that says "Manage this domain", from there go into the Calendar section and choose one of the "Share all information..." options. And then go get a cup of coffee. It took 20 suspenseful minutes before this change was reflected in my calendar and I was able to sync it, so have patience! LazyOK, at this point I can open up iCal and ask it to refresh my calendar from google (control-shift-r), and I can then open up iSync and transfer that calendar over to my phone (command-t). Only problem is that it involves a few mouse clicks or key strokes to open the apps, and then a few key combinations to transfer data. I'm too lazy for that. What I really want is to press a single button and have the whole thing proceed automatically. I'm an absolute beginner on the mac, so I hunted around and decided to try it in AppleScript. Automatically Sync iCalFirst up, here is a script to sync iCal:
tell application "iCal"
activate
tell application "System Events"
keystroke "R" using {command down, shift down}
end tell
delay 5
quit
end tell
(I got most of this from here.) This is not a pretty script. Particularly, the "delay 5" loop is a crummy way of letting iCal sync before quitting. And there isn't an API for refreshing a subscribed calendar, so we are left with sending key strokes, which fails at times. But this works well enough. Automatically Sync PhoneHere is a script to perform the next step, pushing the schedule onto the phone: tell application "iSync" activate synchronize repeat if not syncing then exit repeat end repeat quit end tell This script is much neater. I took both of these scripts and converted them into a single application using the Script Editor (Applications -> AppleScript -> Script Editor) and saved the new application in my applications folder. The option is under File -> Save As. I can now run one program to sync my schedule onto my phone! Mapping a KeyAll that remains is to map a keyboard shortcut to the application I just built. To do this I used QuickSilver, which makes it easy to set up "triggers" (keyboard shortcuts) to do just this sort of thing. See the tutorial here. Of course I could just get an iPhone, in which case this would undoubtedly work with less rickety scripting. Until then... |
|
|
Crystal |
2008-01-30 |
![]() Just got back from snowboarding at Crystal Mt. |
|
|
Data |
2008-01-30 |
![]() ![]() I saw this thing driving up I5 today. That truck has some serious hardware! |
|
|
Solidity of artefacts vs. solidity of process |
2007-12-10 |
|
I watched this Clay Shirky video months ago and it stuck in my head. Then it turned up again on IT Conversations on my drive home tonight and it made some more sense. Any particular system or architecture or project is an edifice. People and culture are a renewable building material. |
|


