Colin Stearns / Mbed 2 deprecated qcControl

Dependencies:   mbed

Fork of dgps by Colin Stearns

Committer:
krobertson
Date:
Wed Apr 23 03:34:39 2014 +0000
Revision:
37:417d6403a708
Parent:
34:c0b13ce5408c
Parent:
36:53b69e471b5a
Child:
38:6f814050895d
merged changes

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);
krobertson 32:9cb7bc3fc9e0 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 32:9cb7bc3fc9e0 30 DH::Locs().add(LHType_targ,DataLocation(points[i].lat,points[i].lon,6.0f));
krobertson 20:81d5655fecc2 31 }
dylanembed123 34:c0b13ce5408c 32 USB::getSerial().printf("Waypoints size: %d\r\n",DH::Locs().getI(LHType_targ,LHIType_size));
dylanembed123 34:c0b13ce5408c 33 for(int i=0;i<DH::Locs().getI(LHType_targ,LHIType_size);i++){
dylanembed123 34:c0b13ce5408c 34 DataLocation thisData=DH::Locs().getC(LHType_targ,i);
krobertson 32:9cb7bc3fc9e0 35 USB::getSerial().printf("Waypoint %d: %f,%f\r\n",i,thisData.getLat(),thisData.getLon());
krobertson 32:9cb7bc3fc9e0 36 }
krobertson 32:9cb7bc3fc9e0 37 DH::Locs().inc(LHType_targ,0,true);
krobertson 20:81d5655fecc2 38 return 1;
krobertson 20:81d5655fecc2 39 }
krobertson 20:81d5655fecc2 40
krobertson 32:9cb7bc3fc9e0 41 void GPSHandle::next_waypoint(){
krobertson 32:9cb7bc3fc9e0 42 DH::Locs().inc(LHType_targ);
krobertson 32:9cb7bc3fc9e0 43 USB::getSerial().printf("Sending Quadcopter to next waypoint\r\n");
krobertson 32:9cb7bc3fc9e0 44 //code to command quad to go to next waypoint goes here
krobertson 32:9cb7bc3fc9e0 45 }
krobertson 32:9cb7bc3fc9e0 46
krobertson 20:81d5655fecc2 47 void GPSHandle::sendLoc(){
krobertson 20:81d5655fecc2 48 wait_us(100000);
krobertson 20:81d5655fecc2 49 getPS().openConnection();
krobertson 20:81d5655fecc2 50 wait_us(100000);
krobertson 20:81d5655fecc2 51 unsigned int sID=getPS().getSuperID();
krobertson 20:81d5655fecc2 52 getPS().sendPacket(0,NULL,0,PT_EMPTY);
krobertson 20:81d5655fecc2 53 getPS().sendPacket(sID,NULL,0,PT_SENDLOC);
krobertson 32:9cb7bc3fc9e0 54 getPS().sendPacket(sID,(char*)(&DH::Locs().getC(LHType_locs,DH::Locs().getI(LHType_locs))),sizeof(DataLocation));
krobertson 20:81d5655fecc2 55 getPS().sendPacket(sID,NULL,0,PT_END);
krobertson 20:81d5655fecc2 56 wait_us(100000);
krobertson 20:81d5655fecc2 57 getPS().closeConnection();
krobertson 20:81d5655fecc2 58 wait_us(100000);
krobertson 20:81d5655fecc2 59 }
krobertson 20:81d5655fecc2 60
krobertson 20:81d5655fecc2 61 bool GPSHandle::if_image_location(){
krobertson 29:b47b4fd78c00 62 double lon_thresh = 0.00005;
krobertson 29:b47b4fd78c00 63 double lat_thresh = 0.000035;
krobertson 20:81d5655fecc2 64 USB::getSerial().printf("Checking if at waypoint\r\n");
krobertson 32:9cb7bc3fc9e0 65 DataLocation current_loc = DH::Locs().getC(LHType_locs,DH::Locs().getI(LHType_locs));
krobertson 32:9cb7bc3fc9e0 66 DataLocation next_waypoint = DH::Locs().getC(LHType_targ,DH::Locs().getI(LHType_targ));
krobertson 32:9cb7bc3fc9e0 67 USB::getSerial().printf("current: %f,%f ... waypoint: %f,%f \r\n",current_loc.getLat(),current_loc.getLon(),next_waypoint.getLat(),next_waypoint.getLon());
krobertson 32:9cb7bc3fc9e0 68 double lat_diff = (current_loc.getLat()>next_waypoint.getLat()) ? current_loc.getLat() - next_waypoint.getLat() : next_waypoint.getLat() - current_loc.getLat();
krobertson 32:9cb7bc3fc9e0 69 double lon_diff = (current_loc.getLon()>next_waypoint.getLon()) ? current_loc.getLon() - next_waypoint.getLon() : next_waypoint.getLon() - current_loc.getLon();
krobertson 32:9cb7bc3fc9e0 70 USB::getSerial().printf("lat diff: %f, lon dif %f \r\n",lat_diff,lon_diff);
krobertson 32:9cb7bc3fc9e0 71 return (lat_diff < lat_thresh && lon_diff < lon_thresh);
krobertson 20:81d5655fecc2 72 }
krobertson 20:81d5655fecc2 73
dylanembed123 14:6be57da62283 74 static bool reading = false;
dylanembed123 14:6be57da62283 75 //static std::stringstream line;
dylanembed123 14:6be57da62283 76 static char line[MAXREADIN+10];
dylanembed123 14:6be57da62283 77 static int line_i=0;
dylanembed123 14:6be57da62283 78 char* getNext(char*& field){
dylanembed123 14:6be57da62283 79 char* output=new char[MAXREADIN+1];
dylanembed123 14:6be57da62283 80 int i;
dylanembed123 14:6be57da62283 81 for(i=0;i<MAXREADIN;i++){
dylanembed123 14:6be57da62283 82 if(field[i]=='\0'||field[i]==',')break;
dylanembed123 14:6be57da62283 83 }
dylanembed123 14:6be57da62283 84 for(int a=0;a<i;a++){
dylanembed123 14:6be57da62283 85 output[a]=field[a];
dylanembed123 14:6be57da62283 86 }
dylanembed123 14:6be57da62283 87 output[i]='\0';
dylanembed123 14:6be57da62283 88 field=&field[i+1];
dylanembed123 14:6be57da62283 89 return output;
dylanembed123 14:6be57da62283 90 }
dylanembed123 14:6be57da62283 91
stearnsc 8:28b866df62cf 92 void GPSHandle::handleUpdate(){
krobertson 32:9cb7bc3fc9e0 93 char c;
krobertson 32:9cb7bc3fc9e0 94 reading = false;
krobertson 36:53b69e471b5a 95 while(getPS().rx_ready_with_timeout(&GPS::getSerial())){
krobertson 32:9cb7bc3fc9e0 96 c = GPS::getSerial().getc();
krobertson 36:53b69e471b5a 97 USB::getSerial().printf("%c",c);
krobertson 36:53b69e471b5a 98 if (reading) {
krobertson 36:53b69e471b5a 99 if(line_i>=MAXREADIN){reading=false;return;}
krobertson 36:53b69e471b5a 100 if (c == '*') { //sentence buffer complete; we're ignoring the checksum
krobertson 36:53b69e471b5a 101 char* field=line;
krobertson 36:53b69e471b5a 102 char* op;
krobertson 36:53b69e471b5a 103 op=getNext(field);delete op; //GPGGA
krobertson 36:53b69e471b5a 104 if(op[0]=='G'||op[1]=='P'||op[2]=='G'||op[3]=='G'||op[4]=='A'){
krobertson 36:53b69e471b5a 105 op=getNext(field);double timeS = atof(op);delete op; //time
krobertson 36:53b69e471b5a 106 op=getNext(field);double latitude = atof(op);delete op; //latitude
krobertson 36:53b69e471b5a 107 op=getNext(field);delete op; //N or S
krobertson 36:53b69e471b5a 108 op=getNext(field);double longitude = atof(op);delete op; //longitude
krobertson 36:53b69e471b5a 109 op=getNext(field);delete op; //E or W
krobertson 36:53b69e471b5a 110 op=getNext(field);delete op; //skip
krobertson 36:53b69e471b5a 111 op=getNext(field);delete op; //skip
krobertson 36:53b69e471b5a 112 op=getNext(field);delete op; //skip
krobertson 36:53b69e471b5a 113 op=getNext(field);delete op; //altitude
krobertson 36:53b69e471b5a 114 double altitude = atof(op);
krobertson 36:53b69e471b5a 115 if(timeS>0.5f){
krobertson 36:53b69e471b5a 116 USB::getSerial().printf("\nMy GPS data: Lat: %f, Lon: %f, Alt: %f, Time:%f\r\n",latitude,longitude,altitude,timeS);
krobertson 36:53b69e471b5a 117 DH::Locs().add(LHType_locs,DataLocation(latitude,longitude,altitude,timeS));
krobertson 36:53b69e471b5a 118 USB::getSerial().printf("Current Time:%f\r\n",DH::Locs().getC().getTime());
krobertson 36:53b69e471b5a 119 return;
krobertson 36:53b69e471b5a 120 }
krobertson 36:53b69e471b5a 121 }
krobertson 36:53b69e471b5a 122 //update whatever needs updating when gps updates
krobertson 36:53b69e471b5a 123 //pc.printf("My GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n",
krobertson 36:53b69e471b5a 124 //gpsData.latitude, gpsData.longitude, gpsData.altitude, gpsData.time
krobertson 36:53b69e471b5a 125 //);
krobertson 36:53b69e471b5a 126 reading = false;
krobertson 36:53b69e471b5a 127 } else {
krobertson 36:53b69e471b5a 128 line[line_i]=c;
krobertson 36:53b69e471b5a 129 line_i=(line_i+1)%MAXREADIN;
krobertson 36:53b69e471b5a 130 }
krobertson 36:53b69e471b5a 131 }else if (c == '$') {
krobertson 32:9cb7bc3fc9e0 132 reading = true;
krobertson 32:9cb7bc3fc9e0 133 line_i=0;
krobertson 32:9cb7bc3fc9e0 134 }
krobertson 32:9cb7bc3fc9e0 135 }
dylanembed123 14:6be57da62283 136 return;
stearnsc 8:28b866df62cf 137 }
stearnsc 8:28b866df62cf 138
stearnsc 8:28b866df62cf 139 //sends: "$<command>*<checksum>\r\l"
dylanembed123 9:da906eeac51e 140 void GPSHandle::sendGpsCommand(std::string command){
stearnsc 8:28b866df62cf 141 uint8_t checksum = 0;
dylanembed123 9:da906eeac51e 142 //pc.printf("Sending command to gps: ");
dylanembed123 14:6be57da62283 143 GPS::getSerial().putc('$');
dylanembed123 9:da906eeac51e 144 //pc.putc('$');
stearnsc 8:28b866df62cf 145 char c;
stearnsc 8:28b866df62cf 146 for (int i = 0; i < command.length(); i++) {
stearnsc 8:28b866df62cf 147 c = command[i];
stearnsc 8:28b866df62cf 148 checksum ^= c;
dylanembed123 14:6be57da62283 149 GPS::getSerial().putc(c);
dylanembed123 9:da906eeac51e 150 //pc.putc(c);
stearnsc 8:28b866df62cf 151 }
dylanembed123 14:6be57da62283 152 GPS::getSerial().putc('*');
dylanembed123 9:da906eeac51e 153 //pc.putc('*');
stearnsc 8:28b866df62cf 154 string checkSumString;
stearnsc 8:28b866df62cf 155 while (checksum > 0) {
stearnsc 8:28b866df62cf 156 uint8_t checksumChar = checksum & 0x0F;
stearnsc 8:28b866df62cf 157 if (checksumChar >= 10) {
stearnsc 8:28b866df62cf 158 checksumChar -= 10;
stearnsc 8:28b866df62cf 159 checksumChar += 'A';
stearnsc 8:28b866df62cf 160 } else {
stearnsc 8:28b866df62cf 161 checksumChar += '0';
stearnsc 8:28b866df62cf 162 }
stearnsc 8:28b866df62cf 163 checkSumString.push_back((char) checksumChar);
stearnsc 8:28b866df62cf 164 checksum = checksum >> 4;
stearnsc 8:28b866df62cf 165 }
stearnsc 8:28b866df62cf 166
stearnsc 8:28b866df62cf 167 for (int i = checkSumString.length() - 1; i >= 0; i--) {
dylanembed123 14:6be57da62283 168 GPS::getSerial().putc(checkSumString[i]);
dylanembed123 9:da906eeac51e 169 //pc.putc(checkSumString[i]);
stearnsc 8:28b866df62cf 170 }
dylanembed123 14:6be57da62283 171 GPS::getSerial().putc('\r');
dylanembed123 9:da906eeac51e 172 //pc.putc('\r');
dylanembed123 14:6be57da62283 173 GPS::getSerial().putc('\n');
dylanembed123 9:da906eeac51e 174 //pc.putc('\n');
stearnsc 8:28b866df62cf 175 }
stearnsc 8:28b866df62cf 176
stearnsc 8:28b866df62cf 177 int stringToDecimal(string s)
stearnsc 8:28b866df62cf 178 {
stearnsc 8:28b866df62cf 179 int mult = 1;
stearnsc 8:28b866df62cf 180 int result = 0;
stearnsc 8:28b866df62cf 181 for (int i = s.length() - 1; i >=0; i--) {
stearnsc 8:28b866df62cf 182 if (s[i] != '.') {
stearnsc 8:28b866df62cf 183 result += (s[i] - '0') * mult;
stearnsc 8:28b866df62cf 184 mult *= 10;
dylanembed123 7:c75d5e5e6bfc 185 }
dylanembed123 7:c75d5e5e6bfc 186 }
stearnsc 8:28b866df62cf 187 return result;
dylanembed123 7:c75d5e5e6bfc 188 }
dylanembed123 7:c75d5e5e6bfc 189
dylanembed123 7:c75d5e5e6bfc 190 bool GPSHandle::check(){
dylanembed123 7:c75d5e5e6bfc 191 return true;
dylanembed123 14:6be57da62283 192 }
dylanembed123 14:6be57da62283 193 void GPSHandle::run(){
dylanembed123 14:6be57da62283 194 if(!initialized){initialized=true;setup();}
krobertson 32:9cb7bc3fc9e0 195 handleUpdate();
dylanembed123 7:c75d5e5e6bfc 196 }