Colin Stearns
/
qcControl
QC Control software
Fork of dgps by
Revision 0:9c001c4e7bf4, committed 2014-02-16
- Comitter:
- stearnsc
- Date:
- Sun Feb 16 21:26:54 2014 +0000
- Child:
- 1:aef1562834b4
- Child:
- 2:f480097b329d
- Commit message:
- cs: First pass of gps parsing
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Feb 16 21:26:54 2014 +0000 @@ -0,0 +1,160 @@ +#include "mbed.h" +#include <string> +#include <sstream> + +Serial pc(USBTX,USBRX); +Serial xbee(p9,p10);//tx, rx +Serial gps(p28,p27); + +typedef struct { + int latitude; //in .0001 minutes + int longitude; //in .0001 minutes + int altitude; //in decimeters + int time; //in milliseconds +} GpsData; + +GpsData gpsData; +GpsData otherGps; + +void readSerial(Serial &s, char str[], int size){ + for (int i = 0; i < size; i++){ + str[i] = s.getc(); + } +} + +//sends: "$<command>*<checksum>\r\l" +void sendGpsCommand(string command){ + uint8_t checksum = 0; + pc.printf("Sending command to gps: "); + gps.putc('$'); + pc.putc('$'); + char c; + for (int i = 0; i < command.length(); i++){ + c = command[i]; + checksum ^= c; + gps.putc(c); + pc.putc(c); + } + gps.putc('*'); + pc.putc('*'); + string checkSumString; + while (checksum > 0){ + uint8_t checksumChar = checksum & 0x0F; + if (checksumChar >= 10){ + checksumChar -= 10; + checksumChar += 'A'; + } else { + checksumChar += '0'; + } + checkSumString.push_back((char) checksumChar); + checksum = checksum >> 4; + } + + for (int i = checkSumString.length() - 1; i >= 0; i--){ + gps.putc(checkSumString[i]); + pc.putc(checkSumString[i]); + } + gps.putc('\r'); + pc.putc('\r'); + gps.putc('\n'); + pc.putc('\n'); +} + +long parseDec(string s){ + int mult = 1; + int result = 0; + for (int i = s.length() - 1; i >=0; i--){ + if (s[i] != '.'){ + result += (s[i] - '0') * mult; + mult *= 10; + } + } + return result; +} + +//cs: little endian parsing +int nextInt(char *data, int i){ + i |= data[i]; + i |= (data[i + 1] << 8); + i |= (data[i + 2] << 16); + i |= (data[i + 3] << 24); + return i; +} + +void handleXbeeGps(){ + static bool reading = false; + static char packet[16]; + static int i = 0; + + char c = xbee.getc(); + if (reading){ + packet[i] = c; + i++; + if (i == 16){ + i = 0; + otherGps.latitude = nextInt(packet, 0); + otherGps.longitude = nextInt(packet, 4); + otherGps.altitude = nextInt(packet, 8); + otherGps.time = nextInt(packet, 12); + + pc.printf("His GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n", + otherGps.latitude, otherGps.longitude, otherGps.altitude, otherGps.time + ); + reading = false; + } + } else if (c == 'X'){ + reading = true; + } +} + +void handleGpsData(){ + static bool reading = false; + static stringstream line; + + char c = gps.getc(); + + if (reading){ + if (c == '*'){ //sentence buffer complete; we're ignoring the checksum + string field; + std::getline(line, field, ','); //GPGGA + std::getline(line, field, ','); //time + gpsData.time = parseDec(field); + std::getline(line, field, ','); //latitude + gpsData.latitude = parseDec(field); + std::getline(line, field, ','); //N or S + std::getline(line, field, ','); //longitude + gpsData.longitude = parseDec(field); + std::getline(line, field, ','); //E or W + std::getline(line, field, ','); //skip + std::getline(line, field, ','); //skip + std::getline(line, field, ','); //skip + std::getline(line, field, ','); //altitude + gpsData.altitude = parseDec(field); + + //update whatever needs updating when gps updates + pc.printf("My GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n", + gpsData.latitude, gpsData.longitude, gpsData.altitude, gpsData.time + ); + + line.str(string("")); + reading = false; + + } else { + line.put(c); + } + + } else if (c == '$') { + reading = true; + } +} + +int main(){ + gps.baud(57600); + xbee.baud(9600); + pc.baud(57600); + +// sendGpsCommand("PMTK301,1"); +// while(true){pc.putc(gps.getc());} + gps.attach(&handleGpsData, Serial::RxIrq); +// xbee.attach(&handleXbeeGps, Serial::RxIrq); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Feb 16 21:26:54 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/a9913a65894f \ No newline at end of file