TrajPlayback.cpp
Go to the documentation of this file.
00001 /*
00002  * TrajPlayback.cpp
00003  *
00004  *  Created on: Nov 12, 2011
00005  *      Author: mriedel
00006  */
00007 
00008 #include "TrajPlayback.hpp"
00009 
00010 // boost
00011 #include <boost/algorithm/string.hpp>
00012 #include <boost/lexical_cast.hpp>
00013 
00014 
00015 // Declare
00016 PLUGINLIB_DECLARE_CLASS(tk_be_common, TrajPlayback, telekyb_behavior::TrajPlayback, TELEKYB_NAMESPACE::Behavior);
00017 
00018 namespace telekyb_behavior {
00019 
00020 TrajPlayback::TrajPlayback()
00021         : Behavior("tk_be_common/TrajPlayback", BehaviorType::Air)
00022 {
00023 
00024 }
00025 
00026 void TrajPlayback::initialize()
00027 {
00028         tTrajectoryFilename = addOption<std::string>("tTrajectoryFilename",
00029                         "File with Trajectory Information",
00030                         "Traj.txt",
00031                         false,false);
00032 
00033         //parameterInitialized = true;
00034 }
00035 
00036 void TrajPlayback::destroy()
00037 {
00038 
00039 }
00040 
00041 bool TrajPlayback::willBecomeActive(const TKState& currentState, const Behavior& previousBehavior)
00042 {
00043         // TODO: Cannot activate TrajPlayback Behavior while Flying.
00044 
00045         ROS_INFO_STREAM("Playing back file with name " << tTrajectoryFilename->getValue());
00046 
00047         file.open(tTrajectoryFilename->getValue().c_str());
00048 
00049         if (!file.is_open()) {
00050                 ROS_ERROR_STREAM("Unable to open Trajectory File: " << tTrajectoryFilename->getValue());
00051                 return false;
00052         }
00053 
00054         // ok it's open
00055         if (!setNextTrajInput()) {
00056                 file.close();
00057                 return false;
00058         }
00059 
00060         double positionThreshold = 0.5;
00061         double velocityThreshold = .01;
00062         double positionError = (currentState.position - nextTrajInput.position).norm();
00063 
00064         double velocityError = (currentState.linVelocity - nextTrajInput.velocity).norm();
00065         if (positionError > positionThreshold){// || velocityError  > velocityThreshold) {
00066                 ROS_ERROR_STREAM("Current Position: " << currentState.position.transpose() << " First Traj: " << nextTrajInput.position.transpose());
00067                 ROS_ERROR("Conditions not met: position error %f; velocity error: %f", positionError, velocityError);
00068                 file.close();
00069                 return false;
00070         }
00071 
00072         return true;
00073 }
00074 
00075 void TrajPlayback::didBecomeActive(const TKState& currentState, const Behavior& previousBehavior)
00076 {
00077         timer.reset();
00078 
00079 }
00080 
00081 void TrajPlayback::willBecomeInActive(const TKState& currentState, const Behavior& nextBehavior)
00082 {
00083         // not used
00084 }
00085 
00086 void TrajPlayback::didBecomeInActive(const TKState& currentState, const Behavior& nextBehavior)
00087 {
00088         // hover does not need this
00089         ROS_INFO("TrajPlayback became inactive");
00090         file.close();
00091 }
00092 
00093 bool TrajPlayback::setNextTrajInput()
00094 {
00095         std::string line;
00096         getline(file,line);
00097 
00098         std::vector<std::string> tokenList;
00099         boost::split(tokenList, line, boost::is_any_of(" \t"), boost::token_compress_on);
00100 
00101 /*      for (int i = 0; i < tokenList.size(); ++i) {
00102                 double temp = 0;
00103                 try {
00104                         temp = boost::lexical_cast<double>(tokenList[i]);
00105                 } catch (boost::bad_lexical_cast& e) {
00106                         ROS_ERROR("Casting error: %s", e.what());
00107                 }
00108 
00109                 ROS_INFO("Token Number: %d Value: %f", i , temp);
00110         }*/
00111 
00112         if (tokenList.size() == 12) {
00113                 //ROS_INFO("Found 12 tokens per line, will use old trajectory file definition without jerk and snap.");
00114                 try {
00115                         nextTimeStep = boost::lexical_cast<double>(tokenList[0]);
00116         
00117                         Position3D position(
00118                                         boost::lexical_cast<double>(tokenList[1]),
00119                                         boost::lexical_cast<double>(tokenList[2]),
00120                                         boost::lexical_cast<double>(tokenList[3])
00121                         );
00122 
00123                         Velocity3D velocity(
00124                                         boost::lexical_cast<double>(tokenList[4]),
00125                                         boost::lexical_cast<double>(tokenList[5]),
00126                                         boost::lexical_cast<double>(tokenList[6])
00127                         );
00128         
00129                         Acceleration3D acceleration(
00130                                         boost::lexical_cast<double>(tokenList[7]),
00131                                         boost::lexical_cast<double>(tokenList[8]),
00132                                         boost::lexical_cast<double>(tokenList[9])
00133                         );
00134 
00135                         Eigen::Vector3d jerk(0,0,0);
00136 
00137                         Eigen::Vector3d snap(0,0,0);
00138 
00139                         nextTrajInput.setPosition(position, velocity, acceleration, jerk, snap);
00140                         nextTrajInput.setYawAngle(
00141                                         boost::lexical_cast<double>(tokenList[10]),
00142                                         boost::lexical_cast<double>(tokenList[11]),
00143                                         0
00144                                         );
00145 
00146                 } catch (boost::bad_lexical_cast& e) {
00147                         ROS_ERROR("Casting error: %s", e.what());
00148                         return false;
00149                 }
00150                 return true;
00151         }
00152 
00153 
00154         if (tokenList.size() < 19) {
00155                 ROS_ERROR("Line has less than 19 Tokens!");
00156                 return false;
00157         }
00158         try {
00159                 //ROS_INFO("Found 19 or more tokens per line, will use new trajectory file definition including jerk and snap.");
00160                 nextTimeStep = boost::lexical_cast<double>(tokenList[0]);
00161 
00162                 Position3D position(
00163                                 boost::lexical_cast<double>(tokenList[1]),
00164                                 boost::lexical_cast<double>(tokenList[2]),
00165                                 boost::lexical_cast<double>(tokenList[3])
00166                 );
00167 
00168                 Velocity3D velocity(
00169                                 boost::lexical_cast<double>(tokenList[4]),
00170                                 boost::lexical_cast<double>(tokenList[5]),
00171                                 boost::lexical_cast<double>(tokenList[6])
00172                 );
00173 
00174                 Acceleration3D acceleration(
00175                                 boost::lexical_cast<double>(tokenList[7]),
00176                                 boost::lexical_cast<double>(tokenList[8]),
00177                                 boost::lexical_cast<double>(tokenList[9])
00178                 );
00179 
00180                 Eigen::Vector3d jerk(
00181                                 boost::lexical_cast<double>(tokenList[10]),
00182                                 boost::lexical_cast<double>(tokenList[11]),
00183                                 boost::lexical_cast<double>(tokenList[12])
00184                 );
00185 
00186                 Eigen::Vector3d snap(
00187                                 boost::lexical_cast<double>(tokenList[13]),
00188                                 boost::lexical_cast<double>(tokenList[14]),
00189                                 boost::lexical_cast<double>(tokenList[15])
00190                 );
00191 
00192                 nextTrajInput.setPosition(position, velocity, acceleration, jerk, snap);
00193                 nextTrajInput.setYawAngle(
00194                                 boost::lexical_cast<double>(tokenList[16]),
00195                                 boost::lexical_cast<double>(tokenList[17]),
00196                                 boost::lexical_cast<double>(tokenList[18])
00197                                 );
00198 
00199         } catch (boost::bad_lexical_cast& e) {
00200                 ROS_ERROR("Casting error: %s", e.what());
00201                 return false;
00202         }
00203 
00204         return true;
00205 }
00206 
00207 // called everytime a new TKState is available AND it is the NEW Behavior of an active Switch
00208 void TrajPlayback::trajectoryStepCreation(const TKState& currentState, TKTrajectory& generatedTrajInput)
00209 {
00210         generatedTrajInput.setVelocity( Velocity3D::Zero() );
00211         generatedTrajInput.setYawRate( 0.0 );
00212 }
00213 
00214 // called everytime a new TKState is available. Should return false if invalid (swtich to next behavior, or Hover if undef).
00215 void TrajPlayback::trajectoryStepActive(const TKState& currentState, TKTrajectory& generatedTrajInput)
00216 {
00217         //ROS_INFO("TrajStep Called!");
00218         if (timer.getElapsed().toDSec() > nextTimeStep) {
00219                 setNextTrajInput();
00220         }
00221 
00222 
00223         generatedTrajInput = nextTrajInput;
00224 }
00225 
00226 // called everytime a new TKState is available AND it is the OLD Behavior of an active Switch
00227 void TrajPlayback::trajectoryStepTermination(const TKState& currentState, TKTrajectory& generatedTrajInput)
00228 {
00229         // Same as active
00230         trajectoryStepActive(currentState,generatedTrajInput);
00231 }
00232 
00233 // Return true if the active Behavior is (still) valid. Initiate Switch otherwise
00234 bool TrajPlayback::isValid(const TKState& currentState) const
00235 {
00236         // never turns invalid
00237         return file.good();
00238 }
00239 
00240 }
00241 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


tk_be_common
Author(s): Martin Riedel
autogenerated on Mon Nov 11 2013 11:14:29