Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include "FormationControlElement.hpp"
00009
00010
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
00021 ros::shutdown();
00022 return;
00023 }
00024
00025
00026 if (useMKInterface_) {
00027 ROS_INFO("Creating MKInterface!");
00028
00029 mkInterface = telekyb_interface::MKInterface::getMKInterface(robotID, 5.0);
00030 if (!mkInterface) {
00031
00032 ros::shutdown();
00033 return;
00034 }
00035 }
00036
00037 bController = core->getBehaviorController();
00038 oController = core->getOptionController();
00039
00040
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
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
00065 if (ground.isNull() || hover.isNull() || normalBreak.isNull() || takeOff.isNull() || land.isNull()) {
00066 ROS_FATAL("Unable to get SystemBehavior for UAV %d !!!", robotID);
00067
00068 ros::shutdown();
00069 }
00070
00071
00072
00073 takeOff.getOptionContainer().getOption("tTakeOffVertically").set(false);
00074
00075
00076 takeOff.setParameterInitialized(true);
00077
00078
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
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
00099 ros::shutdown();
00100 }
00101
00102
00103
00104
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
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
00145 motorState.value = MotorState::Off;
00146 return mkInterface->setMKValue(motorState);
00147 } else if (motorState.value == MotorState::Off) {
00148
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
00177 bController->switchBehavior(takeOff);
00178 return true;
00179
00180 } else {
00181
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
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212