Colin Stearns / Mbed 2 deprecated qcControl

Dependencies:   mbed

Fork of dgps by Colin Stearns

Committer:
stearnsc
Date:
Tue Apr 01 17:58:35 2014 +0000
Revision:
8:28b866df62cf
Parent:
7:c75d5e5e6bfc
Child:
9:da906eeac51e
cs: move GPS parsing into handleGPS; initial flightControl implemented. Doesn't compile.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stearnsc 8:28b866df62cf 1 #include "mbed.h"
stearnsc 8:28b866df62cf 2
stearnsc 8:28b866df62cf 3 Serial gps(p28,p27);
stearnsc 8:28b866df62cf 4
dylanembed123 7:c75d5e5e6bfc 5 void GPSHandle::setup(){
stearnsc 8:28b866df62cf 6 gps.baud(57600);
stearnsc 8:28b866df62cf 7 sendGpsCommand("PMTK301,1");
stearnsc 8:28b866df62cf 8 gps.attach(this, &handleUpdate, Serial::RxIrq);
stearnsc 8:28b866df62cf 9 //cs: Send other standard init commands? Not strictly speaking necessary,
stearnsc 8:28b866df62cf 10 // but forces "up to date documentation" in the form of always knowing
stearnsc 8:28b866df62cf 11 // gps config.
dylanembed123 7:c75d5e5e6bfc 12 }
dylanembed123 7:c75d5e5e6bfc 13
stearnsc 8:28b866df62cf 14 void GPSHandle::handleUpdate(){
stearnsc 8:28b866df62cf 15 {
stearnsc 8:28b866df62cf 16 static bool reading = false;
stearnsc 8:28b866df62cf 17 static stringstream line;
stearnsc 8:28b866df62cf 18
stearnsc 8:28b866df62cf 19 char c = gps.getc();
stearnsc 8:28b866df62cf 20
stearnsc 8:28b866df62cf 21 if (reading) {
stearnsc 8:28b866df62cf 22 if (c == '*') { //sentence buffer complete; we're ignoring the checksum
stearnsc 8:28b866df62cf 23 string field;
stearnsc 8:28b866df62cf 24 std::getline(line, field, ','); //GPGGA
stearnsc 8:28b866df62cf 25 std::getline(line, field, ','); //time
stearnsc 8:28b866df62cf 26 gpsData.time = stringToDecimal(field);
stearnsc 8:28b866df62cf 27 std::getline(line, field, ','); //latitude
stearnsc 8:28b866df62cf 28 gpsData.latitude = stringToDecimal(field);
stearnsc 8:28b866df62cf 29 std::getline(line, field, ','); //N or S
stearnsc 8:28b866df62cf 30 std::getline(line, field, ','); //longitude
stearnsc 8:28b866df62cf 31 gpsData.longitude = stringToDecimal(field);
stearnsc 8:28b866df62cf 32 std::getline(line, field, ','); //E or W
stearnsc 8:28b866df62cf 33 std::getline(line, field, ','); //skip
stearnsc 8:28b866df62cf 34 std::getline(line, field, ','); //skip
stearnsc 8:28b866df62cf 35 std::getline(line, field, ','); //skip
stearnsc 8:28b866df62cf 36 std::getline(line, field, ','); //altitude
stearnsc 8:28b866df62cf 37 gpsData.altitude = stringToDecimal(field);
stearnsc 8:28b866df62cf 38
stearnsc 8:28b866df62cf 39 //update whatever needs updating when gps updates
stearnsc 8:28b866df62cf 40 // pc.printf("My GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n",
stearnsc 8:28b866df62cf 41 // gpsData.latitude, gpsData.longitude, gpsData.altitude, gpsData.time
stearnsc 8:28b866df62cf 42 // );
stearnsc 8:28b866df62cf 43
stearnsc 8:28b866df62cf 44 line.str(string(""));
stearnsc 8:28b866df62cf 45 reading = false;
stearnsc 8:28b866df62cf 46
stearnsc 8:28b866df62cf 47 } else {
stearnsc 8:28b866df62cf 48 line.put(c);
stearnsc 8:28b866df62cf 49 }
stearnsc 8:28b866df62cf 50
stearnsc 8:28b866df62cf 51 } else if (c == '$') {
stearnsc 8:28b866df62cf 52 reading = true;
stearnsc 8:28b866df62cf 53 }
stearnsc 8:28b866df62cf 54 }
stearnsc 8:28b866df62cf 55
stearnsc 8:28b866df62cf 56 //sends: "$<command>*<checksum>\r\l"
stearnsc 8:28b866df62cf 57 void sendGpsCommand(string command)
stearnsc 8:28b866df62cf 58 {
stearnsc 8:28b866df62cf 59 uint8_t checksum = 0;
stearnsc 8:28b866df62cf 60 pc.printf("Sending command to gps: ");
stearnsc 8:28b866df62cf 61 gps.putc('$');
stearnsc 8:28b866df62cf 62 pc.putc('$');
stearnsc 8:28b866df62cf 63 char c;
stearnsc 8:28b866df62cf 64 for (int i = 0; i < command.length(); i++) {
stearnsc 8:28b866df62cf 65 c = command[i];
stearnsc 8:28b866df62cf 66 checksum ^= c;
stearnsc 8:28b866df62cf 67 gps.putc(c);
stearnsc 8:28b866df62cf 68 pc.putc(c);
stearnsc 8:28b866df62cf 69 }
stearnsc 8:28b866df62cf 70 gps.putc('*');
stearnsc 8:28b866df62cf 71 pc.putc('*');
stearnsc 8:28b866df62cf 72 string checkSumString;
stearnsc 8:28b866df62cf 73 while (checksum > 0) {
stearnsc 8:28b866df62cf 74 uint8_t checksumChar = checksum & 0x0F;
stearnsc 8:28b866df62cf 75 if (checksumChar >= 10) {
stearnsc 8:28b866df62cf 76 checksumChar -= 10;
stearnsc 8:28b866df62cf 77 checksumChar += 'A';
stearnsc 8:28b866df62cf 78 } else {
stearnsc 8:28b866df62cf 79 checksumChar += '0';
stearnsc 8:28b866df62cf 80 }
stearnsc 8:28b866df62cf 81 checkSumString.push_back((char) checksumChar);
stearnsc 8:28b866df62cf 82 checksum = checksum >> 4;
stearnsc 8:28b866df62cf 83 }
stearnsc 8:28b866df62cf 84
stearnsc 8:28b866df62cf 85 for (int i = checkSumString.length() - 1; i >= 0; i--) {
stearnsc 8:28b866df62cf 86 gps.putc(checkSumString[i]);
stearnsc 8:28b866df62cf 87 pc.putc(checkSumString[i]);
stearnsc 8:28b866df62cf 88 }
stearnsc 8:28b866df62cf 89 gps.putc('\r');
stearnsc 8:28b866df62cf 90 pc.putc('\r');
stearnsc 8:28b866df62cf 91 gps.putc('\n');
stearnsc 8:28b866df62cf 92 pc.putc('\n');
stearnsc 8:28b866df62cf 93 }
stearnsc 8:28b866df62cf 94
stearnsc 8:28b866df62cf 95 int stringToDecimal(string s)
stearnsc 8:28b866df62cf 96 {
stearnsc 8:28b866df62cf 97 int mult = 1;
stearnsc 8:28b866df62cf 98 int result = 0;
stearnsc 8:28b866df62cf 99 for (int i = s.length() - 1; i >=0; i--) {
stearnsc 8:28b866df62cf 100 if (s[i] != '.') {
stearnsc 8:28b866df62cf 101 result += (s[i] - '0') * mult;
stearnsc 8:28b866df62cf 102 mult *= 10;
dylanembed123 7:c75d5e5e6bfc 103 }
dylanembed123 7:c75d5e5e6bfc 104 }
stearnsc 8:28b866df62cf 105 return result;
dylanembed123 7:c75d5e5e6bfc 106 }
dylanembed123 7:c75d5e5e6bfc 107
dylanembed123 7:c75d5e5e6bfc 108 bool GPSHandle::check(){
dylanembed123 7:c75d5e5e6bfc 109 return true;
dylanembed123 7:c75d5e5e6bfc 110 }