-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDeviceDriverImpl.cpp
398 lines (330 loc) · 16.1 KB
/
DeviceDriverImpl.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
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
#include "KdlSolver.hpp"
#include <string>
#include <yarp/os/Bottle.h>
#include <yarp/os/Property.h>
#include <yarp/os/ResourceFinder.h>
#include <yarp/os/Value.h>
#include <yarp/sig/Matrix.h>
#include <kdl/frames.hpp>
#include <kdl/jntarray.hpp>
#include <kdl/joint.hpp>
#include <kdl/rigidbodyinertia.hpp>
#include <kdl/rotationalinertia.hpp>
#include <kdl/segment.hpp>
#include <kdl/chainfksolverpos_recursive.hpp>
#include <kdl/chainiksolverpos_lma.hpp>
#include <kdl/chainiksolverpos_nr_jl.hpp>
#include <kdl/chainiksolvervel_pinv.hpp>
#include <kdl/chainidsolver_recursive_newton_euler.hpp>
#include <Eigen/Core> // Eigen::Matrix
#include <ColorDebug.h>
#include "KinematicRepresentation.hpp"
#include "ConfigurationSelector.hpp"
#include "ChainIkSolverPos_ST.hpp"
#include "ChainIkSolverPos_ID.hpp"
// ------------------- DeviceDriver Related ------------------------------------
namespace
{
bool getMatrixFromProperties(const yarp::os::Searchable & options, const std::string & tag, yarp::sig::Matrix & H)
{
yarp::os::Bottle * bH = options.find(tag).asList();
if (!bH)
{
CD_WARNING("Unable to find tag %s.\n", tag.c_str());
return false;
}
int i = 0;
int j = 0;
H.zero();
for (int cnt = 0; cnt < bH->size() && cnt < H.rows() * H.cols(); cnt++)
{
H(i, j) = bH->get(cnt).asFloat64();
if (++j >= H.cols())
{
i++;
j = 0;
}
}
return true;
}
bool parseLmaFromBottle(const yarp::os::Bottle & b, Eigen::Matrix<double, 6, 1> & L)
{
if (b.size() != 6)
{
CD_WARNING("Wrong bottle size (expected: %d, was: %d).\n", 6, b.size());
return false;
}
for (int i = 0; i < b.size(); i++)
{
L(i) = b.get(i).asFloat64();
}
return true;
}
bool retrieveJointLimits(const yarp::os::Searchable & options, KDL::JntArray & qMin, KDL::JntArray & qMax)
{
int nrOfJoints = qMin.rows();
if (!options.check("mins") || !options.check("maxs"))
{
CD_ERROR("Missing 'mins' and/or 'maxs' option(s).\n");
return false;
}
yarp::os::Bottle * maxs = options.findGroup("maxs", "joint upper limits (meters or degrees)").get(1).asList();
yarp::os::Bottle * mins = options.findGroup("mins", "joint lower limits (meters or degrees)").get(1).asList();
if (maxs == YARP_NULLPTR || mins == YARP_NULLPTR)
{
CD_ERROR("Empty 'mins' and/or 'maxs' option(s)\n");
return false;
}
if (maxs->size() < nrOfJoints || mins->size() < nrOfJoints)
{
CD_ERROR("chain.getNrOfJoints (%d) > maxs.size() or mins.size() (%d, %d)\n", nrOfJoints, maxs->size(), mins->size());
return false;
}
for (int motor = 0; motor < nrOfJoints; motor++)
{
qMax(motor) = roboticslab::KinRepresentation::degToRad(maxs->get(motor).asFloat64());
qMin(motor) = roboticslab::KinRepresentation::degToRad(mins->get(motor).asFloat64());
if (qMin(motor) == qMax(motor))
{
CD_WARNING("qMin[%1$d] == qMax[%1$d] (%2$f)\n", motor, qMin(motor));
}
else if (qMin(motor) > qMax(motor))
{
CD_ERROR("qMin[%1$d] > qMax[%1$d] (%2$f > %3$f)\n", motor, qMin(motor), qMax(motor));
return false;
}
}
return true;
}
}
// -----------------------------------------------------------------------------
bool roboticslab::KdlSolver::open(yarp::os::Searchable& config)
{
CD_DEBUG("config: %s.\n", config.toString().c_str());
//-- kinematics
std::string kinematics = config.check("kinematics",yarp::os::Value(DEFAULT_KINEMATICS),"path to file with description of robot kinematics").asString();
CD_INFO("kinematics: %s [%s]\n", kinematics.c_str(),DEFAULT_KINEMATICS);
yarp::os::ResourceFinder rf;
rf.setVerbose(false);
rf.setDefaultContext("kinematics");
std::string kinematicsFullPath = rf.findFileByName(kinematics);
yarp::os::Property fullConfig;
fullConfig.fromConfigFile(kinematicsFullPath.c_str());
fullConfig.fromString(config.toString(),false); //-- Can override kinematics file contents.
fullConfig.setMonitor(config.getMonitor(), "KdlSolver");
CD_DEBUG("fullConfig: %s.\n", fullConfig.toString().c_str());
//-- numlinks
int numLinks = fullConfig.check("numLinks",yarp::os::Value(DEFAULT_NUM_LINKS),"chain number of segments").asInt32();
CD_INFO("numLinks: %d [%d]\n",numLinks,DEFAULT_NUM_LINKS);
//-- gravity
yarp::os::Value defaultGravityValue;
yarp::os::Bottle *defaultGravityBottle = defaultGravityValue.asList();
defaultGravityBottle->addFloat64(0);
defaultGravityBottle->addFloat64(0);
defaultGravityBottle->addFloat64(-9.81);
yarp::os::Value gravityValue = fullConfig.check("gravity", defaultGravityValue, "gravity vector (SI units)");
yarp::os::Bottle *gravityBottle = gravityValue.asList();
KDL::Vector gravity(gravityBottle->get(0).asFloat64(),gravityBottle->get(1).asFloat64(),gravityBottle->get(2).asFloat64());
CD_INFO("gravity: %s [%s]\n",gravityBottle->toString().c_str(),defaultGravityBottle->toString().c_str());
//-- H0
yarp::sig::Matrix defaultYmH0(4,4);
defaultYmH0.eye();
yarp::sig::Matrix ymH0(4,4);
std::string ymH0_str("H0");
if( ! getMatrixFromProperties(fullConfig, ymH0_str, ymH0))
{
ymH0 = defaultYmH0;
}
KDL::Vector kdlVec0(ymH0(0,3),ymH0(1,3),ymH0(2,3));
KDL::Rotation kdlRot0( ymH0(0,0),ymH0(0,1),ymH0(0,2),ymH0(1,0),ymH0(1,1),ymH0(1,2),ymH0(2,0),ymH0(2,1),ymH0(2,2));
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::None), KDL::Frame(kdlRot0,kdlVec0))); //-- H0 = Frame(kdlRot0,kdlVec0);
CD_INFO("H0:\n%s\n[%s]\n",ymH0.toString().c_str(),defaultYmH0.toString().c_str());
//-- links
for(int linkIndex=0;linkIndex<numLinks;linkIndex++)
{
std::string link("link_");
std::ostringstream s;
s << linkIndex;
link += s.str();
yarp::os::Bottle &bLink = fullConfig.findGroup(link);
if( ! bLink.isNull() ) {
//-- Kinematic
double linkOffset = bLink.check("offset",yarp::os::Value(0.0), "DH joint angle (degrees)").asFloat64();
double linkD = bLink.check("D",yarp::os::Value(0.0), "DH link offset (meters)").asFloat64();
double linkA = bLink.check("A",yarp::os::Value(0.0), "DH link length (meters)").asFloat64();
double linkAlpha = bLink.check("alpha",yarp::os::Value(0.0), "DH link twist (degrees)").asFloat64();
//-- Dynamic
if( bLink.check("mass") && bLink.check("cog") && bLink.check("inertia")) {
double linkMass = bLink.check("mass",yarp::os::Value(0.0), "link mass (SI units)").asFloat64();
yarp::os::Bottle linkCog = bLink.findGroup("cog", "vector of link's center of gravity (SI units)").tail();
yarp::os::Bottle linkInertia = bLink.findGroup("inertia", "vector of link's inertia (SI units)").tail();
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::RotZ), KDL::Frame::DH(linkA,KinRepresentation::degToRad(linkAlpha),linkD,KinRepresentation::degToRad(linkOffset)),
KDL::RigidBodyInertia(linkMass,KDL::Vector(linkCog.get(0).asFloat64(),linkCog.get(1).asFloat64(),linkCog.get(2).asFloat64()),
KDL::RotationalInertia(linkInertia.get(0).asFloat64(),linkInertia.get(1).asFloat64(),linkInertia.get(2).asFloat64(),0,0,0))));
CD_INFO("Added: %s (offset %f) (D %f) (A %f) (alpha %f) (mass %f) (cog %f %f %f) (inertia %f %f %f)\n",
link.c_str(), linkOffset,linkD,linkA,linkAlpha,linkMass,
linkCog.get(0).asFloat64(),linkCog.get(1).asFloat64(),linkCog.get(2).asFloat64(),
linkInertia.get(0).asFloat64(),linkInertia.get(1).asFloat64(),linkInertia.get(2).asFloat64());
}
else //-- No mass -> skip dynamics
{
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::RotZ),KDL::Frame::DH(linkA,KinRepresentation::degToRad(linkAlpha),linkD,KinRepresentation::degToRad(linkOffset))));
CD_INFO("Added: %s (offset %f) (D %f) (A %f) (alpha %f)\n",link.c_str(), linkOffset,linkD,linkA,linkAlpha);
}
continue;
}
std::string xyzLink("xyzLink_");
std::ostringstream xyzS;
xyzS << linkIndex;
xyzLink += xyzS.str();
CD_WARNING("Not found: \"%s\", looking for \"%s\" instead.\n", link.c_str(), xyzLink.c_str());
yarp::os::Bottle &bXyzLink = fullConfig.findGroup(xyzLink);
if( bXyzLink.isNull() ) {
CD_ERROR("Not found: \"%s\" either.\n", xyzLink.c_str());
return false;
}
double linkX = bXyzLink.check("x",yarp::os::Value(0.0), "X coordinate of next frame (meters)").asFloat64();
double linkY = bXyzLink.check("y",yarp::os::Value(0.0), "Y coordinate of next frame (meters)").asFloat64();
double linkZ = bXyzLink.check("z",yarp::os::Value(0.0), "Z coordinate of next frame (meters)").asFloat64();
std::string linkTypes = "joint type (Rot[XYZ]|InvRot[XYZ]|Trans[XYZ]|InvTrans[XYZ]), e.g. 'RotZ'";
std::string linkType = bXyzLink.check("Type",yarp::os::Value("NULL"), linkTypes.c_str()).asString();
if(linkType == "RotX") {
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::RotX),KDL::Frame(KDL::Vector(linkX,linkY,linkZ))));
} else if(linkType == "RotY") {
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::RotY),KDL::Frame(KDL::Vector(linkX,linkY,linkZ))));
} else if(linkType == "RotZ") {
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::RotZ),KDL::Frame(KDL::Vector(linkX,linkY,linkZ))));
} else if(linkType == "InvRotX") {
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::RotX,-1.0),KDL::Frame(KDL::Vector(linkX,linkY,linkZ))));
} else if(linkType == "InvRotY") {
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::RotY,-1.0),KDL::Frame(KDL::Vector(linkX,linkY,linkZ))));
} else if(linkType == "InvRotZ") {
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::RotZ,-1.0),KDL::Frame(KDL::Vector(linkX,linkY,linkZ))));
} else if(linkType == "TransX") {
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::TransX),KDL::Frame(KDL::Vector(linkX,linkY,linkZ))));
} else if(linkType == "TransY") {
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::TransY),KDL::Frame(KDL::Vector(linkX,linkY,linkZ))));
} else if(linkType == "TransZ") {
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::TransZ),KDL::Frame(KDL::Vector(linkX,linkY,linkZ))));
} else if(linkType == "InvTransX") {
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::TransX,-1.0),KDL::Frame(KDL::Vector(linkX,linkY,linkZ))));
} else if(linkType == "InvTransY") {
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::TransY,-1.0),KDL::Frame(KDL::Vector(linkX,linkY,linkZ))));
} else if(linkType == "InvTransZ") {
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::TransZ,-1.0),KDL::Frame(KDL::Vector(linkX,linkY,linkZ))));
} else {
CD_WARNING("Link joint type \"%s\" unrecognized!\n",linkType.c_str());
}
CD_SUCCESS("Added: %s (Type %s) (x %f) (y %f) (z %f)\n",xyzLink.c_str(),linkType.c_str(),linkX,linkY,linkZ);
}
//-- HN
yarp::sig::Matrix defaultYmHN(4,4);
defaultYmHN.eye();
yarp::sig::Matrix ymHN(4,4);
std::string ymHN_str("HN");
if( ! getMatrixFromProperties(fullConfig, ymHN_str, ymHN))
{
ymHN = defaultYmHN;
}
KDL::Vector kdlVecN(ymHN(0,3),ymHN(1,3),ymHN(2,3));
KDL::Rotation kdlRotN( ymHN(0,0),ymHN(0,1),ymHN(0,2),ymHN(1,0),ymHN(1,1),ymHN(1,2),ymHN(2,0),ymHN(2,1),ymHN(2,2));
chain.addSegment(KDL::Segment(KDL::Joint(KDL::Joint::None), KDL::Frame(kdlRotN,kdlVecN)));
CD_INFO("HN:\n%s\n[%s]\n",ymHN.toString().c_str(),defaultYmHN.toString().c_str());
CD_INFO("Chain number of segments (post- H0 and HN): %d\n",chain.getNrOfSegments());
CD_INFO("Chain number of joints (post- H0 and HN): %d\n",chain.getNrOfJoints());
fkSolverPos = new KDL::ChainFkSolverPos_recursive(chain);
ikSolverVel = new KDL::ChainIkSolverVel_pinv(chain);
idSolver = new KDL::ChainIdSolver_RNE(chain, gravity);
//-- IK solver algorithm.
std::string ik = fullConfig.check("ik", yarp::os::Value(DEFAULT_IK_SOLVER), "IK solver algorithm (lma, nrjl, st, id)").asString();
if (ik == "lma")
{
std::string weightsStr = fullConfig.check("weights", yarp::os::Value(DEFAULT_LMA_WEIGHTS), "LMA algorithm weights (bottle of 6 doubles)").asString();
yarp::os::Bottle weights(weightsStr);
Eigen::Matrix<double, 6, 1> L;
if (!parseLmaFromBottle(weights, L))
{
CD_ERROR("Unable to parse LMA weights.\n");
return false;
}
ikSolverPos = new KDL::ChainIkSolverPos_LMA(chain, L);
}
else if (ik == "nrjl")
{
KDL::JntArray qMax(chain.getNrOfJoints());
KDL::JntArray qMin(chain.getNrOfJoints());
//-- Joint limits.
if (!retrieveJointLimits(fullConfig, qMin, qMax))
{
CD_ERROR("Unable to retrieve joint limits.\n");
return false;
}
//-- Precision and max iterations.
double eps = fullConfig.check("eps", yarp::os::Value(DEFAULT_EPS), "IK solver precision (meters)").asFloat64();
double maxIter = fullConfig.check("maxIter", yarp::os::Value(DEFAULT_MAXITER), "maximum number of iterations").asInt32();
ikSolverPos = new KDL::ChainIkSolverPos_NR_JL(chain, qMin, qMax, *fkSolverPos, *ikSolverVel, maxIter, eps);
}
else if (ik == "st")
{
KDL::JntArray qMax(chain.getNrOfJoints());
KDL::JntArray qMin(chain.getNrOfJoints());
//-- Joint limits.
if (!retrieveJointLimits(fullConfig, qMin, qMax))
{
CD_ERROR("Unable to retrieve joint limits.\n");
return false;
}
//-- IK configuration selection strategy.
std::string strategy = fullConfig.check("invKinStrategy", yarp::os::Value(DEFAULT_STRATEGY), "IK configuration strategy").asString();
if (strategy == "leastOverallAngularDisplacement")
{
ConfigurationSelectorLeastOverallAngularDisplacementFactory factory(qMin, qMax);
ikSolverPos = ChainIkSolverPos_ST::create(chain, factory);
}
else if (strategy == "humanoidGait")
{
ConfigurationSelectorHumanoidGaitFactory factory(qMin, qMax);
ikSolverPos = ChainIkSolverPos_ST::create(chain, factory);
}
else
{
CD_ERROR("Unsupported IK strategy: %s.\n", strategy.c_str());
return false;
}
if (ikSolverPos == NULL)
{
CD_ERROR("Unable to solve IK.\n");
return false;
}
}
else if (ik == "id")
{
KDL::JntArray qMax(chain.getNrOfJoints());
KDL::JntArray qMin(chain.getNrOfJoints());
//-- Joint limits.
if (!retrieveJointLimits(fullConfig, qMin, qMax))
{
CD_ERROR("Unable to retrieve joint limits.\n");
return false;
}
ikSolverPos = new ChainIkSolverPos_ID(chain, qMin, qMax, *fkSolverPos);
}
else
{
CD_ERROR("Unsupported IK solver algorithm: %s.\n", ik.c_str());
return false;
}
originalChain = chain;
return true;
}
// -----------------------------------------------------------------------------
bool roboticslab::KdlSolver::close()
{
delete fkSolverPos;
delete ikSolverPos;
delete ikSolverVel;
delete idSolver;
return true;
}
// -----------------------------------------------------------------------------