SCL  1.0
Standard Control Library : Control, dynamics, physics, and simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Groups Pages
CTrajectoryGenerator.hpp
1 /* This file is part of scl, a control and simulation library
2 for robots and biomechanical models.
3 
4 scl is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8 
9 Alternatively, you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
13 
14 scl is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public
20 License and a copy of the GNU General Public License along with
21 scl. If not, see <http://www.gnu.org/licenses/>.
22 */
23 /* \file CTrajectoryGenerator.hpp
24  *
25  * Created on: Apr 12, 2011
26  *
27  * Copyright (C) 2011
28  *
29  * Author: Samir Menon <smenon@stanford.edu>
30  */
31 
32 #ifndef CTRAJECTORYGENERATOR_HPP_
33 #define CTRAJECTORYGENERATOR_HPP_
34 
35 #include <scl/DataTypes.hpp>
36 #include <scl/util/FileFunctions.hpp>
37 
38 #include <sutil/CMappedList.hpp>
39 
40 #include <Eigen/Core>
41 #include <string>
42 
43 namespace scl
44 {
50  template <sUInt task_dof_>
52  {
53  public:
55  virtual ~CTrajectoryGenerator();
56 
58  virtual sBool initTraj(
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);
73 
83  virtual sBool setTrajFromTxtFile(
85  const std::string& arg_file,
87  const sLongLong arg_traj_slices,
89  const sBool arg_has_time=true);
90 
96  virtual sBool setTrajFromFunc(sFloat (*arg_func) (sFloat),
98  const sLongLong arg_traj_slices,
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);
109 
115  virtual sBool getCurrGoal(Eigen::Matrix<sFloat,task_dof_,1>& arg_pos_curr,
116  Eigen::Matrix<sFloat,task_dof_,1>& arg_vel_curr,
117  Eigen::Matrix<sFloat,task_dof_,1>& arg_acc_curr);
118 
121  virtual sBool saveCurrState(
123  const Eigen::Matrix<sFloat,task_dof_,1>& arg_state,
125  const sFloat arg_time);
126 
130  virtual sBool getLoggedPositions(
131  sutil::CMappedList<sFloat, Eigen::Matrix<sFloat,task_dof_,1> >& ret_traj);
132 
133  protected:
136 
139 
141  Eigen::Matrix<sFloat,task_dof_,1>* traj_curr_pos_;
143  Eigen::Matrix<sFloat,task_dof_,1> traj_curr_vel_;
145  Eigen::Matrix<sFloat,task_dof_,1> traj_curr_acc_;
146 
148  Eigen::Matrix<sFloat,task_dof_,1>* traj_curr_pos_;
150  Eigen::Matrix<sFloat,task_dof_,1> traj_curr_vel_;
152  Eigen::Matrix<sFloat,task_dof_,1> traj_curr_acc_;
153 
158 
159  Eigen::Matrix<sFloat,task_dof_,1> max_pos_, max_vel_, max_acc_,
160  min_pos_, min_vel_, min_acc_;
161 
162  TypeIOTG otg_;
163 
164  sBool traj_has_been_init_;
165 
166  public:
167  //Contains fixed sized eigen matrices
168  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
169  };
170 
171 
172  //NOTE TODO : Don't really like initializing stuff in the constructor.
173  //Ask Torsten to update his OTG API and move stuff to an init function.
174  //Else hack it yourself.
175  template <sUInt task_dof_>
177  otg_(task_dof_)
178  { traj_has_been_init_ = false; }
179 
180  template <sUInt task_dof_>
181  CTrajectoryGenerator<task_dof_>::~CTrajectoryGenerator()
182  { }
183 
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)
200  {
201  try
202  {
203  //Some error checks.
204  if(0 >= arg_cycle_time)
205  { throw(std::runtime_error("Can't generate a trajectory for negative or zero servo cycle time.")); }
206  if(0 >= task_dof_)
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.")); }
220 
221  otg_.SetCycleTimeInSeconds(arg_cycle_time);
222 
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;
229 
230  traj_has_been_init_ = true;
231  }
232  catch(std::exception& e)
233  { std::cout<<"\nCTrajectoryGenerator::initTraj() Error : "<<e.what(); }
234 
235  return traj_has_been_init_;
236  }
237 
238  template <sUInt task_dof_>
241  const std::string& arg_file,
243  const sLongLong arg_traj_slices,
245  const sBool arg_has_time)
246  {
247  bool flag;
248  try
249  {
250  Eigen::MatrixXd traj;
251 
252  if(arg_has_time)
253  {
254  traj.resize(arg_traj_slices,task_dof_+1);
255  flag = scl_util::readEigenMatFromFile(traj,
256  arg_traj_slices,task_dof_+1,arg_file);
257  if(false==flag)
258  { throw(std::runtime_error("Could not read trajectory and time from the file.")); }
259 
260  //Load the trajectory into the pilemap
261  for(sUInt i = 0; i< arg_traj_slices; ++i)
262  {
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);
266  if(S_NULL == slice)
267  {
268  traj_desired_.clear();
269  std::stringstream ss;
270  ss<<i;
271  std::string s("Could not allocate trajectory's slice on the pilemap. At slice : ");
272  s = s + ss.str();
273  throw(std::runtime_error(s.c_str()));
274  }
275  }
276  }
277  else
278  {
279  traj.resize(arg_traj_slices,task_dof_);
280  flag = scl_util::readEigenMatFromFile(traj,
281  arg_traj_slices,task_dof_,arg_file);
282  if(false==flag)
283  { throw(std::runtime_error("Could not read trajectory from the file.")); }
284 
285  //Load the trajectory into the pilemap
286  for(sUInt i = 0; i< arg_traj_slices; ++i)
287  {
288  Eigen::Matrix<sFloat,task_dof_,1> * slice = traj_desired_.create(i, traj.row(i));
289  if(S_NULL == slice)
290  {
291  traj_desired_.clear();
292  throw(std::runtime_error("Could not allocate trajectory's slice on the pilemap."));
293  }
294  }
295  }
296 
297  return true;
298  }
299  catch(std::exception& e)
300  {
301  std::cout<<"\nCTrajectoryGenerator::readTrajFromTxtFile() Error : "<<e.what();
302  return false;
303  }
304  }
305 
306  template <sUInt task_dof_>
309  const sLongLong arg_traj_slices,
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)
320  {
321  try
322  {
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.")); }
329 
330  sFloat time=0.0;
331 
332  Eigen::Matrix<sFloat,task_dof_,1> slice;
333  slice = arg_pos_start;
334 
335  for(sUInt i=0; i<arg_traj_slices;++i)
336  {
337  //Compute the next step according to the given function
338  sFloat x = arg_func(time);
339  slice = arg_pos_start + arg_scale*x + arg_stagger;
340 
341  //Store the trajectory in the pilemap.
342  Eigen::Matrix<sFloat,task_dof_,1> * vec = S_NULL;
343  vec = traj_desired_.create(time,slice);
344  if(S_NULL==vec)
345  { throw(std::runtime_error("Could not create trajectory slice on the pile.")); }
346 
347  time+=arg_time_per_slice;//Increment the time
348  }
349  return true;
350  }
351  catch(std::exception& e)
352  {
353  std::cout<<"\nCTrajectoryGenerator::setTrajFromFunc() Error : "<<e.what();
354  return false;
355  }
356  }
357 
358 
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)
364  {
365 // for(int i=0;i<task_dof_;++i)
366 // {
367 // otg_.GetNextMotionState_Position();
368 // }
369  }
370 
371  template <sUInt task_dof_>
374  const Eigen::Matrix<sFloat,task_dof_,1>& arg_state,
376  const sFloat arg_time)
377  {
378  Eigen::Matrix<sFloat,task_dof_,1> * vec = S_NULL;
379  //Adds an entry to the trajectory logs
380  vec = traj_achieved_.create(arg_time,arg_state);
381  if(S_NULL == vec)
382  { return false; }
383  else
384  { return true; }
385  }
386 
387 }
388 
389 #endif /* CTrajectoryGenerator_HPP_ */
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)