Position-Dependent Event Triggering on the Nokia N900

Nokia N900

Finding the coordinates of the destination

First the program asks for the destination address:

addr = raw_input('\nAddress: ') # Read destination address.

A sample input would be "Karl-Wilhelm-Strasse 4, Karlsruhe". We get the coordinates of the destination with a request to Google Maps.

# Encode query string into URL
url = 'http://maps.google.com/?q=' + urllib.quote(addr) + '&output=js'
# Get location as XML file
xml = urllib.urlopen(url).read()

Google Maps answers with an XML file; the following code writes longitude and latitude to the variables latDest and longDest:

# Strip lat/long coordinates from XML
latDest, longDest = 0.0,0.0
center = xml[xml.find('{center')+9:xml.find('}',xml.find('{center'))]
center = center.replace('lat:', '').replace('lng:', '')
latDest, longDest = center.split(',')
latDest, longDest = float(latDest), float(longDest)

With the sample input from above, the variables latDest and longDest contain these coordinates:

  • latDest: 49.010149 # Destination latitude

  • longDest: 8.420162 # Destination longitude

The Python API googlemaps delivers the same result:

from googlemaps import GoogleMaps
gmaps = GoogleMaps(API_KEY) # Set API_KEY to your Google Maps API Key
address = 'Karl-Wilhelm-Strasse 4, Karlsruhe'
lat, lng = gmaps.address_to_latlng(address)

For the API we need a valid Google account and a valid API key.

Determining the current position

The current position comes from the GPS receiver GPS5030 (Texas Instruments) of the Nokia N900. The receiver supports Assisted GPS (A-GPS); we control it via liblocation. The library exists as a C and a Python implementation; it provides the two objects control and device, which we initialise as follows:

control = location.GPSDControl.get_default()
device = location.GPSDevice()

With GPSDControl we start and stop the receiver. We set the update interval of the position with the method set_properties(); it must match the way we travel. The following code snippet sets the interval to 60 s (suitable for pedestrians):

control.set_properties(preferred_interval=location.INTERVAL_DEFAULT)

GPSDevice provides information about the receiver status and the current GPS data (the fix) as a tuple (immutable list). This fix tuple contains the following information:

  • timestamp

  • latitude and longitude of the current position

  • altitude

  • speed

Once we have initialised GPSDControl and GPSDevice, we query latitude and longitude like this:

latPosition = float(device.fix[4])  # Latitude of actual position
longPosition = float(device.fix[5]) # Longitude of actual position.

Great-circle distance (orthodromic distance)

Great-circle route between two points on the globe

The great-circle distance or orthodromic distance is the shortest distance between two points on the surface of a sphere, measured along the surface. In other words: the great-circle distance is the shortest connection between two GPS coordinates.

With the orthodromic distance we calculate the distance between the current position (A) and the destination (B). For this we convert longitude and latitude to radians. We get the central angle between the two points from the spherical law of cosines. If we multiply the central angle by the earth's radius (approx. 6370 km), we get the distance in metres.

def SphericalDistance(latPosition,longPosition):
  longDestRad = (longDest * (2 * pi / 360))
  latDestRad = (latDest * (2 * pi / 360))
  longPositionRad = (longPosition * (2 * pi / 360))
  latPositionRad = (latPosition * (2 * pi / 360))

  distance = sin(latDestRad) * sin(latPositionRad) + cos(latDestRad) * \
             cos(latPositionRad) * cos(abs(longPositionRad - longDestRad))
  if ((distance >= 1) or (distance <= -1)):
      distance = 0
  else:
      distance = acos(distance)
      distance = distance * 6370000 # Change this for other planets.
  return distance # orthodromic distance

Position-dependent event triggering

The variable distance that the method SphericalDistance() returns holds the straight-line distance between the current position and the destination in metres. We compare distance with a given minimum distance and trigger one of the following events as soon as distance is less than or equal to this minimum distance:

  • Playing an audio file ("Arrival in about 5 minutes").

  • Toggling one or more LEDs (at night).

  • Sending an SMS ("I'll arrive in about 5 minutes").

The following listing shows how we get the Nokia N900 to send an SMS.

def SendSMS():
  string = 'at+cmgs='+"\""+number+"\""+'\r'
  child = pexpect.spawn('pnatd');
  child.send('at\r');
  time.sleep(0.25);
  child.send('at+cmgf=1\r');
  time.sleep(0.25);
  child.send(string);
  child.send('Schatz mach das Essen warm!');
  child.send(chr(26));
  child.send(chr(26));
  child.sendeof();