Colin Stearns / Mbed 2 deprecated qcControl

Dependencies:   mbed

Fork of dgps by Colin Stearns

Committer:
dylanembed123
Date:
Mon Apr 21 13:54:55 2014 +0000
Revision:
21:c546eab07e28
Parent:
16:4f5d20b87dc3
Child:
22:9880a26886db
Issue command to flight controller.

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"
dylanembed123 21:c546eab07e28 7 #include "mavcontrol.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
stearnsc 0:9c001c4e7bf4 28 //sends: "$<command>*<checksum>\r\l"
stearnsc 8:28b866df62cf 29 void sendGpsCommand(string command)
stearnsc 8:28b866df62cf 30 {
stearnsc 0:9c001c4e7bf4 31 uint8_t checksum = 0;
stearnsc 0:9c001c4e7bf4 32 pc.printf("Sending command to gps: ");
stearnsc 0:9c001c4e7bf4 33 gps.putc('$');
stearnsc 0:9c001c4e7bf4 34 pc.putc('$');
stearnsc 0:9c001c4e7bf4 35 char c;
stearnsc 8:28b866df62cf 36 for (int i = 0; i < command.length(); i++) {
stearnsc 0:9c001c4e7bf4 37 c = command[i];
stearnsc 0:9c001c4e7bf4 38 checksum ^= c;
stearnsc 0:9c001c4e7bf4 39 gps.putc(c);
stearnsc 0:9c001c4e7bf4 40 pc.putc(c);
stearnsc 0:9c001c4e7bf4 41 }
stearnsc 0:9c001c4e7bf4 42 gps.putc('*');
stearnsc 0:9c001c4e7bf4 43 pc.putc('*');
stearnsc 0:9c001c4e7bf4 44 string checkSumString;
stearnsc 8:28b866df62cf 45 while (checksum > 0) {
stearnsc 0:9c001c4e7bf4 46 uint8_t checksumChar = checksum & 0x0F;
stearnsc 8:28b866df62cf 47 if (checksumChar >= 10) {
stearnsc 0:9c001c4e7bf4 48 checksumChar -= 10;
stearnsc 8:28b866df62cf 49 checksumChar += 'A';
stearnsc 0:9c001c4e7bf4 50 } else {
stearnsc 8:28b866df62cf 51 checksumChar += '0';
stearnsc 0:9c001c4e7bf4 52 }
stearnsc 0:9c001c4e7bf4 53 checkSumString.push_back((char) checksumChar);
stearnsc 8:28b866df62cf 54 checksum = checksum >> 4;
stearnsc 0:9c001c4e7bf4 55 }
stearnsc 0:9c001c4e7bf4 56
stearnsc 8:28b866df62cf 57 for (int i = checkSumString.length() - 1; i >= 0; i--) {
stearnsc 0:9c001c4e7bf4 58 gps.putc(checkSumString[i]);
stearnsc 8:28b866df62cf 59 pc.putc(checkSumString[i]);
stearnsc 8:28b866df62cf 60 }
stearnsc 0:9c001c4e7bf4 61 gps.putc('\r');
stearnsc 0:9c001c4e7bf4 62 pc.putc('\r');
stearnsc 0:9c001c4e7bf4 63 gps.putc('\n');
stearnsc 0:9c001c4e7bf4 64 pc.putc('\n');
stearnsc 0:9c001c4e7bf4 65 }
stearnsc 8:28b866df62cf 66 //
stearnsc 8:28b866df62cf 67 ////cs: little endian parsing
stearnsc 8:28b866df62cf 68 //int nextInt(char *data, int i)
stearnsc 8:28b866df62cf 69 //{
stearnsc 8:28b866df62cf 70 // i |= data[i];
stearnsc 8:28b866df62cf 71 // i |= (data[i + 1] << 8);
stearnsc 8:28b866df62cf 72 // i |= (data[i + 2] << 16);
stearnsc 8:28b866df62cf 73 // i |= (data[i + 3] << 24);
stearnsc 8:28b866df62cf 74 // return i;
stearnsc 8:28b866df62cf 75 //}
stearnsc 0:9c001c4e7bf4 76
stearnsc 8:28b866df62cf 77 //void handleXbeeGps()
stearnsc 8:28b866df62cf 78 //{
stearnsc 8:28b866df62cf 79 // static bool reading = false;
stearnsc 8:28b866df62cf 80 // static char packet[16];
stearnsc 8:28b866df62cf 81 // static int i = 0;
stearnsc 8:28b866df62cf 82 //
stearnsc 8:28b866df62cf 83 // char c = xbee.getc();
stearnsc 8:28b866df62cf 84 // if (reading) {
stearnsc 8:28b866df62cf 85 // packet[i] = c;
stearnsc 8:28b866df62cf 86 // i++;
stearnsc 8:28b866df62cf 87 // if (i == 16) {
stearnsc 8:28b866df62cf 88 // i = 0;
stearnsc 8:28b866df62cf 89 // otherGps.latitude = nextInt(packet, 0);
stearnsc 8:28b866df62cf 90 // otherGps.longitude = nextInt(packet, 4);
stearnsc 8:28b866df62cf 91 // otherGps.altitude = nextInt(packet, 8);
stearnsc 8:28b866df62cf 92 // otherGps.time = nextInt(packet, 12);
stearnsc 8:28b866df62cf 93 //
stearnsc 8:28b866df62cf 94 // pc.printf("His GPS data: Lat: %d, Lon: %d, Alt: %d, Time:%d\r\n",
stearnsc 8:28b866df62cf 95 // otherGps.latitude, otherGps.longitude, otherGps.altitude, otherGps.time
stearnsc 8:28b866df62cf 96 // );
stearnsc 8:28b866df62cf 97 // reading = false;
stearnsc 8:28b866df62cf 98 // }
stearnsc 8:28b866df62cf 99 // } else if (c == 'X') {
stearnsc 8:28b866df62cf 100 // reading = true;
stearnsc 8:28b866df62cf 101 // }
stearnsc 8:28b866df62cf 102 //}
dylanembed123 7:c75d5e5e6bfc 103
dylanembed123 21:c546eab07e28 104 typedef struct MAV_REQUEST_LIST_S{
dylanembed123 21:c546eab07e28 105 char targSys;
dylanembed123 21:c546eab07e28 106 char targComp;
dylanembed123 21:c546eab07e28 107 }MAV_REQUEST_LIST;
dylanembed123 21:c546eab07e28 108
dylanembed123 21:c546eab07e28 109 typedef struct MAV_COUNT_S{
dylanembed123 21:c546eab07e28 110 char targSys;
dylanembed123 21:c546eab07e28 111 char targComp;
dylanembed123 21:c546eab07e28 112 uint16_t count;
dylanembed123 21:c546eab07e28 113 }MAV_COUNT;
dylanembed123 21:c546eab07e28 114
dylanembed123 21:c546eab07e28 115 typedef struct mav_APM_S{
dylanembed123 21:c546eab07e28 116 float parm1;
dylanembed123 21:c546eab07e28 117 float parm2;
dylanembed123 21:c546eab07e28 118 float parm3;
dylanembed123 21:c546eab07e28 119 float parm4;
dylanembed123 21:c546eab07e28 120 float parm5;
dylanembed123 21:c546eab07e28 121 float parm6;
dylanembed123 21:c546eab07e28 122 float parm7;
dylanembed123 21:c546eab07e28 123 uint16_t cmd;
dylanembed123 21:c546eab07e28 124 uint8_t targSys;
dylanembed123 21:c546eab07e28 125 uint8_t targComp;
dylanembed123 21:c546eab07e28 126 uint8_t confirm;
dylanembed123 21:c546eab07e28 127 } mav_APM;
dylanembed123 7:c75d5e5e6bfc 128
stearnsc 8:28b866df62cf 129 int main()
stearnsc 8:28b866df62cf 130 {
dylanembed123 21:c546eab07e28 131 char input[9];
dylanembed123 21:c546eab07e28 132 input[0]=0x00;
dylanembed123 21:c546eab07e28 133 input[1]=0x00;
dylanembed123 21:c546eab07e28 134 input[2]=0x00;
dylanembed123 21:c546eab07e28 135 input[3]=0x00;
dylanembed123 21:c546eab07e28 136 input[4]=0x02;
dylanembed123 21:c546eab07e28 137 input[5]=0x03;
dylanembed123 21:c546eab07e28 138 input[6]=0x51;
dylanembed123 21:c546eab07e28 139 input[7]=0x04;
dylanembed123 21:c546eab07e28 140 input[8]=0x03;
dylanembed123 21:c546eab07e28 141 /*
dylanembed123 21:c546eab07e28 142 while(true){
dylanembed123 21:c546eab07e28 143 char input[9];
dylanembed123 21:c546eab07e28 144 input[0]=0x00;
dylanembed123 21:c546eab07e28 145 input[1]=0x00;
dylanembed123 21:c546eab07e28 146 input[2]=0x00;
dylanembed123 21:c546eab07e28 147 input[3]=0x00;
dylanembed123 21:c546eab07e28 148 input[4]=0x02;
dylanembed123 21:c546eab07e28 149 input[5]=0x03;
dylanembed123 21:c546eab07e28 150 input[6]=0x51;
dylanembed123 21:c546eab07e28 151 input[7]=0x04;
dylanembed123 21:c546eab07e28 152 input[8]=0x03;
dylanembed123 21:c546eab07e28 153 Mav::sendOutput(0,input,9);
dylanembed123 21:c546eab07e28 154 wait_ms(50);
dylanembed123 21:c546eab07e28 155 }
dylanembed123 21:c546eab07e28 156 */
dylanembed123 21:c546eab07e28 157 //Mav::sendOutput(MAV_CMD_NAV_TAKEOFF,NULL,0);
dylanembed123 21:c546eab07e28 158 //Mav::sendOutput(MAVLINK_MSG_ID_LOITERU,NULL,0);
dylanembed123 21:c546eab07e28 159 //Mav::sendOutput(MAVLINK_MSG_ID_LOITERU,NULL,0);
dylanembed123 21:c546eab07e28 160 //Mav::sendOutput(MAV_CMD_NAV_LAND,NULL,0);
dylanembed123 21:c546eab07e28 161 //Mav::sendOutput(MAVLINK_MSG_ID_REQUEST_LIST,NULL,0);
dylanembed123 21:c546eab07e28 162 USB::getSerial().printf("System startup...\n");
dylanembed123 21:c546eab07e28 163 char nextCmd[512+1];
dylanembed123 21:c546eab07e28 164 int readIndex=0;
dylanembed123 21:c546eab07e28 165 bool reading=false;
dylanembed123 21:c546eab07e28 166
dylanembed123 21:c546eab07e28 167 MAV_REQUEST_LIST req;req.targSys=1;req.targComp=1;
dylanembed123 21:c546eab07e28 168 mav_APM issueCmd;
dylanembed123 21:c546eab07e28 169 issueCmd.targSys=1;issueCmd.targComp=1;issueCmd.cmd=17;issueCmd.parm1=76;issueCmd.parm2=5.0f;issueCmd.parm2=6.0f;issueCmd.parm3=7.0f;
dylanembed123 21:c546eab07e28 170 while(true){
dylanembed123 21:c546eab07e28 171 if(Mav::getSerial().readable()>0){
dylanembed123 21:c546eab07e28 172 char input=Mav::getSerial().getc();
dylanembed123 21:c546eab07e28 173 USB::getSerial().printf("> %x\n",input);
dylanembed123 21:c546eab07e28 174 if(input==0xFE){
dylanembed123 21:c546eab07e28 175 reading=true;
dylanembed123 21:c546eab07e28 176 readIndex=1;
dylanembed123 21:c546eab07e28 177 }else if(reading){
dylanembed123 21:c546eab07e28 178 nextCmd[std::min(readIndex++,512)]=input;
dylanembed123 21:c546eab07e28 179 }
dylanembed123 21:c546eab07e28 180 }
dylanembed123 21:c546eab07e28 181 // Output debug info
dylanembed123 21:c546eab07e28 182 if(readIndex==1&&reading){
dylanembed123 21:c546eab07e28 183 USB::getSerial().printf("Got CMD len %d messageid %d \n",nextCmd[1],nextCmd[5]);
dylanembed123 21:c546eab07e28 184 if(nextCmd[5]==0){
dylanembed123 21:c546eab07e28 185 //Mav::sendOutput(0,input,9);
dylanembed123 21:c546eab07e28 186 //Mav::sendOutput(MAVLINK_MSG_ID_LOITERU,(char*)&issueCmd,sizeof(mav_APM));
dylanembed123 21:c546eab07e28 187 Mav::sendOutput(MAVLINK_MSG_ID_REQUEST_LIST,(char*)&req,sizeof(MAV_REQUEST_LIST));
dylanembed123 21:c546eab07e28 188 //Mav::sendOutput(MAVLINK_MSG_ID_REQUEST_LIST,(char*)&req,sizeof(MAV_REQUEST_LIST));
dylanembed123 21:c546eab07e28 189 }
dylanembed123 21:c546eab07e28 190 }
dylanembed123 21:c546eab07e28 191 }
dylanembed123 21:c546eab07e28 192
dylanembed123 21:c546eab07e28 193 int outLength;
dylanembed123 21:c546eab07e28 194 char* output=Mav::generatePacket(MAVLINK_MSG_ID_REQUEST_LIST,NULL,0,&outLength);
dylanembed123 21:c546eab07e28 195 for(int i=0;i<outLength;i++){
dylanembed123 21:c546eab07e28 196 Mav::getSerial().putc(output[i]);
dylanembed123 21:c546eab07e28 197 }
dylanembed123 21:c546eab07e28 198 while(1){}
dylanembed123 21:c546eab07e28 199
dylanembed123 14:6be57da62283 200 //getPS().sendPacket(0,NULL,0,PT_EMPTY);getPS().sendPacket(55,NULL,0,PT_IMAGE);
dylanembed123 15:e3e03a9df89e 201 //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 202 //while(true){}
dylanembed123 15:e3e03a9df89e 203 /* PacketStruct* packs[4];
dylanembed123 14:6be57da62283 204 for(int i=0;i<4;i){
dylanembed123 14:6be57da62283 205 packs[i]=getPS().getNextPacket();
dylanembed123 14:6be57da62283 206 if(packs[i]!=NULL)i++;
dylanembed123 14:6be57da62283 207 }
dylanembed123 14:6be57da62283 208 for(int i=0;i<4;i++){
dylanembed123 14:6be57da62283 209 PacketStruct* pack=packs[i];
dylanembed123 14:6be57da62283 210 if(pack!=NULL){
dylanembed123 14:6be57da62283 211 USB::getSerial().printf("Got Packet!\n");
dylanembed123 14:6be57da62283 212 USB::getSerial().printf(" > %d\n",pack->type);
dylanembed123 14:6be57da62283 213 for(int i=0;i<sizeof(PacketStruct);i++){
dylanembed123 14:6be57da62283 214 USB::getSerial().printf("%d\n",((char*)pack)[i]);
dylanembed123 14:6be57da62283 215 }
dylanembed123 14:6be57da62283 216 USB::getSerial().printf("\n",pack->type);
dylanembed123 14:6be57da62283 217 }else{
dylanembed123 14:6be57da62283 218 //USB::getSerial().printf(".");
dylanembed123 14:6be57da62283 219 }
dylanembed123 14:6be57da62283 220 }
dylanembed123 14:6be57da62283 221 while(true){}
dylanembed123 14:6be57da62283 222 */
dylanembed123 13:a6d3cf2b018e 223 //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 224 ImageHandle imageHand;
dylanembed123 9:da906eeac51e 225 GPSHandle gpsHand;
dylanembed123 11:97625c27ab90 226 /// Main Loop
dylanembed123 13:a6d3cf2b018e 227 USB::getSerial().printf("Starting %d\n",sizeof(PacketStruct));
dylanembed123 13:a6d3cf2b018e 228 //while(1){
dylanembed123 13:a6d3cf2b018e 229 //USB::getSerial().printf("Test\n");
dylanembed123 13:a6d3cf2b018e 230 //XBEE::getSerial().printf("ABC\n");
dylanembed123 13:a6d3cf2b018e 231 //}
dylanembed123 14:6be57da62283 232 USB::getSerial().printf("Check GPS\n");
dylanembed123 11:97625c27ab90 233 while(1){
dylanembed123 11:97625c27ab90 234 // Run image handler
dylanembed123 15:e3e03a9df89e 235 USB::getSerial().printf("Check Image\n");
dylanembed123 15:e3e03a9df89e 236 imageHand.run();
dylanembed123 11:97625c27ab90 237 // Run GPS handler
dylanembed123 15:e3e03a9df89e 238 USB::getSerial().printf("Check GPS\n");
dylanembed123 14:6be57da62283 239 gpsHand.run();
dylanembed123 16:4f5d20b87dc3 240 USB::getSerial().printf("GPS Time: %f\n",DH::Locs().getC().getTime());
dylanembed123 15:e3e03a9df89e 241 // Read packet
dylanembed123 15:e3e03a9df89e 242 USB::getSerial().printf("Read Input\n");
dylanembed123 16:4f5d20b87dc3 243 //PacketStruct* pack=getPS().lastValid;//getPS().getNextPacket();
dylanembed123 16:4f5d20b87dc3 244 //if(pack!=NULL){
dylanembed123 16:4f5d20b87dc3 245 // USB::getSerial().printf("Received Type: %d\n",pack->type);
dylanembed123 16:4f5d20b87dc3 246 // if(pack->type==PT_REQLOC){
dylanembed123 16:4f5d20b87dc3 247 if(getPS().outputDevice.readable()>0){
dylanembed123 16:4f5d20b87dc3 248 char input=getPS().outputDevice.getc();
dylanembed123 16:4f5d20b87dc3 249 //if(getPS().outputDevice.getc()=='A'){
dylanembed123 15:e3e03a9df89e 250 // Send location
dylanembed123 15:e3e03a9df89e 251 unsigned int sID=getPS().getSuperID();
dylanembed123 15:e3e03a9df89e 252 getPS().sendPacket(0,NULL,0,PT_EMPTY);
dylanembed123 15:e3e03a9df89e 253 getPS().sendPacket(sID,NULL,0,PT_SENDLOC);
dylanembed123 15:e3e03a9df89e 254 getPS().sendPacket(sID,(char*)(&DH::Locs().getC().getLoc()),sizeof(DataLocation));
dylanembed123 15:e3e03a9df89e 255 getPS().sendPacket(sID,NULL,0,PT_END);
dylanembed123 16:4f5d20b87dc3 256 //}
dylanembed123 15:e3e03a9df89e 257 }
dylanembed123 11:97625c27ab90 258 }
dylanembed123 7:c75d5e5e6bfc 259 /// Main Loop
stearnsc 8:28b866df62cf 260 while(true) {
stearnsc 8:28b866df62cf 261 gps.baud(57600);
stearnsc 8:28b866df62cf 262 xbee.baud(9600);
stearnsc 8:28b866df62cf 263 pc.baud(57600);
stearnsc 8:28b866df62cf 264
stearnsc 8:28b866df62cf 265 sendGpsCommand("PMTK301,1");
stearnsc 8:28b866df62cf 266 while(true) {
stearnsc 8:28b866df62cf 267 pc.putc(gps.getc());
stearnsc 8:28b866df62cf 268 }
dylanembed123 9:da906eeac51e 269 //gps.attach(&handleGpsData, Serial::RxIrq);
dylanembed123 9:da906eeac51e 270 //xbee.attach(&handleXbeeGps, Serial::RxIrq)//;
dylanembed123 6:434d20e99e49 271 }
dylanembed123 9:da906eeac51e 272 }