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