QC Control software

Dependencies:   mbed

Fork of dgps by Colin Stearns

Committer:
dylanembed123
Date:
Thu Apr 10 02:19:07 2014 +0000
Revision:
14:6be57da62283
Parent:
9:da906eeac51e
Child:
15:e3e03a9df89e
Update GPS;

Who changed what in which revision?

UserRevisionLine numberNew 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);
stearnsc 8:28b866df62cf 56
dylanembed123 14:6be57da62283 57 USB::getSerial().printf("\nMy GPS data: Lat: %f, Lon: %f, Alt: %f, Time:%f\r\n",latitude,longitude,altitude,timeS);
dylanembed123 14:6be57da62283 58 Locs().add(LHType_locs,DataLocation(latitude,longitude,altitude,timeS));
dylanembed123 14:6be57da62283 59 }
stearnsc 8:28b866df62cf 60 //update whatever needs updating when gps updates
stearnsc 8:28b866df62cf 61 // pc.printf("My GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n",
stearnsc 8:28b866df62cf 62 // gpsData.latitude, gpsData.longitude, gpsData.altitude, gpsData.time
stearnsc 8:28b866df62cf 63 // );
stearnsc 8:28b866df62cf 64
stearnsc 8:28b866df62cf 65 reading = false;
stearnsc 8:28b866df62cf 66 } else {
dylanembed123 14:6be57da62283 67 line[line_i]=c;
dylanembed123 14:6be57da62283 68 line_i=(line_i+1)%MAXREADIN;
stearnsc 8:28b866df62cf 69 }
stearnsc 8:28b866df62cf 70
stearnsc 8:28b866df62cf 71 } else if (c == '$') {
stearnsc 8:28b866df62cf 72 reading = true;
dylanembed123 14:6be57da62283 73 line_i=0;
stearnsc 8:28b866df62cf 74 }
dylanembed123 14:6be57da62283 75 return;
stearnsc 8:28b866df62cf 76 }
stearnsc 8:28b866df62cf 77
stearnsc 8:28b866df62cf 78 //sends: "$<command>*<checksum>\r\l"
dylanembed123 9:da906eeac51e 79 void GPSHandle::sendGpsCommand(std::string command){
stearnsc 8:28b866df62cf 80 uint8_t checksum = 0;
dylanembed123 9:da906eeac51e 81 //pc.printf("Sending command to gps: ");
dylanembed123 14:6be57da62283 82 GPS::getSerial().putc('$');
dylanembed123 9:da906eeac51e 83 //pc.putc('$');
stearnsc 8:28b866df62cf 84 char c;
stearnsc 8:28b866df62cf 85 for (int i = 0; i < command.length(); i++) {
stearnsc 8:28b866df62cf 86 c = command[i];
stearnsc 8:28b866df62cf 87 checksum ^= c;
dylanembed123 14:6be57da62283 88 GPS::getSerial().putc(c);
dylanembed123 9:da906eeac51e 89 //pc.putc(c);
stearnsc 8:28b866df62cf 90 }
dylanembed123 14:6be57da62283 91 GPS::getSerial().putc('*');
dylanembed123 9:da906eeac51e 92 //pc.putc('*');
stearnsc 8:28b866df62cf 93 string checkSumString;
stearnsc 8:28b866df62cf 94 while (checksum > 0) {
stearnsc 8:28b866df62cf 95 uint8_t checksumChar = checksum & 0x0F;
stearnsc 8:28b866df62cf 96 if (checksumChar >= 10) {
stearnsc 8:28b866df62cf 97 checksumChar -= 10;
stearnsc 8:28b866df62cf 98 checksumChar += 'A';
stearnsc 8:28b866df62cf 99 } else {
stearnsc 8:28b866df62cf 100 checksumChar += '0';
stearnsc 8:28b866df62cf 101 }
stearnsc 8:28b866df62cf 102 checkSumString.push_back((char) checksumChar);
stearnsc 8:28b866df62cf 103 checksum = checksum >> 4;
stearnsc 8:28b866df62cf 104 }
stearnsc 8:28b866df62cf 105
stearnsc 8:28b866df62cf 106 for (int i = checkSumString.length() - 1; i >= 0; i--) {
dylanembed123 14:6be57da62283 107 GPS::getSerial().putc(checkSumString[i]);
dylanembed123 9:da906eeac51e 108 //pc.putc(checkSumString[i]);
stearnsc 8:28b866df62cf 109 }
dylanembed123 14:6be57da62283 110 GPS::getSerial().putc('\r');
dylanembed123 9:da906eeac51e 111 //pc.putc('\r');
dylanembed123 14:6be57da62283 112 GPS::getSerial().putc('\n');
dylanembed123 9:da906eeac51e 113 //pc.putc('\n');
stearnsc 8:28b866df62cf 114 }
stearnsc 8:28b866df62cf 115
stearnsc 8:28b866df62cf 116 int stringToDecimal(string s)
stearnsc 8:28b866df62cf 117 {
stearnsc 8:28b866df62cf 118 int mult = 1;
stearnsc 8:28b866df62cf 119 int result = 0;
stearnsc 8:28b866df62cf 120 for (int i = s.length() - 1; i >=0; i--) {
stearnsc 8:28b866df62cf 121 if (s[i] != '.') {
stearnsc 8:28b866df62cf 122 result += (s[i] - '0') * mult;
stearnsc 8:28b866df62cf 123 mult *= 10;
dylanembed123 7:c75d5e5e6bfc 124 }
dylanembed123 7:c75d5e5e6bfc 125 }
stearnsc 8:28b866df62cf 126 return result;
dylanembed123 7:c75d5e5e6bfc 127 }
dylanembed123 7:c75d5e5e6bfc 128
dylanembed123 7:c75d5e5e6bfc 129 bool GPSHandle::check(){
dylanembed123 7:c75d5e5e6bfc 130 return true;
dylanembed123 14:6be57da62283 131 }
dylanembed123 14:6be57da62283 132 void GPSHandle::run(){
dylanembed123 14:6be57da62283 133 if(!initialized){initialized=true;setup();}
dylanembed123 7:c75d5e5e6bfc 134 }