Friday 15 March 2013

Snowboard HUD Part 5 - GPS Integration

For the GPS integration, the articles from Kevin Townsend have been invaluable. The hardware I'm using is the Ultimate GPS Breakout Board. Mine was from Cool Components in the UK.



As per the suggestion in Kevin's article I'm also using a USB to serial converter rather than using the GPIO pins directly. I got this from Amazon UK (http://www.amazon.co.uk/gp/product/B008AGDTA4)

This worked just by plugging it into the Raspberry Pi and is accessible probably as:

/dev/ttyUSB0

The instructions on Kevin's page are really comprehensive so I wont repeat them here. The only mistake I made was in connecting the GPS breakout to the serial.

The coloured cables from the USB to serial are:

Red    VCC
Black    GND
Green    TXD
White    RXD

Everytime I do something with serial I do this wrong, and it is completely logical I just seem to do it wrong every time! The TXD from the USB to serial needs to connect to the RX on the GPS breakout. I've put a picture below. Sorry the cables for TX and RX are both yellow, it doesn't help with the understanding but it's all I had.



Once you get it all working, on the Raspberry Pi if you run

cgps -s

you should see output something like this:


However, what we really need is to be able to access this information from the Python program created earlier. In order to do this Kevin has another article with examples http://learn.adafruit.com/adafruit-ultimate-gps-on-the-raspberry-pi/using-your-gps

Based on these examples, my updated code is:


# coding=UTF-8

import pygame
import sys
import datetime
import gps

def draw_borders(screen):
    """Draws some simple borders to the display"""
    pygame.draw.lines(screen, (255, 255, 255), False, [(0, 30), (width, 30)], 2)

def draw_time(screen):
    """Draws the time to the display"""
    the_time = datetime.datetime.now()
    time_as_string = the_time.strftime('%H:%M')

    font = pygame.font.Font(None, 42)
    text = font.render(time_as_string, 1, (255, 255, 255))
    textpos = text.get_rect(centerx=screen.get_width()/2, centery=15)
    screen.blit(text, textpos)  # paste the text into the background

def draw_speed(screen, the_speed):
    """Draws the speed to the display"""

    # get the speed...
    if the_speed:
        the_speed_string = '{} kph'.format(int(the_speed)) # display as whole number
    else:
        the_speed_string = '-- kph'

    font = pygame.font.Font(None, 160)
    text = font.render(the_speed_string, 1, (255, 255, 255))
    textpos = text.get_rect(centerx=screen.get_width()/2, centery=screen.get_height()/2)
    screen.blit(text, textpos)  # paste the text into the background


def draw_altitude(screen, the_altitude):
    """Draws the altitude to the display"""
    if the_altitude:
        the_altitude_string = '{} m'.format(the_altitude)
    else:
        the_altitude_string = '-- m'

    font = pygame.font.Font(None, 65)
    text = font.render(the_altitude_string, 1, (255, 255, 255))
    textpos = text.get_rect(centerx=120, centery=screen.get_height()-20)
    screen.blit(text, textpos)  # paste the text into the background


def draw_temp(screen):
    """Draws the temperature to the display"""
    the_temp = 0
    the_temp_string = u'{}°C'.format(the_temp)

    font = pygame.font.Font(None, 65)
    text = font.render(the_temp_string, 1, (255, 255, 255))
    textpos = text.get_rect(centerx=screen.get_width()-80, centery=screen.get_height()-20)
    screen.blit(text, textpos)  # paste the text into the background


if __name__ == '__main__':

    pygame.init()

    size = width, height = 650, 400
    black = 0, 0, 0

    screen = pygame.display.set_mode(size)

    # set up gps if possible
   
    gps_session = gps.gps("localhost", "2947")
    gps_session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)

    speed = None
    altitude = None

    while True:
        # Checks for key presses, e.g. escape to quit
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE: sys.exit()

        # Fill the screen with black background
        screen.fill(black)

        # get GPS data
        gps_report = gps_session.next()
        altitude_temp = gps_report.get('alt')
        speed_temp = gps_report.get('speed')
        # need to do this temp stuff otherwise it flickers
        # back to '--' if gps is temporarily lost
        if altitude_temp:
           altitude = altitude_temp
        if speed_temp:
            speed = speed_temp

        # Draw all the stuff
        draw_borders(screen)
        draw_time(screen)
        draw_speed(screen, speed)
        draw_altitude(screen, altitude)
        draw_temp(screen)

        # Update the display
        pygame.display.flip()




And the output:




I haven't been able to test the speed yet until I get a battery pack, but you can see that the altitude has been updated.

It is also worth looking at the other details that are stored within the gps_report variable. The Python dictionary keys are in italics.

longitude     lon
latitude     lat
time     time
altitude     alt (m)
speed     speed (kph)
climb     climb (m/min)
heading     track (degrees) (I think)

Might be able to add more of this information in later.




No comments:

Post a Comment