Colin Stearns / Mbed 2 deprecated qcControl

Dependencies:   mbed

Fork of dgps by Colin Stearns

Committer:
krobertson
Date:
Tue Apr 22 15:39:27 2014 +0000
Revision:
30:327191ff57e8
Parent:
20:81d5655fecc2
Child:
31:6f68fa0aeee5
am i screwed

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
krobertson 20:81d5655fecc2 5 GPSHandle* GPSHandle::hand = NULL;
krobertson 20:81d5655fecc2 6
dylanembed123 14:6be57da62283 7 void testVar(){
dylanembed123 14:6be57da62283 8 }
dylanembed123 7:c75d5e5e6bfc 9 void GPSHandle::setup(){
dylanembed123 14:6be57da62283 10 //gpsGPS().getSerial().baud(57600);
stearnsc 8:28b866df62cf 11 sendGpsCommand("PMTK301,1");
dylanembed123 14:6be57da62283 12 //GPS::getSerial().attach(&GPSHandle::handleUpdate,Serial::RxIrq);
dylanembed123 14:6be57da62283 13 GPS::getSerial().attach(this,&GPSHandle::handleUpdate,Serial::RxIrq);
stearnsc 8:28b866df62cf 14 //cs: Send other standard init commands? Not strictly speaking necessary,
stearnsc 8:28b866df62cf 15 // but forces "up to date documentation" in the form of always knowing
stearnsc 8:28b866df62cf 16 // gps config.
dylanembed123 7:c75d5e5e6bfc 17 }
dylanembed123 7:c75d5e5e6bfc 18
krobertson 20:81d5655fecc2 19 char GPSHandle::readWaypoints(){
krobertson 20:81d5655fecc2 20 USB::getSerial().printf("getting waypoitns\r\n");
krobertson 20:81d5655fecc2 21 PacketStruct pack;
krobertson 20:81d5655fecc2 22 char rx_status = getPS().receivePacket(&pack);
krobertson 20:81d5655fecc2 23 if(rx_status != 1){
krobertson 20:81d5655fecc2 24 return rx_status;
krobertson 20:81d5655fecc2 25 }
krobertson 20:81d5655fecc2 26 Point* points = (Point*)pack.data;
krobertson 20:81d5655fecc2 27 unsigned int num_points = pack.size;
krobertson 20:81d5655fecc2 28 for(int i=0;i<num_points;i++){
krobertson 20:81d5655fecc2 29 USB::getSerial().printf("Adding Waypoint: %f, %f\r\n",points[i].lat,points[i].lon);
krobertson 20:81d5655fecc2 30 }
krobertson 20:81d5655fecc2 31 return 1;
krobertson 20:81d5655fecc2 32 }
krobertson 20:81d5655fecc2 33
krobertson 20:81d5655fecc2 34 void GPSHandle::sendLoc(){
krobertson 20:81d5655fecc2 35 wait_us(100000);
krobertson 20:81d5655fecc2 36 getPS().openConnection();
krobertson 20:81d5655fecc2 37 wait_us(100000);
krobertson 20:81d5655fecc2 38 unsigned int sID=getPS().getSuperID();
krobertson 20:81d5655fecc2 39 getPS().sendPacket(0,NULL,0,PT_EMPTY);
krobertson 20:81d5655fecc2 40 getPS().sendPacket(sID,NULL,0,PT_SENDLOC);
krobertson 30:327191ff57e8 41 getPS().sendPacket(sID,(char*)(&DH::locs().getC(LHType_locs,DH::locs().getI(LHType_locs))),sizeof(DataLocation));
krobertson 20:81d5655fecc2 42 getPS().sendPacket(sID,NULL,0,PT_END);
krobertson 20:81d5655fecc2 43 wait_us(100000);
krobertson 20:81d5655fecc2 44 getPS().closeConnection();
krobertson 20:81d5655fecc2 45 wait_us(100000);
krobertson 20:81d5655fecc2 46 }
krobertson 20:81d5655fecc2 47
krobertson 20:81d5655fecc2 48 bool GPSHandle::if_image_location(){
krobertson 20:81d5655fecc2 49 USB::getSerial().printf("Checking if at waypoint\r\n");
krobertson 30:327191ff57e8 50 //DH::locs().getC(LHType_locs,DH::locs().getI(LHType_locs)){
krobertson 30:327191ff57e8 51
krobertson 30:327191ff57e8 52 //}
krobertson 20:81d5655fecc2 53 return true;
krobertson 20:81d5655fecc2 54 }
krobertson 20:81d5655fecc2 55
dylanembed123 14:6be57da62283 56 static bool reading = false;
dylanembed123 14:6be57da62283 57 //static std::stringstream line;
dylanembed123 14:6be57da62283 58 static char line[MAXREADIN+10];
dylanembed123 14:6be57da62283 59 static int line_i=0;
dylanembed123 14:6be57da62283 60 char* getNext(char*& field){
dylanembed123 14:6be57da62283 61 char* output=new char[MAXREADIN+1];
dylanembed123 14:6be57da62283 62 int i;
dylanembed123 14:6be57da62283 63 for(i=0;i<MAXREADIN;i++){
dylanembed123 14:6be57da62283 64 if(field[i]=='\0'||field[i]==',')break;
dylanembed123 14:6be57da62283 65 }
dylanembed123 14:6be57da62283 66 for(int a=0;a<i;a++){
dylanembed123 14:6be57da62283 67 output[a]=field[a];
dylanembed123 14:6be57da62283 68 }
dylanembed123 14:6be57da62283 69 output[i]='\0';
dylanembed123 14:6be57da62283 70 field=&field[i+1];
dylanembed123 14:6be57da62283 71 return output;
dylanembed123 14:6be57da62283 72 }
dylanembed123 14:6be57da62283 73
stearnsc 8:28b866df62cf 74 void GPSHandle::handleUpdate(){
dylanembed123 14:6be57da62283 75 if(GPS::getSerial().readable()<=0){return;}
dylanembed123 14:6be57da62283 76 char c = GPS::getSerial().getc();
dylanembed123 14:6be57da62283 77 //USB::getSerial().printf("%c",c);
stearnsc 8:28b866df62cf 78 if (reading) {
dylanembed123 14:6be57da62283 79 if(line_i>=MAXREADIN){reading=false;return;}
stearnsc 8:28b866df62cf 80 if (c == '*') { //sentence buffer complete; we're ignoring the checksum
dylanembed123 14:6be57da62283 81 char* field=line;
dylanembed123 14:6be57da62283 82 char* op;
dylanembed123 14:6be57da62283 83 op=getNext(field);delete op; //GPGGA
dylanembed123 14:6be57da62283 84 if(op[0]=='G'||op[1]=='P'||op[2]=='G'||op[3]=='G'||op[4]=='A'){
dylanembed123 14:6be57da62283 85 op=getNext(field);double timeS = atof(op);delete op; //time
dylanembed123 14:6be57da62283 86 op=getNext(field);double latitude = atof(op);delete op; //latitude
dylanembed123 14:6be57da62283 87 op=getNext(field);delete op; //N or S
dylanembed123 14:6be57da62283 88 op=getNext(field);double longitude = atof(op);delete op; //longitude
dylanembed123 14:6be57da62283 89 op=getNext(field);delete op; //E or W
dylanembed123 14:6be57da62283 90 op=getNext(field);delete op; //skip
dylanembed123 14:6be57da62283 91 op=getNext(field);delete op; //skip
dylanembed123 14:6be57da62283 92 op=getNext(field);delete op; //skip
dylanembed123 14:6be57da62283 93 op=getNext(field);delete op; //altitude
dylanembed123 14:6be57da62283 94 double altitude = atof(op);
dylanembed123 16:4f5d20b87dc3 95 if(timeS>0.5f){
dylanembed123 15:e3e03a9df89e 96 //USB::getSerial().printf("\nMy GPS data: Lat: %f, Lon: %f, Alt: %f, Time:%f\r\n",latitude,longitude,altitude,timeS);
dylanembed123 15:e3e03a9df89e 97 DH::Locs().add(LHType_locs,DataLocation(latitude,longitude,altitude,timeS));
dylanembed123 15:e3e03a9df89e 98 //USB::getSerial().printf("Current Time:%f\r\n",DH::Locs().getC().getTime());
dylanembed123 16:4f5d20b87dc3 99 }
dylanembed123 14:6be57da62283 100 }
stearnsc 8:28b866df62cf 101 //update whatever needs updating when gps updates
stearnsc 8:28b866df62cf 102 // pc.printf("My GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n",
stearnsc 8:28b866df62cf 103 // gpsData.latitude, gpsData.longitude, gpsData.altitude, gpsData.time
stearnsc 8:28b866df62cf 104 // );
stearnsc 8:28b866df62cf 105
stearnsc 8:28b866df62cf 106 reading = false;
stearnsc 8:28b866df62cf 107 } else {
dylanembed123 14:6be57da62283 108 line[line_i]=c;
dylanembed123 14:6be57da62283 109 line_i=(line_i+1)%MAXREADIN;
stearnsc 8:28b866df62cf 110 }
stearnsc 8:28b866df62cf 111
stearnsc 8:28b866df62cf 112 } else if (c == '$') {
stearnsc 8:28b866df62cf 113 reading = true;
dylanembed123 14:6be57da62283 114 line_i=0;
stearnsc 8:28b866df62cf 115 }
dylanembed123 14:6be57da62283 116 return;
stearnsc 8:28b866df62cf 117 }
stearnsc 8:28b866df62cf 118
stearnsc 8:28b866df62cf 119 //sends: "$<command>*<checksum>\r\l"
dylanembed123 9:da906eeac51e 120 void GPSHandle::sendGpsCommand(std::string command){
stearnsc 8:28b866df62cf 121 uint8_t checksum = 0;
dylanembed123 9:da906eeac51e 122 //pc.printf("Sending command to gps: ");
dylanembed123 14:6be57da62283 123 GPS::getSerial().putc('$');
dylanembed123 9:da906eeac51e 124 //pc.putc('$');
stearnsc 8:28b866df62cf 125 char c;
stearnsc 8:28b866df62cf 126 for (int i = 0; i < command.length(); i++) {
stearnsc 8:28b866df62cf 127 c = command[i];
stearnsc 8:28b866df62cf 128 checksum ^= c;
dylanembed123 14:6be57da62283 129 GPS::getSerial().putc(c);
dylanembed123 9:da906eeac51e 130 //pc.putc(c);
stearnsc 8:28b866df62cf 131 }
dylanembed123 14:6be57da62283 132 GPS::getSerial().putc('*');
dylanembed123 9:da906eeac51e 133 //pc.putc('*');
stearnsc 8:28b866df62cf 134 string checkSumString;
stearnsc 8:28b866df62cf 135 while (checksum > 0) {
stearnsc 8:28b866df62cf 136 uint8_t checksumChar = checksum & 0x0F;
stearnsc 8:28b866df62cf 137 if (checksumChar >= 10) {
stearnsc 8:28b866df62cf 138 checksumChar -= 10;
stearnsc 8:28b866df62cf 139 checksumChar += 'A';
stearnsc 8:28b866df62cf 140 } else {
stearnsc 8:28b866df62cf 141 checksumChar += '0';
stearnsc 8:28b866df62cf 142 }
stearnsc 8:28b866df62cf 143 checkSumString.push_back((char) checksumChar);
stearnsc 8:28b866df62cf 144 checksum = checksum >> 4;
stearnsc 8:28b866df62cf 145 }
stearnsc 8:28b866df62cf 146
stearnsc 8:28b866df62cf 147 for (int i = checkSumString.length() - 1; i >= 0; i--) {
dylanembed123 14:6be57da62283 148 GPS::getSerial().putc(checkSumString[i]);
dylanembed123 9:da906eeac51e 149 //pc.putc(checkSumString[i]);
stearnsc 8:28b866df62cf 150 }
dylanembed123 14:6be57da62283 151 GPS::getSerial().putc('\r');
dylanembed123 9:da906eeac51e 152 //pc.putc('\r');
dylanembed123 14:6be57da62283 153 GPS::getSerial().putc('\n');
dylanembed123 9:da906eeac51e 154 //pc.putc('\n');
stearnsc 8:28b866df62cf 155 }
stearnsc 8:28b866df62cf 156
stearnsc 8:28b866df62cf 157 int stringToDecimal(string s)
stearnsc 8:28b866df62cf 158 {
stearnsc 8:28b866df62cf 159 int mult = 1;
stearnsc 8:28b866df62cf 160 int result = 0;
stearnsc 8:28b866df62cf 161 for (int i = s.length() - 1; i >=0; i--) {
stearnsc 8:28b866df62cf 162 if (s[i] != '.') {
stearnsc 8:28b866df62cf 163 result += (s[i] - '0') * mult;
stearnsc 8:28b866df62cf 164 mult *= 10;
dylanembed123 7:c75d5e5e6bfc 165 }
dylanembed123 7:c75d5e5e6bfc 166 }
stearnsc 8:28b866df62cf 167 return result;
dylanembed123 7:c75d5e5e6bfc 168 }
dylanembed123 7:c75d5e5e6bfc 169
dylanembed123 7:c75d5e5e6bfc 170 bool GPSHandle::check(){
dylanembed123 7:c75d5e5e6bfc 171 return true;
dylanembed123 14:6be57da62283 172 }
dylanembed123 14:6be57da62283 173 void GPSHandle::run(){
dylanembed123 14:6be57da62283 174 if(!initialized){initialized=true;setup();}
dylanembed123 7:c75d5e5e6bfc 175 }