-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathMinimumJerkTrajectoryGenerator.cpp
360 lines (294 loc) · 10.9 KB
/
MinimumJerkTrajectoryGenerator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Copyright (C) 2018 Istituto Italiano di Tecnologia (IIT)
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* GNU Lesser General Public License v2.1 or any later version.
*/
#include "MinimumJerkTrajectoryGenerator.h"
#include "Core/BlockInformation.h"
#include "Core/Log.h"
#include "Core/Parameter.h"
#include "Core/Parameters.h"
#include "Core/Signal.h"
#include <iCub/ctrl/minJerkCtrl.h>
#include <yarp/sig/Vector.h>
#include <cmath>
#include <ostream>
#include <tuple>
using namespace wbt;
// INDICES: PARAMETERS, INPUTS, OUTPUT
// ===================================
enum ParamIndex
{
Bias = Block::NumberOfParameters - 1,
SampleTime,
SettlingTime,
FirstDerivative,
SecondDerivative,
ReadInitialValue,
ExtSettlingTime,
ResetOnChange
};
enum InputIndex
{
InputSignal = 0,
// Other optional inputs
};
static int InputIndex_InitialValue = InputIndex::InputSignal;
static int InputIndex_ExtSettlingTime = InputIndex::InputSignal;
enum OutputIndex
{
FilteredSignal = 0,
// Other optional outputs
};
static int OutputIndex_FirstDer = OutputIndex::FilteredSignal;
static int OutputIndex_SecondDer = OutputIndex::FilteredSignal;
// BLOCK PIMPL
// ===========
class MinimumJerkTrajectoryGenerator::impl
{
public:
std::unique_ptr<iCub::ctrl::minJerkTrajGen> generator;
double previousSettlingTime;
bool firstRun = true;
bool readInitialValue = false;
bool readExternalSettlingTime = false;
bool resetOnSettlingTimeChange = false;
bool computeFirstDerivative = false;
bool computeSecondDerivative = false;
yarp::sig::Vector initialValues;
yarp::sig::Vector reference;
};
// BLOCK CLASS
// ===========
MinimumJerkTrajectoryGenerator::MinimumJerkTrajectoryGenerator()
: pImpl{new impl()}
{}
MinimumJerkTrajectoryGenerator::~MinimumJerkTrajectoryGenerator() = default;
unsigned MinimumJerkTrajectoryGenerator::numberOfParameters()
{
return Block::numberOfParameters() + 7;
}
bool MinimumJerkTrajectoryGenerator::parseParameters(BlockInformation* blockInfo)
{
const std::vector<ParameterMetadata> metadata{
{ParameterType::DOUBLE, ParamIndex::SampleTime, 1, 1, "SampleTime"},
{ParameterType::DOUBLE, ParamIndex::SettlingTime, 1, 1, "SettlingTime"},
{ParameterType::BOOL, ParamIndex::FirstDerivative, 1, 1, "ComputeFirstDerivative"},
{ParameterType::BOOL, ParamIndex::SecondDerivative, 1, 1, "ComputeSecondDerivative"},
{ParameterType::BOOL, ParamIndex::ReadInitialValue, 1, 1, "ReadInitialValue"},
{ParameterType::BOOL, ParamIndex::ExtSettlingTime, 1, 1, "ReadExternalSettlingTime"},
{ParameterType::BOOL, ParamIndex::ResetOnChange, 1, 1, "ResetOnSettlingTimeChange"}};
for (const auto& md : metadata) {
if (!blockInfo->addParameterMetadata(md)) {
wbtError << "Failed to store parameter metadata";
return false;
}
}
return blockInfo->parseParameters(m_parameters);
}
bool MinimumJerkTrajectoryGenerator::configureSizeAndPorts(BlockInformation* blockInfo)
{
// PARAMETERS
// ==========
if (!MinimumJerkTrajectoryGenerator::parseParameters(blockInfo)) {
wbtError << "Failed to parse parameters.";
return false;
}
bool computeFirstDerivative = false;
bool computeSecondDerivative = false;
bool readInitialValue = false;
bool readExternalSettlingTime = false;
bool ok = true;
ok = ok && m_parameters.getParameter("ComputeFirstDerivative", computeFirstDerivative);
ok = ok && m_parameters.getParameter("ComputeSecondDerivative", computeSecondDerivative);
ok = ok && m_parameters.getParameter("ReadInitialValue", readInitialValue);
ok = ok && m_parameters.getParameter("ReadExternalSettlingTime", readExternalSettlingTime);
if (!ok) {
wbtError << "Failed to get parameters after their parsing.";
return false;
}
// INPUTS
// ======
//
// 1) The reference signal (1xn)
// 2) The optional initial conditions (1xn)
// 3) The optional settling time
//
// OUTPUTS
// =======
//
// 1) The calculated trajectory (1xn)
// 2) The optional 1st derivative (1xn)
// 3) The optional 2nd derivative (1xn)
//
// Optional inputs indices
if (readInitialValue) {
InputIndex_InitialValue = InputIndex::InputSignal + 1;
}
if (readExternalSettlingTime) {
InputIndex_ExtSettlingTime = InputIndex_InitialValue + 1;
}
// Optional output indices
if (computeFirstDerivative) {
OutputIndex_FirstDer = OutputIndex::FilteredSignal + 1;
}
if (computeSecondDerivative) {
OutputIndex_SecondDer = OutputIndex_FirstDer + 1;
}
BlockInformation::IOData ioData;
ioData.input.emplace_back(
InputIndex::InputSignal, std::vector<int>{Signal::DynamicSize}, DataType::DOUBLE);
ioData.output.emplace_back(
OutputIndex::FilteredSignal, std::vector<int>{Signal::DynamicSize}, DataType::DOUBLE);
// Handle optional inputs
if (readInitialValue) {
ioData.input.emplace_back(InputIndex_InitialValue, std::vector<int>{1}, DataType::DOUBLE);
}
if (readExternalSettlingTime) {
ioData.input.emplace_back(
InputIndex_ExtSettlingTime, std::vector<int>{Signal::DynamicSize}, DataType::DOUBLE);
}
// Handle optional outputs
if (computeFirstDerivative) {
ioData.output.emplace_back(
OutputIndex_FirstDer, std::vector<int>{Signal::DynamicSize}, DataType::DOUBLE);
}
if (computeSecondDerivative) {
ioData.output.emplace_back(
OutputIndex_SecondDer, std::vector<int>{Signal::DynamicSize}, DataType::DOUBLE);
}
if (!blockInfo->setIOPortsData(ioData)) {
wbtError << "Failed to configure input / output ports.";
return false;
}
return true;
}
bool MinimumJerkTrajectoryGenerator::initialize(BlockInformation* blockInfo)
{
if (!Block::initialize(blockInfo)) {
return false;
}
// PARAMETERS
// ==========
if (!MinimumJerkTrajectoryGenerator::parseParameters(blockInfo)) {
wbtError << "Failed to parse parameters.";
return false;
}
double sampleTime = 0;
double settlingTime = 0;
bool ok = true;
ok = ok && m_parameters.getParameter("SampleTime", sampleTime);
ok = ok && m_parameters.getParameter("SettlingTime", settlingTime);
ok = ok && m_parameters.getParameter("ComputeFirstDerivative", pImpl->computeFirstDerivative);
ok = ok && m_parameters.getParameter("ComputeSecondDerivative", pImpl->computeSecondDerivative);
ok = ok && m_parameters.getParameter("ReadInitialValue", pImpl->readInitialValue);
ok = ok
&& m_parameters.getParameter("ReadExternalSettlingTime", pImpl->readExternalSettlingTime);
ok =
ok
&& m_parameters.getParameter("ResetOnSettlingTimeChange", pImpl->resetOnSettlingTimeChange);
if (!ok) {
wbtError << "Failed to get parameters after their parsing.";
return false;
}
// CLASS INITIALIZATION
// ====================
// Get the input port width
const auto inputPortWidth = blockInfo->getInputPortWidth(InputIndex::InputSignal);
pImpl->previousSettlingTime = settlingTime;
pImpl->initialValues.resize(inputPortWidth);
pImpl->reference.resize(inputPortWidth);
pImpl->generator = std::unique_ptr<iCub::ctrl::minJerkTrajGen>(
new iCub::ctrl::minJerkTrajGen(inputPortWidth, sampleTime, settlingTime));
pImpl->firstRun = true;
return true;
}
bool MinimumJerkTrajectoryGenerator::output(const BlockInformation* blockInfo)
{
if (pImpl->readExternalSettlingTime) {
InputSignalPtr externalTimeSignal =
blockInfo->getInputPortSignal(InputIndex_ExtSettlingTime);
if (!externalTimeSignal) {
wbtError << "Input signal not valid.";
return false;
}
const double externalTime = externalTimeSignal->get<double>(0);
if (std::abs(pImpl->previousSettlingTime - externalTime) > 1e-5) {
pImpl->previousSettlingTime = externalTime;
pImpl->generator->setT(externalTime);
if (pImpl->resetOnSettlingTimeChange && !pImpl->firstRun) {
pImpl->generator->init(pImpl->generator->getPos());
}
}
}
if (pImpl->firstRun) {
pImpl->firstRun = false;
InputSignalPtr initialValuesSignal = blockInfo->getInputPortSignal(InputIndex_InitialValue);
if (!initialValuesSignal) {
wbtError << "Input signal not valid.";
return false;
}
for (unsigned i = 0; i < initialValuesSignal->getWidth(); ++i) {
pImpl->initialValues[i] = initialValuesSignal->get<double>(i);
}
pImpl->generator->init(pImpl->initialValues);
}
// Input signal
// ------------
InputSignalPtr referencesSignal = blockInfo->getInputPortSignal(InputIndex::InputSignal);
if (!referencesSignal) {
wbtError << "Input signal not valid.";
return false;
}
for (unsigned i = 0; i < referencesSignal->getWidth(); ++i) {
pImpl->reference[i] = referencesSignal->get<double>(i);
}
pImpl->generator->computeNextValues(pImpl->reference);
const yarp::sig::Vector& signal = pImpl->generator->getPos();
// Output signal
// -------------
OutputSignalPtr outputSignal = blockInfo->getOutputPortSignal(OutputIndex::FilteredSignal);
if (!outputSignal) {
wbtError << "Output signal not valid.";
return false;
}
if (!outputSignal->setBuffer(signal.data(), signal.size())) {
wbtError << "Failed to set output buffer.";
return false;
}
// First derivative
// ----------------
if (pImpl->computeFirstDerivative) {
const yarp::sig::Vector& derivative = pImpl->generator->getVel();
OutputSignalPtr firstDerivativeSignal =
blockInfo->getOutputPortSignal(OutputIndex_FirstDer);
if (!firstDerivativeSignal) {
wbtError << "Output signal not valid.";
return false;
}
if (!firstDerivativeSignal->setBuffer(derivative.data(),
firstDerivativeSignal->getWidth())) {
wbtError << "Failed to set output buffer.";
return false;
}
}
// Second derivative
// -----------------
if (pImpl->computeSecondDerivative) {
const yarp::sig::Vector& derivative = pImpl->generator->getAcc();
OutputSignalPtr secondDerivativeSignal =
blockInfo->getOutputPortSignal(OutputIndex_SecondDer);
if (!secondDerivativeSignal) {
wbtError << "Output signal not valid.";
return false;
}
if (!secondDerivativeSignal->setBuffer(derivative.data(),
secondDerivativeSignal->getWidth())) {
wbtError << "Failed to set output buffer.";
return false;
}
}
return true;
}