QC Control software

Dependencies:   mbed

Fork of dgps by Colin Stearns

Committer:
krobertson
Date:
Sun Apr 20 22:01:05 2014 +0000
Revision:
19:8c1f2a2204fb
Parent:
18:e72ee7aed088
Child:
20:81d5655fecc2
downlink seems to be reliable now

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 7:c75d5e5e6bfc 4 #include "adapt/usb.h"
dylanembed123 9:da906eeac51e 5 #include "handle/handleCamera.h"
dylanembed123 9:da906eeac51e 6 #include "handle/handleGPS.h"
stearnsc 8:28b866df62cf 7
stearnsc 0:9c001c4e7bf4 8 Serial pc(USBTX,USBRX);
stearnsc 0:9c001c4e7bf4 9 Serial xbee(p9,p10);//tx, rx
stearnsc 0:9c001c4e7bf4 10 Serial gps(p28,p27);
dylanembed123 6:434d20e99e49 11 Serial camera(p13,p14);
stearnsc 0:9c001c4e7bf4 12
stearnsc 0:9c001c4e7bf4 13 typedef struct {
stearnsc 0:9c001c4e7bf4 14 int latitude; //in .0001 minutes
stearnsc 0:9c001c4e7bf4 15 int longitude; //in .0001 minutes
stearnsc 0:9c001c4e7bf4 16 int altitude; //in decimeters
stearnsc 0:9c001c4e7bf4 17 int time; //in milliseconds
stearnsc 0:9c001c4e7bf4 18 } GpsData;
stearnsc 0:9c001c4e7bf4 19
stearnsc 8:28b866df62cf 20 void readSerial(Serial &s, char str[], int size)
stearnsc 8:28b866df62cf 21 {
stearnsc 8:28b866df62cf 22 for (int i = 0; i < size; i++) {
stearnsc 8:28b866df62cf 23 str[i] = s.getc();
stearnsc 0:9c001c4e7bf4 24 }
stearnsc 0:9c001c4e7bf4 25 }
stearnsc 0:9c001c4e7bf4 26
krobertson 18:e72ee7aed088 27 void connection_lost(){
krobertson 18:e72ee7aed088 28 USB::getSerial().printf("TCP connection lost!\r\n");
krobertson 18:e72ee7aed088 29 }
krobertson 18:e72ee7aed088 30
stearnsc 0:9c001c4e7bf4 31 //sends: "$<command>*<checksum>\r\l"
stearnsc 8:28b866df62cf 32 void sendGpsCommand(string command)
stearnsc 8:28b866df62cf 33 {
stearnsc 0:9c001c4e7bf4 34 uint8_t checksum = 0;
stearnsc 0:9c001c4e7bf4 35 pc.printf("Sending command to gps: ");
stearnsc 0:9c001c4e7bf4 36 gps.putc('$');
stearnsc 0:9c001c4e7bf4 37 pc.putc('$');
stearnsc 0:9c001c4e7bf4 38 char c;
stearnsc 8:28b866df62cf 39 for (int i = 0; i < command.length(); i++) {
stearnsc 0:9c001c4e7bf4 40 c = command[i];
stearnsc 0:9c001c4e7bf4 41 checksum ^= c;
stearnsc 0:9c001c4e7bf4 42 gps.putc(c);
stearnsc 0:9c001c4e7bf4 43 pc.putc(c);
stearnsc 0:9c001c4e7bf4 44 }
stearnsc 0:9c001c4e7bf4 45 gps.putc('*');
stearnsc 0:9c001c4e7bf4 46 pc.putc('*');
stearnsc 0:9c001c4e7bf4 47 string checkSumString;
stearnsc 8:28b866df62cf 48 while (checksum > 0) {
stearnsc 0:9c001c4e7bf4 49 uint8_t checksumChar = checksum & 0x0F;
stearnsc 8:28b866df62cf 50 if (checksumChar >= 10) {
stearnsc 0:9c001c4e7bf4 51 checksumChar -= 10;
stearnsc 8:28b866df62cf 52 checksumChar += 'A';
stearnsc 0:9c001c4e7bf4 53 } else {
stearnsc 8:28b866df62cf 54 checksumChar += '0';
stearnsc 0:9c001c4e7bf4 55 }
stearnsc 0:9c001c4e7bf4 56 checkSumString.push_back((char) checksumChar);
stearnsc 8:28b866df62cf 57 checksum = checksum >> 4;
stearnsc 0:9c001c4e7bf4 58 }
stearnsc 0:9c001c4e7bf4 59
stearnsc 8:28b866df62cf 60 for (int i = checkSumString.length() - 1; i >= 0; i--) {
stearnsc 0:9c001c4e7bf4 61 gps.putc(checkSumString[i]);
stearnsc 8:28b866df62cf 62 pc.putc(checkSumString[i]);
stearnsc 8:28b866df62cf 63 }
stearnsc 0:9c001c4e7bf4 64 gps.putc('\r');
stearnsc 0:9c001c4e7bf4 65 pc.putc('\r');
stearnsc 0:9c001c4e7bf4 66 gps.putc('\n');
stearnsc 0:9c001c4e7bf4 67 pc.putc('\n');
stearnsc 0:9c001c4e7bf4 68 }
stearnsc 8:28b866df62cf 69 //
stearnsc 8:28b866df62cf 70 ////cs: little endian parsing
stearnsc 8:28b866df62cf 71 //int nextInt(char *data, int i)
stearnsc 8:28b866df62cf 72 //{
stearnsc 8:28b866df62cf 73 // i |= data[i];
stearnsc 8:28b866df62cf 74 // i |= (data[i + 1] << 8);
stearnsc 8:28b866df62cf 75 // i |= (data[i + 2] << 16);
stearnsc 8:28b866df62cf 76 // i |= (data[i + 3] << 24);
stearnsc 8:28b866df62cf 77 // return i;
stearnsc 8:28b866df62cf 78 //}
stearnsc 0:9c001c4e7bf4 79
stearnsc 8:28b866df62cf 80 //void handleXbeeGps()
stearnsc 8:28b866df62cf 81 //{
stearnsc 8:28b866df62cf 82 // static bool reading = false;
stearnsc 8:28b866df62cf 83 // static char packet[16];
stearnsc 8:28b866df62cf 84 // static int i = 0;
stearnsc 8:28b866df62cf 85 //
stearnsc 8:28b866df62cf 86 // char c = xbee.getc();
stearnsc 8:28b866df62cf 87 // if (reading) {
stearnsc 8:28b866df62cf 88 // packet[i] = c;
stearnsc 8:28b866df62cf 89 // i++;
stearnsc 8:28b866df62cf 90 // if (i == 16) {
stearnsc 8:28b866df62cf 91 // i = 0;
stearnsc 8:28b866df62cf 92 // otherGps.latitude = nextInt(packet, 0);
stearnsc 8:28b866df62cf 93 // otherGps.longitude = nextInt(packet, 4);
stearnsc 8:28b866df62cf 94 // otherGps.altitude = nextInt(packet, 8);
stearnsc 8:28b866df62cf 95 // otherGps.time = nextInt(packet, 12);
stearnsc 8:28b866df62cf 96 //
stearnsc 8:28b866df62cf 97 // pc.printf("His GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n",
stearnsc 8:28b866df62cf 98 // otherGps.latitude, otherGps.longitude, otherGps.altitude, otherGps.time
stearnsc 8:28b866df62cf 99 // );
stearnsc 8:28b866df62cf 100 // reading = false;
stearnsc 8:28b866df62cf 101 // }
stearnsc 8:28b866df62cf 102 // } else if (c == 'X') {
stearnsc 8:28b866df62cf 103 // reading = true;
stearnsc 8:28b866df62cf 104 // }
stearnsc 8:28b866df62cf 105 //}
dylanembed123 7:c75d5e5e6bfc 106
dylanembed123 7:c75d5e5e6bfc 107
stearnsc 8:28b866df62cf 108 int main()
stearnsc 8:28b866df62cf 109 {
dylanembed123 14:6be57da62283 110 //getPS().sendPacket(0,NULL,0,PT_EMPTY);getPS().sendPacket(55,NULL,0,PT_IMAGE);
dylanembed123 15:e3e03a9df89e 111 //char output[256];for(int i=0;i<256;i++){output[i]=i;}getPS().sendPacket(0,NULL,0,PT_IMAGE);getPS().sendPacket(55,output,256);getPS().sendPacket(55,output,5);getPS().sendPacket(55,NULL,0,PT_END);
dylanembed123 15:e3e03a9df89e 112 //while(true){}
dylanembed123 15:e3e03a9df89e 113 /* PacketStruct* packs[4];
dylanembed123 14:6be57da62283 114 for(int i=0;i<4;i){
dylanembed123 14:6be57da62283 115 packs[i]=getPS().getNextPacket();
dylanembed123 14:6be57da62283 116 if(packs[i]!=NULL)i++;
dylanembed123 14:6be57da62283 117 }
dylanembed123 14:6be57da62283 118 for(int i=0;i<4;i++){
dylanembed123 14:6be57da62283 119 PacketStruct* pack=packs[i];
dylanembed123 14:6be57da62283 120 if(pack!=NULL){
dylanembed123 14:6be57da62283 121 USB::getSerial().printf("Got Packet!\n");
dylanembed123 14:6be57da62283 122 USB::getSerial().printf(" > %d\n",pack->type);
dylanembed123 14:6be57da62283 123 for(int i=0;i<sizeof(PacketStruct);i++){
dylanembed123 14:6be57da62283 124 USB::getSerial().printf("%d\n",((char*)pack)[i]);
dylanembed123 14:6be57da62283 125 }
dylanembed123 14:6be57da62283 126 USB::getSerial().printf("\n",pack->type);
dylanembed123 14:6be57da62283 127 }else{
dylanembed123 14:6be57da62283 128 //USB::getSerial().printf(".");
dylanembed123 14:6be57da62283 129 }
dylanembed123 14:6be57da62283 130 }
dylanembed123 14:6be57da62283 131 while(true){}
dylanembed123 14:6be57da62283 132 */
dylanembed123 13:a6d3cf2b018e 133 //getPS().sendPacket(55,NULL,0,PT_IMAGE);char output[256];for(int i=0;i<256;i++){output[i]=i;}getPS().sendPacket(55,output,256,PT_IMAGE);getPS().sendPacket(55,output,5,PT_IMAGE);getPS().sendPacket(55,NULL,0,PT_END);while(true){}
dylanembed123 7:c75d5e5e6bfc 134 ImageHandle imageHand;
dylanembed123 9:da906eeac51e 135 GPSHandle gpsHand;
dylanembed123 11:97625c27ab90 136 /// Main Loop
dylanembed123 13:a6d3cf2b018e 137 USB::getSerial().printf("Starting %d\n",sizeof(PacketStruct));
dylanembed123 13:a6d3cf2b018e 138 //while(1){
dylanembed123 13:a6d3cf2b018e 139 //USB::getSerial().printf("Test\n");
dylanembed123 13:a6d3cf2b018e 140 //XBEE::getSerial().printf("ABC\n");
dylanembed123 13:a6d3cf2b018e 141 //}
dylanembed123 14:6be57da62283 142 USB::getSerial().printf("Check GPS\n");
krobertson 18:e72ee7aed088 143
krobertson 18:e72ee7aed088 144 USB::getSerial().printf("Connect to the wifly network now!\r\n");
krobertson 18:e72ee7aed088 145 __disable_irq();
krobertson 18:e72ee7aed088 146 XBEE::getTCPInterrupt().fall(&connection_lost);
krobertson 18:e72ee7aed088 147
krobertson 19:8c1f2a2204fb 148 getPS().openConnection();
krobertson 19:8c1f2a2204fb 149 getPS().closeConnection();
krobertson 18:e72ee7aed088 150 //for(int a=0;a<1000000000;a++);
dylanembed123 11:97625c27ab90 151 while(1){
krobertson 19:8c1f2a2204fb 152 while(1){
krobertson 19:8c1f2a2204fb 153 USB::getSerial().printf("sending 0's\n");
krobertson 19:8c1f2a2204fb 154 //getPS().openConnection();
krobertson 19:8c1f2a2204fb 155 USB::getSerial().printf("connection open!\r\n");
krobertson 19:8c1f2a2204fb 156 imageHand.run();
krobertson 19:8c1f2a2204fb 157 USB::getSerial().printf("sent all data\r\n");
krobertson 19:8c1f2a2204fb 158 //for(int a=0;a<10000000;a++);
krobertson 19:8c1f2a2204fb 159 //getPS().closeConnection();
krobertson 19:8c1f2a2204fb 160 //for(int a=0;a<10000000;a++);
krobertson 19:8c1f2a2204fb 161 }
krobertson 18:e72ee7aed088 162
dylanembed123 11:97625c27ab90 163 // Run image handler
dylanembed123 15:e3e03a9df89e 164 USB::getSerial().printf("Check Image\n");
dylanembed123 15:e3e03a9df89e 165 imageHand.run();
dylanembed123 11:97625c27ab90 166 // Run GPS handler
dylanembed123 15:e3e03a9df89e 167 USB::getSerial().printf("Check GPS\n");
dylanembed123 14:6be57da62283 168 gpsHand.run();
dylanembed123 16:4f5d20b87dc3 169 USB::getSerial().printf("GPS Time: %f\n",DH::Locs().getC().getTime());
dylanembed123 15:e3e03a9df89e 170 // Read packet
dylanembed123 15:e3e03a9df89e 171 USB::getSerial().printf("Read Input\n");
dylanembed123 16:4f5d20b87dc3 172 //PacketStruct* pack=getPS().lastValid;//getPS().getNextPacket();
dylanembed123 16:4f5d20b87dc3 173 //if(pack!=NULL){
dylanembed123 16:4f5d20b87dc3 174 // USB::getSerial().printf("Received Type: %d\n",pack->type);
dylanembed123 16:4f5d20b87dc3 175 // if(pack->type==PT_REQLOC){
dylanembed123 16:4f5d20b87dc3 176 if(getPS().outputDevice.readable()>0){
dylanembed123 16:4f5d20b87dc3 177 char input=getPS().outputDevice.getc();
dylanembed123 16:4f5d20b87dc3 178 //if(getPS().outputDevice.getc()=='A'){
dylanembed123 15:e3e03a9df89e 179 // Send location
dylanembed123 15:e3e03a9df89e 180 unsigned int sID=getPS().getSuperID();
dylanembed123 15:e3e03a9df89e 181 getPS().sendPacket(0,NULL,0,PT_EMPTY);
dylanembed123 15:e3e03a9df89e 182 getPS().sendPacket(sID,NULL,0,PT_SENDLOC);
dylanembed123 15:e3e03a9df89e 183 getPS().sendPacket(sID,(char*)(&DH::Locs().getC().getLoc()),sizeof(DataLocation));
dylanembed123 15:e3e03a9df89e 184 getPS().sendPacket(sID,NULL,0,PT_END);
dylanembed123 16:4f5d20b87dc3 185 //}
dylanembed123 15:e3e03a9df89e 186 }
dylanembed123 11:97625c27ab90 187 }
dylanembed123 7:c75d5e5e6bfc 188 /// Main Loop
stearnsc 8:28b866df62cf 189 while(true) {
stearnsc 8:28b866df62cf 190 gps.baud(57600);
stearnsc 8:28b866df62cf 191 xbee.baud(9600);
stearnsc 8:28b866df62cf 192 pc.baud(57600);
stearnsc 8:28b866df62cf 193
stearnsc 8:28b866df62cf 194 sendGpsCommand("PMTK301,1");
stearnsc 8:28b866df62cf 195 while(true) {
stearnsc 8:28b866df62cf 196 pc.putc(gps.getc());
stearnsc 8:28b866df62cf 197 }
dylanembed123 9:da906eeac51e 198 //gps.attach(&handleGpsData, Serial::RxIrq);
dylanembed123 9:da906eeac51e 199 //xbee.attach(&handleXbeeGps, Serial::RxIrq)//;
dylanembed123 6:434d20e99e49 200 }
dylanembed123 9:da906eeac51e 201 }