cubic-interpolation.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2012 LAAS-CNRS
3 //
4 // Author: Florent Lamiraux
5 //
6 
8 
11 #include <dynamic-graph/command.h>
12 #include <dynamic-graph/factory.h>
13 
14 namespace dynamicgraph {
15 namespace sot {
16 namespace tools {
17 DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CubicInterpolation, "CubicInterpolation");
19  : Entity(name),
20  soutSOUT_("CubicInterpolation(" + name + ")::output(vector)::sout"),
21  soutdotSOUT_("CubicInterpolation(" + name + ")::output(vector)::soutdot"),
22  initSIN_(NULL, "CubicInterpolation(" + name + ")::input(vector)::init"),
23  goalSIN_(NULL, "CubicInterpolation(" + name + ")::input(vector)::goal"),
24  startTime_(0),
25  samplingPeriod_(0.),
26  state_(0),
27  p0_(),
28  p1_(),
29  p2_(),
30  p3_() {
36  boost::bind(&CubicInterpolation::computeSout, this, _1, _2));
38  boost::bind(&CubicInterpolation::computeSoutdot, this, _1, _2));
39  std::string docstring;
40  docstring =
41  " Set sampling period of control discretization.\n"
42  "\n"
43  " Input:\n"
44  " - a floating point value.\n"
45  "\n";
46  addCommand("setSamplingPeriod",
48  *this, &CubicInterpolation::setSamplingPeriod, docstring));
49  docstring =
50  " Start tracking.\n"
51  "\n"
52  " Input\n"
53  " - duration of the motion.\n"
54  "\n"
55  "\n Read init and goal signals, compute output trajectory and"
56  " start\n"
57  "tracking.\n";
59  *this, &CubicInterpolation::start, docstring));
60  docstring =
61  " Reset interpolation before calling start again\n"
62  "\n"
63  " After the end of an interpolation, goal signal is copied into\n"
64  " sout signal. Calling reset make the entity copy init signal into\n"
65  " sout signal.\n";
67  *this, &CubicInterpolation::reset, docstring));
68 }
69 
71 
72 std::string CubicInterpolation::getDocString() const {
73  std::string doc =
74  "Perform a cubic interpolation in between two vectors.\n"
75  "\n"
76  " Initial pose is given by signal 'init', Target position is given"
77  " by signal\n"
78  " 'goal'. Interpolation is performed with zero velocities at start"
79  " and goal\n"
80  " positions.\n";
81  return doc;
82 }
83 
85 
87  double t;
88  switch (state_) {
89  case 0:
90  sout = initSIN_.accessCopy();
91  break;
92  case 1:
93  t = (double)(inTime - startTime_) * samplingPeriod_;
94  sout = p0_ + (p1_ + (p2_ + p3_ * t) * t) * t;
95  if (t >= duration_) {
96  state_ = 2;
97  }
98  break;
99  case 2:
100  sout = goalSIN_.accessCopy();
101  default:
102  break;
103  }
104  return sout;
105 }
106 
108  const sigtime_t& inTime) {
109  soutdot.resize(initSIN_.accessCopy().size());
110  double t;
111  switch (state_) {
112  case 0:
113  soutdot.setZero();
114  break;
115  case 1:
116  t = (double)(inTime - startTime_) * samplingPeriod_;
117  soutdot = p1_ + (p2_ * 2 + p3_ * (3 * t)) * t;
118  if (t >= duration_) {
119  state_ = 2;
120  }
121  break;
122  case 2:
123  soutdot.setZero();
124  default:
125  break;
126  }
127  return soutdot;
128 }
129 
130 void CubicInterpolation::setSamplingPeriod(const double& period) {
131  samplingPeriod_ = period;
132 }
133 
134 void CubicInterpolation::start(const double& duration) { doStart(duration); }
135 
136 void CubicInterpolation::doStart(const double& duration) {
137  // Check that sampling period has been initialized
138  if (samplingPeriod_ <= 0)
140  "CubicInterpolation: samplingPeriod should"
141  " be positive. Are you sure you did\n"
142  "initialize it?");
143  if (state_ == 0) {
144  duration_ = duration;
145  startTime_ = soutSOUT_.getTime();
146  double T = duration;
147  // Initial position
148  p0_ = initSIN_.accessCopy();
149  // Initial velocity
150  p1_.resize(p0_.size());
151  p1_.fill(0.);
152  // Goal position
153  Vector P_T;
154  P_T = goalSIN_.accessCopy();
155  // Final velocity
156  Vector D_T(P_T.size());
157  D_T.fill(0.);
158  p2_ = (D_T + p1_ * 2) * (-1. / T) + (P_T - p0_) * (3. / (T * T));
159  p3_ = (P_T - p0_) * (-2 / (T * T * T)) + (p1_ + D_T) * (1. / (T * T));
160  state_ = 1;
161  }
162 }
163 } // namespace tools
164 } // namespace sot
165 } // namespace dynamicgraph
dynamicgraph::sot::tools::CubicInterpolation::p1_
Vector p1_
Definition: cubic-interpolation.hh:50
dynamicgraph::sot::tools::CubicInterpolation::setSamplingPeriod
void setSamplingPeriod(const double &period)
Set sampling period of control discretization.
Definition: cubic-interpolation.cc:130
dynamicgraph::sot::tools::CubicInterpolation::goalSIN_
dynamicgraph::SignalPtr< Vector, sigtime_t > goalSIN_
Definition: cubic-interpolation.hh:38
dynamicgraph::SignalPtr::accessCopy
virtual const T & accessCopy() const
dynamicgraph::sot::ExceptionSignal
T
int T
dynamicgraph::sot::tools::CubicInterpolation::samplingPeriod_
double samplingPeriod_
Definition: cubic-interpolation.hh:44
dynamicgraph
dynamicgraph::sot::tools::CubicInterpolation::p3_
Vector p3_
Definition: cubic-interpolation.hh:52
dynamicgraph::Entity
dynamicgraph::sot::tools::CubicInterpolation::computeSoutdot
Vector & computeSoutdot(Vector &sout, const sigtime_t &inTime)
Definition: cubic-interpolation.cc:107
dynamicgraph::sot::tools::CubicInterpolation::startTime_
sigtime_t startTime_
Definition: cubic-interpolation.hh:43
dynamicgraph::Signal::setFunction
virtual void setFunction(boost::function2< T &, T &, Time > t, Mutex *mutexref=NULL)
dynamicgraph::sot::tools::CubicInterpolation::p2_
Vector p2_
Definition: cubic-interpolation.hh:51
dynamicgraph::sot::ExceptionSignal::NOT_INITIALIZED
NOT_INITIALIZED
dynamicgraph::sot::tools::CubicInterpolation::~CubicInterpolation
virtual ~CubicInterpolation()
Definition: cubic-interpolation.cc:70
dynamicgraph::command::makeCommandVoid0
CommandVoid0< E > * makeCommandVoid0(E &entity, boost::function< void(E *)> function, const std::string &docString)
command-bind.h
dynamicgraph::sot::tools::CubicInterpolation::computeSout
Vector & computeSout(Vector &sout, const sigtime_t &inTime)
Definition: cubic-interpolation.cc:86
command-setter.h
dynamicgraph::sot::tools::CubicInterpolation::getDocString
virtual std::string getDocString() const
Documentation.
Definition: cubic-interpolation.cc:72
dynamicgraph::sot::tools::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CubicInterpolationSE3, "CubicInterpolationSE3")
dynamicgraph::sigtime_t
int64_t sigtime_t
dynamicgraph::sot::tools::CubicInterpolation::initSIN_
dynamicgraph::SignalPtr< Vector, sigtime_t > initSIN_
Definition: cubic-interpolation.hh:37
dynamicgraph::sot::tools::CubicInterpolation::soutSOUT_
dynamicgraph::Signal< Vector, sigtime_t > soutSOUT_
Definition: cubic-interpolation.hh:35
dynamicgraph::sot::tools::CubicInterpolation::state_
unsigned state_
Definition: cubic-interpolation.hh:47
dynamicgraph::sot::Vector
Vector
dynamicgraph::sot::tools::CubicInterpolation::start
void start(const double &duration)
Start tracking.
Definition: cubic-interpolation.cc:134
cubic-interpolation.hh
dynamicgraph::sot::double
double
dynamicgraph::sot::tools::CubicInterpolation::doStart
virtual void doStart(const double &duration)
Definition: cubic-interpolation.cc:136
dynamicgraph::sot::tools::CubicInterpolation::soutdotSOUT_
dynamicgraph::Signal< Vector, sigtime_t > soutdotSOUT_
Definition: cubic-interpolation.hh:36
dynamicgraph::Entity::addCommand
void addCommand(const std::string &name, command::Command *command)
t
Transform3f t
dynamicgraph::command::Setter
command.h
dynamicgraph::Entity::signalRegistration
void signalRegistration(const SignalArray< sigtime_t > &signals)
dynamicgraph::sot::tools::CubicInterpolation::p0_
Vector p0_
Definition: cubic-interpolation.hh:49
dynamicgraph::sot::tools::CubicInterpolation::reset
void reset()
Reset state to 0 before starting a new motion.
Definition: cubic-interpolation.cc:84
compile.name
name
Definition: compile.py:23
dynamicgraph::sot::tools::CubicInterpolation::duration_
double duration_
Definition: cubic-interpolation.hh:45
dynamicgraph::sot::tools::CubicInterpolation::CubicInterpolation
CubicInterpolation(const std::string &name)
Definition: cubic-interpolation.cc:18


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