Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of dgps by
handle/handleGPS.cpp@16:4f5d20b87dc3, 2014-04-19 (annotated)
- Committer:
- dylanembed123
- Date:
- Sat Apr 19 14:39:19 2014 +0000
- Revision:
- 16:4f5d20b87dc3
- Parent:
- 15:e3e03a9df89e
- Child:
- 20:81d5655fecc2
Update code;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dylanembed123 | 9:da906eeac51e | 1 | #include "handleGPS.h" |
stearnsc | 8:28b866df62cf | 2 | |
dylanembed123 | 9:da906eeac51e | 3 | //Serial gps(p28,p27); |
stearnsc | 8:28b866df62cf | 4 | |
dylanembed123 | 14:6be57da62283 | 5 | void testVar(){ |
dylanembed123 | 14:6be57da62283 | 6 | } |
dylanembed123 | 7:c75d5e5e6bfc | 7 | void GPSHandle::setup(){ |
dylanembed123 | 14:6be57da62283 | 8 | //gpsGPS().getSerial().baud(57600); |
stearnsc | 8:28b866df62cf | 9 | sendGpsCommand("PMTK301,1"); |
dylanembed123 | 14:6be57da62283 | 10 | //GPS::getSerial().attach(&GPSHandle::handleUpdate,Serial::RxIrq); |
dylanembed123 | 14:6be57da62283 | 11 | GPS::getSerial().attach(this,&GPSHandle::handleUpdate,Serial::RxIrq); |
stearnsc | 8:28b866df62cf | 12 | //cs: Send other standard init commands? Not strictly speaking necessary, |
stearnsc | 8:28b866df62cf | 13 | // but forces "up to date documentation" in the form of always knowing |
stearnsc | 8:28b866df62cf | 14 | // gps config. |
dylanembed123 | 7:c75d5e5e6bfc | 15 | } |
dylanembed123 | 7:c75d5e5e6bfc | 16 | |
dylanembed123 | 14:6be57da62283 | 17 | static bool reading = false; |
dylanembed123 | 14:6be57da62283 | 18 | //static std::stringstream line; |
dylanembed123 | 14:6be57da62283 | 19 | static char line[MAXREADIN+10]; |
dylanembed123 | 14:6be57da62283 | 20 | static int line_i=0; |
dylanembed123 | 14:6be57da62283 | 21 | char* getNext(char*& field){ |
dylanembed123 | 14:6be57da62283 | 22 | char* output=new char[MAXREADIN+1]; |
dylanembed123 | 14:6be57da62283 | 23 | int i; |
dylanembed123 | 14:6be57da62283 | 24 | for(i=0;i<MAXREADIN;i++){ |
dylanembed123 | 14:6be57da62283 | 25 | if(field[i]=='\0'||field[i]==',')break; |
dylanembed123 | 14:6be57da62283 | 26 | } |
dylanembed123 | 14:6be57da62283 | 27 | for(int a=0;a<i;a++){ |
dylanembed123 | 14:6be57da62283 | 28 | output[a]=field[a]; |
dylanembed123 | 14:6be57da62283 | 29 | } |
dylanembed123 | 14:6be57da62283 | 30 | output[i]='\0'; |
dylanembed123 | 14:6be57da62283 | 31 | field=&field[i+1]; |
dylanembed123 | 14:6be57da62283 | 32 | return output; |
dylanembed123 | 14:6be57da62283 | 33 | } |
dylanembed123 | 14:6be57da62283 | 34 | |
stearnsc | 8:28b866df62cf | 35 | void GPSHandle::handleUpdate(){ |
dylanembed123 | 14:6be57da62283 | 36 | if(GPS::getSerial().readable()<=0){return;} |
dylanembed123 | 14:6be57da62283 | 37 | char c = GPS::getSerial().getc(); |
dylanembed123 | 14:6be57da62283 | 38 | //USB::getSerial().printf("%c",c); |
stearnsc | 8:28b866df62cf | 39 | if (reading) { |
dylanembed123 | 14:6be57da62283 | 40 | if(line_i>=MAXREADIN){reading=false;return;} |
stearnsc | 8:28b866df62cf | 41 | if (c == '*') { //sentence buffer complete; we're ignoring the checksum |
dylanembed123 | 14:6be57da62283 | 42 | char* field=line; |
dylanembed123 | 14:6be57da62283 | 43 | char* op; |
dylanembed123 | 14:6be57da62283 | 44 | op=getNext(field);delete op; //GPGGA |
dylanembed123 | 14:6be57da62283 | 45 | if(op[0]=='G'||op[1]=='P'||op[2]=='G'||op[3]=='G'||op[4]=='A'){ |
dylanembed123 | 14:6be57da62283 | 46 | op=getNext(field);double timeS = atof(op);delete op; //time |
dylanembed123 | 14:6be57da62283 | 47 | op=getNext(field);double latitude = atof(op);delete op; //latitude |
dylanembed123 | 14:6be57da62283 | 48 | op=getNext(field);delete op; //N or S |
dylanembed123 | 14:6be57da62283 | 49 | op=getNext(field);double longitude = atof(op);delete op; //longitude |
dylanembed123 | 14:6be57da62283 | 50 | op=getNext(field);delete op; //E or W |
dylanembed123 | 14:6be57da62283 | 51 | op=getNext(field);delete op; //skip |
dylanembed123 | 14:6be57da62283 | 52 | op=getNext(field);delete op; //skip |
dylanembed123 | 14:6be57da62283 | 53 | op=getNext(field);delete op; //skip |
dylanembed123 | 14:6be57da62283 | 54 | op=getNext(field);delete op; //altitude |
dylanembed123 | 14:6be57da62283 | 55 | double altitude = atof(op); |
dylanembed123 | 16:4f5d20b87dc3 | 56 | if(timeS>0.5f){ |
dylanembed123 | 15:e3e03a9df89e | 57 | //USB::getSerial().printf("\nMy GPS data: Lat: %f, Lon: %f, Alt: %f, Time:%f\r\n",latitude,longitude,altitude,timeS); |
dylanembed123 | 15:e3e03a9df89e | 58 | DH::Locs().add(LHType_locs,DataLocation(latitude,longitude,altitude,timeS)); |
dylanembed123 | 15:e3e03a9df89e | 59 | //USB::getSerial().printf("Current Time:%f\r\n",DH::Locs().getC().getTime()); |
dylanembed123 | 16:4f5d20b87dc3 | 60 | } |
dylanembed123 | 14:6be57da62283 | 61 | } |
stearnsc | 8:28b866df62cf | 62 | //update whatever needs updating when gps updates |
stearnsc | 8:28b866df62cf | 63 | // pc.printf("My GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n", |
stearnsc | 8:28b866df62cf | 64 | // gpsData.latitude, gpsData.longitude, gpsData.altitude, gpsData.time |
stearnsc | 8:28b866df62cf | 65 | // ); |
stearnsc | 8:28b866df62cf | 66 | |
stearnsc | 8:28b866df62cf | 67 | reading = false; |
stearnsc | 8:28b866df62cf | 68 | } else { |
dylanembed123 | 14:6be57da62283 | 69 | line[line_i]=c; |
dylanembed123 | 14:6be57da62283 | 70 | line_i=(line_i+1)%MAXREADIN; |
stearnsc | 8:28b866df62cf | 71 | } |
stearnsc | 8:28b866df62cf | 72 | |
stearnsc | 8:28b866df62cf | 73 | } else if (c == '$') { |
stearnsc | 8:28b866df62cf | 74 | reading = true; |
dylanembed123 | 14:6be57da62283 | 75 | line_i=0; |
stearnsc | 8:28b866df62cf | 76 | } |
dylanembed123 | 14:6be57da62283 | 77 | return; |
stearnsc | 8:28b866df62cf | 78 | } |
stearnsc | 8:28b866df62cf | 79 | |
stearnsc | 8:28b866df62cf | 80 | //sends: "$<command>*<checksum>\r\l" |
dylanembed123 | 9:da906eeac51e | 81 | void GPSHandle::sendGpsCommand(std::string command){ |
stearnsc | 8:28b866df62cf | 82 | uint8_t checksum = 0; |
dylanembed123 | 9:da906eeac51e | 83 | //pc.printf("Sending command to gps: "); |
dylanembed123 | 14:6be57da62283 | 84 | GPS::getSerial().putc('$'); |
dylanembed123 | 9:da906eeac51e | 85 | //pc.putc('$'); |
stearnsc | 8:28b866df62cf | 86 | char c; |
stearnsc | 8:28b866df62cf | 87 | for (int i = 0; i < command.length(); i++) { |
stearnsc | 8:28b866df62cf | 88 | c = command[i]; |
stearnsc | 8:28b866df62cf | 89 | checksum ^= c; |
dylanembed123 | 14:6be57da62283 | 90 | GPS::getSerial().putc(c); |
dylanembed123 | 9:da906eeac51e | 91 | //pc.putc(c); |
stearnsc | 8:28b866df62cf | 92 | } |
dylanembed123 | 14:6be57da62283 | 93 | GPS::getSerial().putc('*'); |
dylanembed123 | 9:da906eeac51e | 94 | //pc.putc('*'); |
stearnsc | 8:28b866df62cf | 95 | string checkSumString; |
stearnsc | 8:28b866df62cf | 96 | while (checksum > 0) { |
stearnsc | 8:28b866df62cf | 97 | uint8_t checksumChar = checksum & 0x0F; |
stearnsc | 8:28b866df62cf | 98 | if (checksumChar >= 10) { |
stearnsc | 8:28b866df62cf | 99 | checksumChar -= 10; |
stearnsc | 8:28b866df62cf | 100 | checksumChar += 'A'; |
stearnsc | 8:28b866df62cf | 101 | } else { |
stearnsc | 8:28b866df62cf | 102 | checksumChar += '0'; |
stearnsc | 8:28b866df62cf | 103 | } |
stearnsc | 8:28b866df62cf | 104 | checkSumString.push_back((char) checksumChar); |
stearnsc | 8:28b866df62cf | 105 | checksum = checksum >> 4; |
stearnsc | 8:28b866df62cf | 106 | } |
stearnsc | 8:28b866df62cf | 107 | |
stearnsc | 8:28b866df62cf | 108 | for (int i = checkSumString.length() - 1; i >= 0; i--) { |
dylanembed123 | 14:6be57da62283 | 109 | GPS::getSerial().putc(checkSumString[i]); |
dylanembed123 | 9:da906eeac51e | 110 | //pc.putc(checkSumString[i]); |
stearnsc | 8:28b866df62cf | 111 | } |
dylanembed123 | 14:6be57da62283 | 112 | GPS::getSerial().putc('\r'); |
dylanembed123 | 9:da906eeac51e | 113 | //pc.putc('\r'); |
dylanembed123 | 14:6be57da62283 | 114 | GPS::getSerial().putc('\n'); |
dylanembed123 | 9:da906eeac51e | 115 | //pc.putc('\n'); |
stearnsc | 8:28b866df62cf | 116 | } |
stearnsc | 8:28b866df62cf | 117 | |
stearnsc | 8:28b866df62cf | 118 | int stringToDecimal(string s) |
stearnsc | 8:28b866df62cf | 119 | { |
stearnsc | 8:28b866df62cf | 120 | int mult = 1; |
stearnsc | 8:28b866df62cf | 121 | int result = 0; |
stearnsc | 8:28b866df62cf | 122 | for (int i = s.length() - 1; i >=0; i--) { |
stearnsc | 8:28b866df62cf | 123 | if (s[i] != '.') { |
stearnsc | 8:28b866df62cf | 124 | result += (s[i] - '0') * mult; |
stearnsc | 8:28b866df62cf | 125 | mult *= 10; |
dylanembed123 | 7:c75d5e5e6bfc | 126 | } |
dylanembed123 | 7:c75d5e5e6bfc | 127 | } |
stearnsc | 8:28b866df62cf | 128 | return result; |
dylanembed123 | 7:c75d5e5e6bfc | 129 | } |
dylanembed123 | 7:c75d5e5e6bfc | 130 | |
dylanembed123 | 7:c75d5e5e6bfc | 131 | bool GPSHandle::check(){ |
dylanembed123 | 7:c75d5e5e6bfc | 132 | return true; |
dylanembed123 | 14:6be57da62283 | 133 | } |
dylanembed123 | 14:6be57da62283 | 134 | void GPSHandle::run(){ |
dylanembed123 | 14:6be57da62283 | 135 | if(!initialized){initialized=true;setup();} |
dylanembed123 | 7:c75d5e5e6bfc | 136 | } |