Colin Stearns
/
qcControl
QC Control software
Fork of dgps by
main.cpp@20:81d5655fecc2, 2014-04-22 (annotated)
- Committer:
- krobertson
- Date:
- Tue Apr 22 04:26:31 2014 +0000
- Revision:
- 20:81d5655fecc2
- Parent:
- 19:8c1f2a2204fb
- Child:
- 24:e65416d6de22
send and receive commands.; waypoint processing; get locationions; send images
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stearnsc | 0:9c001c4e7bf4 | 1 | #include "mbed.h" |
stearnsc | 0:9c001c4e7bf4 | 2 | #include <string> |
stearnsc | 0:9c001c4e7bf4 | 3 | #include <sstream> |
dylanembed123 | 7:c75d5e5e6bfc | 4 | #include "adapt/usb.h" |
dylanembed123 | 9:da906eeac51e | 5 | #include "handle/handleCamera.h" |
dylanembed123 | 9:da906eeac51e | 6 | #include "handle/handleGPS.h" |
krobertson | 20:81d5655fecc2 | 7 | #include "handle/handleCommand.h" |
stearnsc | 8:28b866df62cf | 8 | |
stearnsc | 0:9c001c4e7bf4 | 9 | Serial pc(USBTX,USBRX); |
stearnsc | 0:9c001c4e7bf4 | 10 | Serial xbee(p9,p10);//tx, rx |
stearnsc | 0:9c001c4e7bf4 | 11 | Serial gps(p28,p27); |
dylanembed123 | 6:434d20e99e49 | 12 | Serial camera(p13,p14); |
stearnsc | 0:9c001c4e7bf4 | 13 | |
stearnsc | 0:9c001c4e7bf4 | 14 | typedef struct { |
stearnsc | 0:9c001c4e7bf4 | 15 | int latitude; //in .0001 minutes |
stearnsc | 0:9c001c4e7bf4 | 16 | int longitude; //in .0001 minutes |
stearnsc | 0:9c001c4e7bf4 | 17 | int altitude; //in decimeters |
stearnsc | 0:9c001c4e7bf4 | 18 | int time; //in milliseconds |
stearnsc | 0:9c001c4e7bf4 | 19 | } GpsData; |
stearnsc | 0:9c001c4e7bf4 | 20 | |
stearnsc | 8:28b866df62cf | 21 | void readSerial(Serial &s, char str[], int size) |
stearnsc | 8:28b866df62cf | 22 | { |
stearnsc | 8:28b866df62cf | 23 | for (int i = 0; i < size; i++) { |
stearnsc | 8:28b866df62cf | 24 | str[i] = s.getc(); |
stearnsc | 0:9c001c4e7bf4 | 25 | } |
stearnsc | 0:9c001c4e7bf4 | 26 | } |
stearnsc | 0:9c001c4e7bf4 | 27 | |
krobertson | 18:e72ee7aed088 | 28 | void connection_lost(){ |
krobertson | 18:e72ee7aed088 | 29 | USB::getSerial().printf("TCP connection lost!\r\n"); |
krobertson | 18:e72ee7aed088 | 30 | } |
krobertson | 18:e72ee7aed088 | 31 | |
stearnsc | 0:9c001c4e7bf4 | 32 | //sends: "$<command>*<checksum>\r\l" |
stearnsc | 8:28b866df62cf | 33 | void sendGpsCommand(string command) |
stearnsc | 8:28b866df62cf | 34 | { |
stearnsc | 0:9c001c4e7bf4 | 35 | uint8_t checksum = 0; |
stearnsc | 0:9c001c4e7bf4 | 36 | pc.printf("Sending command to gps: "); |
stearnsc | 0:9c001c4e7bf4 | 37 | gps.putc('$'); |
stearnsc | 0:9c001c4e7bf4 | 38 | pc.putc('$'); |
stearnsc | 0:9c001c4e7bf4 | 39 | char c; |
stearnsc | 8:28b866df62cf | 40 | for (int i = 0; i < command.length(); i++) { |
stearnsc | 0:9c001c4e7bf4 | 41 | c = command[i]; |
stearnsc | 0:9c001c4e7bf4 | 42 | checksum ^= c; |
stearnsc | 0:9c001c4e7bf4 | 43 | gps.putc(c); |
stearnsc | 0:9c001c4e7bf4 | 44 | pc.putc(c); |
stearnsc | 0:9c001c4e7bf4 | 45 | } |
stearnsc | 0:9c001c4e7bf4 | 46 | gps.putc('*'); |
stearnsc | 0:9c001c4e7bf4 | 47 | pc.putc('*'); |
stearnsc | 0:9c001c4e7bf4 | 48 | string checkSumString; |
stearnsc | 8:28b866df62cf | 49 | while (checksum > 0) { |
stearnsc | 0:9c001c4e7bf4 | 50 | uint8_t checksumChar = checksum & 0x0F; |
stearnsc | 8:28b866df62cf | 51 | if (checksumChar >= 10) { |
stearnsc | 0:9c001c4e7bf4 | 52 | checksumChar -= 10; |
stearnsc | 8:28b866df62cf | 53 | checksumChar += 'A'; |
stearnsc | 0:9c001c4e7bf4 | 54 | } else { |
stearnsc | 8:28b866df62cf | 55 | checksumChar += '0'; |
stearnsc | 0:9c001c4e7bf4 | 56 | } |
stearnsc | 0:9c001c4e7bf4 | 57 | checkSumString.push_back((char) checksumChar); |
stearnsc | 8:28b866df62cf | 58 | checksum = checksum >> 4; |
stearnsc | 0:9c001c4e7bf4 | 59 | } |
stearnsc | 0:9c001c4e7bf4 | 60 | |
stearnsc | 8:28b866df62cf | 61 | for (int i = checkSumString.length() - 1; i >= 0; i--) { |
stearnsc | 0:9c001c4e7bf4 | 62 | gps.putc(checkSumString[i]); |
stearnsc | 8:28b866df62cf | 63 | pc.putc(checkSumString[i]); |
stearnsc | 8:28b866df62cf | 64 | } |
stearnsc | 0:9c001c4e7bf4 | 65 | gps.putc('\r'); |
stearnsc | 0:9c001c4e7bf4 | 66 | pc.putc('\r'); |
stearnsc | 0:9c001c4e7bf4 | 67 | gps.putc('\n'); |
stearnsc | 0:9c001c4e7bf4 | 68 | pc.putc('\n'); |
stearnsc | 0:9c001c4e7bf4 | 69 | } |
stearnsc | 8:28b866df62cf | 70 | // |
stearnsc | 8:28b866df62cf | 71 | ////cs: little endian parsing |
stearnsc | 8:28b866df62cf | 72 | //int nextInt(char *data, int i) |
stearnsc | 8:28b866df62cf | 73 | //{ |
stearnsc | 8:28b866df62cf | 74 | // i |= data[i]; |
stearnsc | 8:28b866df62cf | 75 | // i |= (data[i + 1] << 8); |
stearnsc | 8:28b866df62cf | 76 | // i |= (data[i + 2] << 16); |
stearnsc | 8:28b866df62cf | 77 | // i |= (data[i + 3] << 24); |
stearnsc | 8:28b866df62cf | 78 | // return i; |
stearnsc | 8:28b866df62cf | 79 | //} |
stearnsc | 0:9c001c4e7bf4 | 80 | |
stearnsc | 8:28b866df62cf | 81 | //void handleXbeeGps() |
stearnsc | 8:28b866df62cf | 82 | //{ |
stearnsc | 8:28b866df62cf | 83 | // static bool reading = false; |
stearnsc | 8:28b866df62cf | 84 | // static char packet[16]; |
stearnsc | 8:28b866df62cf | 85 | // static int i = 0; |
stearnsc | 8:28b866df62cf | 86 | // |
stearnsc | 8:28b866df62cf | 87 | // char c = xbee.getc(); |
stearnsc | 8:28b866df62cf | 88 | // if (reading) { |
stearnsc | 8:28b866df62cf | 89 | // packet[i] = c; |
stearnsc | 8:28b866df62cf | 90 | // i++; |
stearnsc | 8:28b866df62cf | 91 | // if (i == 16) { |
stearnsc | 8:28b866df62cf | 92 | // i = 0; |
stearnsc | 8:28b866df62cf | 93 | // otherGps.latitude = nextInt(packet, 0); |
stearnsc | 8:28b866df62cf | 94 | // otherGps.longitude = nextInt(packet, 4); |
stearnsc | 8:28b866df62cf | 95 | // otherGps.altitude = nextInt(packet, 8); |
stearnsc | 8:28b866df62cf | 96 | // otherGps.time = nextInt(packet, 12); |
stearnsc | 8:28b866df62cf | 97 | // |
stearnsc | 8:28b866df62cf | 98 | // pc.printf("His GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n", |
stearnsc | 8:28b866df62cf | 99 | // otherGps.latitude, otherGps.longitude, otherGps.altitude, otherGps.time |
stearnsc | 8:28b866df62cf | 100 | // ); |
stearnsc | 8:28b866df62cf | 101 | // reading = false; |
stearnsc | 8:28b866df62cf | 102 | // } |
stearnsc | 8:28b866df62cf | 103 | // } else if (c == 'X') { |
stearnsc | 8:28b866df62cf | 104 | // reading = true; |
stearnsc | 8:28b866df62cf | 105 | // } |
stearnsc | 8:28b866df62cf | 106 | //} |
dylanembed123 | 7:c75d5e5e6bfc | 107 | |
dylanembed123 | 7:c75d5e5e6bfc | 108 | |
stearnsc | 8:28b866df62cf | 109 | int main() |
stearnsc | 8:28b866df62cf | 110 | { |
krobertson | 20:81d5655fecc2 | 111 | //handlers |
krobertson | 20:81d5655fecc2 | 112 | //ImageHandle imageHand; |
krobertson | 20:81d5655fecc2 | 113 | //GPSHandle gpsHand; |
krobertson | 20:81d5655fecc2 | 114 | //CommandHandle commHand; |
krobertson | 20:81d5655fecc2 | 115 | |
dylanembed123 | 13:a6d3cf2b018e | 116 | USB::getSerial().printf("Starting %d\n",sizeof(PacketStruct)); |
dylanembed123 | 14:6be57da62283 | 117 | USB::getSerial().printf("Check GPS\n"); |
krobertson | 20:81d5655fecc2 | 118 | USB::getSerial().printf("Connect to the wifly network now!\r\n"); |
krobertson | 20:81d5655fecc2 | 119 | //XBEE::getTCPInterrupt().fall(&connection_lost); |
krobertson | 18:e72ee7aed088 | 120 | |
krobertson | 20:81d5655fecc2 | 121 | //checking connection to egg before continuing |
krobertson | 19:8c1f2a2204fb | 122 | getPS().openConnection(); |
krobertson | 19:8c1f2a2204fb | 123 | getPS().closeConnection(); |
krobertson | 20:81d5655fecc2 | 124 | |
krobertson | 20:81d5655fecc2 | 125 | //Main Loop |
krobertson | 20:81d5655fecc2 | 126 | //while(1){ |
krobertson | 19:8c1f2a2204fb | 127 | while(1){ |
krobertson | 20:81d5655fecc2 | 128 | USB::getSerial().printf("Requesting commands from egg...\r\n"); |
krobertson | 20:81d5655fecc2 | 129 | wait_us(100000); |
krobertson | 20:81d5655fecc2 | 130 | CommandHandle::getCommandHand().run(); |
krobertson | 20:81d5655fecc2 | 131 | wait_us(100000); |
krobertson | 20:81d5655fecc2 | 132 | if(GPSHandle::getGPSHand().if_image_location()){ |
krobertson | 20:81d5655fecc2 | 133 | USB::getSerial().printf("Taking picture and sending...\r\n"); |
krobertson | 20:81d5655fecc2 | 134 | wait_us(100000); |
krobertson | 20:81d5655fecc2 | 135 | ImageHandle::getImageHand().run(); |
krobertson | 20:81d5655fecc2 | 136 | USB::getSerial().printf("sent all data\r\n"); |
krobertson | 20:81d5655fecc2 | 137 | wait_us(100000); |
krobertson | 20:81d5655fecc2 | 138 | } |
krobertson | 20:81d5655fecc2 | 139 | wait_us(1000000); |
krobertson | 20:81d5655fecc2 | 140 | //} |
krobertson | 18:e72ee7aed088 | 141 | |
dylanembed123 | 11:97625c27ab90 | 142 | // Run image handler |
krobertson | 20:81d5655fecc2 | 143 | //USB::getSerial().printf("Check Image\n"); |
krobertson | 20:81d5655fecc2 | 144 | // imageHand.run(); |
dylanembed123 | 11:97625c27ab90 | 145 | // Run GPS handler |
krobertson | 20:81d5655fecc2 | 146 | // USB::getSerial().printf("Check GPS\n"); |
krobertson | 20:81d5655fecc2 | 147 | //gpsHand.run(); |
krobertson | 20:81d5655fecc2 | 148 | //USB::getSerial().printf("GPS Time: %f\n",DH::Locs().getC().getTime()); |
dylanembed123 | 15:e3e03a9df89e | 149 | // Read packet |
krobertson | 20:81d5655fecc2 | 150 | //USB::getSerial().printf("Read Input\n"); |
dylanembed123 | 16:4f5d20b87dc3 | 151 | //PacketStruct* pack=getPS().lastValid;//getPS().getNextPacket(); |
dylanembed123 | 16:4f5d20b87dc3 | 152 | //if(pack!=NULL){ |
dylanembed123 | 16:4f5d20b87dc3 | 153 | // USB::getSerial().printf("Received Type: %d\n",pack->type); |
dylanembed123 | 16:4f5d20b87dc3 | 154 | // if(pack->type==PT_REQLOC){ |
krobertson | 20:81d5655fecc2 | 155 | //if(getPS().outputDevice.readable()>0){ |
krobertson | 20:81d5655fecc2 | 156 | // char input=getPS().outputDevice.getc(); |
krobertson | 20:81d5655fecc2 | 157 | // //if(getPS().outputDevice.getc()=='A'){ |
krobertson | 20:81d5655fecc2 | 158 | // // Send location |
krobertson | 20:81d5655fecc2 | 159 | // unsigned int sID=getPS().getSuperID(); |
krobertson | 20:81d5655fecc2 | 160 | // getPS().sendPacket(0,NULL,0,PT_EMPTY); |
krobertson | 20:81d5655fecc2 | 161 | // getPS().sendPacket(sID,NULL,0,PT_SENDLOC); |
krobertson | 20:81d5655fecc2 | 162 | // getPS().sendPacket(sID,(char*)(&DH::Locs().getC().getLoc()),sizeof(DataLocation)); |
krobertson | 20:81d5655fecc2 | 163 | // getPS().sendPacket(sID,NULL,0,PT_END); |
krobertson | 20:81d5655fecc2 | 164 | // //} |
krobertson | 20:81d5655fecc2 | 165 | // } |
dylanembed123 | 11:97625c27ab90 | 166 | } |
dylanembed123 | 7:c75d5e5e6bfc | 167 | /// Main Loop |
krobertson | 20:81d5655fecc2 | 168 | // while(true) { |
krobertson | 20:81d5655fecc2 | 169 | // gps.baud(57600); |
krobertson | 20:81d5655fecc2 | 170 | // xbee.baud(9600); |
krobertson | 20:81d5655fecc2 | 171 | // pc.baud(57600); |
krobertson | 20:81d5655fecc2 | 172 | // |
krobertson | 20:81d5655fecc2 | 173 | // sendGpsCommand("PMTK301,1"); |
krobertson | 20:81d5655fecc2 | 174 | // while(true) { |
krobertson | 20:81d5655fecc2 | 175 | // pc.putc(gps.getc()); |
krobertson | 20:81d5655fecc2 | 176 | // } |
dylanembed123 | 9:da906eeac51e | 177 | //gps.attach(&handleGpsData, Serial::RxIrq); |
dylanembed123 | 9:da906eeac51e | 178 | //xbee.attach(&handleXbeeGps, Serial::RxIrq)//; |
krobertson | 20:81d5655fecc2 | 179 | //} |
dylanembed123 | 9:da906eeac51e | 180 | } |