Go to the documentation of this file.00001
00022 #include "KIFlyTo.h"
00023 #include "../DroneController.h"
00024 #include "../ControlNode.h"
00025 #include "../../HelperFunctions.h"
00026
00027
00028 double angleFromTo(double angle, double min, double sup)
00029 {
00030 while(angle < min) angle += 360;
00031 while(angle >= sup) angle -= 360;
00032 return angle;
00033 }
00034
00035
00036 KIFlyTo::KIFlyTo(DronePosition checkpointP,
00037 double stayTime,
00038 double maxControlFactorP,
00039 double initialReachedDistP,
00040 double stayWithinDistP)
00041 {
00042 stayTimeMs = (int)(1000*stayTime);
00043 maxControlFactor = maxControlFactorP;
00044 initialReachedDist = initialReachedDistP;
00045 stayWithinDist = stayWithinDistP;
00046
00047 checkpoint = checkpointP;
00048
00049 reachedAtClock = -1;
00050 reached = false;
00051
00052 targetSet = false;
00053
00054 isCompleted = false;
00055
00056 char buf[200];
00057 snprintf(buf,200,"goto %.2f %.2f %.2f %.2f", checkpointP.pos[0], checkpointP.pos[1], checkpointP.pos[2], checkpointP.yaw);
00058 command = buf;
00059 }
00060
00061
00062 KIFlyTo::~KIFlyTo(void)
00063 {
00064 }
00065
00066
00067 bool KIFlyTo::update(const uga_tum_ardrone::filter_stateConstPtr statePtr)
00068 {
00069 if(!targetSet)
00070 controller->setTarget(checkpoint);
00071 targetSet = true;
00072
00073
00074 if(!isCompleted && reached && (getMS() - reachedAtClock) > stayTimeMs)
00075 {
00076 printf("checkpoint done!\n");
00077 isCompleted = true;
00078 }
00079 if(isCompleted)
00080 {
00081 node->sendControlToDrone(controller->update(statePtr));
00082 return true;
00083 }
00084
00085
00086
00087 TooN::Vector<3> diffs = TooN::makeVector(
00088 statePtr->x - checkpoint.pos[0],
00089 statePtr->y - checkpoint.pos[1],
00090 statePtr->z - checkpoint.pos[2]);
00091
00092 double checkpointYaw = angleFromTo(checkpoint.yaw, -180, 180);
00093 double stateYaw = angleFromTo(statePtr->yaw, -180, 180);
00094
00095 double diffYaw = stateYaw - checkpointYaw;
00096 diffYaw = angleFromTo(diffYaw, -180, 180);
00097 double diffDistSquared = diffs[0] * diffs[0] + diffs[1] * diffs[1] + diffs[2] * diffs[2];
00098
00099
00100 if(!reached && diffDistSquared < initialReachedDist * initialReachedDist && diffYaw*diffYaw < 25)
00101 {
00102 reached = true;
00103 reachedAtClock = getMS();
00104 printf("target reached initially!\n");
00105 }
00106
00107
00108 if(reached && (diffDistSquared > stayWithinDist * stayWithinDist || diffYaw*diffYaw > 25))
00109 {
00110 reached = false;
00111 printf("target lost again!\n");
00112 }
00113
00114
00115 node->sendControlToDrone(controller->update(statePtr));
00116 return false;
00117 }