Experiment.cpp
Go to the documentation of this file.
00001 /*
00002  * Experiment.cpp
00003  *
00004  *  Created on: Nov 15, 2011
00005  *      Author: mriedel
00006  */
00007 
00008 #include "Experiment.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 ExperimentOptions::ExperimentOptions()
00017         : OptionContainer("ExperimentOptions")
00018 {
00019         robotID = addOption<int>("robotID", "Specify the robotID of the TeleKybCore to connect to.", 0, false, true);
00020         tJoystickTopic = addOption<std::string>("tJoystickTopic",
00021                         "Joysticktopic to use (sensor_msgs::Joy)", "/TeleKyb/tJoy/joy", false, true);
00022         tUseMKInterface = addOption<bool>("tUseMKInterface", "Set to true with MKInterface!", false, false, true);
00023 
00024         tTrajectoryFileName = addOption<std::string>("tTrajectoryFileName",
00025                         "Full path of the trajectory file", "", true, true);
00026 
00027         tTrajectoryStartPoint = addOption<Eigen::Vector3d>("tTrajectoryStartPoint",
00028                         "Starting position of the trajectory", Eigen::Vector3d(0.0,0.0,-1.0), true, true);
00029 }
00030 
00031 Experiment::Experiment()
00032         : mainNodeHandle( ROSModule::Instance().getMainNodeHandle() ), core(NULL), mkInterface(NULL)
00033 {
00034         core = telekyb_interface::TeleKybCore::getTeleKybCore(options.robotID->getValue());
00035         if (!core) {
00036                 // fail
00037                 ros::shutdown();
00038                 return;
00039         }
00040 
00041         //options.tUseMKInterface->setValue(true);
00042         if (options.tUseMKInterface->getValue()) {
00043                 ROS_INFO("Creating MKInterface!");
00044                 // use MKInterface
00045                 mkInterface = telekyb_interface::MKInterface::getMKInterface(options.robotID->getValue());
00046                 //mkInterface = telekyb_interface::MKInterface::getMKInterface(1); // BEWARE TEMPORARY!!!
00047                 if (!mkInterface) {
00048                         // fail
00049                         ros::shutdown();
00050                         return;
00051                 }
00052         }
00053 
00054         bController = core->getBehaviorController();
00055         oController = core->getOptionController();
00056 
00057         //activeBehavior = bController->getActiveBehaviorReference();
00058         bController->setActiveBehaviorListener(this);
00059 
00060         activeBehaviorPtr = bController->getActiveBehaviorPointer();
00061 
00062         setupExperiment();
00063 }
00064 
00065 Experiment::~Experiment()
00066 {
00067         if (mkInterface) { delete mkInterface; }
00068 
00069         delete core;
00070 }
00071 
00072 void Experiment::setupExperiment()
00073 {
00074         // load Behaviors
00075         ground = bController->getSystemBehavior("tk_behavior/Ground");
00076         hover = bController->getSystemBehavior("tk_behavior/Hover");
00077         normalBreak = bController->getSystemBehavior("tk_behavior/NormalBrake");
00078         takeOff = bController->getSystemBehavior("tk_behavior/TakeOff");
00079         land = bController->getSystemBehavior("tk_behavior/Land");
00080 
00081 
00082         // sanity check
00083         if (ground.isNull() || hover.isNull() || normalBreak.isNull() || takeOff.isNull() || land.isNull()) {
00084                 ROS_FATAL("Unable to get SystemBehavior!!!");
00085                 //ROS_BREAK();
00086                 ros::shutdown();
00087         }
00088 
00089 
00090         // setup takeoff
00091         takeOff.getOptionContainer().getOption("tTakeOffVelocity").set<double>(0.3);
00092         takeOff.getOptionContainer().getOption("tTakeOffDestination").set(Position3D(0.0,0.0,-1.0));
00093         takeOff.getOptionContainer().getOption("tTakeOffVertically").set(true);
00094 
00095         // done
00096         takeOff.setParameterInitialized(true);
00097 
00098 
00099         land.getOptionContainer().getOption("tLandVelocity").set<double>(0.4);
00100         land.getOptionContainer().getOption("tLandDestination").set(Position3D(0.0,0.0,0.0));
00101         land.getOptionContainer().getOption("tLandDestinationHeight").set<double>(-0.3);
00102         land.getOptionContainer().getOption("tLandVertically").set(true);
00103 
00104 
00105         // done
00106         land.setParameterInitialized(true);
00107 
00108 
00109         //joystick = bController->loadBehavior("tk_be_common/Joystick");
00110         trajPlayback = bController->loadBehavior("tk_be_common/TrajPlayback");
00111 
00112 /*      if ( joystick.isNull() ) {
00113                 ROS_FATAL("Unable to load Joystick!!!");
00114                 //ROS_BREAK();
00115                 ros::shutdown();
00116         }
00117 
00118         // setup joystick
00119         joystick.getOptionContainer().getOption("tJoystickTopic").set(options.tJoystickTopic->getValue());
00120         joystick.setParameterInitialized(true);
00121 */
00122         if ( trajPlayback.isNull() ) {
00123                 ROS_FATAL("Unable to load Trajectory Playback!!!");
00124                 //ROS_BREAK();
00125                 ros::shutdown();
00126         }
00127 
00128         trajPlayback.getOptionContainer().getOption("tTrajectoryFilename").set(options.tTrajectoryFileName->getValue());
00129         trajPlayback.setParameterInitialized(true);
00130 
00131         flyto1 = bController->loadBehavior("tk_be_common/LinearFlyTo");
00132         //flyto2 = bController->loadBehavior("tk_be_common/LinearFlyTo");
00133         //flyto3 = bController->loadBehavior("tk_be_common/LinearFlyTo");
00134 
00135         flyto1.getOptionContainer().getOption("tFlyToDestination").set(options.tTrajectoryStartPoint->getValue());
00136         //flyto2.getOptionContainer().getOption("tFlyToDestination").set(Position3D(1.0,-1.0,-1.0));
00137         //flyto3.getOptionContainer().getOption("tFlyToDestination").set(Position3D(-1.0,0.0,-1.5));
00138 
00139         flyto1.setParameterInitialized(true);
00140         //flyto2.setParameterInitialized(true);
00141         //flyto3.setParameterInitialized(true);
00142 
00143         //flyto1.setNextBehavior(flyto2);
00144         //flyto2.setNextBehavior(flyto3);
00145         //flyto3.setNextBehavior(flyto1);
00146 
00147         if (*activeBehaviorPtr != ground) {
00148                 ROS_ERROR("UAV not in Ground Behavior during Startup");
00149                 ros::shutdown();
00150         }
00151 
00152         // lastly start Controller
00153         joySub = mainNodeHandle.subscribe(options.tJoystickTopic->getValue()
00154                         , 10, &Experiment::joystickCB, this);
00155 }
00156 
00157 
00158 
00159 
00160 void Experiment::joystickCB(const sensor_msgs::Joy::ConstPtr& msg)
00161 {
00162         // use button 2
00163         if (msg->buttons.size() < 9) {
00164                 ROS_ERROR("Jostick does not publish enough buttons.");
00165                 return;
00166         }
00167 
00168         // Emergency
00169         if (msg->buttons[6]) {
00170                 ROS_WARN("Emergency Button pressed!");
00171                 //mkInterface->setEmergency();
00172         }
00173 
00174         /*// Button 1! toggle Motors for mkInterface Only in Ground
00175         if (mkInterface && *activeBehaviorPtr == ground && msg->buttons[0]) {
00176                 ROS_INFO("Toggle Motorstate!");
00177 
00178                 MKSingleValuePacket motorState(MKDataDefines::MOTOR_STATE,0);
00179                 if (!mkInterface->updateMKValue(motorState)) {
00180                         ROS_ERROR("Could not get Motorstate!");
00181                         return;
00182                 }
00183 
00184                 if (motorState.value == MotorState::On) {
00185                         // stop
00186                         motorState.value = MotorState::Off;
00187                         mkInterface->setMKValue(motorState);
00188                 } else if (motorState.value == MotorState::Off) {
00189                         // start
00190                         motorState.value = MotorState::Init;
00191                         mkInterface->setMKValue(motorState);
00192                 } else if (motorState.value == MotorState::Init) {
00193                         motorState.value = MotorState::On;
00194                         mkInterface->setMKValue(motorState);
00195                 }
00196 
00197 
00198         }*/
00199 
00200         if (msg->buttons[2]) {
00201                 if (*activeBehaviorPtr == ground) {
00202 
00203                         /*if (mkInterface) {
00204                                 MKSingleValuePacket motorState(MKDataDefines::MOTOR_STATE,0);
00205                                 if (!mkInterface->updateMKValue(motorState)) {
00206                                         ROS_ERROR("Could not get Motorstate for liftoff!");
00207                                         return;
00208                                 }
00209                                 if (motorState.value == MotorState::On) {
00210                                         bController->switchBehavior(takeOff);
00211                                 } else {
00212                                         ROS_ERROR("Motors have to be on for liftOff!");
00213                                         return;
00214                                 }
00215                         }*/
00216 
00217                         // takeoff
00218                         bController->switchBehavior(takeOff);
00219                 } else {
00220                         // flying -> land
00221                         bController->switchBehavior(land);
00222                 }
00223         }
00224 
00225         /*if (msg->buttons[3]) {
00226                 bController->switchBehavior(joystick);
00227         }
00228 */
00229         if (msg->buttons[4]) {
00230                 bController->switchBehavior(trajPlayback);
00231         }
00232 
00233         // right back
00234         if (msg->buttons[3]) {
00235                 bController->switchBehavior(flyto1);
00236         }
00237 
00238         // what to do
00239 }
00240 
00241 void Experiment::activeBehaviorChanged(telekyb_interface::Behavior newActiveBehavior)
00242 {
00243 // Automatically turn off Motors
00244 //      if (mkInterface && newActiveBehavior == ground) {
00245 //              MKSingleValuePacket motorState(MKDataDefines::MOTOR_STATE,0);
00246 //              motorState.value = MotorState::Off;
00247 //              mkInterface->setMKValue(motorState);
00248 //      }
00249 
00250         if(newActiveBehavior == normalBreak) {
00251                 ROS_INFO("Going to normalbrake");
00252         }
00253 
00254 }
00255 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


tk_exp_smurf
Author(s): Riccardo Spica
autogenerated on Thu Apr 25 2013 11:16:16