FormationControl.cpp
Go to the documentation of this file.
00001 /*
00002  * FormationControl.cpp
00003  *
00004  *  Created on: Nov 15, 2011
00005  *      Author: mriedel
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 // Options
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         // create elements
00044         std::vector<int> robotIDs = options.tRobotIDs->getValue();
00045         formationElements.resize(robotIDs.size());
00046 
00047 
00048         Eigen::Vector3d center(0,0,-0.5);
00049 //      double halfEdgeLength = 1.0;
00050 
00051         // init random
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                 // setup neighbor an distances
00057                 //telekyb_interface::Behavior formationBehavior = formationElements[i]->formation;
00058 
00059 //              Eigen::Vector3d qcPos;
00060 //              for (int j = 0; j < 3; ++j) {
00061 //                      int toggle = i & (1 << j);
00062 //                      toggle = toggle ? 1 : -1;
00063 //                      qcPos(j) = center(j) + toggle*halfEdgeLength;
00064 //              }
00065 //              ROS_WARN_STREAM("Takeoff Position: " << std::endl << qcPos);
00066 //
00067 //
00068                 double liftOffVariance = (rand() % 101) / 100.0; // between 0 and 1
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); // remove oneself
00074                 formationElements[i]->formation4.getOptionContainer().getOption("tNeighbors").set(neighborVector);
00075                 // tJoystickTopic
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                 // Tetraeder
00082                 std::vector<double> tetraederDistanceVector(neighborVector.size(),2.5);
00083                 // Also configure ReformationBehavior
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                 // Square
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                 // automatic change
00099                 formationElements[i]->tetraeder.setNextBehavior(formationElements[i]->formation4);
00100                 formationElements[i]->square.setNextBehavior(formationElements[i]->formation4);
00101 
00102                 // Subset 3 Triangle
00103                 if (i < 3) {
00104                         std::vector<int> neighborVector3 = robotIDs;
00105                         neighborVector3.erase(neighborVector3.begin() + i); // remove oneself
00106                         neighborVector3.erase(neighborVector3.begin()+2, neighborVector3.end()); // only keep the first two!
00107                         std::vector<double> triangleDistanceVector(neighborVector3.size(),2.5);
00108 
00109                         // 3 formationControl
00110                         formationElements[i]->formation3.getOptionContainer().getOption("tNeighbors").set(neighborVector3);
00111                         // tJoystickTopic
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                         // Also configure ReformationBehavior
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 //      for (unsigned int i = 0; i < formationElements.size()-1; i++) {
00128 //              std::vector<int> neighborVector = robotIDs;
00129 //              neighborVector.erase(neighborVector.size()-1);
00130 //              std::vector<double> neighborDistanceVector(formationElements.size()-1,2.0);
00131 //
00132 //      }
00133 
00134 
00135         // lastly start Controller
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         // use button 2
00146         if (msg->buttons.size() < 9) {
00147                 ROS_ERROR("Joytick does not publish enough buttons.");
00148                 return;
00149         }
00150 
00151         // Emergency
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                 // this only works on ground.
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                         //formationElements[i]->switchIntoFormation();
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                         //formationElements[i]->switchIntoFormation();
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                         //formationElements[i]->switchIntoFormation();
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         // 6 is taken by emerygency
00195         if (msg->buttons[7]) {
00196 //              for (unsigned int i = 0; i < formationElements.size()-1; i++) {
00197 //                      //formationElements[i]->switchIntoFormation();
00198 //                      formationElements[i]->bController->switchBehavior(formationElements[i]->triangle);
00199 //              }
00200 
00201                 formationElements[formationElements.size()-1]->bController->switchBehavior(formationElements[formationElements.size()-1]->takeOff);
00202         }
00203 
00204 //      if (msg->buttons[3]) {
00205 //              bController->switchBehavior(calibrator);
00206 //      }
00207 
00208 
00209         // what to do
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