Experiment_old.cpp
Go to the documentation of this file.
00001 /*
00002  * Experiment.cpp
00003  *
00004  *  Created on: Oct 26, 2011
00005  *      Author: mriedel
00006  */
00007 
00008 #include "Experiment.hpp"
00009 
00010 #include <telekyb_base/ROS.hpp>
00011 
00012 #include <boost/algorithm/string.hpp>
00013 
00014 namespace telekyb {
00015 
00016 Experiment::Experiment()
00017         : currState(State::Init), options(ExperimentOptions::InstancePtr()), setNextNeutralPosition(false)
00018 {
00019         nodeHandle = ROSModule::Instance().getMainNodeHandle();
00020         joySub = nodeHandle.subscribe(options->tJoystickTopic->getValue()
00021                         , 10, &Experiment::joystickCB, this);
00022         inputTransformSub = nodeHandle.subscribe(options->tTransformInTopic->getValue()
00023                         , 1, &Experiment::inputCB, this);
00024         inputTransformPub = nodeHandle.advertise<geometry_msgs::TransformStamped>(options->tTransformOutTopic->getValue(),1);
00025         udp.initSender(options->tClientHostname->getValue().c_str(), options->tClientPort->getValue());
00026         udp.initReceiver(options->tClientPort->getValue());
00027 
00028 
00029         neutralPosition = Position3D::Zero();
00030         maxInputOffset = options->tMaxInputOffset->getValue();
00031 
00032 }
00033 
00034 Experiment::~Experiment()
00035 {
00036 
00037 }
00038 
00039 void Experiment::readSpin()
00040 {
00041         // know that it's less then 255.
00042         char buffer[255];
00043         int recvBytes;
00044         while(ros::ok()) {
00045                 udp.doSelect();
00046                 if ((recvBytes = udp.recvRaw(buffer,sizeof(buffer))) > 0) {
00047                         std::string msg(buffer, recvBytes);
00048                         ROS_INFO_STREAM("Got Message: " << msg);
00049                         // parse #
00050                         std::vector<std::string> segments;
00051                         boost::split(segments, msg, boost::is_any_of("#"));
00052                         ROS_INFO_STREAM("segments.size(): " << segments.size());
00053 
00054 
00055                         std::string command = segments.at(1);
00056 
00057                         ROS_INFO_STREAM("Got command: " << command);
00058 
00059 
00060 
00061                         // State Machine
00062                         if (currState == State::Init && command == "readyV") {
00063                                 ROS_INFO("State: init->config");
00064                                 currState = State::Config;
00065                         } else if (currState == State::Ready && command == "target") {
00066                                 ROS_INFO("Got target!");
00067                                 //currState = State::Liftoff;
00068                         } else if (currState == State::Touchdown && command == "flyto") {
00069                                 ROS_INFO("Got Flyto!");
00070                                 currState = State::Flyto;
00071                         } else if (currState == State::User && command == "flyto") {
00072                                 ROS_INFO("Got Flyto!");
00073                                 currState = State::Flyto;
00074                         }
00075 
00076                 }
00077 
00078                 usleep(10);
00079         }
00080 }
00081 
00082 void Experiment::joystickCB(const sensor_msgs::Joy::ConstPtr& msg)
00083 {
00084         ROS_INFO("Received Joystick CB");
00085         ROS_INFO("Size: %d, Value: %d", msg->buttons.size(), msg->buttons.at(0));
00086         ROS_INFO("CurrentState: %s", currState.str());
00087 
00088         std::string command;
00089         if (msg->buttons.size() > 1 && msg->buttons.at(3)) {
00090                 // Button 1. -> Test emergency
00091                 // send error. sleep send recovered
00092                 ROS_INFO("Button 3 pressed");
00093                 ROS_INFO("Sending Error!");
00094                 command = "#error#Something went wrong!!#";
00095                 udp.doSelect();
00096                 udp.sendRaw(command.c_str(),command.size()+1);
00097                 // sleep 10
00098                 ROS_INFO("Sleeping 10!");
00099                 sleep(10);
00100                 command = "#recovered#";
00101                 udp.doSelect();
00102                 udp.sendRaw(command.c_str(),command.size()+1);
00103                 ROS_INFO("Recovered");
00104 
00105         } else if (msg->buttons.size() > 0 && msg->buttons.at(0)) {
00106                 // Button 0. -> Test cycle
00107                 ROS_INFO("Button 0 pressed");
00108                 // State Machine
00109                 if (currState == State::Init) {
00110                         command = "#startExp#";
00111                         udp.doSelect();
00112                         udp.sendRaw(command.c_str(),command.size()+1);
00113                         ROS_INFO("Send startExp");
00114                 } else if (currState == State::Config) {
00115                         command = "#readyT#";
00116                         udp.doSelect();
00117                         udp.sendRaw(command.c_str(),command.size()+1);
00118                         ROS_INFO("State: config->ready");
00119                         currState = State::Ready;
00120                 } else if (currState == State::Ready) {
00121                         command = "#liftoff#";
00122                         udp.doSelect();
00123                         udp.sendRaw(command.c_str(),command.size()+1);
00124                         ROS_INFO("State: ready->liftoff");
00125                         currState = State::Liftoff;
00126                 } else if (currState == State::Liftoff) {
00127                         ROS_INFO("State: liftoff->user");
00128                         currState = State::User;
00129                 } else if (currState == State::User) {
00130                         ROS_INFO("State: user->touchdown");
00131                         command = "#touchdown#";
00132                         udp.doSelect();
00133                         udp.sendRaw(command.c_str(),command.size()+1);
00134                         currState = State::Touchdown;
00135                 } else if (currState == State::Touchdown) {
00136                         ROS_INFO("State: touchdown->ready");
00137                         currState = State::Ready;
00138                 } else if (currState == State::Flyto) {
00139                         command = "#readyT#";
00140                         udp.doSelect();
00141                         udp.sendRaw(command.c_str(),command.size()+1);
00142                         ROS_INFO("State: flyto->ready");
00143                         currState = State::Ready;
00144                 }
00145 
00146         } else if (msg->buttons.size() > 0 && msg->buttons.at(1)) {
00147                 ROS_INFO("Button 1 pressed");
00148                 // reset once to current position
00149                 setNextNeutralPosition = true;
00150 
00151         } else {
00152                 // do nothing
00153         }
00154 }
00155 
00156 void Experiment::inputCB(const geometry_msgs::TransformStamped::Ptr& msg)
00157 {
00158         if (currState == State::Init) {
00159                 // to not send anything
00160                 return;
00161         }
00162 
00163         // get input. -> transform resend
00164         Position3D transform;
00165         transform << msg->transform.translation.x, msg->transform.translation.y, msg->transform.translation.z;
00166 
00167         if (setNextNeutralPosition) {
00168                 neutralPosition = transform;
00169                 setNextNeutralPosition = false;
00170         }
00171 
00172         if (neutralPosition == Position3D::Zero()) {
00173                 return;
00174         }
00175 
00176         //transform
00177         transform -= neutralPosition;
00178         transform(0) = -transform(0);
00179         transform(1) = -transform(1);
00180 
00181         //scale
00182         transform = transform / maxInputOffset;
00183 
00184         // 1.0 is max
00185 
00186         msg->transform.translation.x = std::min(transform(0),1.0);
00187         msg->transform.translation.y = std::min(transform(1),1.0);
00188         msg->transform.translation.z = std::min(transform(2),1.0);
00189 
00190         msg->transform.translation.x = std::max(transform(0),-1.0);
00191         msg->transform.translation.y = std::max(transform(1),-1.0);
00192         msg->transform.translation.z = std::max(transform(2),-1.0);
00193 
00194 
00195         // send
00196         inputTransformPub.publish(msg);
00197 }
00198 
00199 
00200 
00201 // State function
00202 void Experiment::init()
00203 {
00204 //      ROS_INFO("currState: %s, sizeof: %d, strlen+1: %d",
00205 //                      currState.str(), sizeof(currState.str()), strlen(currState.str())+1);
00206 
00207         // send startExp
00208         //SendCmds::startExp.optional_value();
00209 //      SendCmds cmd = SendCmds::startExp;
00210 //      std::string msg = cmd.value();
00211         //std::cout << "StartExpString: " <<  msg << std::endl;
00212 
00213         //SendCmds::optional_value
00214 
00215 
00216         // wait for readyV
00217 }
00218 
00219 void Experiment::ready()
00220 {
00221 
00222 }
00223 void Experiment::liftoff()
00224 {
00225 
00226 }
00227 void Experiment::user()
00228 {
00229 
00230 }
00231 void Experiment::touchdown()
00232 {
00233 
00234 }
00235 void Experiment::flyto()
00236 {
00237         // send liftoff
00238 
00239         sleep(10);
00240         // send touchdown
00241 
00242 }
00243 
00244 void Experiment::error()
00245 {
00246 
00247 }
00248 
00249 
00250 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


tk_vrqc2011
Author(s): Martin Riedel
autogenerated on Wed Apr 24 2013 11:26:17