Colin Stearns / Mbed 2 deprecated qcControl

Dependencies:   mbed

Fork of dgps by Colin Stearns

Committer:
krobertson
Date:
Sun Apr 20 08:04:37 2014 +0000
Revision:
18:e72ee7aed088
Parent:
16:4f5d20b87dc3
Child:
19:8c1f2a2204fb
new communication module. Need to add open and close connection to sync packet and end packet but otherwise looks like it will work.

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 18:e72ee7aed088 148 getPS().wait_for_egg();
krobertson 18:e72ee7aed088 149 //for(int a=0;a<1000000000;a++);
dylanembed123 11:97625c27ab90 150 while(1){
krobertson 18:e72ee7aed088 151 //while(1){
krobertson 18:e72ee7aed088 152 // USB::getSerial().printf("sending 0's\n");
krobertson 18:e72ee7aed088 153 // getPS().openConnection();
krobertson 18:e72ee7aed088 154 // USB::getSerial().printf("connection open!\r\n");
krobertson 18:e72ee7aed088 155 // for(int b=0;b<50;b++){
krobertson 18:e72ee7aed088 156 // for(int a=0;a<sizeof(PacketStruct)-1;a++){
krobertson 18:e72ee7aed088 157 // while(!getPS().outputDevice.writeable()){}
krobertson 18:e72ee7aed088 158 // getPS().outputDevice.putc(0);
krobertson 18:e72ee7aed088 159 // }
krobertson 18:e72ee7aed088 160 // while(!getPS().outputDevice.writeable()){}
krobertson 18:e72ee7aed088 161 // getPS().outputDevice.putc(0xFF);
krobertson 18:e72ee7aed088 162 // }
krobertson 18:e72ee7aed088 163 // USB::getSerial().printf("sent all data\r\n");
krobertson 18:e72ee7aed088 164 // //for(int a=0;a<10000000;a++);
krobertson 18:e72ee7aed088 165 // getPS().closeConnection();
krobertson 18:e72ee7aed088 166 // //for(int a=0;a<10000000;a++);
krobertson 18:e72ee7aed088 167 // }
krobertson 18:e72ee7aed088 168
dylanembed123 11:97625c27ab90 169 // Run image handler
dylanembed123 15:e3e03a9df89e 170 USB::getSerial().printf("Check Image\n");
dylanembed123 15:e3e03a9df89e 171 imageHand.run();
dylanembed123 11:97625c27ab90 172 // Run GPS handler
dylanembed123 15:e3e03a9df89e 173 USB::getSerial().printf("Check GPS\n");
dylanembed123 14:6be57da62283 174 gpsHand.run();
dylanembed123 16:4f5d20b87dc3 175 USB::getSerial().printf("GPS Time: %f\n",DH::Locs().getC().getTime());
dylanembed123 15:e3e03a9df89e 176 // Read packet
dylanembed123 15:e3e03a9df89e 177 USB::getSerial().printf("Read Input\n");
dylanembed123 16:4f5d20b87dc3 178 //PacketStruct* pack=getPS().lastValid;//getPS().getNextPacket();
dylanembed123 16:4f5d20b87dc3 179 //if(pack!=NULL){
dylanembed123 16:4f5d20b87dc3 180 // USB::getSerial().printf("Received Type: %d\n",pack->type);
dylanembed123 16:4f5d20b87dc3 181 // if(pack->type==PT_REQLOC){
dylanembed123 16:4f5d20b87dc3 182 if(getPS().outputDevice.readable()>0){
dylanembed123 16:4f5d20b87dc3 183 char input=getPS().outputDevice.getc();
dylanembed123 16:4f5d20b87dc3 184 //if(getPS().outputDevice.getc()=='A'){
dylanembed123 15:e3e03a9df89e 185 // Send location
dylanembed123 15:e3e03a9df89e 186 unsigned int sID=getPS().getSuperID();
dylanembed123 15:e3e03a9df89e 187 getPS().sendPacket(0,NULL,0,PT_EMPTY);
dylanembed123 15:e3e03a9df89e 188 getPS().sendPacket(sID,NULL,0,PT_SENDLOC);
dylanembed123 15:e3e03a9df89e 189 getPS().sendPacket(sID,(char*)(&DH::Locs().getC().getLoc()),sizeof(DataLocation));
dylanembed123 15:e3e03a9df89e 190 getPS().sendPacket(sID,NULL,0,PT_END);
dylanembed123 16:4f5d20b87dc3 191 //}
dylanembed123 15:e3e03a9df89e 192 }
dylanembed123 11:97625c27ab90 193 }
dylanembed123 7:c75d5e5e6bfc 194 /// Main Loop
stearnsc 8:28b866df62cf 195 while(true) {
stearnsc 8:28b866df62cf 196 gps.baud(57600);
stearnsc 8:28b866df62cf 197 xbee.baud(9600);
stearnsc 8:28b866df62cf 198 pc.baud(57600);
stearnsc 8:28b866df62cf 199
stearnsc 8:28b866df62cf 200 sendGpsCommand("PMTK301,1");
stearnsc 8:28b866df62cf 201 while(true) {
stearnsc 8:28b866df62cf 202 pc.putc(gps.getc());
stearnsc 8:28b866df62cf 203 }
dylanembed123 9:da906eeac51e 204 //gps.attach(&handleGpsData, Serial::RxIrq);
dylanembed123 9:da906eeac51e 205 //xbee.attach(&handleXbeeGps, Serial::RxIrq)//;
dylanembed123 6:434d20e99e49 206 }
dylanembed123 9:da906eeac51e 207 }