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


tum_ardrone
Author(s):
autogenerated on Sat Jun 8 2019 20:27:23