-
Notifications
You must be signed in to change notification settings - Fork 17
/
GetMeasurement.cpp
355 lines (315 loc) · 10.5 KB
/
GetMeasurement.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
/*
* 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 "WBToolbox/Block/GetMeasurement.h"
#include "WBToolbox/Base/Configuration.h"
#include "WBToolbox/Base/RobotInterface.h"
#include <BlockFactory/Core/BlockInformation.h>
#include <BlockFactory/Core/Log.h>
#include <BlockFactory/Core/Parameter.h>
#include <BlockFactory/Core/Parameters.h>
#include <BlockFactory/Core/Signal.h>
#include <yarp/dev/ICurrentControl.h>
#include <yarp/dev/IEncoders.h>
#include <yarp/dev/IMotorEncoders.h>
#include <yarp/dev/IPWMControl.h>
#include <yarp/dev/ITorqueControl.h>
#include <cmath>
#include <ostream>
#include <tuple>
#include <vector>
using namespace wbt::block;
using namespace blockfactory::core;
// INDICES: PARAMETERS, INPUTS, OUTPUT
// ===================================
enum ParamIndex
{
Bias = wbt::base::WBBlock::NumberOfParameters - 1,
MeasType
};
enum OutputIndex
{
Measurement = 0,
};
// BLOCK PIMPL
// ===========
class GetMeasurement::impl
{
public:
enum class MeasuredType
{
// Joint Measurements
JOINT_POS,
JOINT_VEL,
JOINT_ACC,
JOINT_TORQUE,
// Motor measurements
MOTOR_POS,
MOTOR_VEL,
MOTOR_ACC,
MOTOR_CURRENT,
MOTOR_PWM
};
std::vector<double> measurement;
MeasuredType measuredType;
static void deg2rad(std::vector<double>& v)
{
const double Deg2Rad = M_PI / 180.0;
for (auto& element : v) {
element *= Deg2Rad;
}
}
};
// BLOCK CLASS
// ===========
GetMeasurement::GetMeasurement()
: pImpl{new impl()}
{}
GetMeasurement::~GetMeasurement() = default;
unsigned GetMeasurement::numberOfParameters()
{
return WBBlock::numberOfParameters() + 1;
}
bool GetMeasurement::parseParameters(BlockInformation* blockInfo)
{
const ParameterMetadata measTypeMetadata(
ParameterType::STRING, ParamIndex::MeasType, 1, 1, "MeasuredType");
if (!blockInfo->addParameterMetadata(measTypeMetadata)) {
bfError << "Failed to store parameters metadata.";
return false;
}
return blockInfo->parseParameters(m_parameters);
}
bool GetMeasurement::configureSizeAndPorts(BlockInformation* blockInfo)
{
if (!WBBlock::configureSizeAndPorts(blockInfo)) {
return false;
}
// Get the DoFs
const int dofs = getRobotInterface()->getConfiguration().getNumberOfDoFs();
// INPUTS
// ======
//
// No inputs
//
// OUTPUTS
// =======
//
// 1) Vector with the information asked (1xDoFs)
//
const bool ok = blockInfo->setPortsInfo(
{
// Inputs
},
{
// Outputs
{OutputIndex::Measurement, Port::Dimensions{dofs}, Port::DataType::DOUBLE},
});
if (!ok) {
bfError << "Failed to configure input / output ports.";
return false;
}
return true;
}
bool GetMeasurement::initialize(BlockInformation* blockInfo)
{
if (!WBBlock::initialize(blockInfo)) {
return false;
}
// PARAMETERS
// ==========
if (!GetMeasurement::parseParameters(blockInfo)) {
bfError << "Failed to parse parameters.";
return false;
}
// Read the measured type
std::string measuredType;
if (!m_parameters.getParameter("MeasuredType", measuredType)) {
bfError << "Could not read measured type parameter.";
return false;
}
// Set the measured type
if (measuredType == "Joints Position") {
pImpl->measuredType = impl::MeasuredType::JOINT_POS;
}
else if (measuredType == "Joints Velocity") {
pImpl->measuredType = impl::MeasuredType::JOINT_VEL;
}
else if (measuredType == "Joints Acceleration") {
pImpl->measuredType = impl::MeasuredType::JOINT_ACC;
}
else if (measuredType == "Joints Torque") {
pImpl->measuredType = impl::MeasuredType::JOINT_TORQUE;
}
else if (measuredType == "Motor Position") {
pImpl->measuredType = impl::MeasuredType::MOTOR_POS;
}
else if (measuredType == "Motor Velocity") {
pImpl->measuredType = impl::MeasuredType::MOTOR_VEL;
}
else if (measuredType == "Motor Acceleration") {
pImpl->measuredType = impl::MeasuredType::MOTOR_ACC;
}
else if (measuredType == "Motor Current") {
pImpl->measuredType = impl::MeasuredType::MOTOR_CURRENT;
}
else if (measuredType == "Motor PWM") {
pImpl->measuredType = impl::MeasuredType::MOTOR_PWM;
}
else {
bfError << "Measurement not supported.";
return false;
}
// CLASS INITIALIZATION
// ====================
// Get the DoFs
const auto dofs = getRobotInterface()->getConfiguration().getNumberOfDoFs();
// Initialize the size of the output vector
pImpl->measurement.resize(dofs);
return true;
}
bool GetMeasurement::terminate(const BlockInformation* blockInfo)
{
return WBBlock::terminate(blockInfo);
}
bool GetMeasurement::output(const BlockInformation* blockInfo)
{
// Get the RobotInterface
const auto robotInterface = getRobotInterface();
bool ok;
switch (pImpl->measuredType) {
//
// JOINT MEASUREMENTS
// ==================
//
case impl::MeasuredType::JOINT_POS: {
// Get the interface
yarp::dev::IEncoders* iEncoders = nullptr;
if (!robotInterface->getInterface(iEncoders) || !iEncoders) {
bfError << "Failed to get IEncoders interface.";
return false;
}
// Get the measurement
ok = iEncoders->getEncoders(pImpl->measurement.data());
GetMeasurement::impl::deg2rad(pImpl->measurement);
break;
}
case impl::MeasuredType::JOINT_VEL: {
// Get the interface
yarp::dev::IEncoders* iEncoders = nullptr;
if (!robotInterface->getInterface(iEncoders) || !iEncoders) {
bfError << "Failed to get IEncoders interface.";
return false;
}
// Get the measurement
ok = iEncoders->getEncoderSpeeds(pImpl->measurement.data());
GetMeasurement::impl::deg2rad(pImpl->measurement);
break;
}
case impl::MeasuredType::JOINT_ACC: {
// Get the interface
yarp::dev::IEncoders* iEncoders = nullptr;
if (!robotInterface->getInterface(iEncoders) || !iEncoders) {
bfError << "Failed to get IEncoders interface.";
return false;
}
// Get the measurement
ok = iEncoders->getEncoderAccelerations(pImpl->measurement.data());
GetMeasurement::impl::deg2rad(pImpl->measurement);
break;
}
case impl::MeasuredType::JOINT_TORQUE: {
// Get the interface
yarp::dev::ITorqueControl* iTorqueControl = nullptr;
if (!robotInterface->getInterface(iTorqueControl) || !iTorqueControl) {
bfError << "Failed to get ITorqueControl interface.";
return false;
}
// Get the measurement
ok = iTorqueControl->getTorques(pImpl->measurement.data());
break;
}
//
// MOTOR MEASUREMENTS
// ==================
//
case impl::MeasuredType::MOTOR_POS: {
// Get the interface
yarp::dev::IMotorEncoders* iMotorEncoders = nullptr;
if (!robotInterface->getInterface(iMotorEncoders) || !iMotorEncoders) {
bfError << "Failed to get IMotorEncoders interface.";
return false;
}
// Get the measurement
ok = iMotorEncoders->getMotorEncoders(pImpl->measurement.data());
GetMeasurement::impl::deg2rad(pImpl->measurement);
break;
}
case impl::MeasuredType::MOTOR_VEL: {
// Get the interface
yarp::dev::IMotorEncoders* iMotorEncoders = nullptr;
if (!robotInterface->getInterface(iMotorEncoders) || !iMotorEncoders) {
bfError << "Failed to get IMotorEncoders interface.";
return false;
}
// Get the measurement
ok = iMotorEncoders->getMotorEncoderSpeeds(pImpl->measurement.data());
GetMeasurement::impl::deg2rad(pImpl->measurement);
break;
}
case impl::MeasuredType::MOTOR_ACC: {
// Get the interface
yarp::dev::IMotorEncoders* iMotorEncoders = nullptr;
if (!robotInterface->getInterface(iMotorEncoders) || !iMotorEncoders) {
bfError << "Failed to get IMotorEncoders interface.";
return false;
}
// Get the measurement
ok = iMotorEncoders->getMotorEncoderAccelerations(pImpl->measurement.data());
GetMeasurement::impl::deg2rad(pImpl->measurement);
break;
}
case impl::MeasuredType::MOTOR_CURRENT: {
// Get the interface
yarp::dev::ICurrentControl* iCurrentControl = nullptr;
if (!robotInterface->getInterface(iCurrentControl) || !iCurrentControl) {
bfError << "Failed to get ICurrentControl interface.";
return false;
}
// Get the measurement
ok = iCurrentControl->getCurrents(pImpl->measurement.data());
break;
}
case impl::MeasuredType::MOTOR_PWM: {
// Get the interface
yarp::dev::IPWMControl* iPWMControl = nullptr;
if (!robotInterface->getInterface(iPWMControl) || !iPWMControl) {
bfError << "Failed to get IPWMControl interface.";
return false;
}
// Get the measurement
ok = iPWMControl->getDutyCycles(pImpl->measurement.data());
break;
}
}
if (!ok) {
bfError << "Failed to get measurement.";
return false;
}
// Get the output signal
OutputSignalPtr output = blockInfo->getOutputPortSignal(OutputIndex::Measurement);
if (!output) {
bfError << "Output signal not valid.";
return false;
}
// Fill the output buffer
if (!output->setBuffer(pImpl->measurement.data(), output->getWidth())) {
bfError << "Failed to set output buffer.";
return false;
}
return true;
}