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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715 | /**
* @file InverseKinematics.cpp
*
* @author <a href="mailto:xu@informatik.hu-berlin.de">Xu, Yuan</a>
* @author <a href="mailto:welter@informatik.hu-berlin.de">Welter, Oliver</a>
* Implementation of Inverse Kinematics
*/
//#include "Representations/Debug/Stopwatch.h"
// Tools
//#include "Tools/TemplateUtility.h"
#include <algorithm>
#include "Tools/NaoInfo.h"
#include "InverseKinematics.h"
namespace Kinematics {
using namespace naoth;
using namespace std;
InverseKinematics::InverseKinematics(bool autoInit)<--- Member variable 'InverseKinematics::lock' is not initialized in the constructor.
: maxError(1.0)
{
if (autoInit){
init();
}
}
void InverseKinematics::init()
{
theKinematicChain.init(theJointData);
}
InverseKinematics::~InverseKinematics()
{
}
void InverseKinematics::calcJacobi(const list<Link*>& linkList, const Vector3<double>& affector, Matrix_nxn<double, 6>& J) const<--- Technically the member function 'Kinematics::InverseKinematics::calcJacobi' can be static. [+]The member function 'Kinematics::InverseKinematics::calcJacobi' can be made a static function. Making a function static can bring a performance benefit since no 'this' instance is passed to the function. This change should not cause compiler errors but it does not necessarily make sense conceptually. Think about your design and the task of the function first - is it a function that must not access members of class instances?
{
ASSERT(linkList.size()==6);
Vector3<double> a;
Vector3<double> n;
int i=0;
for(list<Link*>::const_iterator it = linkList.begin(); it != linkList.end(); ++it)
{
Link* link = *(it);
Vector3<double> dp = affector - link->p;
a = link->R * link->a;
n = a^dp;
J[0][i] = n.x;
J[1][i] = n.y;
J[2][i] = n.z;
J[3][i] = a.x;
J[4][i] = a.y;
J[5][i] = a.z;
i++;
}
}//end calcJacobi
bool InverseKinematics::updateJoints(const list<Link*>& linkList, const Vector_n<double,6>& dq) const
{
ASSERT(linkList.size()==6);
int i = 0;
bool updated = false;
for(list<Link*>::const_iterator it = linkList.begin(); it != linkList.end(); ++it)
{
Link* j = *it;
if (!lock[j->jointID])
{
*(j->q) += dq[i];
// TODO: do we need the magic number 0.001 ?
if (!updated && fabs(dq[i]) > Math::fromDegrees(0.001)) updated = true;
}
i++;
}
return updated;
}//end updateJoints
double InverseKinematics::gotoTargetJacobianInversion(const list<Link*>& linkList, const Pose3D& target, const Vector3<double>& offset, Mask mask) const
{
const double lambda = 1;
const int max_iterations = 100;
Pose3D affector;
Vector_n<double,6> dq;
Vector_n<double,6> err;
static Matrix_nxn<double, 6 > J;
double errlen2 = 0;
int i = 0;
while (true)
{
calculateAffector(linkList, offset, affector);
calcError(affector, target, err, mask);
errlen2 = errlen(err);
if (errlen2 < maxError || i++ > max_iterations)
{
break;
}
calcJacobi(linkList, affector.translation, J);
try
{
dq = J.solve(err) * lambda;
} catch (MVException &e)
{
cerr << "IK(" << i << ") " << e.getDescription() << endl; //TODO handle singular matrix
break;
}
if ( !updateJoints(linkList, dq) ) break;
}//end while
normalizeLinkAngles(linkList);
return errlen2;
}//end gotoTargetJacobianInversion
double InverseKinematics::gotoTargetJacobianTranspose(const list<Link*>& linkList, const Pose3D& target, const Vector3<double>& offset, Mask mask) const
{
const double lambda = 0.00001;
const int max_iterations = 100;
Pose3D affector;
Vector_n<double,6> dq;
Vector_n<double,6> err;
static Matrix_nxn<double, 6 > J;
double errlen2 = 0;
for(int i=0;i<max_iterations;i++)
{
calculateAffector(linkList, offset, affector);
calcError(affector, target, err, mask);
errlen2 = errlen(err);
if (errlen2 < maxError )
{
normalizeLinkAngles(linkList);
return errlen2;
}
calcJacobi(linkList, affector.translation, J);
dq = lambda * ( J.transpose() * err );
updateJoints(linkList, dq);
}//end for
normalizeLinkAngles(linkList);
return errlen2;
}//end gotoTargetJacobianTranspose
double InverseKinematics::gotoTargetCCD(const list<Link*>& linkList, const Pose3D& target, const Vector3<double>& offset, Mask mask) const
{
// ACHTUNG: the end effector should never be the same as the last joint (!)
// this check is here, because it happend from time to time
assert(offset.abs2() > 0 && "the end effector should never be the same as the last joint (!)");
// TODO: parameter?
const double alpha = 0.00001;
const double max_error = Math::fromDegrees(1);
const int max_iterations = 1000; //was 100
const double wo = 1;
const double wi = 1;
const double wo0 = (mask&MASK_WX) ? wo : 0;
const double wo1 = (mask&MASK_WY) ? wo : 0;
const double wo2 = (mask&MASK_WZ) ? wo : 0;
Pose3D affector;
const RotationMatrix& uc = affector.rotation;
const RotationMatrix& ud = target.rotation;
calculateAffector(linkList, offset, affector);
for (int i = 0; i < max_iterations; i++)
{
double delta = 0;
// iterate over all joints
for (list<Link*>::const_reverse_iterator iter = linkList.rbegin(); iter != linkList.rend(); ++iter)
{
Link* link = *iter;
if (lock[link->jointID] || link->q == NULL ) continue;
// vector: joint --> target
Vector3<double> Pid = target.translation - link->p;
const double Lid = Pid.abs();
// vector: joint --> affector
Vector3<double> Pic = affector.translation - link->p;
// ignore some dimensions if requested
if (0 == (mask & MASK_X)) Pic[0] = 0;
if (0 == (mask & MASK_Y)) Pic[1] = 0;
if (0 == (mask & MASK_Z)) Pic[2] = 0;
const double Lic = Pic.abs();
// ??
const double ruo = std::min(Lid, Lic) / std::max(Lid, Lic);
ASSERT(!Math::isNan(ruo));
// rotation axis of the joint?
Vector3<double> axis = link->R * link->a;
// ??
const double wp = alpha * (1 + ruo);
// calculate correction for the joint angle?
double k1 = wp * (Pid * axis)*(Pic * axis) + (wo0 * (ud[0] * axis)*(uc[0] * axis) + wo1 * (ud[1] * axis)*(uc[1] * axis) + wo2 * (ud[2] * axis)*(uc[2] * axis));
double k2 = wp * (Pid * Pic) + (ud[0] * uc[0] * wo0 + ud[1] * uc[1] * wo1 + ud[2] * uc[2] * wo2);
double k3 = axis * ((Pic^Pid) * wp + (uc[0]^ud[0]) * wo0 + (uc[1]^ud[1]) * wo1 + (uc[2]^ud[2]) * wo2);
double theta = atan2(k3, k2 - k1) * wi;
// apply correction and accumulate the error
(*(link->q)) += theta; // modify the joint
delta += fabs(theta);
// new position of the effector
calculateAffector(linkList, link, offset, affector);
}//end for linkList
if (delta < max_error)
{
// cout << "break @ " << i << endl;
break;
}
}//end for iterations
// calcluate the error of result
Vector_n<double,6> err;
calcError(affector, target, err, mask);
return errlen(err);
}//end gotoTargetCCD
double InverseKinematics::calculateLowerNaoLegJointsAnalyticaly(bool leftLeg, Pose3D& target)
{
const Link& pelvis = theKinematicChain.theLinks[leftLeg?KinematicChain::LPelvis:KinematicChain::RPelvis];
JointData::JointID kneePitch = leftLeg ? JointData::LKneePitch : JointData::RKneePitch;
JointData::JointID anklePitch = leftLeg ? JointData::LAnklePitch : JointData::RAnklePitch;
JointData::JointID ankleRoll = leftLeg ? JointData::LAnkleRoll : JointData::RAnkleRoll;
// TODO: transform to the targets koordinates?
Vector3<double> r = target.invert() * pelvis.b;
double C = r.abs();
const double maxlen = NaoInfo::ThighLength + NaoInfo::TibiaLength;
double kneeAng = 0;
//double error = 0;
if (C >= maxlen )
{
//cerr << "InverseKinematics can not reach this pose! length=" << C << endl;
// error = C - maxlen;
C = maxlen;
r.normalize(C);
target.translation = pelvis.b - target.rotation * r;
}
else
{
double c3 = (Math::sqr(NaoInfo::ThighLength) + Math::sqr(NaoInfo::TibiaLength) - Math::sqr(C))
/ (2.0 * NaoInfo::ThighLength * NaoInfo::TibiaLength);
kneeAng = Math::normalize(Math::pi - acos(c3));
}
//
theJointData.position[kneePitch] = kneeAng;
//
double alpha = asin((NaoInfo::ThighLength / C) * sin(Math::pi - kneeAng));
theJointData.position[anklePitch] = -(atan2(r.x, (Math::sgn(r.z) * sqrt(Math::sqr(r.y) + Math::sqr(r.z)))) + alpha);
theJointData.position[ankleRoll] = atan2(r.y, r.z);
return 0;//error;
}//end calculateLowerLegJointsAnalyticaly
double InverseKinematics::calculateNaoLegJointsAnalyticaly(bool leftLeg, Pose3D& target)
{
// calculate the last three joints
double error = calculateLowerNaoLegJointsAnalyticaly(leftLeg, target);
JointData::JointID hipYawPitch = leftLeg ? JointData::LHipYawPitch : JointData::RHipYawPitch;
JointData::JointID hipRoll = leftLeg ? JointData::LHipRoll : JointData::RHipRoll;
JointData::JointID hipPitch = leftLeg ? JointData::LHipPitch : JointData::RHipPitch;
JointData::JointID kneePitch = leftLeg ? JointData::LKneePitch : JointData::RKneePitch;
JointData::JointID anklePitch = leftLeg ? JointData::LAnklePitch : JointData::RAnklePitch;
JointData::JointID ankleRoll = leftLeg ? JointData::LAnkleRoll : JointData::RAnkleRoll;
RotationMatrix Foot2Thigh =
RotationMatrix::getRotationX(-theJointData.position[ankleRoll])
.rotateY(-theJointData.position[kneePitch]-theJointData.position[anklePitch]);
double rotX = leftLeg?-Math::pi_4:Math::pi_4; //Math::fromDegrees(leftLeg?-45.0:45.0);
RotationMatrix HipO2Foot = RotationMatrix::getRotationX(rotX) * target.rotation;
RotationMatrix HipO2Thigh = HipO2Foot * Foot2Thigh;
theJointData.position[hipRoll] = asin(HipO2Thigh[1][2]) - rotX;
theJointData.position[hipYawPitch] = atan2(-HipO2Thigh[1][0] , HipO2Thigh[1][1] ) * (leftLeg?-1.0:1.0);
theJointData.position[hipPitch] = atan2(-HipO2Thigh[0][2] , HipO2Thigh[2][2] );
return error;
}//end calculateNaoLegJointsAnalyticaly
double InverseKinematics::gotoLeg(bool leftLeg, Pose3D& target, const Vector3<double>& offset, Mask mask)
{
// solve analyticaly (for Nao only)
if ( mask == MASK_ALL ) return gotoLegAnalytical(leftLeg, target, offset);
// solve general inverse kinematics with CCD
list<Link*> linkList;
getRoute(leftLeg ? KinematicChain::LFoot : KinematicChain::RFoot, linkList);
return gotoTarget(linkList, target, offset, mask);
}// end gotoLeg
double InverseKinematics::gotoLegAnalytical(bool leftLeg, Pose3D& target, const Vector3<double>& offset)
{
target.translation -= target.rotation*offset;
double error = calculateNaoLegJointsAnalyticaly(leftLeg, target);
target.translation += target.rotation*offset;
//TODO: calculate error for the position?
return error;
}//end gotoLeg
double InverseKinematics::gotoLegs(
const Pose3D& tc,
Pose3D tlf,
Pose3D trf,
const Vector3<double>& leftOffset,
const Vector3<double>& rightOffset,
Mask leftFootMask,
Mask rightFootMask)
{
//STOPWATCH_START("InverseKinematics");
tlf = tc.local(tlf);
trf = tc.local(trf);
// unlock all joints
for (int i = 0; i < JointData::numOfJoint; i++)
{
lock[i] = false;
}
// calculate the right and the left foot
double lerr = gotoLeg(true, tlf, leftOffset);
double rerr = gotoLeg(false, trf, rightOffset);
// calculate the hip-yaw-pitch deviation
// NOTE: they have to be references
double& lhyp = theJointData.position[JointData::LHipYawPitch];
double& rhyp = theJointData.position[JointData::RHipYawPitch];
double err = lhyp - rhyp;
// try to adjust the legs (max 5 times) ...
const double max_hip_error = Math::fromDegrees(0.5);
const int max_trials = 5;
int trials = 0;
while ( fabs(err) > max_hip_error || lerr > maxError || rerr > maxError )
{
// TODO: fixme!!!
if (true || trials < 2 || ( (leftFootMask == MASK_ALL) && (rightFootMask == MASK_ALL)) )
{
double derr = err * 0.25;
RotationMatrix rot = RotationMatrix::getRotationZ(derr);
tlf.rotation *= rot;
tlf.translation = rot * tlf.translation;
trf.rotation *= rot;
trf.translation = rot * trf.translation;
lerr = gotoLeg(true, tlf, leftOffset);
rerr = gotoLeg(false, trf, rightOffset);
}
else
{
if (leftFootMask == MASK_ALL)
{
if ( lerr > maxError ) lerr =gotoLeg(true, tlf, leftOffset, leftFootMask);
rhyp = lhyp;
lock[JointData::RHipYawPitch] = true;
rerr = gotoLeg(false, trf, rightOffset);<--- Variable 'rerr' is reassigned a value before the old one has been used.
rerr = gotoLeg(false, trf, rightOffset, rightFootMask);
} else // ASSERT(rightFootMask == MASK_ALL)
{
if ( rerr > maxError )
{
rerr = gotoLeg(false, trf, rightOffset, rightFootMask);
}
lhyp = rhyp;
lock[JointData::LHipYawPitch] = true;
lerr = gotoLeg(true, tlf, leftOffset);<--- Variable 'lerr' is reassigned a value before the old one has been used.
lerr = gotoLeg(true, tlf, leftOffset, leftFootMask);
}
}
err = lhyp - rhyp;
if ( trials++ > max_trials)
{
cerr<<"InverseKinematics:Warning lhyp-rhyp=" << Math::toDegrees(err) << " lerr="<<lerr<<" rerr="<<rerr<<endl;
break;
}
}//end while
//STOPWATCH_STOP("InverseKinematics");
return lerr + rerr;
}//end gotoLegs
double InverseKinematics::gotoArms(
const Pose3D& tc,
const Pose3D& targetLeftHand,
const Pose3D& targetRightHand,
const Vector3<double>& leftOffset,
const Vector3<double>& rightOffset,
Mask leftHandMask,
Mask rightHandMask)
{
//STOPWATCH_START("InverseKinematics --- ARMS");
// transform to the chest coordinates
Pose3D tlh = tc.local(targetLeftHand);
Pose3D trh = tc.local(targetRightHand);
// unlock all joints
for (int i = 0; i < JointData::numOfJoint; i++)
{
lock[i] = false;
}
//fix the ElbowYaw joint
lock[JointData::LElbowYaw] = true;
lock[JointData::RElbowYaw] = true;
theJointData.position[JointData::LElbowYaw] = 0.0;
theJointData.position[JointData::RElbowYaw] = -0.0;
// calculate the right and the left arm
double lerr = gotoArm(true, tlh, leftOffset, leftHandMask);
double rerr = gotoArm(false, trh, rightOffset, rightHandMask);
// TODO: handle error
//STOPWATCH_STOP("InverseKinematics --- ARMS");
return lerr + rerr;
}//end gotoArms
double InverseKinematics::gotoArm(bool leftArm, const Pose3D& target, const Vector3<double>& offset, Mask mask)
{
list<Link*> linkList;
getRoute(leftArm?KinematicChain::LForeArm:KinematicChain::RForeArm, linkList);
return gotoTarget(linkList, target, offset, mask, CCD);
}//end gotoArm
void InverseKinematics::calcError(const Pose3D& current, const Pose3D& target, Vector_n<double,6>& result, Mask mask)
{
Vector3<double> perr = target.translation - current.translation;
RotationMatrix rerr = current.rotation.invert() * target.rotation;
Vector3<double> n = rerr.toQuaternion();
Vector3<double> werr = current.rotation * n;
result[0] = (mask&MASK_X) ? perr[0] : 0;
result[1] = (mask&MASK_Y) ? perr[1] : 0;
result[2] = (mask&MASK_Z) ? perr[2] : 0;
result[3] = (mask&MASK_WX) ? werr[0] : 0;
result[4] = (mask&MASK_WY) ? werr[1] : 0;
result[5] = (mask&MASK_WZ) ? werr[2] : 0;
}//end calcError
double InverseKinematics::errlen(const Vector_n<double,6>& e)
{
double error = Math::sqr(e[0]) + Math::sqr(e[1]) + Math::sqr(e[2])
+ Math::toDegrees(Math::sqr(e[3]) + Math::sqr(e[4]) + Math::sqr(e[5]));
return error;
}//end errlen
void InverseKinematics::normalizeLinkAngles(const list<Link*>& linkList)
{
// normalize angles
for(list<Link*>::const_iterator it = linkList.begin(); it != linkList.end(); ++it)
{
(*it)->normalizeJointAngle();
}
}//end normalizeLinkAngles
void InverseKinematics::getRoute(Link* actuatorLink, list<Link*>& linkList)
{
Link *m = actuatorLink->mother;
if(NULL != m)
{
getRoute(m, linkList);
}
linkList.push_back(actuatorLink);
}//end getRoute
void InverseKinematics::linkListForwardKinematics(const list<Link*>& linkList)
{
for (list<Link*>::const_iterator iter = linkList.begin(); iter != linkList.end(); ++iter)
{
(*iter)->updateFromMother();
}
}//end linkListForwardKinematics
void InverseKinematics::linkListForwardKinematics(const list<Link*>& linkList, Link* beginLink)
{
bool start = false;
for ( list<Link*>::const_iterator iter = linkList.begin(); iter != linkList.end(); ++iter )
{
Link* l = *iter;
if ( l == beginLink ) start = true;
if (start)
{
l->updateFromMother();
}
}//end for
}//end linkListForwardKinematics
void InverseKinematics::calculateAffector(const list<Link*>& linkList, const Vector3<double>& offset, Pose3D& affector)
{
linkListForwardKinematics(linkList);
Link* end = *linkList.rbegin();
affector.rotation = end->R;
affector.translation = end->R * offset + end->p;
}//end calculateAffector
void InverseKinematics::calculateAffector(const list<Link*>& linkList, Link* beginLink, const Vector3<double>& offset, Pose3D& affector)
{
linkListForwardKinematics(linkList, beginLink);
Link* end = *linkList.rbegin();
affector.rotation = end->R;
affector.translation = end->R * offset + end->p;
}//end calculateAffector
void InverseKinematics::getRoute(KinematicChain::LinkID actuatorID, list<Link*>& linkList)
{
getRoute(&theKinematicChain.theLinks[actuatorID], linkList);
}//end getRoute
void InverseKinematics::test()
{
cout << "************** Test LEGS Inverse Kinematics ******************\n";
double refAng[JointData::numOfJoint];
for (unsigned int i = 0; i < JointData::numOfJoint; i++)
{
refAng[i] = Math::random(theJointData.min[i], theJointData.max[i]);
theJointData.position[i] = refAng[i];
}
refAng[JointData::LHipYawPitch] = refAng[JointData::RHipYawPitch];
theJointData.position[JointData::LHipYawPitch] = theJointData.position[JointData::RHipYawPitch];
cout<<"joints: ";
for (int i = 10; i < JointData::numOfJoint; i++)
{
cout<<JointData::getJointName((JointData::JointID)i)<<"="<<Math::toDegrees(theJointData.position[i])<<' ';
}
cout<<endl<<endl;
list<Link*> leftList, rightList;
getRoute(KinematicChain::LFoot, leftList);
getRoute(KinematicChain::RFoot, rightList);
theKinematicChain.theLinks[KinematicChain::Torso].p = Vector3<double>(0, 0, 0);
theKinematicChain.theLinks[KinematicChain::Torso].R = RotationMatrix::getRotationZ(0);
Vector3<double> footOffset(0, 0, -NaoInfo::FootHeight);
Pose3D leftTarget, rightTarget;
calculateAffector(leftList, footOffset, leftTarget);
calculateAffector(rightList, footOffset, rightTarget);
Pose3D chestTarget(theKinematicChain.theLinks[KinematicChain::Torso].R, theKinematicChain.theLinks[KinematicChain::Torso].p);
//leftTarget.rotateX(Math::fromDegrees(90));
cout << "left target foot: " << leftTarget.translation <<" ("<< leftTarget.translation.abs() << ")"<< endl;
int count = 0;
while (count++ < 1)
{
for (unsigned int i = 0; i < JointData::numOfJoint; i++)
{
theJointData.position[i] = Math::random(theJointData.min[i], theJointData.max[i]);
}
theJointData.position[JointData::LHipYawPitch] = theJointData.position[JointData::RHipYawPitch];
linkListForwardKinematics(leftList);
linkListForwardKinematics(rightList);
double err = gotoLegs(chestTarget, leftTarget, rightTarget, footOffset, footOffset, MASK_POS);
Pose3D lf, rf;
calculateAffector(leftList, footOffset, lf);
calculateAffector(rightList, footOffset, rf);
cout << "err (" << err << ") : \n";
for (int i = 10; i < JointData::numOfJoint; i++)
{
cout << JointData::getJointName((JointData::JointID)i) << "=" << Math::toDegrees(theJointData.position[i] - refAng[i]) << ' ';
}
cout << endl<<endl;
lf.rotation -= leftTarget.rotation;
rf.rotation -= rightTarget.rotation;
lf.translation -= leftTarget.translation;
rf.translation -= rightTarget.translation;
//cout << "left : " << lf << endl;
//cout << "right : " << rf << endl;
cout << "left hand translation: " << lf.translation << endl;
cout << "left hand rotation: " << lf.rotation << endl << endl;
cout << "right hand translation: " << rf.translation << endl;
cout << "right hand rotation: " << rf.rotation << endl << endl;
}
}
void InverseKinematics::testArms()
{
cout << "******/\\****** Test ARMS Inverse Kinematics *********/\\*******\n";
double refAng[JointData::numOfJoint];
for (unsigned int i = 0; i < JointData::numOfJoint; i++)
{
refAng[i] = Math::random(theJointData.min[i], theJointData.max[i]);
theJointData.position[i] = refAng[i];
}
refAng[JointData::LHipYawPitch] = refAng[JointData::RHipYawPitch];
theJointData.position[JointData::LHipYawPitch] = theJointData.position[JointData::RHipYawPitch];
cout<<"joints: ";
for (int i = JointData::RShoulderRoll; i < JointData::RHipYawPitch; i++)
{
cout<<JointData::getJointName((JointData::JointID)i)<<"="<<Math::toDegrees(theJointData.position[i]/*theKinematicChain.theLinks[i].q*/)<<' ';
}
cout<<endl<<endl;
list<Link*> leftList, rightList;
getRoute(KinematicChain::LForeArm, leftList);
cout << "KINEMATIC CHAIN >LEFT<:" << endl;
for(list<Link*>::const_reverse_iterator iter = leftList.rbegin(); iter != leftList.rend(); ++iter)
{
cout << KinematicChain::getLinkName(*iter) << endl;
}
getRoute(KinematicChain::RForeArm, rightList);
cout << "KINEMATIC CHAIN >RIGHT<:" << endl;
for(list<Link*>::const_reverse_iterator iter = rightList.rbegin(); iter != rightList.rend(); ++iter)
{
cout << KinematicChain::getLinkName(*iter) << endl;
}
theKinematicChain.theLinks[KinematicChain::Torso].p = Vector3<double>(0, 0, 0);
theKinematicChain.theLinks[KinematicChain::Torso].R = RotationMatrix::getRotationZ(0);
Vector3<double> handOffset(NaoInfo::LowerArmLength+NaoInfo::HandOffsetX, 0, 0);
Pose3D leftTarget, rightTarget;
calculateAffector(leftList, handOffset, leftTarget);
calculateAffector(rightList, handOffset, rightTarget);
Pose3D chestTarget(theKinematicChain.theLinks[KinematicChain::Torso].R, theKinematicChain.theLinks[KinematicChain::Torso].p);
//leftTarget.rotateX(Math::fromDegrees(90));
//leftTarget.translate(50,0,0);
cout << "left target hand: " << leftTarget.translation <<" ("<< leftTarget.translation.abs() << ")"<< endl<<endl;
//rightTarget.translate(100,0,0);
//cout << "right target hand: " << rightTarget.translation <<" ("<< rightTarget.translation.abs() << ")"<< endl;
int count = 0;
while (count++ < 1)
{
for (unsigned int i = 0; i < JointData::numOfJoint; i++)
{
theJointData.position[i] = Math::random(theJointData.min[i], theJointData.max[i]);
}
theJointData.position[JointData::LHipYawPitch] = theJointData.position[JointData::RHipYawPitch];
linkListForwardKinematics(leftList);
linkListForwardKinematics(rightList);
cout << " > MASK POS < " << endl;
double err = gotoArms(chestTarget, leftTarget, rightTarget, handOffset, handOffset, MASK_POS); //was POS
Pose3D lf, rf;
calculateAffector(leftList, handOffset, lf);
calculateAffector(rightList, handOffset, rf);
cout << "err (" << err << ") : \n";
// for (int i = JointData::RShoulderRoll; i < JointData::RHipYawPitch; i++)
// {
// cout << JointData::getJointName((JointData::JointID)i) << "=" << Math::toDegrees(theJointData.position[i] /*theKinematicChain.theLinks[i].q*/ - refAng[i]) << ' ';
// }
// cout << endl<<endl;
lf.rotation -= leftTarget.rotation;
rf.rotation -= rightTarget.rotation;
lf.translation -= leftTarget.translation;
rf.translation -= rightTarget.translation;
cout << "left hand translation: " << lf.translation << endl;
cout << "left hand rotation: " << lf.rotation << endl << endl;
cout << "right hand translation: " << rf.translation << endl;
cout << "right hand rotation: " << rf.rotation << endl << endl;
}
}
} // namespace kinematics
|