OzzMaker
Published © GPL3+

Accessing GPS via I2C on a BerryGPS-IMU

Access GPS via I2C on the Raspberry Pi

IntermediateProtip1 hour1,175
Accessing GPS via I2C on a BerryGPS-IMU

Things used in this project

Story

Read more

Code

i2c-gps-native.py #1

Python
Talk Native I2C to ublox GPS
#! /usr/bin/python
import time
import smbus
import signal
import sys

BUS = None
address = 0x42
gpsReadInterval = 0.03

def connectBus():
    global BUS
    BUS = smbus.SMBus(1)

def parseResponse(gpsLine):

  if(gpsLine.count(36) == 1):                           # Check #1, make sure '$' doesnt appear twice
    if len(gpsLine) < 84:                               # Check #2, 83 is maximun NMEA sentenace length.
        CharError = 0;
        for c in gpsLine:                               # Check #3, Make sure that only readiable ASCII charaters and Carriage Return are seen.
            if (c < 32 or c > 122) and  c != 13:
                CharError+=1
        if (CharError == 0):#    Only proceed if there are no errors.
            gpsChars = ''.join(chr(c) for c in gpsLine)
            if (gpsChars.find('txbuf') == -1):          # Check #4, skip txbuff allocation error

                gpsStr, chkSum = gpsChars.split('*',2)  # Check #5 only split twice to avoid unpack error
                gpsComponents = gpsStr.split(',')

                chkVal = 0

                for ch in gpsStr[1:]: # Remove the $ and do a manual checksum on the rest of the NMEA sentence
                     chkVal ^= ord(ch)
                if (chkVal == int(chkSum, 16)): # Compare the calculated checksum with the one in the NMEA sentence
                     print gpsChars

def handle_ctrl_c(signal, frame):
        sys.exit(130)

#This will capture exit when using Ctrl-C
signal.signal(signal.SIGINT, handle_ctrl_c)

def readGPS():
    c = None
    response = []
    try:
        while True: # Newline, or bad char.
            c = BUS.read_byte(address)

            if c == 255:
                return False
            elif c == 10:
                break
            else:
                response.append(c)

        parseResponse(response)

    except IOError:
        connectBus()
    except Exception,e:
        print e

connectBus()

while True:
    readGPS()
    time.sleep(gpsReadInterval)

i2c-gps-bb.py #2

Python
Bit Bang Native I2C to ublox GPS
import time
import signal
import sys
import pigpio

address = 0x42
gpsReadInterval = 0.03

SDA=2
SCL=3
pi = pigpio.pi()
pi.set_pull_up_down(SDA, pigpio.PUD_UP)
pi.set_pull_up_down(SCL, pigpio.PUD_UP)
pi.bb_i2c_open(SDA,SCL,100000)

def handle_ctrl_c(signal, frame):
        pi.bb_i2c_close(SDA)
        pi.stop()
        sys.exit(130)

#This will capture exit when using Ctrl-C
signal.signal(signal.SIGINT, handle_ctrl_c)

def readGPS():
    c = None
    response = []

    while True: # Newline, or bad char.
        a=pi.bb_i2c_zip(SDA, [4, address, 2, 6, 1])  # Bit bang I2C read. 2 = Start, 6 = read, 1= How many bytes to read
        c = ord(a[1])
        if c == 255:
            return False
        elif c == 10:
            break
        else:
            response.append(c)

    gpsChars = ''.join(chr(c) for c in response)  #Convert list to string
    print gpsChars

while True:
    readGPS()
    time.sleep(gpsReadInterval)

Credits

OzzMaker

OzzMaker

6 projects • 1 follower

Comments