-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathJoint.h
594 lines (517 loc) · 13.3 KB
/
Joint.h
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// Copyright 2012-2016 CNRS-UM LIRMM, CNRS-AIST JRL
//
// This file is part of RBDyn.
//
// RBDyn is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RBDyn is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with RBDyn. If not, see <http://www.gnu.org/licenses/>.
#pragma once
// includes
// std
#include <cmath>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
// SpaceVecAlg
#include <SpaceVecAlg/SpaceVecAlg>
namespace rbd
{
/**
* Utility function to compute a rotation matrix from the parameter vector.
* @param q parameter vector with a least 4 values.
* @return Rotation matrix in successor frame.
*/
template<typename T>
Eigen::Matrix3<T> QuatToE(const std::vector<T>& q);
/**
* Joint representation.
* Hold joint name (used as identifier) and compute transformation, speed and motion
* subspace.
* WARNING : Free joint translation parameters are in FP coordinate (Example 4.5 p81).
*/
class Joint
{
public:
/// Joint type.
enum Type
{
Rev, ///< Revolute joint about an user specified axis.
Prism, ///< Prismatique joint about an user specified axis.
Spherical, ///< Spherical joint, represented by a quaternion.
Planar, ///< Planar joint (2 prismatic(X, Y) and 1 revolute(Z)).
Cylindrical, ///< Cylindrical joint (Z prismatic, Z revolute).
Free, ///< Free joint, represented by a quaternion.
Fixed ///< Fixed joint.
};
/// Old joint type for Api compatibility
enum OldType
{
RevX, ///< Revolute joint about X axis.
RevY, ///< Revolute joint about Y axis.
RevZ, ///< Revolute joint about Z axis.
PrismX, ///< Prismatique joint about X axis.
PrismY, ///< Prismatique joint about Y axis.
PrismZ ///< Prismatique joint about Z axis.
};
public:
Joint();
/**
* Compatibility constructor
* @param type Joint type.
* @param forward Joint is in forward direction if true.
* @param name Joint name, must be unique in a multibody.
*/
Joint(OldType type, bool forward, std::string name);
/**
* @param type Joint type.
* @param axis User specified.
* @param forward Joint is in forward direction if true.
* @param name Joint name must be unique in a multibody.
*/
Joint(Type type, const Eigen::Vector3d& axis, bool forward,
std::string name);
/**
* @param type Joint type.
* @param forward Joint is in forward direction if true.
* @param name Joint name, must be unique in a multibody.
*/
Joint(Type type, bool forward, std::string name);
/// @return Joint type.
Type type() const
{
return type_;
}
/// @return Joint direction.
double direction() const
{
return dir_;
}
/// @return true if joint is forward, else false.
bool forward() const
{
return dir_ == 1.;
}
/// @param forward Put the joint in forward direction if true.
void forward(bool forward)
{
dir_ = forward ? 1. : -1;
S_ *= dir_;
}
/**
* Number of parameters of the joint.
* @return Number of generalized position variable.
*/
int params() const
{
return params_;
}
/**
* Number of degree of freedom of the joint.
* @return Number of generalized speed and acceleration variable.
*/
int dof() const
{
return dof_;
}
/// @return Joint name.
const std::string& name() const
{
return name_;
}
/// @return Joint motion subspace in successor frame coordinate.
const Eigen::Matrix<double, 6, Eigen::Dynamic>& motionSubspace() const
{
return S_;
}
/**
* Compute the joint transformation from predecessor to successor frame.
* @param q vector of generalized position variable.
* @return Spatial transformation from predecessor to successor frame.
*/
template<typename T>
sva::PTransform<T> pose(const std::vector<T>& q) const;
/**
* Compute the joint velocity.
* @param alpha vector of generalized speed variable.
* @return Spatial motion vector of the joint.
*/
sva::MotionVecd motion(const std::vector<double>& alpha) const;
/**
* Compute the tangential part of the acceleration S*alphaD.
* @param alphaD vector of generalized acceleration variable.
* @return Tangential part of acceleration
*/
sva::MotionVecd tanAccel(const std::vector<double>& alphaD) const;
/**
* @return Joint configuation at zero.
*/
std::vector<double> zeroParam() const;
/**
* @return Joint velocity at zero.
*/
std::vector<double> zeroDof() const;
/**
* Safe version of pose method.
* @see pose
* @throw std::domain_error If the number of generalized position variable is
* wrong.
*/
sva::PTransformd sPose(const std::vector<double>& q) const;
/**
* Safe version of motion method.
* @see motion
* @throw std::domain_error If the number of generalized speed variable is
* wrong.
*/
sva::MotionVecd sMotion(const std::vector<double>& alpha) const;
/**
* Safe version of tanAccel method.
* @see tanAccel
* @throw std::domain_error If the number of generalized acceleration variable is
* wrong.
*/
sva::MotionVecd sTanAccel(const std::vector<double>& alphaD) const;
bool operator==(const Joint& b) const
{
return name_ == b.name_;
}
bool operator!=(const Joint& b) const
{
return name_ != b.name_;
}
public:
/**
* @return Joint configuation at zero.
*/
static std::vector<double> ZeroParam(Type type);
/**
* @return Joint velocity at zero.
*/
static std::vector<double> ZeroDof(Type type);
private:
void constructJoint(Type t, const Eigen::Vector3d& a);
private:
Type type_;
Eigen::Matrix<double, 6, Eigen::Dynamic> S_;
double dir_; ///< joint direction
int params_;
int dof_;
std::string name_;
};
inline std::ostream& operator<<(std::ostream& out, const Joint& b)
{
out << "Joint: " << b.name();
return out;
}
inline Joint::Joint() : dir_(0.0), params_(0), dof_(0), name_("") {}
inline Joint::Joint(OldType type, bool forward, std::string name):
dir_(forward ? 1. : -1),
params_(0),
dof_(0),
name_(name)
{
using namespace Eigen;
switch(type)
{
case RevX:
constructJoint(Rev, Vector3d::UnitX());
break;
case RevY:
constructJoint(Rev, Vector3d::UnitY());
break;
case RevZ:
constructJoint(Rev, Vector3d::UnitZ());
break;
case PrismX:
constructJoint(Prism, Vector3d::UnitX());
break;
case PrismY:
constructJoint(Prism, Vector3d::UnitY());
break;
case PrismZ:
constructJoint(Prism, Vector3d::UnitZ());
break;
default:
constructJoint(Fixed, Vector3d::Zero());
break;
}
}
inline Joint::Joint(Type type, const Eigen::Vector3d& axis,
bool forward, std::string name):
dir_(forward ? 1. : -1),
params_(0),
dof_(0),
name_(name)
{
constructJoint(type, axis);
}
inline Joint::Joint(Type type, bool forward, std::string name):
dir_(forward ? 1. : -1),
params_(0),
dof_(0),
name_(name)
{
constructJoint(type, Eigen::Vector3d::UnitZ());
}
template<typename T>
inline sva::PTransform<T> Joint::pose(const std::vector<T>& q) const
{
using namespace Eigen;
using namespace sva;
Matrix3<T> rot;
switch(type_)
{
case Rev:
// minus S because rotation is anti trigonometric
return PTransform<T>(AngleAxis<T>(-q[0], S_.block<3, 1>(0, 0).cast<T>()).matrix());
case Prism:
return PTransform<T>(Vector3<T>(S_.block<3, 1>(3, 0).cast<T>()*q[0]));
case Spherical:
return PTransform<T>(Quaternion<T>(q[0], dir_*q[1], dir_*q[2], dir_*q[3]).inverse());
case Planar:
rot = sva::RotZ(q[0]);
if(dir_ == 1.)
{
return PTransform<T>(rot, rot.transpose()*Vector3d(q[1], q[2], 0.));
}
else
{
return PTransform<T>(rot, rot.transpose()*Vector3d(q[1], q[2], 0.)).inv();
}
case Cylindrical:
return PTransform<T>(AngleAxis<T>(-q[0], S_.col(0).head<3>().cast<T>()).matrix(),
S_.col(1).tail<3>().cast<T>()*q[1]);
case Free:
rot = QuatToE(q);
if(dir_ == 1.)
{
return PTransform<T>(rot,
Vector3<T>(q[4], q[5], q[6]));
}
else
{
return PTransform<T>(rot,
Vector3<T>(q[4], q[5], q[6])).inv();
}
case Fixed:
default:
return PTransform<T>::Identity();
}
}
inline sva::MotionVecd Joint::motion(const std::vector<double>& alpha) const
{
using namespace Eigen;
using namespace sva;
switch(type_)
{
case Rev:
return MotionVecd((Vector6d() << S_.block<3, 1>(0, 0)*alpha[0],
Vector3d::Zero()).finished());
case Prism:
return MotionVecd((Vector6d() << Vector3d::Zero(),
S_.block<3, 1>(3, 0)*alpha[0]).finished());
case Spherical:
return MotionVecd(S_*Vector3d(alpha[0], alpha[1], alpha[2]));
case Planar:
return MotionVecd(S_*Vector3d(alpha[0], alpha[1], alpha[2]));
case Cylindrical:
return MotionVecd(S_*Vector2d(alpha[0], alpha[1]));
case Free:
return MotionVecd(S_*(Vector6d() << alpha[0], alpha[1], alpha[2],
alpha[3], alpha[4], alpha[5]).finished());
case Fixed:
default:
return MotionVecd(Vector6d::Zero());
}
}
inline sva::MotionVecd Joint::tanAccel(const std::vector<double>& alphaD) const
{
using namespace Eigen;
using namespace sva;
switch(type_)
{
case Rev:
return MotionVecd((Vector6d() << S_.block<3, 1>(0, 0)*alphaD[0],
Vector3d::Zero()).finished());
case Prism:
return MotionVecd((Vector6d() << Vector3d::Zero(),
S_.block<3, 1>(3, 0)*alphaD[0]).finished());
case Spherical:
return MotionVecd(S_*Vector3d(alphaD[0], alphaD[1], alphaD[2]));
case Planar:
return MotionVecd(S_*Vector3d(alphaD[0], alphaD[1], alphaD[2]));
case Cylindrical:
return MotionVecd(S_*Vector2d(alphaD[0], alphaD[1]));
case Free:
return MotionVecd(S_*(Vector6d() << alphaD[0], alphaD[1], alphaD[2],
alphaD[3], alphaD[4], alphaD[5]).finished());
case Fixed:
default:
return MotionVecd(Vector6d::Zero());
}
}
inline std::vector<double> Joint::zeroParam() const
{
return ZeroParam(type_);
}
inline std::vector<double> Joint::zeroDof() const
{
return ZeroDof(type_);
}
inline sva::PTransformd Joint::sPose(const std::vector<double>& q) const
{
if(q.size() != static_cast<unsigned int>(params_))
{
std::ostringstream str;
str << "Wrong number of generalized position variable: expected " <<
params_ << " gived " << q.size();
throw std::domain_error(str.str());
}
return pose(q);
}
inline sva::MotionVecd Joint::sMotion(const std::vector<double>& alpha) const
{
if(alpha.size() != static_cast<unsigned int>(dof_))
{
std::ostringstream str;
str << "Wrong number of generalized speed variable: expected " <<
params_ << " gived " << alpha.size();
throw std::domain_error(str.str());
}
return motion(alpha);
}
inline sva::MotionVecd Joint::sTanAccel(const std::vector<double>& alphaD) const
{
if(alphaD.size() != static_cast<unsigned int>(dof_))
{
std::ostringstream str;
str << "Wrong number of generalized acceleration variable: expected " <<
params_ << " gived " << alphaD.size();
throw std::domain_error(str.str());
}
return tanAccel(alphaD);
}
inline std::vector<double> Joint::ZeroParam(Type type)
{
switch(type)
{
case Rev:
case Prism:
return {0.};
case Spherical:
return {1., 0., 0., 0.};
case Planar:
return {0., 0., 0.};
case Cylindrical:
return {0., 0.};
case Free:
return {1., 0., 0., 0., 0., 0., 0.};
case Fixed:
default:
return {};
}
}
inline std::vector<double> Joint::ZeroDof(Type type)
{
switch(type)
{
case Rev:
case Prism:
return {0.};
case Spherical:
return {0., 0., 0.};
case Planar:
return {0., 0., 0.};
case Cylindrical:
return {0., 0.};
case Free:
return {0., 0., 0., 0., 0., 0.};
case Fixed:
default:
return {};
}
}
inline void Joint::constructJoint(Type t, const Eigen::Vector3d& a)
{
using namespace Eigen;
type_ = t;
switch(t)
{
case Rev:
S_ = dir_*(Vector6d() << a, Vector3d::Zero()).finished();
params_ = 1;
dof_ = 1;
break;
case Prism:
S_ = dir_*(Vector6d() << Vector3d::Zero(), a).finished();
params_ = 1;
dof_ = 1;
break;
case Spherical:
S_ = Matrix<double, 6, 3>::Zero();
S_.block<3, 3>(0, 0).setIdentity();
S_ *= dir_;
params_ = 4;
dof_ = 3;
break;
case Planar:
S_ = Matrix<double, 6, 3>::Zero();
S_.block<3, 3>(2, 0).setIdentity();
S_ *= dir_;
params_ = 3;
dof_ = 3;
break;
case Cylindrical:
S_ = Matrix<double, 6, 2>::Zero();
S_.col(0).head<3>() = a;
S_.col(1).tail<3>() = a;
S_ *= dir_;
params_ = 2;
dof_ = 2;
break;
case Free:
S_ = dir_*Matrix6d::Identity();
params_ = 7;
dof_ = 6;
break;
case Fixed:
default:
S_ = Matrix<double, 6, 0>::Zero();
params_ = 0;
dof_ = 0;
break;
}
}
template<typename T>
inline Eigen::Matrix3<T> QuatToE(const std::vector<T>& q)
{
using namespace Eigen;
T p0 = q[0];
T p1 = q[1];
T p2 = q[2];
T p3 = q[3];
T p0p1 = p0*p1;
T p0p2 = p0*p2;
T p0p3 = p0*p3;
T p1p2 = p1*p2;
T p1p3 = p1*p3;
T p2p3 = p2*p3;
T p0s = std::pow(p0, 2);
T p1s = std::pow(p1, 2);
T p2s = std::pow(p2, 2);
T p3s = std::pow(p3, 2);
return 2.*(Matrix3<T>() << p0s + p1s - 0.5, p1p2 + p0p3, p1p3 - p0p2,
p1p2 - p0p3, p0s + p2s - 0.5, p2p3 + p0p1,
p1p3 + p0p2, p2p3 - p0p1, p0s + p3s - 0.5).finished();
}
} // namespace rbd