00001
00002
00003
00004
00005
00006
00007
00008 #include "TrajPlayback.hpp"
00009
00010
00011 #include <boost/algorithm/string.hpp>
00012 #include <boost/lexical_cast.hpp>
00013
00014
00015
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
00034 }
00035
00036 void TrajPlayback::destroy()
00037 {
00038
00039 }
00040
00041 bool TrajPlayback::willBecomeActive(const TKState& currentState, const Behavior& previousBehavior)
00042 {
00043
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
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){
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
00084 }
00085
00086 void TrajPlayback::didBecomeInActive(const TKState& currentState, const Behavior& nextBehavior)
00087 {
00088
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
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 if (tokenList.size() == 12) {
00113
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
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
00208 void TrajPlayback::trajectoryStepCreation(const TKState& currentState, TKTrajectory& generatedTrajInput)
00209 {
00210 generatedTrajInput.setVelocity( Velocity3D::Zero() );
00211 generatedTrajInput.setYawRate( 0.0 );
00212 }
00213
00214
00215 void TrajPlayback::trajectoryStepActive(const TKState& currentState, TKTrajectory& generatedTrajInput)
00216 {
00217
00218 if (timer.getElapsed().toDSec() > nextTimeStep) {
00219 setNextTrajInput();
00220 }
00221
00222
00223 generatedTrajInput = nextTrajInput;
00224 }
00225
00226
00227 void TrajPlayback::trajectoryStepTermination(const TKState& currentState, TKTrajectory& generatedTrajInput)
00228 {
00229
00230 trajectoryStepActive(currentState,generatedTrajInput);
00231 }
00232
00233
00234 bool TrajPlayback::isValid(const TKState& currentState) const
00235 {
00236
00237 return file.good();
00238 }
00239
00240 }
00241