KIAutoInit.cpp
Go to the documentation of this file.
00001 
00022 #include "KIAutoInit.h"
00023 #include "../DroneController.h"
00024 #include "../ControlNode.h"
00025 #include "../../HelperFunctions.h"
00026 
00027 
00028 KIAutoInit::KIAutoInit(bool resetMap, int imoveTimeMS, int iwaitTimeMS, int reachHeightMS, float controlMult, bool takeoff)
00029 {
00030         stage = NONE;
00031         this->resetMap = resetMap;
00032         moveTimeMS = imoveTimeMS;
00033         waitTimeMS = iwaitTimeMS;
00034         this->reachHeightMS = reachHeightMS;
00035         this->controlCommandMultiplier = controlMult;
00036 
00037         nextUp = false;
00038         stageStarted = false;
00039 
00040         if(!takeoff)
00041                 stage = WAIT_FOR_FIRST;
00042 
00043         char buf[200];
00044         if(resetMap)
00045                 snprintf(buf,200,"autoInit %d %d", imoveTimeMS, iwaitTimeMS);
00046         else
00047                 snprintf(buf,200,"takeoff");
00048 
00049         command = buf;
00050 }
00051 
00052 
00053 KIAutoInit::~KIAutoInit(void)
00054 {
00055 }
00056 
00057 
00058 bool KIAutoInit::update(const uga_tum_ardrone::filter_stateConstPtr statePtr)
00059 {
00060         // no PTAM initialization, just takeoff.
00061         if(!resetMap)
00062         {
00063                 switch(stage)
00064                 {
00065                 case NONE:              // start and proceed
00066                         node->sendTakeoff();
00067                         stageStarted = getMS();
00068                         stage = WAIT_FOR_SECOND;
00069                         node->sendControlToDrone(node->hoverCommand);
00070                         return false;
00071 
00072                 case WAIT_FOR_SECOND:
00073                         if(getMS() - stageStarted < 5000)
00074                         {
00075                                 node->sendControlToDrone(node->hoverCommand);
00076                                 return false;
00077                         }
00078 
00079                         controller->setTarget(DronePosition(
00080                                         TooN::makeVector(statePtr->x,statePtr->y,statePtr->z),statePtr->yaw));
00081                         node->sendControlToDrone(controller->update(statePtr));
00082                         stage = DONE;
00083                         return true;
00084                 case DONE:
00085                         node->sendControlToDrone(controller->update(statePtr));
00086                         return true;
00087                 default:
00088                         return false;
00089                 }
00090                 return true;    // should never happen....
00091         }
00092         else
00093         {
00094                 switch(stage)
00095                 {
00096                 case NONE:              // start and proceed
00097                         node->sendTakeoff();
00098                         node->publishCommand("f reset");        // reset whole filter.
00099 
00100                         stageStarted = getMS();
00101                         stage = STARTED;
00102                         nextUp = true;
00103                         node->sendControlToDrone(node->hoverCommand);
00104                         return false;
00105 
00106                 case STARTED:   // wait 6s to reach hight.
00107                         if(getMS() - stageStarted > reachHeightMS)
00108                         {
00109                             // check that the drone is actually flying
00110 
00111                             if (statePtr->droneState >=3 && statePtr->droneState <= 7 ) {
00112                     stageStarted = getMS();
00113                     stage = WAIT_FOR_FIRST;
00114                             } else {
00115                     // something went wrong, try again
00116                     stage = NONE;
00117                             }
00118                         }
00119                         node->sendControlToDrone(node->hoverCommand);
00120                         return false;
00121 
00122                 case WAIT_FOR_FIRST:    // wait 1s and press space
00123                         if(getMS() - stageStarted > 1000)
00124                         {
00125                                 node->publishCommand("p space");
00126                                 stageStarted = getMS();
00127                                 stage = TOOK_FIRST;
00128                         }
00129                         node->sendControlToDrone(node->hoverCommand);
00130                         return false;
00131 
00132                 case TOOK_FIRST:        // go [up/down] 1s and press space if was initializing.
00133                         if(getMS() - stageStarted < moveTimeMS)
00134                         {
00135                                 if(nextUp)
00136                                         node->sendControlToDrone(ControlCommand(0,0,0,0.6*controlCommandMultiplier));
00137                                 else
00138                                         node->sendControlToDrone(ControlCommand(0,0,0,-0.3*controlCommandMultiplier));
00139                         }
00140                         else if(getMS() - stageStarted < moveTimeMS+waitTimeMS)
00141                         {
00142                                 node->sendControlToDrone(node->hoverCommand);
00143                         }
00144                         else    // time is up, take second KF
00145                         {
00146                                 if(statePtr->ptamState == statePtr->PTAM_INITIALIZING)  // TODO: ptam status enum, this should be PTAM_INITIALIZING
00147                                 {
00148                                         node->publishCommand("p space");
00149                                         stageStarted = getMS();
00150                                         stage = WAIT_FOR_SECOND;
00151                                 }
00152                                 else    // sth was wrong: try again
00153                                 {
00154                                         nextUp = !nextUp;
00155                                         node->publishCommand("p reset");
00156                                         stageStarted = getMS();
00157                                         stage = WAIT_FOR_FIRST;
00158                                 }
00159                                 node->sendControlToDrone(node->hoverCommand);
00160                         }
00161                         return false;
00162 
00163                 case WAIT_FOR_SECOND:
00164 
00165                         // am i done?
00166                         if(statePtr->ptamState == statePtr->PTAM_BEST || statePtr->ptamState == statePtr->PTAM_GOOD || statePtr->ptamState == statePtr->PTAM_TOOKKF) // TODO: PTAM_GOOD or PTAM_BEST or PTAM_TOOKKF
00167                         {
00168                                 controller->setTarget(DronePosition(
00169                                                                         TooN::makeVector(statePtr->x,statePtr->y,statePtr->z),statePtr->yaw));
00170                                 node->sendControlToDrone(controller->update(statePtr));
00171                                 stage = DONE;
00172                                 return true;
00173                         }
00174 
00175                         // am i stil waiting?
00176                         // TODO: change this to something that becomes false, as soon as fail is evident.
00177                         if(getMS() - stageStarted < 2500)       // wait 2s
00178                         {
00179                                 node->sendControlToDrone(node->hoverCommand);
00180                                 return false;
00181                         }
00182 
00183                         // i have failed -> try again.
00184                         nextUp = !nextUp;
00185                         node->publishCommand("p reset");
00186                         stageStarted = getMS();
00187                         stage = WAIT_FOR_FIRST;
00188                         node->sendControlToDrone(node->hoverCommand);
00189                         return false;
00190                 case DONE:
00191                         node->sendControlToDrone(controller->update(statePtr));
00192                         return true;
00193                 default:
00194                         return false;
00195                 }
00196                 return false;   // again: should never happen....
00197         }
00198 }


uga_tum_ardrone
Author(s):
autogenerated on Sat Jun 8 2019 20:30:11