Friday, October 24, 2008

GPS (gpsd) Mono C#

While working on the thing that keeps me most busy (see http://openice.org), I often find myself doing things, going places, and accomplishing things that few other people (if any) have. I'm no expert at any of it, I've just got ideas that need testing...

I decided to make a plugin for nGhost that reports speed, altitude, and bearing. To do this, I decided to see if I could tap into gpsd's dbus interface.

Gpsd emits a dbus-signal every time its data changes (afaik). All one would have to do is register to receive that siganl. Since mono (http://mono-project.org) has fantastic dbus bindings for c#, I went with ndesk.dbus (dbus-sharp) to see if I could do it. Here is the code:


public class GPS
{
private Connection DbusConnection;
public Gpsd gps;
public GPS()
{
DbusConnection = Bus.System;
gps = DbusConnection.GetObject("org.gpsd", new ObjectPath("/org/gpsd"));
}

public void Loop()
{
DbusConnection.Iterate();
}

}

public struct GPSFix
{
public double time;
public int mode;
public double ept;
public double latitude;
public double longitude;
public double eph;
public double altitude;
public double epv;
public double track;
public double epd;
public double speed;
public double eps;
public double climb;
public double epc;
}

public delegate void GPSPositionChangedHandler(GPSFix fix);

[Interface("org.gpsd")]
public interface Gpsd
{
event GPSPositionChangedHandler fix;
}


This is the final interface after quite some time debugging. The main thing to note is that the interface's methods have to match the name of the signal being fired. In our case, "fix".

To tie it in, all you have to do is:


public void OnNewFix(GPSFix fix)
{
///do something with new fix.
}
GPS gps = new GPS();

gps.fix += new GPSPositionChangedHandler(OnNewFix);

///enter main loop...


That's it!

3 comments:

Mike and Starla Rees said...

HI son,

I have no idea what all this means or what you are saying but it look really nice. It probably would do no good to try to explain it to me. I think it is beyond my ability to understand. I love you

Mom

Mike and Starla Rees said...

Don't mind star's pic it just keeps appearing. we need to fix it I guess.

mom

Stefc said...

Hi, this code helps me a lot for interfacing the gpsd from mono under linux, thx.
One problem that I currently have is, that i only get event FIX if another client (like xgps) watching for gpsd :(
If I stops xgps I don't get any broadcast from d-bus, if I restart xgps the broadcasting works again.
Did you know what works wrong with this?