Saturday, January 29, 2011

important gps code note

So I discovered the reason the GPS coordinates would often fail to update. In the code, I ask it to read the serial line and if it starts with $GPGGA, use that line & data. The issue is, there's multiple lines that the GPS returns. SO if, when the program reaches the if statement, and the GPS is not sending that exact line RIGHT then and there, the program moves on to the rest of the code without updating. So the GPS coords only updated every now and then, when the timing happened to be just right. This time between acquisitions could be anywhere between the 1Hz of the GPS unit and NEVER.
What I've done is created another while loop inside the main one that only focuses on reading the serial line. Once the $GPGGA line is acquired, it allows the code to move on. Now the time between acquisitions will be at MOST 1 second.
The old code looked something like this:
while(True):
    line = ser.readline()
    line = line.split(",")
    if line[0] =='$GPGGA':
       if init == True:
            currenttime,Latitude,Longitude = parse_gps(line)
            coord_i = lat_long_into_xyz(Latitude,Longitude) 
            print coord_i
            init = False
       else:
            currenttime,Latitude,Longitude = parse_gps(line)
            coord = lat_long_into_xyz(Latitude,Longitude)
            coord_x = coord[0] - coord_i[0
            coord_y = coord[1] - coord_i[1]
   ~~~rest of code~~~


The WORKING code now looks like this:
while (True)
    line = ser.readline()
    line = line.split(",")
    while(line[0] != '$GPGGA'): #Until it gets the $GPGGA line, stay here
        line = ser.readline()
        line = line.split(",")
    if init == True:
        currenttime,Latitude,Longitude = parse_gps(line)
        coord_i = lat_long_into_xyz(Latitude,Longitude) 
        print coord_i
        init = False
    else:
        currenttime,Latitude,Longitude = parse_gps(line)
    coord = lat_long_into_xyz(Latitude,Longitude)
    coord_x = coord[0] - coord_i[0]
    coord_y = coord[1] - coord_i[1]

1 comment:

  1. Great job Andy. This is still blocking the entire program until it gets a valid GPS update. The compass is thus only updated at the same data rate as the GPS. With the Venus this shouldn't be a problem. It will require more thought in the future. You might want to read a bit about threading in python.

    ReplyDelete