kinematic-planner.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2016 LAAS-CNRS
3 //
4 // Author: Rohan Budhiraja
5 //
6 
8 
10 #include <time.h>
11 
12 #include <fstream>
13 #include <sstream>
14 namespace dynamicgraph {
15 namespace sot {
16 namespace tools {
17 
19  : Entity(name)
20 /*
21  ,distToDrawerSIN(NULL,"KP("+name+")::input(double)::distanceToDrawer")
22  ,objectPositionInDrawerSIN(NULL,"KP("+name+")::input(double)::objectPosinDrawer")
23  ,trajectoryReadySINTERN(boost::bind(&KinematicPlanner::runKinematicPlanner,this,_1,_2),
24  distToDrawerSIN<<objectPositionInDrawerSIN,
25  "KP("+name+")::intern(dummy)::newtoneuler" )
26  ,upperBodyJointPositionSOUT(boost::bind(&KinematicPlanner::computeubJointPosition,this,_1,_2),
27  trajectoryReadySINTERN,
28  "KP("+name+")::output(vector)::upperBodyJointPosition" )
29  ,upperBodyJointVelocitySOUT(boost::bind(&KinematicPlanner::computeubJointVelocity,this,_1,_2),
30  trajectoryReadySINTERN,
31  "KP("+name+")::output(vector)::upperBodyJointVelocity" )
32  ,freeFlyerVelocitySOUT(boost::bind(&KinematicPlanner::computeffVelocity,this,_1,_2),
33  trajectoryReadySINTERN,
34  "KP("+name+")::output(vector)::freeFlyerVelocity" )
35 */
36 {
37  sotDEBUGIN(5);
38 
39  //
40  // Commands
41  //
42  // setParams
43  const std::string docstring1 =
44  "\n"
45  " Set the parameters for the Planner.\n"
46  "\n"
47  " Input:\n"
48  " - INT: Number of Time Steps in one 1.6 s \n"
49  " - INT: Number of periodic sources 1 \n"
50  " - INT: Number of periodic sources 2 \n"
51  " - STRING: Path of the directory from where parameters can be "
52  "loaded \n"
53  "\n";
55  *this, &KinematicPlanner::setParams, docstring1));
56 
57  // runKinematicPlanner
58  const std::string docstring2 =
59  "\n"
60  " Run the Planner to generate upper joint trajectories \n"
61  "\n"
62  " Input:\n"
63  " - none \n"
64  "\n";
65  addCommand("runKinematicPlanner",
67  *this,
68  (void(KinematicPlanner::*)(void)) &
70  docstring2));
71  parametersSet = false;
72 
73  // std::string dir = "/local/rbudhira/git/proyan/sot-tools/src/params2";
74  // setParams(320 , 5 , 4, dir);
75  // delaySources();
76  // blending();
77 
78  sotDEBUGOUT(5);
79 }
80 
82  sotDEBUGIN(15);
83 
84  for (std::list<SignalBase<int>*>::iterator iter = genericSignalRefs.begin();
85  iter != genericSignalRefs.end(); ++iter) {
86  SignalBase<int>* sigPtr = *iter;
87  delete sigPtr;
88  }
89  sotDEBUGOUT(15);
90 }
91 
92 namespace {
93 template <typename Derived>
94 void writeToFile(Eigen::DenseBase<Derived>& inMatrix, std::string& filePath) {
95  std::ofstream file(filePath.c_str());
96  if (file.is_open()) {
97  file << inMatrix;
98  }
99 }
100 } // namespace
101 
102 template <typename Derived>
103 void KinematicPlanner::read2DArray(std::string& fileName,
104  Eigen::DenseBase<Derived>& outArr) {
105  sotDEBUGIN(5);
106  std::ifstream fileStream(fileName.c_str());
107  std::string curLine;
108  double _dbl;
109  std::string _dblString;
110  int _row = 0;
111  while (std::getline(fileStream, curLine)) { //'\n' delimited rows
112  std::istringstream iss(curLine);
113  int _col = 0;
114  while (std::getline(iss, _dblString, '\t')) { // tab-delimited columns
115  _dbl = atof(_dblString.c_str());
116  outArr(_row, _col) = _dbl;
117  _col++;
118  }
119  _row++;
120  }
121  sotDEBUGOUT(5);
122  return;
123 }
124 
125 void KinematicPlanner::setParams(const double& _distanceToDrawer,
126  const double& _objectPositionInDrawer,
127  const std::string& dir) {
128  sotDEBUGIN(5);
129 
130  // Currently Fixed Parameters
131  // TODO: Are these variables?
132  // nTrajectories = 30;
133  nJoints = 16;
134  nGaitCycles = 4;
135 
136  nTimeSteps = 160; //_nTimeSteps; //160
137  nSources1 = 5; //_nSources1; //5
138  nSources2 = 4; //_nSources2; //4
139 
140  npSource.resize(nTimeSteps);
141  pSource1.resize(nTimeSteps, nSources1);
142  pSource2.resize(nTimeSteps, nSources2);
143 
144  pSourceDelayed1.resize(nJoints);
145  pSourceDelayed2.resize(nJoints);
146 
147  // Delays
148  pDelay1.resize(nJoints, nSources1);
149  pDelay2.resize(nJoints, nSources2);
150 
151  // Non Periodic Weights
153 
154  // Periodic Weights
155  wPeriodic1.resize(nSources1);
156  for (int i = 0; i < nSources1; i++)
157  wPeriodic1.at(i).resize(nJoints, nGaitCycles);
158 
159  wPeriodic2.resize(nSources2);
160  for (int i = 0; i < nSources2; i++)
161  wPeriodic2.at(i).resize(nJoints, nGaitCycles);
162 
163  // Mean joint angles
165 
166  loadSourceDelays(dir);
167  dynamicgraph::Vector goalsNew =
168  createSubGoals(_distanceToDrawer, _objectPositionInDrawer);
169  goalAdaption(goalsNew, dir);
170  delaySources();
171 
172  parametersSet = true;
173  sotDEBUGOUT(5);
174  return;
175 }
176 
177 void KinematicPlanner::loadTrainingParams(const std::string& dir,
179  dynamicgraph::Matrix& beta3,
180  Eigen::ArrayXd& mwwn, double& sigma2,
181  int& N, int& K) {
182  Eigen::Vector3d sigmaSqNK;
183  std::string fileName = dir + "/" + "sigmaSqNK";
184  read2DArray<Eigen::Vector3d>(fileName, sigmaSqNK);
185  assert(sigmaSqNK.cols() == 1);
186  assert(sigmaSqNK.rows() == 3);
187 
188  sigma2 = sigmaSqNK(0);
189  N = (int)sigmaSqNK(1);
190  K = (int)sigmaSqNK(2);
191 
192  fileName = dir + "/" + "q";
193  read2DArray<dynamicgraph::Matrix>(fileName, q);
194  assert(q.cols() == 2);
195  assert(q.rows() == N);
196 
197  fileName = dir + "/" + "mwwn";
198  read2DArray<Eigen::ArrayXd>(fileName, mwwn);
199  assert(mwwn.cols() == 1);
200  assert(mwwn.rows() == K);
201 
202  fileName = dir + "/" + "beta3";
203  read2DArray<dynamicgraph::Matrix>(fileName, beta3);
204  assert(beta3.cols() == K);
205  assert(beta3.rows() == N);
206 
207  return;
208 }
209 
210 void KinematicPlanner::loadSourceDelays(const std::string& dir) {
211  sotDEBUGIN(5);
212 
213  // assert(boost::filesystem::is_directory(dir));
214  // Load Source1
215  std::string fileName = dir + "/" + "pSource1";
216  read2DArray<Eigen::ArrayXXd>(fileName, pSource1);
217  assert(pSource1.cols() == nSources1);
218  assert(pSource1.rows() == nTimeSteps);
219 
220  // Load Source2
221  fileName = dir + "/" + "pSource2";
222  read2DArray<Eigen::ArrayXXd>(fileName, pSource2);
223  assert(pSource2.cols() == nSources2);
224  assert(pSource2.rows() == nTimeSteps);
225 
226  // Load Non-Periodic Source
227  fileName = dir + "/" + "npSource";
228  read2DArray<Eigen::ArrayXd>(fileName, npSource);
229  assert(npSource.cols() == 1);
230  assert(npSource.rows() == nTimeSteps);
231 
232  // Load Delays: Source1
233  fileName = dir + "/" + "pDelay1";
234  read2DArray<Eigen::ArrayXXd>(fileName, pDelay1);
235  assert(pDelay1.cols() == nSources1);
236  assert(pDelay1.rows() == nJoints);
237 
238  // Load Delays: Source2
239  fileName = dir + "/" + "pDelay2";
240  read2DArray<Eigen::ArrayXXd>(fileName, pDelay2);
241  assert(pDelay2.cols() == nSources2);
242  assert(pDelay2.rows() == nJoints);
243 
244  sotDEBUGOUT(5);
245  return;
246 }
247 
248 /*
249 
250 void KinematicPlanner::loadTrainingWeights(const std::string& dir) {
251 sotDEBUGIN(5);
252 //Load mean Joint Angles
253 fileName = dir+"/"+"mJointAngle";
254 read2DArray<Eigen::ArrayXXd>(fileName , mJointAngle);
255 assert(mJointAngle.cols() == nGaitCycles);
256 assert(mJointAngle.rows() == nTrajectories*nJoints);
257 
258 //Load Joint Weights: non periodic
259 fileName = dir+"/"+"wNonPeriodic";
260 read2DArray<Eigen::ArrayXXd>(fileName , wNonPeriodic);
261 assert(mJointAngle.cols() == nGaitCycles);
262 assert(mJointAngle.rows() == nTrajectories*nJoints);
263 
264 //Load Joint weights1:
265 fileName = dir+"/"+"wPeriodic1";
266 wPeriodic1.resize(nSources1);
267 std::string specificFile;
268 for (int cnt = 0; cnt< nSources1 ; cnt++){
269  std::ostringstream stm;
270  stm << cnt;
271  specificFile = fileName + "/" + stm.str();
272  read2DArray<Eigen::ArrayXXd>(specificFile, wPeriodic1.at(cnt));
273  assert(wPeriodic1.at(cnt).cols() == nGaitCycles);
274  assert(wPeriodic1.at(cnt).rows() == nTrajectories*nJoints);
275 }
276 //Load Joint weights2:
277 fileName = dir+"/"+"wPeriodic2";
278 wPeriodic2.resize(nSources2);
279 for (int cnt = 0; cnt< nSources2; cnt++){
280  std::ostringstream stm;
281  stm << cnt;
282  specificFile = fileName + "/" + stm.str();
283  read2DArray<Eigen::ArrayXXd>(specificFile, wPeriodic2.at(cnt));
284  assert(wPeriodic2.at(cnt).cols() == nGaitCycles);
285  assert(wPeriodic2.at(cnt).rows() == nTrajectories*nJoints);
286 }
287 
288 }
289 */
290 
292  // TODO: Confirm if this is HalfSpectrum or not
293  sotDEBUGIN(5);
294  pSourceDelayed1.clear();
295  pSourceDelayed2.clear();
296 
297  Eigen::FFT<double> fft;
298  double pi = std::acos(-1);
299  // Prepare for the Delay Exponential
300  Eigen::ArrayXcd V(nTimeSteps);
301  for (int cur_time = 0; cur_time < nTimeSteps; cur_time++) {
302  std::complex<double> pi_freq;
303  pi_freq.real(0);
304  pi_freq.imag(cur_time);
305  V(cur_time) = pi_freq;
306  }
307  V *= 2 * pi / nTimeSteps;
308 
309  // Do the FFT Source1
310  Eigen::ArrayXXcd pSourceInFreq(nTimeSteps, nSources1);
311  for (int cur_src = 0; cur_src < nSources1; cur_src++) {
312  pSourceInFreq.matrix().col(cur_src) =
313  fft.fwd(pSource1.matrix().col(cur_src));
314  }
315 
316  // Do the Delay
317  for (int cJ = 0; cJ < nJoints; cJ++) {
318  Eigen::ArrayXXd pSourceDelayedJoint(nTimeSteps, nSources1);
319  for (int cur_src = 0; cur_src < nSources1; cur_src++) {
320  // DelayedFt = Source(w) * exp^(V*pDelay1(joint, source))
321  const Eigen::ArrayXcd delayedFT =
322  pSourceInFreq.col(cur_src) * (V * pDelay1(cJ, cur_src)).exp();
323  //(V*pDelay1(cT*nJoints+cJ,
324  // cur_src)).unaryExpr<std::complex(*)(std::complex)>(&std::exp);
325  // FFT Inv
326  pSourceDelayedJoint.matrix().col(cur_src) = fft.inv(delayedFT.matrix());
327  }
328  pSourceDelayed1.push_back(pSourceDelayedJoint);
329  }
330 
331  // Do the FFT Source 2
332  pSourceInFreq.resize(Eigen::NoChange, nSources2);
333  for (int cur_src = 0; cur_src < nSources2; cur_src++) {
334  pSourceInFreq.matrix().col(cur_src) =
335  fft.fwd(pSource2.matrix().col(cur_src));
336  }
337 
338  // Do the Delay
339  for (int cJ = 0; cJ < nJoints; cJ++) {
340  Eigen::ArrayXXd pSourceDelayedJoint(nTimeSteps, nSources2);
341  for (int cur_src = 0; cur_src < nSources2; cur_src++) {
342  // DelayedFt = Source(w) * exp^(V*pDelay1(joint, source))
343  const Eigen::ArrayXcd delayedFT =
344  pSourceInFreq.col(cur_src) * (V * pDelay2(cJ, cur_src)).exp();
345  //(V*pDelay2(cT*nJoints+cJ,
346  // cur_src)).unaryExpr<std::complex(*)(std::complex)>(&std::exp);
347  // FFT Inv
348  pSourceDelayedJoint.matrix().col(cur_src) = fft.inv(delayedFT.matrix());
349  }
350  pSourceDelayed2.push_back(pSourceDelayedJoint);
351  }
352 
353  sotDEBUGOUT(5);
354  return;
355 }
356 
358  sotDEBUGIN(5);
359  Eigen::ArrayXd wCurrent;
360  Eigen::ArrayXd mJointAngleCurrent;
361 
362  double pi = std::acos(-1);
363  int halfTime = (int)nTimeSteps / 2;
364  bool specialInterpolatedWeight = false;
365 
366  Eigen::ArrayXXd allJointsOneGaitCycle(nTimeSteps, nJoints);
367  Eigen::ArrayXXd allJointsAllGaitCycle(nTimeSteps * nGaitCycles, nJoints);
368  allJointsOneGaitCycle.setZero();
369  allJointsAllGaitCycle.setZero();
370 
371  for (int cG = 0; cG < nGaitCycles; cG++) { // nGaitCycles = 4
372 
373  for (int t = 0; t < nTimeSteps; t++) { // nTimeSteps = 160
374  stdVectorofArrayXd currentWeight1;
375  stdVectorofArrayXd currentWeight2;
376  if (t < nTimeSteps / 10) {
377  // Matlab starts from 1. TODO:initial blending interpolation check
378  // Beginning of the step
379  if (cG != 0) {
380  specialInterpolatedWeight = true;
381  // Not for the first step
382  // non periodic source intermediate value
383  Eigen::ArrayXd wnpbeg =
384  (npSource(nTimeSteps - 1) * wNonPeriodic.col(cG - 1) +
385  npSource(0) * wNonPeriodic.col(cG)) /
386  2;
387  // interpolation weight
388  double ww = (std::sin(t / (nTimeSteps / 10) * pi / 2) + 1) / 2;
389  wCurrent =
390  (2 * ww - 1) * wNonPeriodic.col(cG) + (1 - (2 * ww - 1)) * wnpbeg;
391  mJointAngleCurrent =
392  ww * mJointAngle.col(cG) + (1 - ww) * mJointAngle.col(cG - 1);
393 
394  for (int cSrc1 = 0; cSrc1 < nSources1; cSrc1++) {
395  Eigen::ArrayXd intWeightCSrc =
396  ww * wPeriodic1.at(cSrc1).col(cG) +
397  (1 - ww) * wPeriodic1.at(cSrc1).col(cG - 1);
398  currentWeight1.push_back(intWeightCSrc);
399  }
400 
401  for (int cSrc2 = 0; cSrc2 < nSources2; cSrc2++) {
402  Eigen::ArrayXd intWeightCSrc =
403  ww * wPeriodic2.at(cSrc2).col(cG) +
404  (1 - ww) * wPeriodic2.at(cSrc2).col(cG - 1);
405  currentWeight2.push_back(intWeightCSrc);
406  }
407  }
408  }
409 
410  else if (t > nTimeSteps * 9 / 10) {
411  // End of Step
412  if (cG != nGaitCycles - 1) {
413  // Not for the last gait cycle
414  specialInterpolatedWeight = true;
415  Eigen::ArrayXd wnpEnd =
416  -(npSource(nTimeSteps - 1) * wNonPeriodic.col(cG) +
417  npSource(0) * wNonPeriodic.col(cG + 1)) /
418  2;
419  double ww =
420  (std::sin((t - nTimeSteps) / (nTimeSteps / 10) * pi / 2) + 1) / 2;
421  wCurrent = (1 - 2 * ww) * wNonPeriodic.col(cG) + 2 * ww * wnpEnd;
422  mJointAngleCurrent =
423  (1 - ww) * mJointAngle.col(cG) + ww * mJointAngle.col(cG + 1);
424 
425  for (int cSrc1 = 0; cSrc1 < nSources1; cSrc1++) {
426  Eigen::ArrayXd intWeightCSrc =
427  (1 - ww) * wPeriodic1.at(cSrc1).col(cG) +
428  ww * wPeriodic1.at(cSrc1).col(cG + 1);
429 
430  currentWeight1.push_back(intWeightCSrc);
431  }
432 
433  for (int cSrc2 = 0; cSrc2 < nSources2; cSrc2++) {
434  Eigen::ArrayXd intWeightCSrc =
435  (1 - ww) * wPeriodic2.at(cSrc2).col(cG) +
436  ww * wPeriodic2.at(cSrc2).col(cG + 1);
437  currentWeight2.push_back(intWeightCSrc);
438  }
439  } //(cG!=nGaitCycles-1)
440  } //(t>nTimeSteps*9/10)
441 
442  // Start Mixing all sources
443  for (int cJ = 0; cJ < nJoints; cJ++) {
444  if (specialInterpolatedWeight) {
445  for (int cSrc1 = 0; cSrc1 < nSources1; cSrc1++)
446  allJointsOneGaitCycle(t, cJ) +=
447  pSourceDelayed1.at(cJ)(t, cSrc1) * currentWeight1.at(cSrc1)(cJ);
448  for (int cSrc2 = 0; cSrc2 < nSources2; cSrc2++)
449  allJointsOneGaitCycle(t, cJ) +=
450  pSourceDelayed2.at(cJ)(t, cSrc2) * currentWeight2.at(cSrc2)(cJ);
451 
452  allJointsOneGaitCycle(t, cJ) += npSource(t) * wCurrent(cJ);
453  allJointsOneGaitCycle(t, cJ) += mJointAngleCurrent(cJ);
454 
455  } else {
456  for (int cSrc1 = 0; cSrc1 < nSources1; cSrc1++)
457  allJointsOneGaitCycle(t, cJ) +=
458  pSourceDelayed1.at(cJ)(t, cSrc1) * wPeriodic1.at(cSrc1)(cJ, cG);
459  for (int cSrc2 = 0; cSrc2 < nSources2; cSrc2++)
460  allJointsOneGaitCycle(t, cJ) +=
461  pSourceDelayed2.at(cJ)(t, cSrc2) * wPeriodic2.at(cSrc2)(cJ, cG);
462 
463  allJointsOneGaitCycle(t, cJ) += npSource(t) * wNonPeriodic(cJ, cG);
464  allJointsOneGaitCycle(t, cJ) += mJointAngle(cJ, cG);
465  }
466  }
467 
468  specialInterpolatedWeight = false;
469  } // Finish iterate through nTimeSteps
470 
471  if (cG == 0) {
472  // Smoothing First Gait Cycle beginning
473  for (int cJ = 0; cJ < nJoints; cJ++) {
474  allJointsOneGaitCycle.col(cJ).head(halfTime).reverseInPlace();
475  smoothEnds(allJointsOneGaitCycle.col(cJ).head(halfTime));
476  allJointsOneGaitCycle.col(cJ).head(halfTime).reverseInPlace();
477  }
478  } else if (cG == nGaitCycles - 1) {
479  // Smoothing Last Gait Cycle end
480  for (int cJ = 0; cJ < nJoints; cJ++) {
481  smoothEnds(allJointsOneGaitCycle.col(cJ).tail(halfTime));
482  }
483  }
484 
485  allJointsAllGaitCycle.middleRows(cG * nTimeSteps, nTimeSteps) =
486  allJointsOneGaitCycle;
487  allJointsOneGaitCycle.setZero();
488  } // Finish Iterate through gait cycles
489 
490  // Complete Set of Upperbody joints
491  Eigen::ArrayXXd allJointsAllGaitCycleNew(nTimeSteps * nGaitCycles, 58);
492 
493  // Time Stamps
494  allJointsAllGaitCycleNew.col(0) = Eigen::ArrayXd::LinSpaced(
496 
497  // Map calculated traj to joint kinematic chain.
498  allJointsAllGaitCycleNew.col(1) = allJointsAllGaitCycle.col(0); // XVel
499  allJointsAllGaitCycleNew.col(2) = allJointsAllGaitCycle.col(1); // YVel
500  allJointsAllGaitCycleNew.col(3).setZero(); // Yaw vel =0
501  allJointsAllGaitCycleNew.col(4) = allJointsAllGaitCycle.col(2); // Chest
502  allJointsAllGaitCycleNew.col(5) = allJointsAllGaitCycle.col(3); // Chest
503  allJointsAllGaitCycleNew.col(6).setZero(); // Head = 0
504  allJointsAllGaitCycleNew.col(7).setZero(); // Head = 0
505  allJointsAllGaitCycleNew.col(8) = allJointsAllGaitCycle.col(4); // LARM
506  allJointsAllGaitCycleNew.col(9) = allJointsAllGaitCycle.col(5); // LARM
507  allJointsAllGaitCycleNew.col(10) = allJointsAllGaitCycle.col(6); // LARM
508  allJointsAllGaitCycleNew.col(11) = allJointsAllGaitCycle.col(7); // LARM
509  allJointsAllGaitCycleNew.col(12) = allJointsAllGaitCycle.col(8); // LARM
510  allJointsAllGaitCycleNew.col(13) = allJointsAllGaitCycle.col(9); // LARM
511  allJointsAllGaitCycleNew.col(14).setZero(); // LARM = 10deg
512  allJointsAllGaitCycleNew.col(15) = allJointsAllGaitCycle.col(10); // RARM
513  allJointsAllGaitCycleNew.col(16) = allJointsAllGaitCycle.col(11); // RARM
514  allJointsAllGaitCycleNew.col(17) = allJointsAllGaitCycle.col(12); // RARM
515  allJointsAllGaitCycleNew.col(18) = allJointsAllGaitCycle.col(13); // RARM
516  allJointsAllGaitCycleNew.col(19) = allJointsAllGaitCycle.col(14); // RARM
517  allJointsAllGaitCycleNew.col(20) = allJointsAllGaitCycle.col(15); // RARM
518  allJointsAllGaitCycleNew.col(21).setZero(); // RARM = 10deg
519 
520  allJointsAllGaitCycleNew.middleCols<18>(4) *= 180 / pi; // Convert to Degrees
521  allJointsAllGaitCycleNew.col(14).setConstant(10); // LARM Final joint = 10deg
522  allJointsAllGaitCycleNew.col(21).setConstant(10); // RARM Final Joint = 10deg
523 
524  // Generate q_dot vectors
525  for (int i = 0; i < allJointsAllGaitCycleNew.rows() - 1; i++)
526  allJointsAllGaitCycleNew.row(i).segment<18>(22) =
527  allJointsAllGaitCycleNew.row(i + 1).segment<18>(4) -
528  allJointsAllGaitCycleNew.row(i).segment<18>(4);
529 
530  allJointsAllGaitCycleNew.row(nTimeSteps * nGaitCycles - 1).segment<18>(22) =
531  allJointsAllGaitCycleNew.row(nTimeSteps * nGaitCycles - 2)
532  .segment<18>(22);
533 
534  savitzkyGolayFilter(allJointsAllGaitCycleNew.leftCols<40>(), 6, 51);
535 
536  // Generate q_dot_dot vectors
537  for (int i = 0; i < allJointsAllGaitCycleNew.rows() - 1; i++)
538  allJointsAllGaitCycleNew.row(i).segment<18>(40) =
539  (allJointsAllGaitCycleNew.row(i + 1).segment<18>(22) -
540  allJointsAllGaitCycleNew.row(i).segment<18>(22)) *
541  200; // 200fps
542 
543  allJointsAllGaitCycleNew.row(nTimeSteps * nGaitCycles - 1).segment<18>(40) =
544  allJointsAllGaitCycleNew.row(nTimeSteps * nGaitCycles - 2)
545  .segment<18>(40);
546 }
547 
548 void KinematicPlanner::smoothEnds(Eigen::Ref<Eigen::ArrayXd> tr) {
549  // smoothends - takes 1d traj tr and enforces
550  // zero velocity and acceleration at the end
551  double pi = std::acos(-1);
552  int N = (int)tr.size();
553  Eigen::ArrayXd shiftedTr(N);
554  shiftedTr(0) = 0;
555  shiftedTr.tail(N - 1) = tr.head(N - 1);
556 
557  // tr=cumsum([tr(1); diff(tr).*(cos([0:N-2]'/(N-1)*pi)+1)/2]);
558  tr -= shiftedTr;
559  for (int i = 1; i < N; i++) {
560  tr(i) *= (std::cos((i - 1) / (N - 1) * pi) + 1) / 2;
561  tr(i) += tr(i - 1);
562  }
563  return;
564 }
565 
566 void KinematicPlanner::bSplineInterpolate(Eigen::ArrayXXd& tr, int factor) {
567  const int splDeg = 3;
568  const int splDim = 1; // Cubic Splines in 1 dimension
569  Eigen::MatrixXd trCopy = tr;
570 
571  tr.resize(nTimeSteps * nGaitCycles * factor, Eigen::NoChange);
572 
573  Eigen::RowVectorXd x_vec(nTimeSteps * nGaitCycles);
574  Eigen::RowVectorXd x_vec_new(nTimeSteps * nGaitCycles * factor);
575  for (int i = 0; i < nTimeSteps * nGaitCycles; i++)
576  x_vec(i) = i / (nTimeSteps * nGaitCycles);
577  for (int i = 0; i < nTimeSteps * nGaitCycles * factor; i++)
578  x_vec_new(i) = i / (nTimeSteps * nGaitCycles * factor);
579 
580  for (int cJ = 0; cJ < nJoints; cJ++) {
581  Eigen::Spline<double, splDim> spl =
582  Eigen::SplineFitting<Eigen::Spline<double, splDim> >::Interpolate(
583  trCopy.col(cJ).transpose(), splDeg, x_vec);
584  for (int t = 0; t < nTimeSteps * nGaitCycles * factor; t++)
585  tr(t, cJ) = spl(x_vec_new(t))(0);
586  }
587  return;
588 }
589 
591  const std::string& dir) {
593  // load for the first 2 steps
594 
595  //(periodic weights + non periodic weight + mean)*2 steps
596  // int K = nJoints*(nSources1+nSources2+2)*2;
597  { // local scope01
598 
599  const std::string dir01 = dir + "/adaption/01";
600  dynamicgraph::Matrix q(30, 2);
601  dynamicgraph::Matrix beta3(30, 288);
602  Eigen::ArrayXd mwwn(288);
603  double sigma2;
604  int N;
605  int K;
606 
607  loadTrainingParams(dir01, q, beta3, mwwn, sigma2, N, K);
608  Eigen::ArrayXd M(K);
609  Eigen::ArrayXd test_point(2);
610  test_point << goals(0), goals(1);
611  for (int k = 0; k < K; k++) {
612  Eigen::MatrixXd X(q.rows(), 3);
613  X.col(0) = q.col(0) - Eigen::MatrixXd::Ones(q.rows(), 1) * test_point(0);
614  X.col(1) = q.col(1) - Eigen::MatrixXd::Ones(q.rows(), 1) * test_point(1);
615  X.col(2) = Eigen::MatrixXd::Ones(q.rows(), 1);
616  Eigen::VectorXd _expdiag =
617  (-(((X * X.transpose()).diagonal().matrix()) / sigma2)).exp();
618  Eigen::MatrixXd Wkern = _expdiag.asDiagonal();
619  M(k) = (((X.transpose() * Wkern * X) * X.transpose() * Wkern).inverse() *
620  beta3.col(k))(q.cols());
621  }
622 
623  Eigen::ArrayXd wwn = M + mwwn;
624 
625  // 2 steps taken
626  // first two steps
627  for (int j = 0; j < 2; j++) {
628  for (int cs1 = 0; cs1 < nSources1; cs1++) {
629  wPeriodic1.at(cs1).col(j) = wwn.segment(
630  j * (nJoints * (nSources1 + nSources2 + 2)) + cs1 * nJoints,
631  nJoints);
632  }
633  for (int cs2 = 0; cs2 < nSources2; cs2++) {
634  wPeriodic2.at(cs2).col(j) =
635  wwn.segment(j * (nJoints * (nSources1 + nSources2 + 2)) +
636  nJoints * nSources1 + cs2 * nJoints,
637  nJoints);
638  }
639  wNonPeriodic.col(j) =
640  wwn.segment(j * (nJoints * (nSources1 + nSources2 + 2)) +
642  nJoints);
643 
644  mJointAngle.col(j) =
645  wwn.segment(j * (nJoints * (nSources1 + nSources2 + 2)) +
646  nJoints * (nSources1 + nSources2 + 1),
647  nJoints);
648  }
649  } // local scope01
650 
651  { // local scope3
652  // load for 3rd step
653  const std::string dir3 = dir + "/adaption/2";
654  dynamicgraph::Matrix q(30, 2);
655  dynamicgraph::Matrix beta3(30, 144);
656  Eigen::ArrayXd mwwn(144);
657  double sigma2;
658  int N;
659  int K;
660 
661  loadTrainingParams(dir3, q, beta3, mwwn, sigma2, N, K);
662 
663  //(periodic weights + non periodic weight + mean)*1 step = 3rd step
664  // int K = nJoints*(nSources1+nSources2+2);
665  Eigen::ArrayXd M(K);
666  Eigen::ArrayXd test_point(2);
667  test_point << goals(1), goals(2);
668  for (int k = 0; k < K; k++) {
669  Eigen::MatrixXd X(q.rows(), 3);
670  X.col(0) = q.col(0) - Eigen::MatrixXd::Ones(q.rows(), 1) * test_point(0);
671  X.col(1) = q.col(1) - Eigen::MatrixXd::Ones(q.rows(), 1) * test_point(1);
672  X.col(2) = Eigen::MatrixXd::Ones(q.rows(), 1);
673  Eigen::VectorXd _expdiag =
674  (-(((X * X.transpose()).diagonal().matrix()) / sigma2)).exp();
675  Eigen::MatrixXd Wkern = _expdiag.asDiagonal();
676  M(k) = (((X.transpose() * Wkern * X) * X.transpose() * Wkern).inverse() *
677  beta3.col(k))(q.cols());
678  }
679 
680  Eigen::ArrayXd wwn = M + mwwn;
681 
682  // 3rd step taken
683  // first two steps
684  for (int cs1 = 0; cs1 < nSources1; cs1++) {
685  wPeriodic1.at(cs1).col(2) = wwn.segment(cs1 * nJoints, nJoints);
686  }
687  for (int cs2 = 0; cs2 < nSources2; cs2++) {
688  wPeriodic2.at(cs2).col(2) =
689  wwn.segment(nJoints * nSources1 + cs2 * nJoints, nJoints);
690  }
691  wNonPeriodic.col(2) =
692  wwn.segment(nJoints * (nSources1 + nSources2), nJoints);
693  mJointAngle.col(2) =
694  wwn.segment(nJoints * (nSources1 + nSources2 + 1), nJoints);
695 
696  } // local scope3
697 
698  { // local scope4
699 
700  // load for 4th step
701  //(periodic weights + non periodic weight + mean)*1 step = 4th step
702  const std::string dir4 = dir + "/adaption/3";
703  dynamicgraph::Matrix q(30, 2);
704  dynamicgraph::Matrix beta3(30, 144);
705  Eigen::ArrayXd mwwn(144);
706  double sigma2;
707  int N;
708  int K;
709 
710  loadTrainingParams(dir4, q, beta3, mwwn, sigma2, N, K);
711  // int K = nJoints*(nSources1+nSources2+2);
712  Eigen::ArrayXd M(K);
713  Eigen::ArrayXd test_point(2);
714  test_point << goals(2), goals(3);
715 
716  for (int k = 0; k < K; k++) {
717  Eigen::MatrixXd X(q.rows(), 3);
718  X.col(0) = q.col(0) - Eigen::MatrixXd::Ones(q.rows(), 1) * test_point(0);
719  X.col(1) = q.col(1) - Eigen::MatrixXd::Ones(q.rows(), 1) * test_point(1);
720  X.col(2) = Eigen::MatrixXd::Ones(q.rows(), 1);
721  Eigen::VectorXd _expdiag =
722  (-(((X * X.transpose()).diagonal().matrix()) / sigma2)).exp();
723  Eigen::MatrixXd Wkern = _expdiag.asDiagonal();
724 
725  M(k) = (((X.transpose() * Wkern * X) * X.transpose() * Wkern).inverse() *
726  beta3.col(k))(q.cols());
727  }
728 
729  Eigen::ArrayXd wwn = M + mwwn;
730 
731  // 3rd step taken
732  for (int cs1 = 0; cs1 < nSources1; cs1++) {
733  wPeriodic1.at(cs1).col(3) = wwn.segment(cs1 * nJoints, nJoints);
734  }
735  for (int cs2 = 0; cs2 < nSources2; cs2++) {
736  wPeriodic2.at(cs2).col(3) =
737  wwn.segment(nJoints * nSources1 + cs2 * nJoints, nJoints);
738  }
739  wNonPeriodic.col(3) =
740  wwn.segment(nJoints * (nSources1 + nSources2), nJoints);
741  mJointAngle.col(3) =
742  wwn.segment(nJoints * (nSources1 + nSources2 + 1), nJoints);
743  } // local scope4
744 }
745 
747  Eigen::Ref<Eigen::ArrayXXd> allJointTraj, int polyOrder, int frameSize) {
748  assert(polyOrder < frameSize);
749  assert(frameSize <=
750  allJointTraj.rows()); // Length of the inputs is >= frameSize
751  assert(frameSize % 2 == 1); // frameSize must be odd
752 
753  // Calc the projection matrix
754 
755  // Create the vandermonde matrix
756  Eigen::ArrayXXd vanderArr(frameSize, polyOrder + 1);
757  vanderArr.col(0).setOnes();
758  vanderArr.col(1) = Eigen::ArrayXd::LinSpaced(frameSize, -(frameSize - 1) / 2,
759  (frameSize - 1) / 2);
760  for (int i = 2; i < polyOrder + 1; i++)
761  vanderArr.col(i) = vanderArr.col(i - 1) * vanderArr.col(1);
762  sotDEBUG(15) << "Printing the VanderArr" << std::endl;
763  sotDEBUG(15) << vanderArr << std::endl;
764 
765  // Cholesky factor and projection
766  Eigen::MatrixXd upperT =
767  (vanderArr.matrix().transpose() * vanderArr.matrix()).llt().matrixU();
768  sotDEBUG(15) << "Printing the Cholesky decomp" << std::endl;
769  sotDEBUG(15) << upperT << std::endl;
770  Eigen::MatrixXd A = vanderArr.matrix() * upperT.inverse();
771 
772  sotDEBUG(15) << "Printing A" << std::endl;
773  sotDEBUG(15) << A << std::endl;
774 
775  Eigen::MatrixXd proj = A * A.transpose();
776 
777  for (int cJ = 0; cJ < allJointTraj.cols(); cJ++) {
778  // transient on:
779  Eigen::VectorXd yit =
780  (proj.bottomRows((frameSize + 1) / 2) *
781  allJointTraj.matrix().col(cJ).head(frameSize).reverse())
782  .reverse();
783 
784  // steady state:
785  Eigen::MatrixXd xb(frameSize, allJointTraj.rows() - frameSize + 1);
786  for (int i = 0; i < allJointTraj.rows() - frameSize + 1; i++)
787  xb.col(i) = allJointTraj.col(cJ).segment(i, frameSize);
788  Eigen::VectorXd yss =
789  (proj.row((frameSize - 1) / 2) * xb.middleCols(1, xb.cols() - 2))
790  .transpose();
791 
792  // transient off:
793  Eigen::VectorXd yot =
794  (proj.topRows((frameSize + 1) / 2) *
795  allJointTraj.matrix().col(cJ).tail(frameSize).reverse())
796  .reverse();
797 
798  allJointTraj.col(cJ) << yit, yss, yot;
799  }
800 }
801 
803  dynamicgraph::Vector goalsNew(4);
804  goalsNew(3) = P;
805  goalsNew(1) = (D - 1.75) * 0.532 + 0.1;
806  goalsNew(2) = (D - goalsNew(1) - 1.64) / 2 + 0.636;
807  goalsNew(0) = (D - goalsNew(1) - 1.64) / 4 + 0.5;
808  return goalsNew;
809 }
810 
811 int& KinematicPlanner::runKinematicPlanner(int& dummy, int time) {
812  sotDEBUGIN(15);
813  assert(parametersSet && "Set Parameters via setParams first");
814  // double distanceToDrawer = distToDrawerSIN.access(time);
815  // double objectPositionInDrawer = objectPositionInDrawerSIN.access(time);
816  // dynamicgraph::Vector goalsNew =
817  // createSubGoals(distanceToDrawer,objectPositionInDrawer); delaySources();
818  blending();
819  return dummy;
820 }
821 
823 } // namespace tools
824 } // namespace sot
825 } // namespace dynamicgraph
dynamicgraph::sot::tools::KinematicPlanner::wNonPeriodic
Eigen::ArrayXXd wNonPeriodic
Definition: kinematic-planner.hh:81
N
N
V
V
dynamicgraph::sot::tools::KinematicPlanner::nJoints
int nJoints
Definition: kinematic-planner.hh:92
dynamicgraph::sot::tools::KinematicPlanner::pDelay2
Eigen::ArrayXXd pDelay2
Definition: kinematic-planner.hh:78
dynamicgraph::sot::tools::KinematicPlanner::setParams
void setParams(const double &_distanceToDrawer, const double &_objectPositionInDrawer, const std::string &dir)
Definition: kinematic-planner.cc:125
dynamicgraph
factor
exp
def exp(x)
dynamicgraph::sot::tools::KinematicPlanner::pSourceDelayed2
stdVectorofArrayXXd pSourceDelayed2
Definition: kinematic-planner.hh:75
i
int i
dynamicgraph::sot::tools::KinematicPlanner::bSplineInterpolate
void bSplineInterpolate(Eigen::ArrayXXd &tr, int factor)
Definition: kinematic-planner.cc:566
dynamicgraph::Entity
dynamicgraph::sot::tools::KinematicPlanner::smoothEnds
void smoothEnds(Eigen::Ref< Eigen::ArrayXd > tr)
Definition: kinematic-planner.cc:548
dynamicgraph::sot::tools::KinematicPlanner::wPeriodic1
stdVectorofArrayXXd wPeriodic1
Definition: kinematic-planner.hh:84
dynamicgraph::sot::tools::KinematicPlanner
Definition: kinematic-planner.hh:41
dynamicgraph::Matrix
Eigen::MatrixXd Matrix
dynamicgraph::sot::tools::KinematicPlanner::stdVectorofArrayXd
std::vector< Eigen::ArrayXd, Eigen::aligned_allocator< Eigen::ArrayXd > > stdVectorofArrayXd
Definition: kinematic-planner.hh:45
dynamicgraph::sot::tools::KinematicPlanner::blending
void blending()
Definition: kinematic-planner.cc:357
dynamicgraph::sot::tools::KinematicPlanner::pDelay1
Eigen::ArrayXXd pDelay1
Definition: kinematic-planner.hh:77
dynamicgraph::sot::tools::KinematicPlanner::mJointAngle
Eigen::ArrayXXd mJointAngle
Definition: kinematic-planner.hh:88
dynamicgraph::sot::tools::KinematicPlanner::goalAdaption
void goalAdaption(dynamicgraph::Vector &goals, const std::string &)
Definition: kinematic-planner.cc:590
dynamicgraph::sot::tools::KinematicPlanner::wPeriodic2
stdVectorofArrayXXd wPeriodic2
Definition: kinematic-planner.hh:85
dynamicgraph::command::makeCommandVoid0
CommandVoid0< E > * makeCommandVoid0(E &entity, boost::function< void(E *)> function, const std::string &docString)
command-bind.h
dynamicgraph::sot::tools::KinematicPlanner::nGaitCycles
int nGaitCycles
Definition: kinematic-planner.hh:93
sotDEBUGOUT
#define sotDEBUGOUT(level)
dynamicgraph::sot::tools::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CubicInterpolationSE3, "CubicInterpolationSE3")
P
P
dynamicgraph::sot::tools::KinematicPlanner::nSources2
int nSources2
Definition: kinematic-planner.hh:96
sotDEBUGIN
#define sotDEBUGIN(level)
D
D
inverse
void inverse(const Eigen::MatrixBase< MatrixIn > &m_in, const Eigen::MatrixBase< MatrixOut > &dest)
dummy
DummyClass dummy
M
M
dynamicgraph::sot::tools::KinematicPlanner::parametersSet
bool parametersSet
Definition: kinematic-planner.hh:120
q
q
dynamicgraph::sot::tools::KinematicPlanner::pSourceDelayed1
stdVectorofArrayXXd pSourceDelayed1
Definition: kinematic-planner.hh:74
dynamicgraph::Vector
Eigen::VectorXd Vector
dynamicgraph::sot::tools::KinematicPlanner::npSource
Eigen::ArrayXd npSource
Definition: kinematic-planner.hh:70
dynamicgraph::sot::tools::KinematicPlanner::pSource2
Eigen::ArrayXXd pSource2
Definition: kinematic-planner.hh:72
X
X
dynamicgraph::sot::tools::KinematicPlanner::runKinematicPlanner
int & runKinematicPlanner(int &dummy, int time)
Definition: kinematic-planner.cc:811
dynamicgraph::sot::tools::KinematicPlanner::genericSignalRefs
std::list< dynamicgraph::SignalBase< sigtime_t > * > genericSignalRefs
Definition: kinematic-planner.hh:98
dynamicgraph::sot::tools::KinematicPlanner::delaySources
void delaySources()
Definition: kinematic-planner.cc:291
dynamicgraph::sot::tools::KinematicPlanner::KinematicPlanner
KinematicPlanner(const std::string &name)
Definition: kinematic-planner.cc:18
dynamicgraph::sot::tools::KinematicPlanner::pSource1
Eigen::ArrayXXd pSource1
Definition: kinematic-planner.hh:71
dynamicgraph::sot::tools::KinematicPlanner::loadSourceDelays
void loadSourceDelays(const std::string &dir)
Definition: kinematic-planner.cc:210
dynamicgraph::sot::tools::KinematicPlanner::savitzkyGolayFilter
void savitzkyGolayFilter(Eigen::Ref< Eigen::ArrayXXd > allJointTraj, int polyOrder, int frameSize)
Definition: kinematic-planner.cc:746
dynamicgraph::sot::tools::KinematicPlanner::loadTrainingParams
void loadTrainingParams(const std::string &dir, dynamicgraph::Matrix &q, dynamicgraph::Matrix &beta3, Eigen::ArrayXd &mwwn, double &sigma2, int &N, int &K)
Definition: kinematic-planner.cc:177
dynamicgraph::sot::tools::KinematicPlanner::createSubGoals
dynamicgraph::Vector createSubGoals(double D, double P)
Definition: kinematic-planner.cc:802
dynamicgraph::Entity::addCommand
void addCommand(const std::string &name, command::Command *command)
t
Transform3f t
dynamicgraph::command::makeCommandVoid3
CommandVoid3< E, T1, T2, T3 > * makeCommandVoid3(E &entity, boost::function< void(E *, const T1 &, const T2 &, const T3 &)> function, const std::string &docString)
llt
llt
dynamicgraph::sot::tools::KinematicPlanner::~KinematicPlanner
virtual ~KinematicPlanner(void)
Definition: kinematic-planner.cc:81
kinematic-planner.hh
dynamicgraph::SignalBase< int >
A
A
dynamicgraph::sot::tools::KinematicPlanner::nSources1
int nSources1
Definition: kinematic-planner.hh:95
dynamicgraph::sot::tools::KinematicPlanner::nTimeSteps
int nTimeSteps
Definition: kinematic-planner.hh:94
compile.name
name
Definition: compile.py:23
dynamicgraph::sot::tools::KinematicPlanner::read2DArray
void read2DArray(std::string &fileName, Eigen::DenseBase< Derived > &outArr)
Definition: kinematic-planner.cc:103
sotDEBUG
#define sotDEBUG(level)


sot-tools
Author(s): Mehdi Benallegue, Francois Keith, Florent Lamiraux, Thomas Moulard, Olivier Stasse, Jorrit T'Hooft
autogenerated on Wed Aug 2 2023 02:35:13