Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: BMP085Test
Fetching contributors…

Cannot retrieve contributors at this time

file 400 lines (335 sloc) 8.858 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
//*****************************************************************************
//
// File Name : 'gps.c'
// Title : GPS Parsing for NMEA Strings
// Original Author : Brad Luyster, LVL1 White Star Balloon Project
// Created : 2010.12.2
// Modified : I won't keep up with this
// Target MCU : Atmel AVR series
//
// http://www.whitestarballoon.com/
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include <avr/pgmspace.h>
#include <avr/wdt.h>


#include "gps.h"

//#define DISABLE_ERROR_CHECKING_THIS_IS_BAD

//$GPRMC,040302.663,A,3939.7,N,10506.6,W,0.27,358.86,200804,,*1A
//^Sentence ^Lat(HHMM.M) ^Speed (Knots)
// ^HHMMSS.XXX ^Lon(HHHMM.M) ^Bearing
// ^GPS Fix (A=active, V=invalid) ^UTC Date
void getGPS(struct gpsData *outputData)
{
// Begin GPRMC Acquisition Section
static char lineBuff[100];
char tempChar;
uint8_t i;
uint8_t errorTracker;

//Needed so the first comparison works out correctly
memset(lineBuff, 0x00, 100);

errorTracker = 0;


//loads a line into a buffer, then checks the line for the GPS line
//that we're looking for. If it takes more than 100 lines to do this,
//something is wrong.
do
{
i=0;
//i < 99 to leave room for the null terminator
while(((tempChar = (char)uart_getchar()) != '\n') && (i < 99) && tempChar!= 0xff)
{
lineBuff[i] = tempChar;
#ifndef DISABLE_ERROR_CHECKING_THIS_IS_BAD
i++;
#else
wdt_reset();
#endif
}
lineBuff[i] = '\0';
errorTracker++;
wdt_reset();
} while((strncmp("$GPRMC", lineBuff, 6) != 0) && errorTracker < 100);
// End GPRMC Acquisition Section

//lprintf("et: %d\n", errorTracker);
    wdt_reset();
//If something is wrong, return before we try to parse the data.
//Set the output status to "Loco".
if(errorTracker >= 100)
{
lprintf_P(PSTR("ERR\n"));
outputData->status = 3;
return;
}

char targetChecksum[3];
uint8_t tSum;
uint8_t checksum = 0;
targetChecksum[0] = lineBuff[i-3];
targetChecksum[1] = lineBuff[i-2];
targetChecksum[2] = '\0';
sscanf(targetChecksum, "%x", &tSum);
for(uint8_t j = 1; j < i-4; j++)
{
checksum = checksum ^ lineBuff[j];
}

/*if(checksum != tSum)
{
outputData->status = 2;
return;
}*/

// Start GPRMC Parse Section
char latdir;
char londir;
int latDegrees;
float latMinutes;
int lonDegrees;
float lonMinutes;
float speed, bearing;
float time, altitude;
char tempStatus;

char localStatus;

int error;

error = sscanf( lineBuff,
"$GPRMC,%f,%c,%f,%c,%f,%c,%f,%f",
&time, &tempStatus, &outputData->latitude, &latdir,
&outputData->longitude, &londir, &speed, &bearing);


outputData->hours = (uint8_t)(time/10000);
outputData->minutes = (uint8_t)(time - (outputData->hours)*10000)/100;
outputData->seconds = (uint8_t)(time - (outputData->hours)*10000 - (outputData->seconds)*100);

if(latdir == 'S')
outputData->latitude = 0-(outputData->latitude);
if(londir == 'W')
outputData->longitude = 0-(outputData->longitude);

latDegrees = (int)(outputData->latitude/100);
latMinutes = (float)(outputData->latitude - latDegrees*100);
outputData->latitude = latDegrees + (latMinutes/60); //Conversion into decimal degrees

lonDegrees = (int)(outputData->longitude/100);
lonMinutes = (float)(outputData->longitude - lonDegrees*100);
outputData->longitude = lonDegrees + (lonMinutes/60); //Conversion into decimal degrees

outputData->speed = (uint8_t)speed;
outputData->bearing = (uint16_t)bearing;
// End GPRMC Parse Section



// Begin GPGSA Acquisition Section

//Needed so the first comparison works out correctly
memset(lineBuff, 0x00, 100);

errorTracker = 0;
do
{
i=0;
//i < 99 to leave room for the null terminator
while(((tempChar = (char)uart_getchar()) != '\n') && i < 99)
{
lineBuff[i] = tempChar;
#ifndef DISABLE_ERROR_CHECKING_THIS_IS_BAD
i++;
#else
wdt_reset();
#endif
}
errorTracker++;
wdt_reset();
lineBuff[i] = '\0';
} while((strncmp("$GPGSA", lineBuff, 6) != 0) && errorTracker < 100);
// End GPGSA Acquisition Section
    wdt_reset();
if(errorTracker >= 100)
{
outputData->status = 3;
return;
}

checksum = 0;
targetChecksum[0] = lineBuff[i-3];
targetChecksum[1] = lineBuff[i-2];
targetChecksum[2] = '\0';
sscanf(targetChecksum, "%x", &tSum);
for(uint8_t j = 1; j < i-4; j++)
{
checksum = checksum ^ lineBuff[j];
}

/*if(checksum != tSum)
{
outputData->status = 2;
return;
}*/


float tempVdop, tempHdop;
// Start GPGSA Parse Section

char *token, *lastToken, *beforeThat;
token = strtok(lineBuff, ",");
while(token != NULL)
{
beforeThat = lastToken;
lastToken = token;
token = strtok(NULL,",");
}
sscanf(beforeThat, "%f", &tempHdop);
sscanf(lastToken, "%f*", &tempVdop);

outputData->hdop = (uint16_t)(tempHdop*10);
outputData->vdop = (uint16_t)(tempVdop*10);
// End GPGSA Parse Section

// Begin GPGGA Acquisition Section

//Needed so the first comparison works out correctly
memset(lineBuff, 0x00, 100);

errorTracker = 0;
do
{
i=0;
//i < 99 to leave room for the null terminator
while(((tempChar = (char)uart_getchar()) != '\n') && (i < 99) && tempChar!= 0xff)
{
lineBuff[i] = tempChar;
#ifndef DISABLE_ERROR_CHECKING_THIS_IS_BAD
i++;
#else
wdt_reset();
#endif
}
lineBuff[i] = '\0';
errorTracker++;
wdt_reset();
} while((strncmp("$GPGGA", lineBuff, 6) != 0) && errorTracker < 100);
// End GPGGA Acquisition Section
    wdt_reset();
if(errorTracker >= 100)
{
outputData->status = 3;
return;
}

checksum = 0;
targetChecksum[0] = lineBuff[i-3];
targetChecksum[1] = lineBuff[i-2];
targetChecksum[2] = '\0';
sscanf(targetChecksum, "%x", &tSum);
for(uint8_t j = 1; j < i-4; j++)
{
checksum = checksum ^ lineBuff[j];
}

/*if(checksum != tSum)
{
outputData->status = 2;
return;
}*/

// Start GPGGA Parse Section



sscanf( lineBuff,
"$GPGGA,%*f,%*f,%*c,%*f,%*c,%*d,%d,%*f,%f",
&outputData->numberOfSats, &altitude );

outputData->altitude = (uint16_t)altitude;
// End GPGGA Parse Section

switch(tempStatus)
{
case 'A':
outputData->status = 0;
break;
case 'V':
outputData->status = 1;
break;
}

}

