QC Control software

Dependencies:   mbed

Fork of dgps by Colin Stearns

Committer:
dylanembed123
Date:
Sun Apr 27 02:31:00 2014 +0000
Revision:
61:aa32e17f6801
Parent:
60:bf851bafc807
Child:
64:d4818fb7813c
Did some strange trickery to get the flight controller to actually return lat, lon and alt before it apparently crashes? I'm starting to see why they didn't include a location function...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dylanembed123 24:e65416d6de22 1 #include "mavcommands.h"
dylanembed123 24:e65416d6de22 2
dylanembed123 24:e65416d6de22 3 // Maximium number of charictors to read without giving up the processor
krobertson 60:bf851bafc807 4 #define MAX_READ_LOOP 2560
dylanembed123 24:e65416d6de22 5
dylanembed123 54:fc7c8b5d4d41 6 #define ENABLE_MOTORS false
dylanembed123 54:fc7c8b5d4d41 7
dylanembed123 24:e65416d6de22 8 MavCmd* MavCmd::mavcmd=NULL;
dylanembed123 24:e65416d6de22 9
dylanembed123 54:fc7c8b5d4d41 10 static void fixLatLonAlt(double& latitude,double& longitude,double& altitude){
dylanembed123 54:fc7c8b5d4d41 11 latitude/=10000000.0f;
dylanembed123 54:fc7c8b5d4d41 12 longitude/=10000000.0f;
dylanembed123 56:daeef3c9ca94 13 altitude/=10000000.0f;
krobertson 60:bf851bafc807 14 USB::getSerial().printf("Got2 lat %f\n",latitude);USB::getSerial().printf("Got lon2 %f\n",longitude);USB::getSerial().printf("Got alt2 %f\n",altitude);
krobertson 60:bf851bafc807 15 return;
dylanembed123 47:fd373c4ea831 16 int degrees;
dylanembed123 47:fd373c4ea831 17 double minutes;
dylanembed123 47:fd373c4ea831 18 degrees = (int)(latitude/100);
dylanembed123 47:fd373c4ea831 19 minutes = latitude - degrees*100;
dylanembed123 47:fd373c4ea831 20 latitude = degrees + minutes/60;
dylanembed123 47:fd373c4ea831 21 degrees = (int)(longitude/100);
dylanembed123 47:fd373c4ea831 22 minutes = longitude - degrees*100;
dylanembed123 47:fd373c4ea831 23 longitude = degrees + minutes/60;
dylanembed123 47:fd373c4ea831 24 }
dylanembed123 47:fd373c4ea831 25
dylanembed123 54:fc7c8b5d4d41 26 bool MavCmd::hasMoved(){
dylanembed123 57:0b725b24f0c7 27 double nLat=DH::Locs().getC(LHType_targ,DH::Locs().getI(LHType_targ)).getLat();
dylanembed123 57:0b725b24f0c7 28 double nLon=DH::Locs().getC(LHType_targ,DH::Locs().getI(LHType_targ)).getLon();
dylanembed123 57:0b725b24f0c7 29 double nAlt=DH::Locs().getC(LHType_targ,DH::Locs().getI(LHType_targ)).getAlt();
dylanembed123 54:fc7c8b5d4d41 30 if(moveValid&&cLat==nLat&&cLon==nLon&&cAlt==nAlt)return false;
dylanembed123 54:fc7c8b5d4d41 31 moveValid=false;
dylanembed123 54:fc7c8b5d4d41 32 cLat=nLat;
dylanembed123 54:fc7c8b5d4d41 33 cLon=nLon;
dylanembed123 54:fc7c8b5d4d41 34 cAlt=nAlt;
dylanembed123 54:fc7c8b5d4d41 35 return true;
dylanembed123 54:fc7c8b5d4d41 36 }
dylanembed123 54:fc7c8b5d4d41 37
dylanembed123 24:e65416d6de22 38 void MavCmd::setupCmds(){
dylanembed123 24:e65416d6de22 39 // Request item list
dylanembed123 24:e65416d6de22 40 req.targSys=1;req.targComp=1;
dylanembed123 24:e65416d6de22 41 // Issue arm motors
dylanembed123 24:e65416d6de22 42 issueArm.targSys=1;issueArm.targComp=250;issueArm.cmd=400;issueArm.parm1=1.0f;
dylanembed123 24:e65416d6de22 43 // Issue disarm motors
dylanembed123 24:e65416d6de22 44 issueDisArm.targSys=1;issueDisArm.targComp=250;issueDisArm.cmd=400;issueDisArm.parm1=0.0f;
dylanembed123 24:e65416d6de22 45 // Issue a mission count
dylanembed123 61:aa32e17f6801 46 issueCount.targSys=1;issueCount.targComp=1;issueCount.count=1;
dylanembed123 24:e65416d6de22 47 // Issue a mission item
dylanembed123 39:1acea80563cf 48 issueItem.targSys=1;issueItem.targComp=1;issueItem.lat=5.0f;issueItem.lon=6.0f;issueItem.alt=7.0f;issueItem.cmd=16;issueItem.seq=0;issueItem.current=0;issueItem.frame=0;issueItem.confirm=0;
dylanembed123 24:e65416d6de22 49 // Issue a take off item
dylanembed123 24:e65416d6de22 50 issueTakeOff.targSys=1;issueTakeOff.targComp=1;issueTakeOff.lat=5.0f;issueTakeOff.lon=6.0f;issueTakeOff.alt=7.0f;issueTakeOff.cmd=22;issueTakeOff.seq=0;issueTakeOff.current=1;
dylanembed123 33:ad63e7013801 51 // Issue start
dylanembed123 33:ad63e7013801 52 issueStart.targSys=1;issueStart.targComp=1;issueStart.lat=5.0f;issueStart.lon=6.0f;issueStart.alt=7.0f;issueStart.cmd=22;issueStart.seq=0;issueStart.current=1;
dylanembed123 52:b4dddb28dffa 53 // Issue location request
dylanembed123 52:b4dddb28dffa 54 issueStreamReq.targSys=1;issueStreamReq.targComp=1;issueStreamReq.streamID=6;issueStreamReq.rate=5;issueStreamReq.start=1;
dylanembed123 52:b4dddb28dffa 55
dylanembed123 24:e65416d6de22 56 // Local variables
dylanembed123 24:e65416d6de22 57 startSetup=true;// Set to true to initiate startup sequence
dylanembed123 24:e65416d6de22 58 readState=0; // Read State
dylanembed123 24:e65416d6de22 59 realLen=0; // How many more bytes need to be read
dylanembed123 24:e65416d6de22 60 readIndex=0; // Current index in next cmd (also the size)
dylanembed123 54:fc7c8b5d4d41 61 cLat=cLon=cAlt=0.0f;
dylanembed123 24:e65416d6de22 62 }
dylanembed123 24:e65416d6de22 63
dylanembed123 24:e65416d6de22 64 char* MavCmd::getNextCmd(){
dylanembed123 61:aa32e17f6801 65 if(Mav::getSerial().readable()>0){
dylanembed123 24:e65416d6de22 66 char input=Mav::getSerial().getc();
dylanembed123 61:aa32e17f6801 67 bool start=false;
dylanembed123 24:e65416d6de22 68 if(readState==0&&input==0xFE){
dylanembed123 61:aa32e17f6801 69 USB::getSerial().printf("\nS: ");
dylanembed123 24:e65416d6de22 70 readState=1;
dylanembed123 24:e65416d6de22 71 readIndex=1;
dylanembed123 61:aa32e17f6801 72 start=true;
dylanembed123 61:aa32e17f6801 73 }
dylanembed123 61:aa32e17f6801 74 USB::getSerial().printf(">%x~",input);
dylanembed123 61:aa32e17f6801 75 if(!start&&readState!=0){
dylanembed123 24:e65416d6de22 76 nextCmd[std::min(readIndex++,512)]=input;
dylanembed123 24:e65416d6de22 77 if(readState==1){
dylanembed123 24:e65416d6de22 78 realLen=input+5-1;
dylanembed123 24:e65416d6de22 79 readState=2;
dylanembed123 24:e65416d6de22 80 }
dylanembed123 24:e65416d6de22 81 if(readState==2){
dylanembed123 24:e65416d6de22 82 realLen--;
dylanembed123 61:aa32e17f6801 83 if(readIndex-1==5&&nextCmd[5]==33){realLen=12;}
dylanembed123 24:e65416d6de22 84 if(realLen==0){
dylanembed123 61:aa32e17f6801 85 USB::getSerial().printf(" E\n");
dylanembed123 24:e65416d6de22 86 readState=0;
dylanembed123 24:e65416d6de22 87 char* output=new char[readIndex];
dylanembed123 24:e65416d6de22 88 for(int i=0;i<readIndex;i++){output[i]=nextCmd[i];}
dylanembed123 24:e65416d6de22 89 return output;
dylanembed123 24:e65416d6de22 90 }
dylanembed123 24:e65416d6de22 91 }
dylanembed123 24:e65416d6de22 92 }
dylanembed123 24:e65416d6de22 93 }
dylanembed123 61:aa32e17f6801 94 return NULL;
dylanembed123 24:e65416d6de22 95 }
dylanembed123 24:e65416d6de22 96
dylanembed123 24:e65416d6de22 97 // Handle the next command (if one is available)
dylanembed123 24:e65416d6de22 98 void MavCmd::handleNextCmd(){
dylanembed123 24:e65416d6de22 99 char* myCmd=getNextCmd();
dylanembed123 24:e65416d6de22 100 // Output debug info
dylanembed123 24:e65416d6de22 101 if(myCmd!=NULL){
dylanembed123 24:e65416d6de22 102 USB::getSerial().printf("Got CMD len %d messageid %d \n",myCmd[1],myCmd[5]);
dylanembed123 24:e65416d6de22 103 if(myCmd[5]==0){
dylanembed123 61:aa32e17f6801 104 //USB::getSerial().printf("Issue Command\n",myCmd[1],myCmd[5]);
dylanembed123 54:fc7c8b5d4d41 105 // Issue count to init waypoint entry
dylanembed123 54:fc7c8b5d4d41 106 //Mav::sendOutput(MAVLINK_MSG_ID_COUNT,(char*)&issueCount,sizeof(MAV_COUNT));
dylanembed123 54:fc7c8b5d4d41 107 // Start sending location data
dylanembed123 54:fc7c8b5d4d41 108 //Mav::sendOutput(MAVLINK_MSG_ID_REQUEST_DATA_STREAM,(char*)&issueStreamReq,sizeof(MAV_DATA_STREAM));
dylanembed123 54:fc7c8b5d4d41 109 //else{
dylanembed123 52:b4dddb28dffa 110 // Start sending location data
dylanembed123 52:b4dddb28dffa 111 //Mav::sendOutput(MAVLINK_MSG_ID_REQUEST_LIST,(char*)&req,sizeof(MAV_REQUEST_LIST));
dylanembed123 52:b4dddb28dffa 112 //}
dylanembed123 24:e65416d6de22 113 }
dylanembed123 24:e65416d6de22 114 // Check for mavlink message request
dylanembed123 24:e65416d6de22 115 if(myCmd[5]==MAVLINK_MSG_ID_MISSION_REQUEST){
dylanembed123 54:fc7c8b5d4d41 116 USB::getSerial().printf("Issue Item %f %f %f\n",cLat,cLon,cAlt);
dylanembed123 24:e65416d6de22 117 // Set sequence to requested sequence
dylanembed123 24:e65416d6de22 118 issueItem.seq=myCmd[6];
dylanembed123 54:fc7c8b5d4d41 119 issueItem.lat=(float)cLat;
dylanembed123 54:fc7c8b5d4d41 120 issueItem.lon=(float)cLon;
dylanembed123 54:fc7c8b5d4d41 121 issueItem.alt=(float)cAlt;
dylanembed123 24:e65416d6de22 122 // Issue mission item
dylanembed123 24:e65416d6de22 123 Mav::sendOutput(MAVLINK_MSG_ID_ITEM,(char*)&issueItem,sizeof(MAV_MISSION_ITEM));
dylanembed123 24:e65416d6de22 124 }
dylanembed123 24:e65416d6de22 125 // Check for mission accepted
dylanembed123 24:e65416d6de22 126 if(myCmd[5]==MAVLINK_MSG_ID_MISSION_ACK){
dylanembed123 54:fc7c8b5d4d41 127 moveValid=true;
dylanembed123 54:fc7c8b5d4d41 128 if(startSetup&&ENABLE_MOTORS){
dylanembed123 54:fc7c8b5d4d41 129 startSetup=false;
dylanembed123 54:fc7c8b5d4d41 130 // Start
dylanembed123 54:fc7c8b5d4d41 131 Mav::sendOutput(MAVLINK_MSG_ID_ITEM,(char*)&issueStart,sizeof(MAV_MISSION_ITEM));
dylanembed123 54:fc7c8b5d4d41 132 wait(1);
dylanembed123 54:fc7c8b5d4d41 133 // Take off
dylanembed123 54:fc7c8b5d4d41 134 Mav::sendOutput(MAVLINK_MSG_ID_ITEM,(char*)&issueTakeOff,sizeof(MAV_MISSION_ITEM));
dylanembed123 54:fc7c8b5d4d41 135 wait(1);
dylanembed123 54:fc7c8b5d4d41 136 // Start sending location data
dylanembed123 54:fc7c8b5d4d41 137 Mav::sendOutput(MAVLINK_MSG_ID_REQUEST_DATA_STREAM,(char*)&issueStreamReq,sizeof(MAV_DATA_STREAM));
dylanembed123 54:fc7c8b5d4d41 138 wait(1);
dylanembed123 54:fc7c8b5d4d41 139 // Arm motors
dylanembed123 54:fc7c8b5d4d41 140 Mav::sendOutput(MAVLINK_MSG_ID_LONG,(char*)&issueArm,sizeof(MAV_APM));
dylanembed123 54:fc7c8b5d4d41 141 }
dylanembed123 24:e65416d6de22 142 }
dylanembed123 52:b4dddb28dffa 143 // Check for GPS
dylanembed123 52:b4dddb28dffa 144 if(myCmd[5]==33){
dylanembed123 61:aa32e17f6801 145 //for(int i=0;i<18;i++){USB::getSerial().printf("Got loc %d/18 %d\n",i,myCmd[6+i]);}
dylanembed123 52:b4dddb28dffa 146 MAV_LOCDATA* input=(MAV_LOCDATA*)&myCmd[6];
krobertson 60:bf851bafc807 147
dylanembed123 54:fc7c8b5d4d41 148 USB::getSerial().printf("Got lat %d\n",input->lat);USB::getSerial().printf("Got lon %d\n",input->lon);USB::getSerial().printf("Got alt %d\n",input->alt);
krobertson 60:bf851bafc807 149 double lat=(double)*((int32_t*)&myCmd[10]);double lon=(double)*((int32_t*)&myCmd[10+4]);double alt=(double)*((int32_t*)&myCmd[10+8]);
dylanembed123 54:fc7c8b5d4d41 150 fixLatLonAlt(lat,lon,alt);
dylanembed123 54:fc7c8b5d4d41 151 USB::getSerial().printf("Got lat fixed %f\n",lat);USB::getSerial().printf("Got lon %f\n",lon);USB::getSerial().printf("Got alt %f\n",alt);
dylanembed123 54:fc7c8b5d4d41 152 DH::Locs().add(LHType_locs,DataLocation(lat,lon,alt));
dylanembed123 52:b4dddb28dffa 153 }
dylanembed123 39:1acea80563cf 154 // Check for waypoint count
dylanembed123 39:1acea80563cf 155 if(myCmd[5]==44){
dylanembed123 39:1acea80563cf 156 USB::getSerial().printf("Waypoints tsys %d tcomp %d cnt %d\n",myCmd[8],myCmd[7],myCmd[6]);
dylanembed123 39:1acea80563cf 157 }
dylanembed123 24:e65416d6de22 158 delete myCmd;
dylanembed123 24:e65416d6de22 159 }
dylanembed123 24:e65416d6de22 160 }