FormationControlElement.cpp
Go to the documentation of this file.
00001 /*
00002  * FormationControlElement.cpp
00003  *
00004  *  Created on: Feb 10, 2012
00005  *      Author: mriedel
00006  */
00007 
00008 #include "FormationControlElement.hpp"
00009 
00010 //#include <telekyb_base/ROS.hpp>
00011 #include <telekyb_base/Time.hpp>
00012 
00013 #include <telekyb_base/Spaces.hpp>
00014 
00015 FormationControlElement::FormationControlElement(int robotID_, bool useMKInterface_)
00016         : robotID(robotID_), core(NULL), mkInterface(NULL)
00017 {
00018         core = telekyb_interface::TeleKybCore::getTeleKybCore(robotID);
00019         if (!core) {
00020                 // fail
00021                 ros::shutdown();
00022                 return;
00023         }
00024 
00025         //options.tUseMKInterface->setValue(true);
00026         if (useMKInterface_) {
00027                 ROS_INFO("Creating MKInterface!");
00028                 // use MKInterface
00029                 mkInterface = telekyb_interface::MKInterface::getMKInterface(robotID, 5.0);
00030                 if (!mkInterface) {
00031                         // fail
00032                         ros::shutdown();
00033                         return;
00034                 }
00035         }
00036 
00037         bController = core->getBehaviorController();
00038         oController = core->getOptionController();
00039 
00040         //activeBehavior = bController->getActiveBehaviorReference();
00041         bController->setActiveBehaviorListener(this);
00042         activeBehaviorPtr = bController->getActiveBehaviorPointer();
00043 
00044         setupFormationControlElement();
00045 }
00046 
00047 FormationControlElement::~FormationControlElement()
00048 {
00049         if (mkInterface) { delete mkInterface; }
00050 
00051         delete core;
00052 }
00053 
00054 void FormationControlElement::setupFormationControlElement()
00055 {
00056         // load Behaviors
00057         ground = bController->getSystemBehavior("tk_behavior/Ground");
00058         hover = bController->getSystemBehavior("tk_behavior/Hover");
00059         normalBreak = bController->getSystemBehavior("tk_behavior/NormalBrake");
00060         takeOff = bController->getSystemBehavior("tk_behavior/TakeOff");
00061         land = bController->getSystemBehavior("tk_behavior/Land");
00062 
00063 
00064         // sanity check
00065         if (ground.isNull() || hover.isNull() || normalBreak.isNull() || takeOff.isNull() || land.isNull()) {
00066                 ROS_FATAL("Unable to get SystemBehavior for UAV %d !!!", robotID);
00067                 //ROS_BREAK();
00068                 ros::shutdown();
00069         }
00070 
00071         // setup takeoff
00072         //takeOff.getOptionContainer().getOption("tTakeOffDestination").set(Position3D(0.0,0.0,-1.0));
00073         takeOff.getOptionContainer().getOption("tTakeOffVertically").set(false);
00074 
00075         // done
00076         takeOff.setParameterInitialized(true);
00077 
00078         //land.getOptionContainer().getOption("tLandDestination").set(Position3D(0.0,0.0,0.0));
00079         land.getOptionContainer().getOption("tLandVertically").set(true);
00080         land.setParameterInitialized(true);
00081 
00082 
00083         formation4 = bController->loadBehavior("tk_formation/Formation");
00084         formation3 = bController->loadBehavior("tk_formation/Formation");
00085 
00086         if ( formation4.isNull() || formation3.isNull()) {
00087                 ROS_FATAL("Unable to load FormationControl for UAV %d !!!", robotID);
00088                 //ROS_BREAK();
00089                 ros::shutdown();
00090         }
00091 
00092 
00093         tetraeder = bController->loadBehavior("tk_formation/FormationReconfiguration");
00094         square = bController->loadBehavior("tk_formation/FormationReconfiguration");
00095         triangle = bController->loadBehavior("tk_formation/FormationReconfiguration");
00096         if ( tetraeder.isNull() || square.isNull() || triangle.isNull() ) {
00097                 ROS_FATAL("Unable to load FormationReconfiguration Behavior for UAV %d !!!", robotID);
00098                 //ROS_BREAK();
00099                 ros::shutdown();
00100         }
00101 
00102         // configure calibrator
00103         //formation.setNextBehavior(land);
00104         //formation.setParameterInitialized(true);
00105 
00106 
00107         if (*activeBehaviorPtr != ground) {
00108                 ROS_ERROR("UAV %d not in Ground Behavior during Startup", robotID);
00109                 ros::shutdown();
00110         }
00111 }
00112 
00113 telekyb_interface::MKInterface* FormationControlElement::getMKInterfacePointer() const
00114 {
00115         return mkInterface;
00116 }
00117 
00118 telekyb_interface::TeleKybCore* FormationControlElement::getTeleKybCorePointer() const
00119 {
00120         return core;
00121 }
00122 
00123 bool FormationControlElement::mkSetEmergency()
00124 {
00125         if (mkInterface) {
00126                 return mkInterface->setEmergency();
00127         }
00128         return false;
00129 }
00130 
00131 bool FormationControlElement::mkToggleMotors()
00132 {
00133         // Button 1! toggle Motors for mkInterface Only in Ground
00134         if (mkInterface && *activeBehaviorPtr == ground) {
00135                 ROS_INFO("Toggle Motorstate!");
00136 
00137                 MKSingleValuePacket motorState(MKDataDefines::MOTOR_STATE,0);
00138                 if (!mkInterface->updateMKValue(motorState)) {
00139                         ROS_ERROR("Could not get Motorstate!");
00140                         return false;
00141                 }
00142 
00143                 if (motorState.value == MotorState::On) {
00144                         // stop
00145                         motorState.value = MotorState::Off;
00146                         return mkInterface->setMKValue(motorState);
00147                 } else if (motorState.value == MotorState::Off) {
00148                         // start
00149                         motorState.value = MotorState::Init;
00150                         return mkInterface->setMKValue(motorState);
00151                 } else if (motorState.value == MotorState::Init) {
00152                         motorState.value = MotorState::On;
00153                         return mkInterface->setMKValue(motorState);
00154                 }
00155         }
00156         return false;
00157 }
00158 
00159 bool FormationControlElement::liftland()
00160 {
00161         if (*activeBehaviorPtr == ground) {
00162 
00163                 if (mkInterface) {
00164                         MKSingleValuePacket motorState(MKDataDefines::MOTOR_STATE,0);
00165                         if (!mkInterface->updateMKValue(motorState)) {
00166                                 ROS_ERROR("Could not get Motorstate!");
00167                                 return false;
00168                         }
00169                         if (motorState.value == MotorState::On) {
00170                                 bController->switchBehavior(takeOff);
00171                         } else {
00172                                 ROS_ERROR("Motors have to be on for liftOff!");
00173                                 return false;
00174                         }
00175                 }
00176                 // takeoff
00177                 bController->switchBehavior(takeOff);
00178                 return true;
00179 
00180         } else {
00181                 // flying -> land
00182                 bController->switchBehavior(land);
00183                 return true;
00184         }
00185 
00186         return false;
00187 }
00188 
00189 void FormationControlElement::activeBehaviorChanged(telekyb_interface::Behavior newActiveBehavior)
00190 {
00191         if (mkInterface && newActiveBehavior == ground) {
00192                 MKSingleValuePacket motorState(MKDataDefines::MOTOR_STATE,0);
00193                 motorState.value = MotorState::Off;
00194                 mkInterface->setMKValue(motorState);
00195         }
00196 }
00197 
00198 //telekyb_interface::Behavior FormationControlElement::getFormationBehavior() const
00199 //{
00200 //      return formation;
00201 //}
00202 
00203 //void FormationControlElement::switchIntoFormation() const
00204 //{
00205 //      bController->switchBehavior(formation);
00206 //}
00207 
00208 
00209 
00210 
00211 
00212 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines


tk_formation
Author(s): Martin Riedel
autogenerated on Mon Nov 11 2013 11:14:18