Friday, June 20, 2014

GPS for the Beaglebone Black: Sparkfun Copernicus II DIP Module - Improved & Tested with Beaglebone Black Rev. C

I published some sample code last October that showed a simple Python script that read NMEA GPS data from the Copernicus II via a serial port.  The code below is a slightly improved version that I have tested with the Beaglebone Black Rev. C (the new model with 4GB NAND) that comes with Debian installed.  This example uses the UART1 overlay included in the /lib/firmware directory.  This code also adds a couple formatting functions for the output.  The output is displayed in the terminal window.

This example assumes that you are working at the command line (via SSH or via an attached monitor, keyboard, and mouse).  This code is in Python and is not intended to run as Bonescript.

Connections



GPS Module  Beaglebone Black
VCC         P9 3
GND         P9 1
TX-B        P9 26
RX-B        P9 24

Code


# -*- coding: utf-8 -*-
# Need to declare encoding in line above in order to use degree sign

import serial
import time
import datetime
import re
import os
from decimal import Decimal

# Use overlay file BB-UART1-00A0.dtbo from /lib/firmware
os.system("echo BB-UART1 > /sys/devices/bone_capemgr.9/slots")

# Adjust the connection speed as needed. 
# You can use Triimble Studio on your PC to detect/set speed
# if you can't figure it out. 
# BB-UART1 maps UART1 to /dev/ttyO1 (note capital letter O, 
# not zero)
serial = serial.Serial("/dev/ttyO1", baudrate=19200)

# Send NMEA config cmd to output only $GPRMC message every second
nmeaCmd = "$PTNLSNM,0100,01*56\r\n"
serial.write(nmeaCmd)

resp = ""

# format UTC time as hh:mm:ss
def formatTime(utc):

        return "%s:%s:%s" % (utc[0:2], utc[2:4], utc[4:6])

# format date as mm/dd/yyyy
def formatDate(date):
        return "%s/%s/20%s" % (date[2:4], date[0:2], date[4:6])

# format latitude as deg°min'sec"
def formatLat(lat):
        deg = lat[0:2]
        min = lat[2:4]
        sec = 60 * Decimal(lat[4:9])
        return "%s°%s'%0.3f\"" % (deg, min, sec)

def formatLong(long):
        deg = long[0:3]
        # If 1st digit in degrees is 0, replace with space
        if deg[0] == '0':
                deg = ' ' + deg[1:]
        min = long[3:5]
        sec = 60 * Decimal(long[5:12])
        return "%s°%s'%0.3f\"" % (deg, min, sec)

while True:
        while (serial.inWaiting() > 0):
                resp += serial.read()
                if "\r\n" in resp:
                        if "$GPRMC" in resp:
                                data = resp.split(',')
                                info = "UTC: %s  Date: %s  N Lat: %s  W Long: %s" % (
                                        formatTime(data[1]), formatDate(data[9]),
                                        formatLat(data[3]), formatLong(data[5]))
                                print info
                        resp = ""

No comments:

Post a Comment