-
Notifications
You must be signed in to change notification settings - Fork 33
/
YarpRobotControl.cpp
560 lines (453 loc) · 20.7 KB
/
YarpRobotControl.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/**
* @file YarpRobotControl.cpp
* @authors Giulio Romualdi
* @copyright 2020 Istituto Italiano di Tecnologia (IIT). This software may be modified and
* distributed under the terms of the GNU Lesser General Public License v2.1 or any later version.
*/
#include <cmath>
#include <unordered_map>
#include <yarp/dev/IAxisInfo.h>
#include <yarp/dev/IControlMode.h>
#include <yarp/dev/IEncodersTimed.h>
#include <yarp/dev/IPositionControl.h>
#include <yarp/dev/IPositionDirect.h>
#include <yarp/dev/ITorqueControl.h>
#include <yarp/dev/IVelocityControl.h>
#include <yarp/dev/PolyDriver.h>
#include <yarp/os/Time.h>
#include <BipedalLocomotion/RobotInterface/YarpRobotControl.h>
using namespace BipedalLocomotion::RobotInterface;
struct YarpRobotControl::Impl
{
/**
* JointsControlValuesAndMode contains the information regarding the desired joint values and
* the control mode for each joint.
*/
struct JointsControlValuesAndMode
{
/** Set containing the indices related to each control mode*/
std::unordered_map<IRobotControl::ControlMode, std::vector<int>> index;
/** Set containing the desired joint values related to each control mode. In case of
* Position, PositionDirect and Velocity the joint values are expressed in deg or deg/s */
std::unordered_map<IRobotControl::ControlMode, Eigen::VectorXd> value;
};
std::shared_ptr<yarp::dev::PolyDriver> robotDevice; /**< PolyDriver */
// YARP Interfaces exposed by the RemoteControlBoardRemapper
yarp::dev::IEncodersTimed* encodersInterface{nullptr}; /**< Encorders interface. */
yarp::dev::IPositionDirect* positionDirectInterface{nullptr}; /**< Direct position control
interface. */
yarp::dev::IPositionControl* positionInterface{nullptr}; /**< Position control interface. */
yarp::dev::IVelocityControl* velocityInterface{nullptr}; /**< Velocity control interface. */
yarp::dev::ITorqueControl* torqueInterface{nullptr}; /**< Torque control interface. */
yarp::dev::IControlMode* controlModeInterface{nullptr}; /**< Control mode interface. */
yarp::dev::IAxisInfo* axisInfoInterface{nullptr}; /**< Axis info interface. */
std::size_t actuatedDOFs; /**< Number of the actuated DoFs. */
Eigen::VectorXd positionFeedback; /**< Current joint position [rad]. */
std::vector<IRobotControl::ControlMode> controlModes; /**< Vector containing the map between the
joint and the current control mode */
std::vector<yarp::conf::vocab32_t> controlModesYarp; /**< Vector containing the map between the
joint and the current yarp control mode */
std::vector<std::string> axesName; /**< List containing the mapping between the joints index and
the their name */
JointsControlValuesAndMode desiredJointValuesAndMode; /**< Struct containing the information
regarding the desired joint value and
the control mode */
double positioningDuration{0.0}; /**< Duration of the trajectory generated when the joint is
controlled in position mode */
double startPositionControlInstant{0.0}; /**< Initial time instant of the trajectory generated
when the joint is controlled in position mode */
double positioningTolerance{0.0}; /**< Max Admissible error for position control joint [rad] */
double positionDirectMaxAdmissibleError{0.0}; /**< Max admissible error for position direct
control joint [rad] */
static IRobotControl::ControlMode
YarpControlModeToControlMode(const yarp::conf::vocab32_t& controlModeYarp)
{
switch (controlModeYarp)
{
case VOCAB_CM_POSITION:
return IRobotControl::ControlMode::Position;
case VOCAB_CM_POSITION_DIRECT:
return IRobotControl::ControlMode::PositionDirect;
case VOCAB_CM_VELOCITY:
return IRobotControl::ControlMode::Velocity;
case VOCAB_CM_TORQUE:
return IRobotControl::ControlMode::Torque;
default:
return IRobotControl::ControlMode::Unknown;
}
}
static yarp::conf::vocab32_t
ControlModeToYarpControlMode(const IRobotControl::ControlMode& controlMode)
{
switch (controlMode)
{
case IRobotControl::ControlMode::Position:
return VOCAB_CM_POSITION;
case IRobotControl::ControlMode::PositionDirect:
return VOCAB_CM_POSITION_DIRECT;
case IRobotControl::ControlMode::Velocity:
return VOCAB_CM_VELOCITY;
case IRobotControl::ControlMode::Torque:
return VOCAB_CM_TORQUE;
default:
return VOCAB_CM_UNKNOWN;
}
}
bool getControlModes()
{
constexpr std::string_view errorPrefix = "[YarpRobotControl::Impl::getControlModes] ";
if (this->controlModeInterface == nullptr)
{
std::cerr << errorPrefix << "The control mode I/F is not ready." << std::endl;
return false;
}
if (!this->controlModeInterface->getControlModes(this->controlModesYarp.data()))
{
std::cerr << errorPrefix << "Error reading the control mode." << std::endl;
return false;
}
for (std::size_t i = 0; i < this->actuatedDOFs; i++)
{
this->controlModes[i] = YarpControlModeToControlMode(this->controlModesYarp[i]);
}
return true;
}
bool setControlModes(const std::vector<IRobotControl::ControlMode>& controlModes)
{
constexpr std::string_view errorPrefix = "[YarpRobotControl::Impl::setControlModes] ";
if (this->controlModeInterface == nullptr)
{
std::cerr << errorPrefix << "The control mode I/F is not ready." << std::endl;
return false;
}
// clear all the stored control modes
this->desiredJointValuesAndMode.index[IRobotControl::ControlMode::Position].clear();
this->desiredJointValuesAndMode.index[IRobotControl::ControlMode::PositionDirect].clear();
this->desiredJointValuesAndMode.index[IRobotControl::ControlMode::Velocity].clear();
this->desiredJointValuesAndMode.index[IRobotControl::ControlMode::Torque].clear();
for (std::size_t i = 0; i < this->actuatedDOFs; i++)
{
// convert the control mode into Yarp control mode
this->controlModesYarp[i] = ControlModeToYarpControlMode(controlModes[i]);
// store the joint associated to a specific control mode
this->desiredJointValuesAndMode.index[controlModes[i]].push_back(i);
}
// set the control mode
if (!this->controlModeInterface->setControlModes(this->controlModesYarp.data()))
{
std::cerr << errorPrefix << "Error settings the control mode." << std::endl;
return false;
}
// resize the desired joint value vector associated to each control mode
for (const auto& [mode, indeces] : this->desiredJointValuesAndMode.index)
{
this->desiredJointValuesAndMode.value[mode].resize(indeces.size());
}
return true;
}
bool getJointPos()
{
constexpr std::string_view errorPrefix = "[YarpRobotControl::Impl::getJointPos] ";
if (this->encodersInterface == nullptr)
{
std::cerr << errorPrefix << "The encoder I/F is not ready." << std::endl;
return false;
}
if (!this->encodersInterface->getEncoders(this->positionFeedback.data()))
{
std::cerr << errorPrefix << "Error reading encoders." << std::endl;
return false;
}
// convert the joint position in radians
this->positionFeedback *= M_PI / 180.0;
return true;
}
bool setDriver(std::shared_ptr<yarp::dev::PolyDriver> robotDevice)
{
constexpr std::string_view errorPrefix = "[YarpRobotControl::Impl::setDriver] ";
if (robotDevice == nullptr)
{
std::cerr << errorPrefix
<< "The robotDevice is pointing to an non initialized memory."
<< std::endl;
return false;
}
// obtain the interfaces
if (!robotDevice->view(encodersInterface) || encodersInterface == nullptr)
{
std::cerr << errorPrefix << "Cannot load the IEncodersTimed interface." << std::endl;
return false;
}
if (!robotDevice->view(positionInterface) || positionInterface == nullptr)
{
std::cerr << errorPrefix << "Cannot load the IPositionControl interface." << std::endl;
return false;
}
if (!robotDevice->view(positionDirectInterface) || positionDirectInterface == nullptr)
{
std::cerr << errorPrefix << "Cannot load the IPositionDirect interface." << std::endl;
return false;
}
if (!robotDevice->view(velocityInterface) || velocityInterface == nullptr)
{
std::cerr << errorPrefix << "Cannot load the IVelocityInterface interface."
<< std::endl;
return false;
}
if (!robotDevice->view(torqueInterface) || torqueInterface == nullptr)
{
std::cerr << errorPrefix << "Cannot load the ITorqueInterface interface." << std::endl;
return false;
}
if (!robotDevice->view(controlModeInterface) || controlModeInterface == nullptr)
{
std::cerr << errorPrefix << "Cannot load the IControlMode interface." << std::endl;
return false;
}
if (!robotDevice->view(axisInfoInterface) || axisInfoInterface == nullptr)
{
std::cerr << errorPrefix << "Cannot load the IAxisInfo interface." << std::endl;
return false;
}
// get the number of degree of freedom
int dofs = 0;
if (!encodersInterface->getAxes(&dofs))
{
std::cerr << errorPrefix << "Cannot get the actuated DoFs." << std::endl;
return false;
}
this->actuatedDOFs = dofs;
// resize vector
this->positionFeedback.resize(this->actuatedDOFs);
this->controlModes.resize(this->actuatedDOFs);
this->controlModesYarp.resize(this->actuatedDOFs);
this->axesName.resize(this->actuatedDOFs);
// populate the axesName vector
for (int i = 0; i < this->actuatedDOFs; i++)
{
this->axisInfoInterface->getAxisName(i, this->axesName[i]);
}
// store the polydriver
this->robotDevice = robotDevice;
return this->getControlModes();
}
/**
* Return the the worst position error for the joint controlled in position direct.
* The first value is the index while the second is the error in radians.
*/
std::pair<int, double> getWorstPositionDirectError(Eigen::Ref<const Eigen::VectorXd> desiredJointValues,
Eigen::Ref<const Eigen::VectorXd> jointPositions) const
{
// clear the std::pair
std::pair<int, double> worstError{0, 0.0};
for (int i = 0; i < this->actuatedDOFs; i++)
{
// we are interested only if the joint is in PositionDirect
if (this->controlModes[i] == IRobotControl::ControlMode::PositionDirect)
{
const double jointError = std::abs(jointPositions[i] - desiredJointValues[i]);
if (jointError > worstError.second)
{
worstError.first = i;
worstError.second = jointError;
}
}
}
return worstError;
}
std::function<bool(const int, const int*, const double*)>
control(const IRobotControl::ControlMode& mode)
{
assert(mode != IRobotControl::ControlMode::Unknown);
switch (mode)
{
case IRobotControl::ControlMode::Position:
return [&](const int nJoints, const int* joints, const double* refs) -> bool {
return this->positionInterface->positionMove(nJoints, joints, refs);
};
case IRobotControl::ControlMode::PositionDirect:
return [&](const int nJoints, const int* joints, const double* refs) -> bool {
return this->positionDirectInterface->setPositions(nJoints, joints, refs);
};
case IRobotControl::ControlMode::Velocity:
return [&](const int nJoints, const int* joints, const double* refs) -> bool {
return this->velocityInterface->velocityMove(nJoints, joints, refs);
};
case IRobotControl::ControlMode::Torque:
return [&](const int nJoints, const int* joints, const double* refs) -> bool {
return this->torqueInterface->setRefTorques(nJoints, joints, refs);
};
default:
return nullptr;
}
return nullptr;
}
bool setReferences(Eigen::Ref<const Eigen::VectorXd> jointValues)
{
constexpr std::string_view errorPrefix = "[YarpRobotControl::Impl::setReferences] ";
if(!this->getJointPos())
{
std::cerr << errorPrefix << "Unable to get the joint position." << std::endl;
return false;
}
const auto worstError = this->getWorstPositionDirectError(jointValues,
this->positionFeedback);
if (worstError.second > this->positionDirectMaxAdmissibleError)
{
std::cerr << errorPrefix << "The worst error between the current and the "
<< "desired position of the joint named '" << this->axesName[worstError.first]
<< "' is greater than " << this->positionDirectMaxAdmissibleError
<< " rad. Error = " << worstError.second << " rad." << std::endl;
return false;
}
for (const auto& [mode, indeces] : this->desiredJointValuesAndMode.index)
{
// if indeces vector is empty no joint is controlled with this specific control mode
if (indeces.empty())
continue;
if (mode == IRobotControl::ControlMode::Unknown)
{
std::string error = " The following joints does not have a specified control "
"mode: ";
for (const auto& index : indeces)
error += "'" + this->axesName[index] + "' ";
std::cerr << errorPrefix << error << ". Please set a feasible control mode."
<< std::endl;
return false;
} else if (mode == IRobotControl::ControlMode::Position)
{
std::vector<double> refSpeeds(indeces.size());
for (int i = 0; i < indeces.size(); i++)
{
const auto jointError = std::abs(jointValues[indeces[i]]
- this->positionFeedback[indeces[i]]);
constexpr double scaling = 180 / M_PI;
constexpr double maxVelocityInDegPerSeconds = 3.0;
refSpeeds[i] = std::max(maxVelocityInDegPerSeconds,
scaling * (jointError / this->positioningDuration));
this->positionInterface->setRefSpeeds(indeces.size(),
indeces.data(),
refSpeeds.data());
}
this->startPositionControlInstant = yarp::os::Time::now();
}
// Yarp wants the quantities in degrees
double scaling = 180 / M_PI;
if (mode == ControlMode::Torque)
scaling = 1;
for (int i = 0; i < indeces.size(); i++)
this->desiredJointValuesAndMode.value[mode][i] = scaling * jointValues[indeces[i]];
if (!this->control(mode)(indeces.size(),
indeces.data(),
this->desiredJointValuesAndMode.value[mode].data()))
{
std::cerr << errorPrefix << "Unable to set the desired joint values." << std::endl;
return false;
}
}
return true;
}
};
YarpRobotControl::YarpRobotControl()
: m_pimpl(std::make_unique<Impl>())
{
}
YarpRobotControl::~YarpRobotControl() = default;
bool YarpRobotControl::setDriver(std::shared_ptr<yarp::dev::PolyDriver> robotDevice)
{
return m_pimpl->setDriver(robotDevice);
}
bool YarpRobotControl::initialize(std::weak_ptr<ParametersHandler::IParametersHandler> handler)
{
constexpr std::string_view errorPrefix = "[YarpRobotControl::initialize] ";
auto ptr = handler.lock();
if (ptr == nullptr)
{
std::cerr << errorPrefix << "The handler is not pointing to an already initialized memory."
<< std ::endl;
return false;
}
bool ok = ptr->getParameter("positioning_duration", m_pimpl->positioningDuration);
ok = ok && ptr->getParameter("positioning_tolerance", m_pimpl->positioningTolerance);
ok = ok && ptr->getParameter("position_direct_max_admissible_error",
m_pimpl->positionDirectMaxAdmissibleError);
return ok;
}
bool YarpRobotControl::setReferences(Eigen::Ref<const Eigen::VectorXd> jointValues,
const std::vector<IRobotControl::ControlMode>& controlModes)
{
if (controlModes != m_pimpl->controlModes)
{
m_pimpl->controlModes = controlModes;
if (!m_pimpl->setControlModes(m_pimpl->controlModes))
{
std::cerr << "[YarpRobotControl::setReferences] Unable to switch in "
"position-direct control mode."
<< std::endl;
return false;
}
}
return m_pimpl->setReferences(jointValues);
}
bool YarpRobotControl::setReferences(Eigen::Ref<const Eigen::VectorXd> desiredJointValues,
const IRobotControl::ControlMode& mode)
{
// check if all the joints are controlled in the desired control mode
if (!std::all_of(m_pimpl->controlModes.begin(),
m_pimpl->controlModes.end(),
[&mode](const auto& m) { return m == mode; }))
{
std::fill(m_pimpl->controlModes.begin(), m_pimpl->controlModes.end(), mode);
if (!m_pimpl->setControlModes(m_pimpl->controlModes))
{
std::cerr << "[YarpRobotControl::setReferences] Unable to the desired control mode."
<< std::endl;
return false;
}
}
return m_pimpl->setReferences(desiredJointValues);
}
bool YarpRobotControl::checkMotionDone(bool& motionDone,
bool& isTimeExpired,
std::vector<std::pair<std::string, double>>& info)
{
constexpr std::string_view errorPrefix = "[YarpRobotControl::checkMotionDone] ";
if (!m_pimpl->positionInterface->checkMotionDone(&motionDone))
{
std::cerr << errorPrefix
<< "Unable to check if the motion is terminated from the Yarp interface."
<< std::endl;
return false;
}
if (!m_pimpl->getJointPos())
{
std::cerr << errorPrefix << "Unable to get the joint position." << std::endl;
return false;
}
info.clear();
const auto& jointPositionControlIndex
= m_pimpl->desiredJointValuesAndMode.index[ControlMode::Position];
const auto& jointPositionControlDesiredValue
= m_pimpl->desiredJointValuesAndMode.value[ControlMode::Position];
for (int i = 0; i < jointPositionControlIndex.size(); i++)
{
const double desiredJointPosRad = jointPositionControlDesiredValue[i] * M_PI / 180;
const double error = std::abs(desiredJointPosRad
- m_pimpl->positionFeedback[jointPositionControlIndex[i]]);
if (error > m_pimpl->positioningTolerance)
{
info.push_back({m_pimpl->axesName[jointPositionControlIndex[i]], error});
}
}
const double now = yarp::os::Time::now();
constexpr double timeTolerance = 1.0;
if (now - m_pimpl->startPositionControlInstant > m_pimpl->positioningDuration + timeTolerance)
{
isTimeExpired = true;
} else
{
isTimeExpired = false;
}
motionDone = motionDone && info.empty();
return true;
}