Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include "FormationControl.hpp"
00009
00010 #include <telekyb_base/ROS.hpp>
00011 #include <telekyb_base/Time.hpp>
00012
00013 #include <telekyb_base/Spaces.hpp>
00014
00015
00016 FormationControlOptions::FormationControlOptions()
00017 : OptionContainer("FormationControlOptions")
00018 {
00019 tRobotIDs = addOption< std::vector<int> >("tRobotIDs",
00020 "Specifies the ID of the Robots to connect to", std::vector<int>() , true, true);
00021 tJoystickTopic = addOption<std::string>("tJoystickTopic",
00022 "Joysticktopic to use (sensor_msgs::Joy)", "/TeleKyb/tJoy/joy", false, true);
00023 tVelocityInputTopic = addOption<std::string>("tVelocityInputTopic",
00024 "Velocity Input Topic to use (geometry_msgs::Vector3Stamped)", "FormationVelocityInput", false, true);
00025 tUseMKInterface = addOption<bool>("tUseMKInterface", "Set to true with MKInterface!", false, false, true);
00026 }
00027
00028 FormationControl::FormationControl()
00029 : mainNodeHandle( ROSModule::Instance().getMainNodeHandle() )
00030 {
00031 setupFormationControl();
00032 }
00033
00034 FormationControl::~FormationControl()
00035 {
00036 for (unsigned int i = 0; i < formationElements.size(); i++) {
00037 delete formationElements[i];
00038 }
00039 }
00040
00041 void FormationControl::setupFormationControl()
00042 {
00043
00044 std::vector<int> robotIDs = options.tRobotIDs->getValue();
00045 formationElements.resize(robotIDs.size());
00046
00047
00048 Eigen::Vector3d center(0,0,-0.5);
00049
00050
00051
00052 srand( time(NULL) );
00053 for (unsigned int i = 0; i < formationElements.size(); i++) {
00054 formationElements[i] = new FormationControlElement(robotIDs[i], options.tUseMKInterface->getValue());
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 double liftOffVariance = (rand() % 101) / 100.0;
00069 formationElements[i]->takeOff.getOptionContainer().getOption("tTakeOffDestination").set(Vector3D(0.0,0.0,-0.5-liftOffVariance));
00070 formationElements[i]->takeOff.getOptionContainer().getOption("tTakeOffVertically").set<bool>(true);
00071
00072 std::vector<int> neighborVector = robotIDs;
00073 neighborVector.erase(neighborVector.begin() + i);
00074 formationElements[i]->formation4.getOptionContainer().getOption("tNeighbors").set(neighborVector);
00075
00076 formationElements[i]->formation4.getOptionContainer().getOption("tJoystickTopic").set(options.tJoystickTopic->getValue());
00077 formationElements[i]->formation4.getOptionContainer().getOption("tVelocityInputTopic").set(options.tVelocityInputTopic->getValue());
00078 formationElements[i]->formation4.setParameterInitialized(true);
00079
00080
00081
00082 std::vector<double> tetraederDistanceVector(neighborVector.size(),2.5);
00083
00084 formationElements[i]->tetraeder.getOptionContainer().getOption("tNeighbors").set(neighborVector);
00085 formationElements[i]->tetraeder.getOptionContainer().getOption("tNeighborDistances").set(tetraederDistanceVector);
00086 formationElements[i]->tetraeder.setParameterInitialized(true);
00087
00088
00089
00090 std::vector<double> squareDistanceVector(neighborVector.size(),2.0);
00091 squareDistanceVector[(i+1)%3] = sqrt(2.0*2.0 + 2.0*2.0);
00092
00093 formationElements[i]->square.getOptionContainer().getOption("tNeighbors").set(neighborVector);
00094 formationElements[i]->square.getOptionContainer().getOption("tNeighborDistances").set(squareDistanceVector);
00095 formationElements[i]->square.setParameterInitialized(true);
00096
00097
00098
00099 formationElements[i]->tetraeder.setNextBehavior(formationElements[i]->formation4);
00100 formationElements[i]->square.setNextBehavior(formationElements[i]->formation4);
00101
00102
00103 if (i < 3) {
00104 std::vector<int> neighborVector3 = robotIDs;
00105 neighborVector3.erase(neighborVector3.begin() + i);
00106 neighborVector3.erase(neighborVector3.begin()+2, neighborVector3.end());
00107 std::vector<double> triangleDistanceVector(neighborVector3.size(),2.5);
00108
00109
00110 formationElements[i]->formation3.getOptionContainer().getOption("tNeighbors").set(neighborVector3);
00111
00112 formationElements[i]->formation3.getOptionContainer().getOption("tJoystickTopic").set(options.tJoystickTopic->getValue());
00113 formationElements[i]->formation3.getOptionContainer().getOption("tVelocityInputTopic").set(options.tVelocityInputTopic->getValue());
00114
00115 formationElements[i]->formation3.setParameterInitialized(true);
00116
00117
00118 formationElements[i]->triangle.getOptionContainer().getOption("tNeighbors").set(neighborVector3);
00119 formationElements[i]->triangle.getOptionContainer().getOption("tNeighborDistances").set(triangleDistanceVector);
00120 formationElements[i]->triangle.setNextBehavior(formationElements[i]->formation3);
00121
00122 formationElements[i]->triangle.setParameterInitialized(true);
00123
00124 }
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 joySub = mainNodeHandle.subscribe(options.tJoystickTopic->getValue()
00137 , 10, &FormationControl::joystickCB, this);
00138 }
00139
00140
00141
00142
00143 void FormationControl::joystickCB(const sensor_msgs::Joy::ConstPtr& msg)
00144 {
00145
00146 if (msg->buttons.size() < 9) {
00147 ROS_ERROR("Joytick does not publish enough buttons.");
00148 return;
00149 }
00150
00151
00152 if (msg->buttons[6]) {
00153 ROS_WARN("Emergency Button pressed! Landing all");
00154 for (unsigned int i = 0; i < formationElements.size(); i++) {
00155 formationElements[i]->mkSetEmergency();
00156 }
00157 }
00158
00159 if (msg->buttons[0]) {
00160
00161 for (unsigned int i = 0; i < formationElements.size(); i++) {
00162 formationElements[i]->mkToggleMotors();
00163 }
00164 }
00165
00166 if (msg->buttons[2]) {
00167 for (unsigned int i = 0; i < formationElements.size(); i++) {
00168 formationElements[i]->liftland();
00169 }
00170 }
00171
00172 if (msg->buttons[3]) {
00173 for (unsigned int i = 0; i < formationElements.size(); i++) {
00174
00175 formationElements[i]->bController->switchBehavior(formationElements[i]->tetraeder);
00176 }
00177 }
00178
00179 if (msg->buttons[4]) {
00180 for (unsigned int i = 0; i < formationElements.size(); i++) {
00181
00182 formationElements[i]->bController->switchBehavior(formationElements[i]->square);
00183 }
00184 }
00185 if (msg->buttons[5]) {
00186 for (unsigned int i = 0; i < formationElements.size()-1; i++) {
00187
00188 formationElements[i]->bController->switchBehavior(formationElements[i]->triangle);
00189 }
00190
00191 formationElements[formationElements.size()-1]->bController->switchBehavior(formationElements[formationElements.size()-1]->land);
00192 }
00193
00194
00195 if (msg->buttons[7]) {
00196
00197
00198
00199
00200
00201 formationElements[formationElements.size()-1]->bController->switchBehavior(formationElements[formationElements.size()-1]->takeOff);
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211 }
00212