GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/cubic-interpolation.cc Lines: 0 81 0.0 %
Date: 2023-01-29 11:05:01 Branches: 0 204 0.0 %

Line Branch Exec Source
1
//
2
// Copyright (C) 2012 LAAS-CNRS
3
//
4
// Author: Florent Lamiraux
5
//
6
7
#include "sot/tools/cubic-interpolation.hh"
8
9
#include <dynamic-graph/command-bind.h>
10
#include <dynamic-graph/command-setter.h>
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");
18
CubicInterpolation::CubicInterpolation(const std::string& name)
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_() {
31
  signalRegistration(soutSOUT_);
32
  signalRegistration(soutdotSOUT_);
33
  signalRegistration(initSIN_);
34
  signalRegistration(goalSIN_);
35
  soutSOUT_.setFunction(
36
      boost::bind(&CubicInterpolation::computeSout, this, _1, _2));
37
  soutdotSOUT_.setFunction(
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",
47
             new command::Setter<CubicInterpolation, double>(
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";
58
  addCommand("start", new command::Setter<CubicInterpolation, double>(
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";
66
  addCommand("reset", command::makeCommandVoid0(
67
                          *this, &CubicInterpolation::reset, docstring));
68
}
69
70
CubicInterpolation::~CubicInterpolation() {}
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
84
void CubicInterpolation::reset() { state_ = 0; }
85
86
Vector& CubicInterpolation::computeSout(Vector& sout, const int& inTime) {
87
  double t;
88
  switch (state_) {
89
    case 0:
90
      sout = initSIN_.accessCopy();
91
      break;
92
    case 1:
93
      t = (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
107
Vector& CubicInterpolation::computeSoutdot(Vector& soutdot, const int& inTime) {
108
  soutdot.resize(initSIN_.accessCopy().size());
109
  double t;
110
  switch (state_) {
111
    case 0:
112
      soutdot.setZero();
113
      break;
114
    case 1:
115
      t = (inTime - startTime_) * samplingPeriod_;
116
      soutdot = p1_ + (p2_ * 2 + p3_ * (3 * t)) * t;
117
      if (t >= duration_) {
118
        state_ = 2;
119
      }
120
      break;
121
    case 2:
122
      soutdot.setZero();
123
    default:
124
      break;
125
  }
126
  return soutdot;
127
}
128
129
void CubicInterpolation::setSamplingPeriod(const double& period) {
130
  samplingPeriod_ = period;
131
}
132
133
void CubicInterpolation::start(const double& duration) { doStart(duration); }
134
135
void CubicInterpolation::doStart(const double& duration) {
136
  // Check that sampling period has been initialized
137
  if (samplingPeriod_ <= 0)
138
    throw ExceptionSignal(ExceptionSignal::NOT_INITIALIZED,
139
                          "CubicInterpolation: samplingPeriod should"
140
                          " be positive. Are you sure you did\n"
141
                          "initialize it?");
142
  if (state_ == 0) {
143
    duration_ = duration;
144
    startTime_ = soutSOUT_.getTime();
145
    double T = duration;
146
    // Initial position
147
    p0_ = initSIN_.accessCopy();
148
    // Initial velocity
149
    p1_.resize(p0_.size());
150
    p1_.fill(0.);
151
    // Goal position
152
    Vector P_T;
153
    P_T = goalSIN_.accessCopy();
154
    // Final velocity
155
    Vector D_T(P_T.size());
156
    D_T.fill(0.);
157
    p2_ = (D_T + p1_ * 2) * (-1. / T) + (P_T - p0_) * (3. / (T * T));
158
    p3_ = (P_T - p0_) * (-2 / (T * T * T)) + (p1_ + D_T) * (1. / (T * T));
159
    state_ = 1;
160
  }
161
}
162
}  // namespace tools
163
}  // namespace sot
164
}  // namespace dynamicgraph