Colin Stearns / Mbed 2 deprecated qcControl

Dependencies:   mbed

Fork of dgps by Colin Stearns

Committer:
krobertson
Date:
Wed Apr 23 01:43:17 2014 +0000
Revision:
32:9cb7bc3fc9e0
Parent:
31:6f68fa0aeee5
Child:
34:c0b13ce5408c
Child:
36:53b69e471b5a
pushing so dylan can look

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 }
krobertson 32:9cb7bc3fc9e0 32 USB::getSerial().printf("Waypoints size: %d\r\n",DH::Locs().getI(LHType_locs,LHIType_size));
krobertson 32:9cb7bc3fc9e0 33 for(int i=0;i<DH::Locs().getI(LHType_locs,LHIType_size);i++){
krobertson 32:9cb7bc3fc9e0 34 DataLocation thisData=DH::Locs().getC(LHType_locs,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;
dylanembed123 14:6be57da62283 95 if(GPS::getSerial().readable()<=0){return;}
krobertson 32:9cb7bc3fc9e0 96 while(GPS::getSerial().readable()){
krobertson 32:9cb7bc3fc9e0 97 c = GPS::getSerial().getc();
krobertson 32:9cb7bc3fc9e0 98 if(c == '$'){
krobertson 32:9cb7bc3fc9e0 99 reading = true;
krobertson 32:9cb7bc3fc9e0 100 line_i=0;
krobertson 32:9cb7bc3fc9e0 101 break;
krobertson 32:9cb7bc3fc9e0 102 }
krobertson 32:9cb7bc3fc9e0 103 USB::getSerial().printf("%c",c);
krobertson 32:9cb7bc3fc9e0 104 }
stearnsc 8:28b866df62cf 105 if (reading) {
dylanembed123 14:6be57da62283 106 if(line_i>=MAXREADIN){reading=false;return;}
stearnsc 8:28b866df62cf 107 if (c == '*') { //sentence buffer complete; we're ignoring the checksum
dylanembed123 14:6be57da62283 108 char* field=line;
dylanembed123 14:6be57da62283 109 char* op;
dylanembed123 14:6be57da62283 110 op=getNext(field);delete op; //GPGGA
dylanembed123 14:6be57da62283 111 if(op[0]=='G'||op[1]=='P'||op[2]=='G'||op[3]=='G'||op[4]=='A'){
dylanembed123 14:6be57da62283 112 op=getNext(field);double timeS = atof(op);delete op; //time
dylanembed123 14:6be57da62283 113 op=getNext(field);double latitude = atof(op);delete op; //latitude
dylanembed123 14:6be57da62283 114 op=getNext(field);delete op; //N or S
dylanembed123 14:6be57da62283 115 op=getNext(field);double longitude = atof(op);delete op; //longitude
dylanembed123 14:6be57da62283 116 op=getNext(field);delete op; //E or W
dylanembed123 14:6be57da62283 117 op=getNext(field);delete op; //skip
dylanembed123 14:6be57da62283 118 op=getNext(field);delete op; //skip
dylanembed123 14:6be57da62283 119 op=getNext(field);delete op; //skip
dylanembed123 14:6be57da62283 120 op=getNext(field);delete op; //altitude
dylanembed123 14:6be57da62283 121 double altitude = atof(op);
dylanembed123 16:4f5d20b87dc3 122 if(timeS>0.5f){
krobertson 32:9cb7bc3fc9e0 123 USB::getSerial().printf("\nMy GPS data: Lat: %f, Lon: %f, Alt: %f, Time:%f\r\n",latitude,longitude,altitude,timeS);
dylanembed123 15:e3e03a9df89e 124 DH::Locs().add(LHType_locs,DataLocation(latitude,longitude,altitude,timeS));
krobertson 32:9cb7bc3fc9e0 125 USB::getSerial().printf("Current Time:%f\r\n",DH::Locs().getC().getTime());
dylanembed123 16:4f5d20b87dc3 126 }
dylanembed123 14:6be57da62283 127 }
stearnsc 8:28b866df62cf 128 //update whatever needs updating when gps updates
stearnsc 8:28b866df62cf 129 // pc.printf("My GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n",
stearnsc 8:28b866df62cf 130 // gpsData.latitude, gpsData.longitude, gpsData.altitude, gpsData.time
stearnsc 8:28b866df62cf 131 // );
stearnsc 8:28b866df62cf 132
stearnsc 8:28b866df62cf 133 reading = false;
stearnsc 8:28b866df62cf 134 } else {
dylanembed123 14:6be57da62283 135 line[line_i]=c;
dylanembed123 14:6be57da62283 136 line_i=(line_i+1)%MAXREADIN;
stearnsc 8:28b866df62cf 137 }
stearnsc 8:28b866df62cf 138
krobertson 32:9cb7bc3fc9e0 139 } //else if (c == '$') {
krobertson 32:9cb7bc3fc9e0 140 // reading = true;
krobertson 32:9cb7bc3fc9e0 141 // line_i=0;
krobertson 32:9cb7bc3fc9e0 142 // }
dylanembed123 14:6be57da62283 143 return;
stearnsc 8:28b866df62cf 144 }
stearnsc 8:28b866df62cf 145
stearnsc 8:28b866df62cf 146 //sends: "$<command>*<checksum>\r\l"
dylanembed123 9:da906eeac51e 147 void GPSHandle::sendGpsCommand(std::string command){
stearnsc 8:28b866df62cf 148 uint8_t checksum = 0;
dylanembed123 9:da906eeac51e 149 //pc.printf("Sending command to gps: ");
dylanembed123 14:6be57da62283 150 GPS::getSerial().putc('$');
dylanembed123 9:da906eeac51e 151 //pc.putc('$');
stearnsc 8:28b866df62cf 152 char c;
stearnsc 8:28b866df62cf 153 for (int i = 0; i < command.length(); i++) {
stearnsc 8:28b866df62cf 154 c = command[i];
stearnsc 8:28b866df62cf 155 checksum ^= c;
dylanembed123 14:6be57da62283 156 GPS::getSerial().putc(c);
dylanembed123 9:da906eeac51e 157 //pc.putc(c);
stearnsc 8:28b866df62cf 158 }
dylanembed123 14:6be57da62283 159 GPS::getSerial().putc('*');
dylanembed123 9:da906eeac51e 160 //pc.putc('*');
stearnsc 8:28b866df62cf 161 string checkSumString;
stearnsc 8:28b866df62cf 162 while (checksum > 0) {
stearnsc 8:28b866df62cf 163 uint8_t checksumChar = checksum & 0x0F;
stearnsc 8:28b866df62cf 164 if (checksumChar >= 10) {
stearnsc 8:28b866df62cf 165 checksumChar -= 10;
stearnsc 8:28b866df62cf 166 checksumChar += 'A';
stearnsc 8:28b866df62cf 167 } else {
stearnsc 8:28b866df62cf 168 checksumChar += '0';
stearnsc 8:28b866df62cf 169 }
stearnsc 8:28b866df62cf 170 checkSumString.push_back((char) checksumChar);
stearnsc 8:28b866df62cf 171 checksum = checksum >> 4;
stearnsc 8:28b866df62cf 172 }
stearnsc 8:28b866df62cf 173
stearnsc 8:28b866df62cf 174 for (int i = checkSumString.length() - 1; i >= 0; i--) {
dylanembed123 14:6be57da62283 175 GPS::getSerial().putc(checkSumString[i]);
dylanembed123 9:da906eeac51e 176 //pc.putc(checkSumString[i]);
stearnsc 8:28b866df62cf 177 }
dylanembed123 14:6be57da62283 178 GPS::getSerial().putc('\r');
dylanembed123 9:da906eeac51e 179 //pc.putc('\r');
dylanembed123 14:6be57da62283 180 GPS::getSerial().putc('\n');
dylanembed123 9:da906eeac51e 181 //pc.putc('\n');
stearnsc 8:28b866df62cf 182 }
stearnsc 8:28b866df62cf 183
stearnsc 8:28b866df62cf 184 int stringToDecimal(string s)
stearnsc 8:28b866df62cf 185 {
stearnsc 8:28b866df62cf 186 int mult = 1;
stearnsc 8:28b866df62cf 187 int result = 0;
stearnsc 8:28b866df62cf 188 for (int i = s.length() - 1; i >=0; i--) {
stearnsc 8:28b866df62cf 189 if (s[i] != '.') {
stearnsc 8:28b866df62cf 190 result += (s[i] - '0') * mult;
stearnsc 8:28b866df62cf 191 mult *= 10;
dylanembed123 7:c75d5e5e6bfc 192 }
dylanembed123 7:c75d5e5e6bfc 193 }
stearnsc 8:28b866df62cf 194 return result;
dylanembed123 7:c75d5e5e6bfc 195 }
dylanembed123 7:c75d5e5e6bfc 196
dylanembed123 7:c75d5e5e6bfc 197 bool GPSHandle::check(){
dylanembed123 7:c75d5e5e6bfc 198 return true;
dylanembed123 14:6be57da62283 199 }
dylanembed123 14:6be57da62283 200 void GPSHandle::run(){
dylanembed123 14:6be57da62283 201 if(!initialized){initialized=true;setup();}
krobertson 32:9cb7bc3fc9e0 202 handleUpdate();
dylanembed123 7:c75d5e5e6bfc 203 }