sot-talos-balance  2.0.5
Collection of dynamic-graph entities aimed at implementing balance control on talos.
nd-trajectory-generator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017,Thomas Flayols, LAAS-CNRS
3  * File derived from sot-torque-control package available on
4  * https://github.com/stack-of-tasks/sot-torque-control
5  *
6  * This file is part of sot-talos-balance.
7  * sot-talos-balance is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation, either version 3 of
10  * the License, or (at your option) any later version.
11  * sot-torque-control is distributed in the hope that it will be
12  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details. You should
15  * have received a copy of the GNU Lesser General Public License along
16  * with sot-talos-balance. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <dynamic-graph/factory.h>
20 
21 #include <sot/core/debug.hh>
22 #include <sot/core/stop-watch.hh>
25 
26 namespace dynamicgraph {
27 namespace sot {
28 namespace talos_balance {
29 namespace dynamicgraph = ::dynamicgraph;
30 using namespace dynamicgraph;
31 using namespace dynamicgraph::command;
32 using namespace std;
33 using namespace Eigen;
34 
35 #define PROFILE_ND_POSITION_DESIRED_COMPUTATION "NdTrajGen: traj computation"
36 #define DOUBLE_INF std::numeric_limits<double>::max()
37 typedef NdTrajectoryGenerator EntityClassName;
40 
41 /* --- DG FACTORY ---------------------------------------------------- */
42 DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(NdTrajectoryGenerator,
43  "NdTrajectoryGenerator");
44 
45 /* ------------------------------------------------------------------- */
46 /* --- CONSTRUCTION -------------------------------------------------- */
47 /* ------------------------------------------------------------------- */
49  : Entity(name),
50  CONSTRUCT_SIGNAL_IN(initial_value, dynamicgraph::Vector),
51  CONSTRUCT_SIGNAL_IN(trigger, bool),
52  CONSTRUCT_SIGNAL(x, OUT, dynamicgraph::Vector),
53  CONSTRUCT_SIGNAL_OUT(dx, dynamicgraph::Vector, m_xSOUT),
54  CONSTRUCT_SIGNAL_OUT(ddx, dynamicgraph::Vector, m_xSOUT),
55  m_initSucceeded(false),
56  m_firstIter(true),
57  m_t(0),
58  m_n(1),
59  m_iterLast(0),
60  m_splineReady(false) {
61  BIND_SIGNAL_TO_FUNCTION(x, OUT, dynamicgraph::Vector);
62 
63  Entity::signalRegistration(m_xSOUT << m_dxSOUT << m_ddxSOUT
64  << m_initial_valueSIN << m_triggerSIN);
65 
66  /* Commands. */
67  addCommand(
68  "init",
69  makeCommandVoid2(*this, &NdTrajectoryGenerator::init,
70  docCommandVoid2("Initialize the entity.",
71  "Time period in seconds (double)",
72  "size of output vector signal (int)")));
73 
74  addCommand(
75  "getValue",
76  makeCommandVoid1(
78  docCommandVoid1("Get the current value of the specified index.",
79  "index (int)")));
80 
81  addCommand("playTrajectoryFile",
82  makeCommandVoid1(
84  docCommandVoid1(
85  "Play the trajectory read from the specified text file.",
86  "(string) File name, path included")));
87 
88  addCommand(
89  "startSinusoid",
90  makeCommandVoid3(
92  docCommandVoid3("Start an infinite sinusoid motion.",
93  "(int) index", "(double) final value",
94  "(double) time to reach the final value in sec")));
95 
96  addCommand(
97  "setSpline",
98  makeCommandVoid2(*this, &NdTrajectoryGenerator::setSpline,
99  docCommandVoid2("Load serialized spline from file",
100  "(string) filename",
101  "(double) time to initial conf")));
102 
103  /* addCommand("startTriangle",
104  makeCommandVoid4(*this, &NdTrajectoryGenerator::startTriangle,
105  docCommandVoid4("Start an infinite triangle
106  wave.",
107  "(int) index",
108  "(double) final values",
109  "(double) time to reach the final
110  value in sec",
111  "(double) time to accelerate in
112  sec")));
113  */
114  addCommand(
115  "startConstAcc",
116  makeCommandVoid3(
118  docCommandVoid3("Start an infinite trajectory with piece-wise "
119  "constant acceleration.",
120  "(int) index", "(double) final values",
121  "(double) time to reach the final value in sec")));
122 
123  addCommand("startLinChirp",
124  makeCommandVoid5(
126  docCommandVoid5("Start a linear-chirp motion.",
127  "(int) index", "(double) final values",
128  "(double) initial frequency [Hz]",
129  "(double) final frequency [Hz]",
130  "(double) trajectory time [sec]")));
131  addCommand("move", makeCommandVoid3(
133  docCommandVoid3(
134  "Move component corresponding to index to the "
135  "specified value with a minimum jerk trajectory.",
136  "(int) index", "(double) final values",
137  "(double) time to reach the final value in sec")));
138  addCommand("set",
139  makeCommandVoid2(
141  docCommandVoid2("Instantaneously set component corresponding "
142  "to index to the specified value.",
143  "(int) index", "(double) desired value")));
144  addCommand(
145  "stop",
146  makeCommandVoid1(
148  docCommandVoid1("Stop the motion of the specified index, or of all "
149  "components of the vector if index is equal to -1.",
150  "(int) index")));
151 }
152 
153 void NdTrajectoryGenerator::init(const double& dt, const unsigned int& n) {
154  if (dt <= 0.0) return SEND_MSG("Timestep must be positive", MSG_TYPE_ERROR);
155  if (n < 1) return SEND_MSG("n must be at least 1", MSG_TYPE_ERROR);
156  m_firstIter = true;
157  m_dt = dt;
158  m_n = n;
159  m_status.resize(m_n, JTG_STOP);
160  m_minJerkTrajGen.resize(m_n);
161  m_sinTrajGen.resize(m_n);
162  // m_triangleTrajGen.resize(m_n);
163  m_constAccTrajGen.resize(m_n);
164  m_linChirpTrajGen.resize(m_n);
165  m_currentTrajGen.resize(m_n);
166  m_noTrajGen.resize(m_n);
167  for (unsigned i = 0; i < m_n; i++) {
168  m_minJerkTrajGen[i] = new parametriccurves::MinimumJerk<double, 1>(5.0);
169  m_sinTrajGen[i] = new parametriccurves::InfiniteSinusoid<double, 1>(5.0);
170  // m_triangleTrajGen[i] = new
171  // parametriccurves::InfiniteTriangle<double,1>(5.0);
172  m_constAccTrajGen[i] =
173  new parametriccurves::InfiniteConstAcc<double, 1>(5.0);
174  m_linChirpTrajGen[i] = new parametriccurves::LinearChirp<double, 1>(5.0);
175  m_noTrajGen[i] = new parametriccurves::Constant<double, 1>(5.0);
177 
178  // Set infinite time for infinite trajectories
179  m_noTrajGen[i]->setTimePeriod(DOUBLE_INF);
180  m_constAccTrajGen[i]->setTimePeriod(DOUBLE_INF);
181  m_sinTrajGen[i]->setTimePeriod(DOUBLE_INF);
182  }
183  m_splineTrajGen = new parametriccurves::Spline<double, Eigen::Dynamic>();
185  new parametriccurves::TextFile<double, Eigen::Dynamic>(dt, n);
186 
187  NdTrajectoryGenerator::setLoggerVerbosityLevel(
188  dynamicgraph::LoggerVerbosity::VERBOSITY_ALL);
189  m_initSucceeded = true;
190 }
191 
192 /* ------------------------------------------------------------------- */
193 /* --- SIGNALS ------------------------------------------------------- */
194 /* ------------------------------------------------------------------- */
195 
197  if (!m_initSucceeded) {
198  SEND_WARNING_STREAM_MSG(
199  "Cannot compute signal positionDes before initialization!");
200  return s;
201  }
202 
203  getProfiler().start(PROFILE_ND_POSITION_DESIRED_COMPUTATION);
204  {
205  if (s.size() != m_n) s.resize(m_n);
206 
207  // at first iteration store initial values
208  if (m_firstIter) {
209  const dynamicgraph::Vector& initial_value = m_initial_valueSIN(iter);
210  if (initial_value.size() != m_n) {
211  SEND_MSG("Unexpected size of input signal initial_value: " +
212  toString(initial_value.size()),
213  MSG_TYPE_ERROR);
214  getProfiler().stop(PROFILE_ND_POSITION_DESIRED_COMPUTATION);
215  return s;
216  }
217  for (unsigned int i = 0; i < m_n; i++)
218  m_currentTrajGen[i]->setInitialPoint(initial_value(i));
219  m_firstIter = false;
220  } else if (iter == static_cast<int>(m_iterLast)) {
221  if (m_triggerSIN(iter) == true && m_splineReady) startSpline();
222  if (m_status[0] == JTG_TEXT_FILE) {
223  s = (*m_textFileTrajGen)(m_t);
224  } else if (m_status[0] == JTG_SPLINE) {
225  s = (*m_splineTrajGen)(m_t);
226  } else
227  for (unsigned int i = 0; i < m_n; i++)
228  s(i) = (*m_currentTrajGen[i])(m_t)[0];
229  getProfiler().stop(PROFILE_ND_POSITION_DESIRED_COMPUTATION);
230  return s;
231  }
232  m_iterLast = iter;
233 
234  const bool& isTriggered = m_triggerSIN(iter);
235 
236  if (isTriggered || m_status[0] != JTG_TEXT_FILE) m_t += m_dt;
237 
238  if (isTriggered && m_splineReady) startSpline();
239 
240  if (m_status[0] == JTG_TEXT_FILE) {
241  if (!isTriggered) {
242  for (unsigned int i = 0; i < m_n; i++) {
243  s(i) = (*m_noTrajGen[i])(m_t)[0];
244  }
245  } else if (!m_textFileTrajGen->checkRange(m_t)) {
246  s = (*m_textFileTrajGen)(m_textFileTrajGen->tmax() -
247  m_dt); // HACK!!! m_dt avoids buffer overflow
248  for (unsigned int i = 0; i < m_n; i++) {
249  m_noTrajGen[i]->setInitialPoint(s(i));
250  m_status[i] = JTG_STOP;
251  }
252  SEND_MSG("Text file trajectory ended.", MSG_TYPE_INFO);
253  m_t = 0;
254  } else
255  s = (*m_textFileTrajGen)(m_t);
256  } else if (m_status[0] == JTG_SPLINE) {
257  if (!m_splineTrajGen->checkRange(m_t)) {
258  s = (*m_splineTrajGen)(m_splineTrajGen->tmax());
259  for (unsigned int i = 0; i < m_n; i++) {
260  m_noTrajGen[i]->setInitialPoint(s(i));
261  m_status[i] = JTG_STOP;
262  }
263  m_splineReady = false;
264  SEND_MSG("Spline trajectory ended. Remember to turn off the trigger.",
265  MSG_TYPE_INFO);
266  m_t = 0;
267  } else
268  s = (*m_splineTrajGen)(m_t);
269  } else {
270  for (unsigned int i = 0; i < m_n; i++) {
271  if (!m_currentTrajGen[i]->checkRange(m_t)) {
272  s(i) = (*m_currentTrajGen[i])(m_currentTrajGen[i]->tmax())[0];
273  m_currentTrajGen[i] = m_noTrajGen[i];
274  m_noTrajGen[i]->setInitialPoint(s(i));
275  m_status[i] = JTG_STOP;
276  SEND_MSG("Trajectory of index " + toString(i) + " ended.",
277  MSG_TYPE_INFO);
278  } else
279  s(i) = (*m_currentTrajGen[i])(m_t)[0];
280  }
281  }
282  }
283  getProfiler().stop(PROFILE_ND_POSITION_DESIRED_COMPUTATION);
284 
285  return s;
286 }
287 
289  if (!m_initSucceeded) {
290  SEND_WARNING_STREAM_MSG(
291  "Cannot compute signal positionDes before initialization!");
292  return s;
293  }
294 
295  const dynamicgraph::Vector& x = m_xSOUT(iter);
296  (void)x; // disable unused variable warning
297 
298  if (s.size() != m_n) s.resize(m_n);
299 
300  const bool& isTriggered = m_triggerSIN(iter);
301 
302  if (m_status[0] == JTG_TEXT_FILE) {
303  if (isTriggered)
304  s = m_textFileTrajGen->derivate(m_t, 1);
305  else
306  s.setZero();
307  } else if (m_status[0] == JTG_SPLINE)
308  s = m_splineTrajGen->derivate(m_t, 1);
309  else
310  for (unsigned int i = 0; i < m_n; i++)
311  s(i) = m_currentTrajGen[i]->derivate(m_t, 1)[0];
312 
313  return s;
314 }
315 
317  if (!m_initSucceeded) {
318  SEND_WARNING_STREAM_MSG(
319  "Cannot compute signal positionDes before initialization!");
320  return s;
321  }
322 
323  const dynamicgraph::Vector& x = m_xSOUT(iter);
324  (void)x; // disable unused variable warning
325 
326  if (s.size() != m_n) s.resize(m_n);
327 
328  const bool& isTriggered = m_triggerSIN(iter);
329 
330  if (m_status[0] == JTG_TEXT_FILE) {
331  if (isTriggered)
332  s = m_textFileTrajGen->derivate(m_t, 2);
333  else
334  s.setZero();
335  } else if (m_status[0] == JTG_SPLINE)
336  s = m_splineTrajGen->derivate(m_t, 2);
337  else
338  for (unsigned int i = 0; i < m_n; i++)
339  s(i) = m_currentTrajGen[i]->derivate(m_t, 2)[0];
340 
341  return s;
342 }
343 
344 /* ------------------------------------------------------------------- */
345 /* --- COMMANDS ------------------------------------------------------ */
346 /* ------------------------------------------------------------------- */
347 
348 void NdTrajectoryGenerator::getValue(const int& id) {
349  if (id < 0 || id >= static_cast<int>(m_n))
350  return SEND_MSG("Index is out of bounds", MSG_TYPE_ERROR);
351 
352  SEND_MSG("Current value of component " + toString(id) + " is " +
353  toString((*m_currentTrajGen[id])(m_t)[0]),
354  MSG_TYPE_INFO);
355 }
356 
357 void NdTrajectoryGenerator::playTrajectoryFile(const std::string& fileName) {
358  if (!m_initSucceeded)
359  return SEND_MSG("Cannot start trajectory before initialization!",
360  MSG_TYPE_ERROR);
361 
362  for (unsigned int i = 0; i < m_n; i++)
363  if (m_status[i] != JTG_STOP)
364  return SEND_MSG("You cannot control component " + toString(i) +
365  " because it is already controlled.",
366  MSG_TYPE_ERROR);
367 
368  if (!m_textFileTrajGen->loadTextFile(fileName)) {
369  std::string msg("Error while loading text file " + fileName);
370  SEND_MSG(msg, MSG_TYPE_ERROR);
371  throw std::runtime_error(msg);
372  }
373 
374  // check current configuration is not too far from initial trajectory
375  // configuration
376  bool needToMoveToInitConf = false;
377  const VectorXd& xInit = m_textFileTrajGen->getInitialPoint();
378  for (unsigned int i = 0; i < m_n; i++) {
379  const double currentVal = (*m_currentTrajGen[i])(m_t)[0];
380  if (fabs(xInit[i] - currentVal) > 0.001) {
381  needToMoveToInitConf = true;
382  // SEND_MSG("Component "+ toString(i) +" is too far from initial
383  // configuration so first i will move it there.", MSG_TYPE_WARNING);
384  SEND_MSG("Component " + toString(i) +
385  " is far from initial configuration (" + toString(xInit[i]) +
386  "->" + toString(currentVal) + ")",
387  MSG_TYPE_WARNING);
388  }
389  }
390  /*
391  // if necessary move joints to initial configuration
392  if(needToMoveToInitConf)
393  {
394  for(unsigned int i=0; i<m_n; i++)
395  {
396  // if(!isJointInRange(i, xInit[i]))
397  // return;
398 
399  m_minJerkTrajGen[i]->setInitialPoint((*m_noTrajGen[i])(m_t)[0]);
400  m_minJerkTrajGen[i]->setFinalPoint(xInit[i]);
401  m_minJerkTrajGen[i]->setTimePeriod(4.0);
402  m_status[i] = JTG_MIN_JERK;
403  m_currentTrajGen[i] = m_minJerkTrajGen[i];
404  }
405  m_t = 0.0;
406  return;
407  }
408  */
409 
410  m_t = 0.0;
411  for (unsigned int i = 0; i < m_n; i++) {
412  m_status[i] = JTG_TEXT_FILE;
413  }
414 }
415 
416 void NdTrajectoryGenerator::setSpline(const std::string& filename,
417  const double& timeToInitConf) {
418  if (!m_initSucceeded)
419  return SEND_MSG("Cannot start spline before initialization!",
420  MSG_TYPE_ERROR);
421 
422  for (unsigned int i = 0; i < m_n; i++)
423  if (m_status[i] != JTG_STOP)
424  return SEND_MSG("You cannot control component " + toString(i) +
425  " because it is already controlled.",
426  MSG_TYPE_ERROR);
427 
428  if (!m_splineTrajGen->loadFromFile(filename))
429  return SEND_MSG("Error while loading spline" + filename, MSG_TYPE_ERROR);
430 
431  SEND_MSG("Spline set to " + filename + ". Now checking initial position",
432  MSG_TYPE_INFO);
433 
434  bool needToMoveToInitConf = false;
435  if (timeToInitConf < 0) {
436  m_splineReady = true;
437  SEND_MSG("Spline Ready. Set trigger to true to start playing",
438  MSG_TYPE_INFO);
439  return;
440  }
441 
442  // check current configuration is not too far from initial configuration
443  const VectorXd& xInit = (*m_splineTrajGen)(0.0);
444  assert(xInit.size() == m_n);
445  for (unsigned int i = 0; i < m_n; i++)
446  if (fabs(xInit[i] - (*m_currentTrajGen[i])(m_t)[0]) > 0.001) {
447  needToMoveToInitConf = true;
448  SEND_MSG("Component " + toString(i) +
449  " is too far from initial configuration so first i will "
450  "move it there.",
451  MSG_TYPE_WARNING);
452  }
453  // if necessary move joints to initial configuration
454  if (needToMoveToInitConf) {
455  for (unsigned int i = 0; i < m_n; i++) {
456  m_minJerkTrajGen[i]->setInitialPoint((*m_noTrajGen[i])(m_t)[0]);
457  m_minJerkTrajGen[i]->setFinalPoint(xInit[i]);
458  m_minJerkTrajGen[i]->setTimePeriod(timeToInitConf);
459  m_status[i] = JTG_MIN_JERK;
461  // SEND_MSG("MinimumJerk trajectory for index "+ toString(i) +"
462  // to go to final position" + toString(xInit[i]),
463  // MSG_TYPE_WARNING);
464  }
465  m_t = 0.0;
466  m_splineReady = true;
467  return;
468  }
469  m_splineReady = true;
470  SEND_MSG("Spline Ready. Set trigger to true to start playing", MSG_TYPE_INFO);
471 }
472 
474  if (m_status[0] == JTG_SPLINE) return;
475  m_t = 0.0;
476  for (unsigned int i = 0; i < m_n; i++) {
477  m_status[i] = JTG_SPLINE;
478  }
479 }
480 
481 void NdTrajectoryGenerator::startSinusoid(const int& id, const double& xFinal,
482  const double& time) {
483  if (!m_initSucceeded)
484  return SEND_MSG("Cannot start sinusoid before initialization!",
485  MSG_TYPE_ERROR);
486 
487  if (id < 0 || id >= static_cast<int>(m_n))
488  return SEND_MSG("Index is out of bounds", MSG_TYPE_ERROR);
489  unsigned int i = id;
490  if (time <= 0.0)
491  return SEND_MSG("Trajectory time must be a positive number",
492  MSG_TYPE_ERROR);
493  if (m_status[i] != JTG_STOP)
494  return SEND_MSG(
495  "You cannot move the specified component because it is already "
496  "controlled.",
497  MSG_TYPE_ERROR);
498  // if(!isJointInRange(i, xFinal))
499  // return;
500 
501  m_sinTrajGen[i]->setInitialPoint((*m_noTrajGen[i])(m_t)[0]);
502  m_sinTrajGen[i]->setFinalPoint(xFinal);
503  m_sinTrajGen[i]->setTrajectoryTime(time);
504  SEND_MSG(
505  "Set initial point of sinusoid to " + toString((*m_noTrajGen[i])(m_t)[0]),
506  MSG_TYPE_DEBUG);
507  m_status[i] = JTG_SINUSOID;
509  m_t = 0.0;
510 }
511 /*
512 void NdTrajectoryGenerator::startTriangle(const int& id, const double& xFinal,
513 const double& time, const double& Tacc)
514 {
515  if(!m_initSucceeded)
516  return SEND_MSG("Cannot start triangle before
517 initialization!",MSG_TYPE_ERROR); if(id<0 || id>=static_cast<int>(m_n)) return
518 SEND_MSG("Index is out of bounds", MSG_TYPE_ERROR); unsigned int i = id;
519  if(m_status[i]!=JTG_STOP)
520  return SEND_MSG("You cannot move the specified component because it is
521 already controlled.", MSG_TYPE_ERROR);
522 // if(!isJointInRange(i, xFinal))
523 // return;
524 
525  m_triangleTrajGen[i]->setInitialPoint(m_noTrajGen[i]->getPos());
526  SEND_MSG("Set initial point of triangular trajectory to
527 "+toString(m_triangleTrajGen[i]->getPos()),MSG_TYPE_DEBUG);
528  m_triangleTrajGen[i]->setFinalPoint(xFinal);
529 
530  if(!m_triangleTrajGen[i]->setTimePeriod(time))
531  return SEND_MSG("Trajectory time cannot be negative.", MSG_TYPE_ERROR);
532 
533  if(!m_triangleTrajGen[i]->set_acceleration_time(Tacc))
534  return SEND_MSG("Acceleration time cannot be negative or larger than half
535 the trajectory time.", MSG_TYPE_ERROR);
536 
537  m_status[i] = JTG_TRIANGLE;
538  m_currentTrajGen[i] = m_triangleTrajGen[i];
539  m_t = 0.0;
540 }
541 */
542 void NdTrajectoryGenerator::startConstAcc(const int& id, const double& xFinal,
543  const double& time) {
544  if (!m_initSucceeded)
545  return SEND_MSG(
546  "Cannot start constant-acceleration trajectory before initialization!",
547  MSG_TYPE_ERROR);
548  if (id < 0 || id >= static_cast<int>(m_n))
549  return SEND_MSG("Index is out of bounds", MSG_TYPE_ERROR);
550  unsigned int i = id;
551  if (time <= 0.0)
552  return SEND_MSG("Trajectory time must be a positive number",
553  MSG_TYPE_ERROR);
554  if (m_status[i] != JTG_STOP)
555  return SEND_MSG(
556  "You cannot move the specified component because it is already "
557  "controlled.",
558  MSG_TYPE_ERROR);
559  // if(!isJointInRange(i, xFinal))
560  // return;
561 
562  m_constAccTrajGen[i]->setInitialPoint((*m_noTrajGen[i])(m_t)[0]);
563  SEND_MSG("Set initial point of const-acc trajectory to " +
564  toString((*m_noTrajGen[i])(m_t)[0]),
565  MSG_TYPE_DEBUG);
566  m_constAccTrajGen[i]->setFinalPoint(xFinal);
567  m_constAccTrajGen[i]->setTrajectoryTime(time);
568  m_status[i] = JTG_CONST_ACC;
570  m_t = 0.0;
571 }
573  const double& xFinal,
574  const double& f0, const double& f1,
575  const double& time) {
576  if (!m_initSucceeded)
577  return SEND_MSG("Cannot start linear chirp before initialization!",
578  MSG_TYPE_ERROR);
579  if (id < 0 || id >= static_cast<int>(m_n))
580  return SEND_MSG("Index is out of bounds", MSG_TYPE_ERROR);
581  unsigned int i = id;
582  if (time <= 0.0)
583  return SEND_MSG("Trajectory time must be a positive number",
584  MSG_TYPE_ERROR);
585  if (m_status[i] != JTG_STOP)
586  return SEND_MSG(
587  "You cannot move the specified component because it is already "
588  "controlled.",
589  MSG_TYPE_ERROR);
590  // if(!isJointInRange(i, xFinal))
591  // return;
592  if (f0 > f1)
593  return SEND_MSG(
594  "f0 " + toString(f0) + " cannot to be more than f1 " + toString(f1),
595  MSG_TYPE_ERROR);
596  if (f0 <= 0.0)
597  return SEND_MSG("Frequency has to be positive " + toString(f0),
598  MSG_TYPE_ERROR);
599 
600  if (!m_linChirpTrajGen[i]->setInitialPoint((*m_noTrajGen[i])(m_t)[0]))
601  return SEND_MSG("Error while setting initial point " +
602  toString((*m_noTrajGen[i])(m_t)[0]),
603  MSG_TYPE_ERROR);
604  if (!m_linChirpTrajGen[i]->setFinalPoint(xFinal))
605  return SEND_MSG("Error while setting final point " + toString(xFinal),
606  MSG_TYPE_ERROR);
607  if (!m_linChirpTrajGen[i]->setTimePeriod(time))
608  return SEND_MSG("Error while setting trajectory time " + toString(time),
609  MSG_TYPE_ERROR);
610  if (!m_linChirpTrajGen[i]->setInitialFrequency(f0))
611  return SEND_MSG("Error while setting initial frequency " + toString(f0),
612  MSG_TYPE_ERROR);
613  if (!m_linChirpTrajGen[i]->setFinalFrequency(f1))
614  return SEND_MSG("Error while setting final frequency " + toString(f1),
615  MSG_TYPE_ERROR);
616  m_status[i] = JTG_LIN_CHIRP;
618  m_t = 0.0;
619 }
620 
621 void NdTrajectoryGenerator::move(const int& id, const double& xFinal,
622  const double& time) {
623  if (!m_initSucceeded)
624  return SEND_MSG("Cannot move value before initialization!", MSG_TYPE_ERROR);
625  unsigned int i = id;
626  if (id < 0 || id >= static_cast<int>(m_n))
627  return SEND_MSG("Index is out of bounds", MSG_TYPE_ERROR);
628  if (time <= 0.0)
629  return SEND_MSG("Trajectory time must be a positive number",
630  MSG_TYPE_ERROR);
631  if (m_status[i] != JTG_STOP)
632  return SEND_MSG(
633  "You cannot move the specified component because it is already "
634  "controlled.",
635  MSG_TYPE_ERROR);
636  // if(!isJointInRange(i, xFinal))
637  // return;
638 
639  m_minJerkTrajGen[i]->setInitialPoint((*m_noTrajGen[i])(m_t)[0]);
640  m_minJerkTrajGen[i]->setFinalPoint(xFinal);
641  m_minJerkTrajGen[i]->setTimePeriod(time);
642  m_status[i] = JTG_MIN_JERK;
644  m_t = 0.0;
645 }
646 
647 void NdTrajectoryGenerator::set(const int& id, const double& xVal) {
648  if (!m_initSucceeded)
649  return SEND_MSG("Cannot set value before initialization!", MSG_TYPE_ERROR);
650  unsigned int i = id;
651  if (id < 0 || id >= static_cast<int>(m_n))
652  return SEND_MSG("Index is out of bounds", MSG_TYPE_ERROR);
653  if (m_status[i] != JTG_STOP)
654  return SEND_MSG(
655  "You cannot set the specified component because it is already "
656  "controlled.",
657  MSG_TYPE_ERROR);
658 
659  m_noTrajGen[i]->setInitialPoint(xVal);
660 
661  // m_status[i] = JTG_STOP;
662  // m_splineReady = false;
663  // m_currentTrajGen[i] = m_noTrajGen[i];
664  // m_t = 0.0;
665 }
666 
667 void NdTrajectoryGenerator::stop(const int& id) {
668  if (!m_initSucceeded)
669  return SEND_MSG("Cannot stop value before initialization!", MSG_TYPE_ERROR);
670 
671  if (id == -1) // Stop entire vector
672  {
673  for (unsigned int i = 0; i < m_n; i++) {
674  m_status[i] = JTG_STOP;
675  // update the initial value
676  m_noTrajGen[i]->setInitialPoint((*m_currentTrajGen[i])(m_t)[0]);
678  }
679  m_t = 0.0;
680  return;
681  }
682  if (id < 0 || id >= static_cast<int>(m_n))
683  return SEND_MSG("Index is out of bounds", MSG_TYPE_ERROR);
684  unsigned int i = id;
685  m_noTrajGen[i]->setInitialPoint((*m_currentTrajGen[i])(m_t)[0]);
686  m_status[i] = JTG_STOP;
687  m_splineReady = false;
689  m_t = 0.0;
690 }
691 
692 /* ------------------------------------------------------------------- */
693 // ************************ PROTECTED MEMBER METHODS ********************
694 /* ------------------------------------------------------------------- */
695 
696 // bool NdTrajectoryGenerator::isJointInRange(const int& id, double x)
697 // {
698 // JointLimits jl = JointUtil::get_limits_from_id(id);
699 // if(x<jl.lower)
700 // {
701 // SEND_MSG("Desired joint angle is smaller than lower limit:
702 // "+toString(jl.lower),MSG_TYPE_ERROR); return false;
703 // }
704 // if(x>jl.upper)
705 // {
706 // SEND_MSG("Desired joint angle is larger than upper limit:
707 // "+toString(jl.upper),MSG_TYPE_ERROR); return false;
708 // }
709 // return true;
710 // }
711 
712 /* ------------------------------------------------------------------- */
713 /* --- ENTITY -------------------------------------------------------- */
714 /* ------------------------------------------------------------------- */
715 
716 void NdTrajectoryGenerator::display(std::ostream& os) const {
717  os << "NdTrajectoryGenerator " << getName();
718  try {
719  getProfiler().report_all(3, os);
720  } catch (ExceptionSignal e) {
721  }
722 }
723 } // namespace talos_balance
724 } // namespace sot
725 } // namespace dynamicgraph
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::m_linChirpTrajGen
std::vector< parametriccurves::LinearChirp< double, 1 > * > m_linChirpTrajGen
Definition: nd-trajectory-generator.hh:184
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::set
void set(const int &id, const double &xVal)
Definition: nd-trajectory-generator.cpp:647
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::display
virtual void display(std::ostream &os) const
Definition: nd-trajectory-generator.cpp:716
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::setSpline
void setSpline(const std::string &filename, const double &timeToInitConf)
Definition: nd-trajectory-generator.cpp:416
sot_talos_balance.test.appli_admittance_end_effector.sot
sot
Definition: appli_admittance_end_effector.py:117
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::getValue
void getValue(const int &id)
Definition: nd-trajectory-generator.cpp:348
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::startSpline
void startSpline()
Definition: nd-trajectory-generator.cpp:473
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::m_splineTrajGen
parametriccurves::Spline< double, Eigen::Dynamic > * m_splineTrajGen
Definition: nd-trajectory-generator.hh:189
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::m_constAccTrajGen
std::vector< parametriccurves::InfiniteConstAcc< double, 1 > * > m_constAccTrajGen
Definition: nd-trajectory-generator.hh:187
dynamicgraph
Definition: treeview.dox:24
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::JTG_SPLINE
@ JTG_SPLINE
Definition: nd-trajectory-generator.hh:165
nd-trajectory-generator.hh
PROFILE_ND_POSITION_DESIRED_COMPUTATION
#define PROFILE_ND_POSITION_DESIRED_COMPUTATION
Definition: nd-trajectory-generator.cpp:35
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::m_minJerkTrajGen
std::vector< parametriccurves::MinimumJerk< double, 1 > * > m_minJerkTrajGen
Definition: nd-trajectory-generator.hh:182
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::m_initSucceeded
bool m_initSucceeded
Definition: nd-trajectory-generator.hh:169
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::JTG_LIN_CHIRP
@ JTG_LIN_CHIRP
Definition: nd-trajectory-generator.hh:161
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::m_firstIter
bool m_firstIter
true if the entity has been successfully initialized
Definition: nd-trajectory-generator.hh:170
commands-helper.hh
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::JTG_CONST_ACC
@ JTG_CONST_ACC
Definition: nd-trajectory-generator.hh:163
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::init
void init(const double &dt, const unsigned int &n)
Definition: nd-trajectory-generator.cpp:153
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::startSinusoid
void startSinusoid(const int &id, const double &xFinal, const double &time)
Definition: nd-trajectory-generator.cpp:481
dynamicgraph::sot::talos_balance::DEFINE_SIGNAL_OUT_FUNCTION
DEFINE_SIGNAL_OUT_FUNCTION(dq, dynamicgraph::Vector)
Definition: admittance-controller-end-effector.cpp:210
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::move
void move(const int &id, const double &xFinal, const double &time)
Definition: nd-trajectory-generator.cpp:621
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::playTrajectoryFile
void playTrajectoryFile(const std::string &fileName)
Definition: nd-trajectory-generator.cpp:357
sot_talos_balance.test.script_test_end_effector.x
x
Definition: script_test_end_effector.py:10
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::m_currentTrajGen
std::vector< parametriccurves::AbstractCurve< double, dynamicgraph::sot::Vector1d > * > m_currentTrajGen
status of the component
Definition: nd-trajectory-generator.hh:180
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::startLinearChirp
void startLinearChirp(const int &id, const double &xFinal, const double &f0, const double &f1, const double &time)
Definition: nd-trajectory-generator.cpp:572
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::JTG_MIN_JERK
@ JTG_MIN_JERK
Definition: nd-trajectory-generator.hh:160
dynamicgraph::sot::talos_balance::math::Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: fwd.hh:36
sot_talos_balance.test.appli_admittance_single_joint.dt
dt
Definition: appli_admittance_single_joint.py:17
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::NdTrajectoryGenerator
NdTrajectoryGenerator(const std::string &name)
Definition: nd-trajectory-generator.cpp:48
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::m_dt
double m_dt
true if it is the first iteration, false otherwise
Definition: nd-trajectory-generator.hh:171
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::JTG_STOP
@ JTG_STOP
Definition: nd-trajectory-generator.hh:158
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::m_noTrajGen
std::vector< parametriccurves::Constant< double, 1 > * > m_noTrajGen
Definition: nd-trajectory-generator.hh:181
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::JTG_SINUSOID
@ JTG_SINUSOID
Definition: nd-trajectory-generator.hh:159
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::m_t
double m_t
control loop time step.
Definition: nd-trajectory-generator.hh:172
DOUBLE_INF
#define DOUBLE_INF
Definition: nd-trajectory-generator.cpp:36
dynamicgraph::sot::talos_balance::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(AdmittanceControllerEndEffector, "AdmittanceControllerEndEffector")
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::m_n
unsigned int m_n
current control loop time.
Definition: nd-trajectory-generator.hh:173
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::m_sinTrajGen
std::vector< parametriccurves::InfiniteSinusoid< double, 1 > * > m_sinTrajGen
Definition: nd-trajectory-generator.hh:183
dynamicgraph::sot::talos_balance::EntityClassName
AdmittanceControllerEndEffector EntityClassName
Definition: admittance-controller-end-effector.cpp:46
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::startConstAcc
void startConstAcc(const int &id, const double &xFinal, const double &time)
Definition: nd-trajectory-generator.cpp:542
sot_talos_balance.test.appli_dcm_zmp_control.name
name
Definition: appli_dcm_zmp_control.py:298
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::m_textFileTrajGen
parametriccurves::TextFile< double, Eigen::Dynamic > * m_textFileTrajGen
Definition: nd-trajectory-generator.hh:188
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::JTG_TEXT_FILE
@ JTG_TEXT_FILE
Definition: nd-trajectory-generator.hh:164
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::m_splineReady
bool m_splineReady
last iter index
Definition: nd-trajectory-generator.hh:175
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::m_status
std::vector< JTG_Status > m_status
true if the spline has been successfully loaded.
Definition: nd-trajectory-generator.hh:177
dynamicgraph::sot::talos_balance::NdTrajectoryGenerator::stop
void stop(const int &id)
Definition: nd-trajectory-generator.cpp:667