//REMOVE BEFORE FLIGHT
void debugPrintRawStrings(void)
{

static char lineBuff[100];
char tempChar;
uint8_t i;
uint8_t errorTracker;
//Needed so the first comparison works out correctly
memset(lineBuff, 0x00, 100);

lprintf_P(PSTR("In Ur GPS Debug\n"));

do
{
i=0;
//i < 99 to leave room for the null terminator
while(((tempChar = (char)uart_getchar()) != '\n') && (i < 99) && tempChar!= 0xff)
{
lineBuff[i] = tempChar;
i++;
}
lineBuff[i] = '\0';
errorTracker++;
} while((strncmp("$GPRMC", lineBuff, 6) != 0));



char targetChecksum[3];
uint8_t tSum;
uint8_t checksum = 0;
targetChecksum[0] = lineBuff[i-3];
targetChecksum[1] = lineBuff[i-2];
targetChecksum[2] = '\0';
sscanf(targetChecksum, "%x", &tSum);
for(uint8_t j = 1; j < i-4; j++)
{
checksum = checksum ^ lineBuff[j];
}

lprintf_P(PSTR("CS: %x TCS: %x\n"), checksum, tSum);

for(uint8_t j = 0; j < i; j++)
{
lprintf("%c", lineBuff[j]);
}
if(tSum == checksum)
{
lprintf_P(PSTR("Checksum Valid\n"));
}

memset(lineBuff, 0x00, 100);

do
{
i=0;
//i < 99 to leave room for the null terminator
while(((tempChar = (char)uart_getchar()) != '\n') && i < 99)
{
lineBuff[i] = tempChar;
i++;
}
lineBuff[i] = '\0';
} while((strncmp("$GPGSA", lineBuff, 6) != 0));
// End GPGSA Acquisition Section

checksum = 0;
targetChecksum[0] = lineBuff[i-3];
targetChecksum[1] = lineBuff[i-2];
targetChecksum[2] = '\0';
sscanf(targetChecksum, "%x", &tSum);
for(uint8_t j = 1; j < i-4; j++)
{
checksum = checksum ^ lineBuff[j];
}

lprintf_P(PSTR("CS: %x TCS: %x\n"), checksum, tSum);

for(uint8_t j = 0; j < i; j++)
{
lprintf("%c", lineBuff[j]);
}
if(tSum == checksum)
{
lprintf_P(PSTR("Checksum Valid\n"));
}

memset(lineBuff, 0x00, 100);
do
{
i=0;
//i < 99 to leave room for the null terminator
while(((tempChar = (char)uart_getchar()) != '\n') && i < 99)
{
lineBuff[i] = tempChar;
i++;
}
lineBuff[i] = '\0';
} while((strncmp("$GPGGA", lineBuff, 6) != 0));
// End GPGGA Acquisition Section

checksum = 0;
targetChecksum[0] = lineBuff[i-3];
targetChecksum[1] = lineBuff[i-2];
targetChecksum[2] = '\0';
sscanf(targetChecksum, "%x", &tSum);
for(uint8_t j = 1; j < i-4; j++)
{
checksum = checksum ^ lineBuff[j];
}

lprintf_P(PSTR("CS: %x TCS: %x\n"), checksum, tSum);

for(uint8_t j = 0; j < i; j++)
{
lprintf("%c", lineBuff[j]);
}
if(tSum == checksum)
{
lprintf_P(PSTR("Checksum Valid\n"));
}

}
Something went wrong with that request. Please try again.