32 #ifndef CTRAJECTORYGENERATOR_HPP_
33 #define CTRAJECTORYGENERATOR_HPP_
35 #include <scl/DataTypes.hpp>
36 #include <scl/util/FileFunctions.hpp>
38 #include <sutil/CMappedList.hpp>
50 template <sUInt task_dof_>
60 const sFloat arg_cycle_time,
62 const Eigen::Matrix<sFloat,task_dof_,1>& arg_max_pos,
64 const Eigen::Matrix<sFloat,task_dof_,1>& arg_max_vel,
66 const Eigen::Matrix<sFloat,task_dof_,1>& arg_max_acc,
68 const Eigen::Matrix<sFloat,task_dof_,1>& arg_min_pos,
70 const Eigen::Matrix<sFloat,task_dof_,1>& arg_min_vel,
72 const Eigen::Matrix<sFloat,task_dof_,1>& arg_min_acc);
85 const std::string& arg_file,
89 const sBool arg_has_time=
true);
100 const sFloat arg_time_per_slice,
102 const Eigen::Matrix<sFloat,task_dof_,1>& arg_pos_start,
105 const Eigen::Matrix<sFloat,task_dof_,1>& arg_scale,
108 const Eigen::Matrix<sFloat,task_dof_,1>& arg_stagger);
116 Eigen::Matrix<sFloat,task_dof_,1>& arg_vel_curr,
117 Eigen::Matrix<sFloat,task_dof_,1>& arg_acc_curr);
123 const Eigen::Matrix<sFloat,task_dof_,1>& arg_state,
159 Eigen::Matrix<sFloat,task_dof_,1> max_pos_, max_vel_, max_acc_,
160 min_pos_, min_vel_, min_acc_;
164 sBool traj_has_been_init_;
168 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
175 template <sUInt task_dof_>
178 { traj_has_been_init_ =
false; }
180 template <sUInt task_dof_>
181 CTrajectoryGenerator<task_dof_>::~CTrajectoryGenerator()
184 template <sUInt task_dof_>
187 const sFloat arg_cycle_time,
189 const Eigen::Matrix<sFloat,task_dof_,1>& arg_max_pos,
191 const Eigen::Matrix<sFloat,task_dof_,1>& arg_max_vel,
193 const Eigen::Matrix<sFloat,task_dof_,1>& arg_max_acc,
195 const Eigen::Matrix<sFloat,task_dof_,1>& arg_min_pos,
197 const Eigen::Matrix<sFloat,task_dof_,1>& arg_min_vel,
199 const Eigen::Matrix<sFloat,task_dof_,1>& arg_min_acc)
204 if(0 >= arg_cycle_time)
205 {
throw(std::runtime_error(
"Can't generate a trajectory for negative or zero servo cycle time.")); }
207 {
throw(std::runtime_error(
"Can't generate a trajectory for zero task dofs.")); }
208 if(arg_max_pos.size() !=
static_cast<sInt>(task_dof_))
209 {
throw(std::runtime_error(
"Max position vector doesn't match task dimensions.")); }
210 if(arg_max_vel.size() !=
static_cast<sInt>(task_dof_))
211 {
throw(std::runtime_error(
"Max velocity vector doesn't match task dimensions.")); }
212 if(arg_max_acc.size() !=
static_cast<sInt>(task_dof_))
213 {
throw(std::runtime_error(
"Max acceleration vector doesn't match task dimensions.")); }
214 if(arg_min_pos.size() !=
static_cast<sInt>(task_dof_))
215 {
throw(std::runtime_error(
"Min position vector doesn't match task dimensions.")); }
216 if(arg_min_vel.size() !=
static_cast<sInt>(task_dof_))
217 {
throw(std::runtime_error(
"Min velocity vector doesn't match task dimensions.")); }
218 if(arg_min_acc.size() !=
static_cast<sInt>(task_dof_))
219 {
throw(std::runtime_error(
"Min acceleration vector doesn't match task dimensions.")); }
221 otg_.SetCycleTimeInSeconds(arg_cycle_time);
223 max_pos_ = arg_max_pos;
224 max_vel_ = arg_max_vel;
225 max_acc_ = arg_max_acc;
226 min_pos_ = arg_min_pos;
227 min_vel_ = arg_min_vel;
228 min_acc_ = arg_min_acc;
230 traj_has_been_init_ =
true;
232 catch(std::exception& e)
233 { std::cout<<
"\nCTrajectoryGenerator::initTraj() Error : "<<e.what(); }
235 return traj_has_been_init_;
238 template <sUInt task_dof_>
241 const std::string& arg_file,
245 const sBool arg_has_time)
250 Eigen::MatrixXd traj;
254 traj.resize(arg_traj_slices,task_dof_+1);
256 arg_traj_slices,task_dof_+1,arg_file);
258 {
throw(std::runtime_error(
"Could not read trajectory and time from the file.")); }
261 for(sUInt i = 0; i< arg_traj_slices; ++i)
263 Eigen::Matrix<sFloat,task_dof_,1> t = traj.row(i);
264 throw(std::runtime_error(
"Traj row needs to go from 1 to end"));
265 Eigen::Matrix<sFloat,task_dof_,1> * slice = traj_desired_.create(traj(i,0), t);
268 traj_desired_.clear();
269 std::stringstream ss;
271 std::string s(
"Could not allocate trajectory's slice on the pilemap. At slice : ");
273 throw(std::runtime_error(s.c_str()));
279 traj.resize(arg_traj_slices,task_dof_);
281 arg_traj_slices,task_dof_,arg_file);
283 {
throw(std::runtime_error(
"Could not read trajectory from the file.")); }
286 for(sUInt i = 0; i< arg_traj_slices; ++i)
288 Eigen::Matrix<sFloat,task_dof_,1> * slice = traj_desired_.create(i, traj.row(i));
291 traj_desired_.clear();
292 throw(std::runtime_error(
"Could not allocate trajectory's slice on the pilemap."));
299 catch(std::exception& e)
301 std::cout<<
"\nCTrajectoryGenerator::readTrajFromTxtFile() Error : "<<e.what();
306 template <sUInt task_dof_>
311 const sFloat arg_time_per_slice,
313 const Eigen::Matrix<sFloat,task_dof_,1>& arg_pos_start,
316 const Eigen::Matrix<sFloat,task_dof_,1>& arg_scale,
319 const Eigen::Matrix<sFloat,task_dof_,1>& arg_stagger)
323 if(arg_pos_start.size() !=
static_cast<sInt>(task_dof_))
324 {
throw(std::runtime_error(
"Start position vector doesn't match task dimensions.")); }
325 if(arg_scale.size() !=
static_cast<sInt>(task_dof_))
326 {
throw(std::runtime_error(
"Scale vector doesn't match task dimensions.")); }
327 if(arg_stagger.size() !=
static_cast<sInt>(task_dof_))
328 {
throw(std::runtime_error(
"Stagger vector doesn't match task dimensions.")); }
332 Eigen::Matrix<sFloat,task_dof_,1> slice;
333 slice = arg_pos_start;
335 for(sUInt i=0; i<arg_traj_slices;++i)
338 sFloat x = arg_func(time);
339 slice = arg_pos_start + arg_scale*x + arg_stagger;
342 Eigen::Matrix<sFloat,task_dof_,1> * vec = S_NULL;
343 vec = traj_desired_.create(time,slice);
345 {
throw(std::runtime_error(
"Could not create trajectory slice on the pile.")); }
347 time+=arg_time_per_slice;
351 catch(std::exception& e)
353 std::cout<<
"\nCTrajectoryGenerator::setTrajFromFunc() Error : "<<e.what();
359 template <sUInt task_dof_>
361 Eigen::Matrix<sFloat,task_dof_,1>& arg_pos_curr,
362 Eigen::Matrix<sFloat,task_dof_,1>& arg_vel_curr,
363 Eigen::Matrix<sFloat,task_dof_,1>& arg_acc_curr)
371 template <sUInt task_dof_>
374 const Eigen::Matrix<sFloat,task_dof_,1>& arg_state,
378 Eigen::Matrix<sFloat,task_dof_,1> * vec = S_NULL;
380 vec = traj_achieved_.create(arg_time,arg_state);
sLongLong traj_curr_idx_
Definition: CTrajectoryGenerator.hpp:157
virtual sBool initTraj(const sFloat arg_cycle_time, const Eigen::Matrix< sFloat, task_dof_, 1 > &arg_max_pos, const Eigen::Matrix< sFloat, task_dof_, 1 > &arg_max_vel, const Eigen::Matrix< sFloat, task_dof_, 1 > &arg_max_acc, const Eigen::Matrix< sFloat, task_dof_, 1 > &arg_min_pos, const Eigen::Matrix< sFloat, task_dof_, 1 > &arg_min_vel, const Eigen::Matrix< sFloat, task_dof_, 1 > &arg_min_acc)
Definition: CTrajectoryGenerator.hpp:185
virtual sBool setTrajFromFunc(sFloat(*arg_func)(sFloat), const sLongLong arg_traj_slices, const sFloat arg_time_per_slice, const Eigen::Matrix< sFloat, task_dof_, 1 > &arg_pos_start, const Eigen::Matrix< sFloat, task_dof_, 1 > &arg_scale, const Eigen::Matrix< sFloat, task_dof_, 1 > &arg_stagger)
Definition: CTrajectoryGenerator.hpp:307
Definition: CMappedList.hpp:85
Eigen::Matrix< sFloat, task_dof_, 1 > traj_curr_vel_
Definition: CTrajectoryGenerator.hpp:143
bool readEigenMatFromFile(Eigen::MatrixXd &arg_mat, const std::string &arg_file)
Definition: FileFunctions.cpp:116
virtual sBool saveCurrState(const Eigen::Matrix< sFloat, task_dof_, 1 > &arg_state, const sFloat arg_time)
Definition: CTrajectoryGenerator.hpp:372
Definition: CTrajectoryGenerator.hpp:51
Eigen::Matrix< sFloat, task_dof_, 1 > * traj_curr_pos_
Definition: CTrajectoryGenerator.hpp:141
Eigen::Matrix< sFloat, task_dof_, 1 > traj_curr_acc_
Definition: CTrajectoryGenerator.hpp:145
bool sBool
Definition: DataTypes.hpp:54
sutil::CMappedList< sFloat, Eigen::Matrix< sFloat, task_dof_, 1 > > traj_desired_
Definition: CTrajectoryGenerator.hpp:135
virtual sBool setTrajFromTxtFile(const std::string &arg_file, const sLongLong arg_traj_slices, const sBool arg_has_time=true)
Definition: CTrajectoryGenerator.hpp:239
sFloat traj_curr_time_
Definition: CTrajectoryGenerator.hpp:155
long long sLongLong
Definition: DataTypes.hpp:66
double sFloat
Definition: DataTypes.hpp:72
int sInt
Definition: DataTypes.hpp:64
sutil::CMappedList< sFloat, Eigen::Matrix< sFloat, task_dof_, 1 > > traj_achieved_
Definition: CTrajectoryGenerator.hpp:138
virtual sBool getCurrGoal(Eigen::Matrix< sFloat, task_dof_, 1 > &arg_pos_curr, Eigen::Matrix< sFloat, task_dof_, 1 > &arg_vel_curr, Eigen::Matrix< sFloat, task_dof_, 1 > &arg_acc_curr)
Definition: CTrajectoryGenerator.hpp:360
virtual sBool getLoggedPositions(sutil::CMappedList< sFloat, Eigen::Matrix< sFloat, task_dof_, 1 > > &ret_traj)