Colin Stearns / Mbed 2 deprecated dgps

Dependencies:   mbed

Committer:
dylanembed123
Date:
Tue Apr 01 15:52:08 2014 +0000
Revision:
4:c75d5e5e6bfc
Parent:
3:434d20e99e49
Child:
6:f0248eb6714d
Update handler and adapter;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stearnsc 0:9c001c4e7bf4 1 #include "mbed.h"
stearnsc 0:9c001c4e7bf4 2 #include <string>
stearnsc 0:9c001c4e7bf4 3 #include <sstream>
dylanembed123 4:c75d5e5e6bfc 4 #include "adapt/usb.h"
dylanembed123 4:c75d5e5e6bfc 5 #include "adapt/camera.h"
dylanembed123 3:434d20e99e49 6 /*
stearnsc 0:9c001c4e7bf4 7 Serial pc(USBTX,USBRX);
stearnsc 0:9c001c4e7bf4 8 Serial xbee(p9,p10);//tx, rx
stearnsc 0:9c001c4e7bf4 9 Serial gps(p28,p27);
dylanembed123 3:434d20e99e49 10 Serial camera(p13,p14);
stearnsc 0:9c001c4e7bf4 11
stearnsc 0:9c001c4e7bf4 12 typedef struct {
stearnsc 0:9c001c4e7bf4 13 int latitude; //in .0001 minutes
stearnsc 0:9c001c4e7bf4 14 int longitude; //in .0001 minutes
stearnsc 0:9c001c4e7bf4 15 int altitude; //in decimeters
stearnsc 0:9c001c4e7bf4 16 int time; //in milliseconds
stearnsc 0:9c001c4e7bf4 17 } GpsData;
stearnsc 0:9c001c4e7bf4 18
stearnsc 0:9c001c4e7bf4 19 GpsData gpsData;
stearnsc 0:9c001c4e7bf4 20 GpsData otherGps;
stearnsc 0:9c001c4e7bf4 21
stearnsc 0:9c001c4e7bf4 22 void readSerial(Serial &s, char str[], int size){
stearnsc 0:9c001c4e7bf4 23 for (int i = 0; i < size; i++){
stearnsc 0:9c001c4e7bf4 24 str[i] = s.getc();
stearnsc 0:9c001c4e7bf4 25 }
stearnsc 0:9c001c4e7bf4 26 }
stearnsc 0:9c001c4e7bf4 27
stearnsc 0:9c001c4e7bf4 28 //sends: "$<command>*<checksum>\r\l"
stearnsc 0:9c001c4e7bf4 29 void sendGpsCommand(string command){
stearnsc 0:9c001c4e7bf4 30 uint8_t checksum = 0;
stearnsc 0:9c001c4e7bf4 31 pc.printf("Sending command to gps: ");
stearnsc 0:9c001c4e7bf4 32 gps.putc('$');
stearnsc 0:9c001c4e7bf4 33 pc.putc('$');
stearnsc 0:9c001c4e7bf4 34 char c;
stearnsc 0:9c001c4e7bf4 35 for (int i = 0; i < command.length(); i++){
stearnsc 0:9c001c4e7bf4 36 c = command[i];
stearnsc 0:9c001c4e7bf4 37 checksum ^= c;
stearnsc 0:9c001c4e7bf4 38 gps.putc(c);
stearnsc 0:9c001c4e7bf4 39 pc.putc(c);
stearnsc 0:9c001c4e7bf4 40 }
stearnsc 0:9c001c4e7bf4 41 gps.putc('*');
stearnsc 0:9c001c4e7bf4 42 pc.putc('*');
stearnsc 0:9c001c4e7bf4 43 string checkSumString;
stearnsc 0:9c001c4e7bf4 44 while (checksum > 0){
stearnsc 0:9c001c4e7bf4 45 uint8_t checksumChar = checksum & 0x0F;
stearnsc 0:9c001c4e7bf4 46 if (checksumChar >= 10){
stearnsc 0:9c001c4e7bf4 47 checksumChar -= 10;
stearnsc 0:9c001c4e7bf4 48 checksumChar += 'A';
stearnsc 0:9c001c4e7bf4 49 } else {
stearnsc 0:9c001c4e7bf4 50 checksumChar += '0';
stearnsc 0:9c001c4e7bf4 51 }
stearnsc 0:9c001c4e7bf4 52 checkSumString.push_back((char) checksumChar);
stearnsc 0:9c001c4e7bf4 53 checksum = checksum >> 4;
stearnsc 0:9c001c4e7bf4 54 }
stearnsc 0:9c001c4e7bf4 55
stearnsc 0:9c001c4e7bf4 56 for (int i = checkSumString.length() - 1; i >= 0; i--){
stearnsc 0:9c001c4e7bf4 57 gps.putc(checkSumString[i]);
stearnsc 0:9c001c4e7bf4 58 pc.putc(checkSumString[i]);
stearnsc 0:9c001c4e7bf4 59 }
stearnsc 0:9c001c4e7bf4 60 gps.putc('\r');
stearnsc 0:9c001c4e7bf4 61 pc.putc('\r');
stearnsc 0:9c001c4e7bf4 62 gps.putc('\n');
stearnsc 0:9c001c4e7bf4 63 pc.putc('\n');
stearnsc 0:9c001c4e7bf4 64 }
stearnsc 0:9c001c4e7bf4 65
stearnsc 0:9c001c4e7bf4 66 long parseDec(string s){
stearnsc 0:9c001c4e7bf4 67 int mult = 1;
stearnsc 0:9c001c4e7bf4 68 int result = 0;
stearnsc 0:9c001c4e7bf4 69 for (int i = s.length() - 1; i >=0; i--){
stearnsc 0:9c001c4e7bf4 70 if (s[i] != '.'){
stearnsc 0:9c001c4e7bf4 71 result += (s[i] - '0') * mult;
stearnsc 0:9c001c4e7bf4 72 mult *= 10;
stearnsc 0:9c001c4e7bf4 73 }
stearnsc 0:9c001c4e7bf4 74 }
stearnsc 0:9c001c4e7bf4 75 return result;
stearnsc 0:9c001c4e7bf4 76 }
stearnsc 0:9c001c4e7bf4 77
stearnsc 0:9c001c4e7bf4 78 //cs: little endian parsing
stearnsc 0:9c001c4e7bf4 79 int nextInt(char *data, int i){
stearnsc 0:9c001c4e7bf4 80 i |= data[i];
stearnsc 0:9c001c4e7bf4 81 i |= (data[i + 1] << 8);
stearnsc 0:9c001c4e7bf4 82 i |= (data[i + 2] << 16);
stearnsc 0:9c001c4e7bf4 83 i |= (data[i + 3] << 24);
stearnsc 0:9c001c4e7bf4 84 return i;
stearnsc 0:9c001c4e7bf4 85 }
stearnsc 0:9c001c4e7bf4 86
stearnsc 0:9c001c4e7bf4 87 void handleXbeeGps(){
stearnsc 0:9c001c4e7bf4 88 static bool reading = false;
stearnsc 0:9c001c4e7bf4 89 static char packet[16];
stearnsc 0:9c001c4e7bf4 90 static int i = 0;
stearnsc 0:9c001c4e7bf4 91
stearnsc 0:9c001c4e7bf4 92 char c = xbee.getc();
stearnsc 0:9c001c4e7bf4 93 if (reading){
stearnsc 0:9c001c4e7bf4 94 packet[i] = c;
stearnsc 0:9c001c4e7bf4 95 i++;
stearnsc 0:9c001c4e7bf4 96 if (i == 16){
stearnsc 0:9c001c4e7bf4 97 i = 0;
stearnsc 0:9c001c4e7bf4 98 otherGps.latitude = nextInt(packet, 0);
stearnsc 0:9c001c4e7bf4 99 otherGps.longitude = nextInt(packet, 4);
stearnsc 0:9c001c4e7bf4 100 otherGps.altitude = nextInt(packet, 8);
stearnsc 0:9c001c4e7bf4 101 otherGps.time = nextInt(packet, 12);
stearnsc 0:9c001c4e7bf4 102
stearnsc 0:9c001c4e7bf4 103 pc.printf("His GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n",
stearnsc 0:9c001c4e7bf4 104 otherGps.latitude, otherGps.longitude, otherGps.altitude, otherGps.time
stearnsc 0:9c001c4e7bf4 105 );
stearnsc 0:9c001c4e7bf4 106 reading = false;
stearnsc 0:9c001c4e7bf4 107 }
stearnsc 0:9c001c4e7bf4 108 } else if (c == 'X'){
stearnsc 0:9c001c4e7bf4 109 reading = true;
stearnsc 0:9c001c4e7bf4 110 }
stearnsc 0:9c001c4e7bf4 111 }
stearnsc 0:9c001c4e7bf4 112
stearnsc 0:9c001c4e7bf4 113 void handleGpsData(){
stearnsc 0:9c001c4e7bf4 114 static bool reading = false;
stearnsc 0:9c001c4e7bf4 115 static stringstream line;
stearnsc 0:9c001c4e7bf4 116
stearnsc 0:9c001c4e7bf4 117 char c = gps.getc();
stearnsc 0:9c001c4e7bf4 118
stearnsc 0:9c001c4e7bf4 119 if (reading){
stearnsc 0:9c001c4e7bf4 120 if (c == '*'){ //sentence buffer complete; we're ignoring the checksum
stearnsc 0:9c001c4e7bf4 121 string field;
stearnsc 0:9c001c4e7bf4 122 std::getline(line, field, ','); //GPGGA
stearnsc 0:9c001c4e7bf4 123 std::getline(line, field, ','); //time
stearnsc 0:9c001c4e7bf4 124 gpsData.time = parseDec(field);
stearnsc 0:9c001c4e7bf4 125 std::getline(line, field, ','); //latitude
stearnsc 0:9c001c4e7bf4 126 gpsData.latitude = parseDec(field);
stearnsc 0:9c001c4e7bf4 127 std::getline(line, field, ','); //N or S
stearnsc 0:9c001c4e7bf4 128 std::getline(line, field, ','); //longitude
stearnsc 0:9c001c4e7bf4 129 gpsData.longitude = parseDec(field);
stearnsc 0:9c001c4e7bf4 130 std::getline(line, field, ','); //E or W
stearnsc 0:9c001c4e7bf4 131 std::getline(line, field, ','); //skip
stearnsc 0:9c001c4e7bf4 132 std::getline(line, field, ','); //skip
stearnsc 0:9c001c4e7bf4 133 std::getline(line, field, ','); //skip
stearnsc 0:9c001c4e7bf4 134 std::getline(line, field, ','); //altitude
stearnsc 0:9c001c4e7bf4 135 gpsData.altitude = parseDec(field);
stearnsc 0:9c001c4e7bf4 136
stearnsc 0:9c001c4e7bf4 137 //update whatever needs updating when gps updates
stearnsc 0:9c001c4e7bf4 138 pc.printf("My GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n",
stearnsc 0:9c001c4e7bf4 139 gpsData.latitude, gpsData.longitude, gpsData.altitude, gpsData.time
stearnsc 0:9c001c4e7bf4 140 );
stearnsc 0:9c001c4e7bf4 141
stearnsc 0:9c001c4e7bf4 142 line.str(string(""));
stearnsc 0:9c001c4e7bf4 143 reading = false;
stearnsc 0:9c001c4e7bf4 144
stearnsc 0:9c001c4e7bf4 145 } else {
stearnsc 0:9c001c4e7bf4 146 line.put(c);
stearnsc 0:9c001c4e7bf4 147 }
stearnsc 0:9c001c4e7bf4 148
stearnsc 0:9c001c4e7bf4 149 } else if (c == '$') {
stearnsc 0:9c001c4e7bf4 150 reading = true;
stearnsc 0:9c001c4e7bf4 151 }
stearnsc 0:9c001c4e7bf4 152 }
dylanembed123 4:c75d5e5e6bfc 153
dylanembed123 4:c75d5e5e6bfc 154
dylanembed123 3:434d20e99e49 155 */
dylanembed123 4:c75d5e5e6bfc 156 Serial gps(p28,p27);
dylanembed123 4:c75d5e5e6bfc 157
stearnsc 0:9c001c4e7bf4 158 int main(){
dylanembed123 4:c75d5e5e6bfc 159 ImageHandle imageHand;
dylanembed123 4:c75d5e5e6bfc 160 GPSHand gpsHand;
dylanembed123 4:c75d5e5e6bfc 161
dylanembed123 4:c75d5e5e6bfc 162 /// Main Loop
dylanembed123 4:c75d5e5e6bfc 163 while(1){
dylanembed123 4:c75d5e5e6bfc 164 // Run image handler
dylanembed123 4:c75d5e5e6bfc 165 imageHand.run();
dylanembed123 4:c75d5e5e6bfc 166 // Run GPS handler
dylanembed123 4:c75d5e5e6bfc 167 gpsHand.run();
dylanembed123 3:434d20e99e49 168 }
dylanembed123 4:c75d5e5e6bfc 169
dylanembed123 4:c75d5e5e6bfc 170 USB::getSerial().printf("Start!\n");
dylanembed123 4:c75d5e5e6bfc 171 gps.baud(57600);
dylanembed123 4:c75d5e5e6bfc 172 while(true){
dylanembed123 4:c75d5e5e6bfc 173 if(gps.readable()>0){
dylanembed123 4:c75d5e5e6bfc 174 char c = gps.getc();
dylanembed123 4:c75d5e5e6bfc 175 USB::getSerial().printf("%c",c);
dylanembed123 4:c75d5e5e6bfc 176 }
dylanembed123 3:434d20e99e49 177 }
dylanembed123 3:434d20e99e49 178 //USB::getSerial().printf("Done(%d)\n",i);
dylanembed123 3:434d20e99e49 179 while(1){
dylanembed123 3:434d20e99e49 180 }
dylanembed123 3:434d20e99e49 181 // gps.baud(57600);
dylanembed123 3:434d20e99e49 182 // xbee.baud(9600);
dylanembed123 3:434d20e99e49 183 // pc.baud(57600);
stearnsc 0:9c001c4e7bf4 184
stearnsc 0:9c001c4e7bf4 185 // sendGpsCommand("PMTK301,1");
stearnsc 0:9c001c4e7bf4 186 // while(true){pc.putc(gps.getc());}
dylanembed123 3:434d20e99e49 187 // gps.attach(&handleGpsData, Serial::RxIrq);
stearnsc 0:9c001c4e7bf4 188 // xbee.attach(&handleXbeeGps, Serial::RxIrq);
stearnsc 0:9c001c4e7bf4 189 }