Source

gazebo / gazebo / physics / rtql8 / RTQL8Joint.cc

The branch 'john_rtql8' does not exist.
Full commit
  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
/*
 * Copyright 2012 Open Source Robotics Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
*/

#include "gazebo/common/Exception.hh"
#include "gazebo/common/Console.hh"

#include "gazebo/physics/World.hh"
#include "gazebo/physics/Link.hh"
#include "gazebo/physics/PhysicsEngine.hh"
#include "gazebo/physics/rtql8/RTQL8Link.hh"
#include "gazebo/physics/rtql8/RTQL8Joint.hh"
//#include "physics/ScrewJoint.hh"

#include "rtql8/kinematics/Dof.h"
#include "rtql8/kinematics/Joint.h"

using namespace gazebo;
using namespace physics;


//////////////////////////////////////////////////
RTQL8Joint::RTQL8Joint(BasePtr _parent)
  : Joint(_parent), rtql8Joint(NULL)
{
}

//////////////////////////////////////////////////
RTQL8Joint::~RTQL8Joint()
{
  this->Detach();

  if (rtql8Joint)
    delete rtql8Joint;
}

//////////////////////////////////////////////////
void RTQL8Joint::Load(sdf::ElementPtr _sdf)
{
   Joint::Load(_sdf);

   // In case this joint is already loaded, we delete rtql8 joint if it is
   // created.
   if (rtql8Joint)
   {
     delete rtql8Joint;
     rtql8Joint = NULL;
   }

   // In Joint::Load(sdf::ElementPtr), this joint stored the information of the
   // parent joint and child joint.
   rtql8::kinematics::BodyNode* parentBodyNode
       = boost::shared_dynamic_cast<RTQL8Link>(this->parentLink)->GetBodyNode();
   rtql8::kinematics::BodyNode* childBodyNode
       = boost::shared_dynamic_cast<RTQL8Link>(this->childLink)->GetBodyNode();

   // In order to create rtql8 joint, we need to know this joint's parent
   // and child link so we create rtql8 joint after the joint is loaded with sdf
   // .
   rtql8Joint = new rtql8::kinematics::Joint(parentBodyNode, childBodyNode);

   // Set Pose: offset from child link origin in child link frame.
   if (this->sdf->HasElement("pose"))
   {
     sdf::ElementPtr poseElem = this->sdf->GetElement("pose");

     math::Pose pose = poseElem->GetValuePose();

     rtql8::kinematics::Dof* tranX = new rtql8::kinematics::Dof(pose.pos.x);
     rtql8::kinematics::Dof* tranY = new rtql8::kinematics::Dof(pose.pos.y);
     rtql8::kinematics::Dof* tranZ = new rtql8::kinematics::Dof(pose.pos.z);

     rtql8::kinematics::TrfmTranslate* tran
         = new rtql8::kinematics::TrfmTranslate(tranX, tranY, tranZ);

     this->rtql8Joint->addTransform(tran, false);

     rtql8::kinematics::Dof* rotW = new rtql8::kinematics::Dof(pose.rot.w);
     rtql8::kinematics::Dof* rotX = new rtql8::kinematics::Dof(pose.rot.x);
     rtql8::kinematics::Dof* rotY = new rtql8::kinematics::Dof(pose.rot.y);
     rtql8::kinematics::Dof* rotZ = new rtql8::kinematics::Dof(pose.rot.z);

     rtql8::kinematics::TrfmRotateQuat* rot
         = new rtql8::kinematics::TrfmRotateQuat(rotW, rotX, rotY, rotZ);

     this->rtql8Joint->addTransform(rot, false);
   }
}

//////////////////////////////////////////////////
void RTQL8Joint::Reset()
{
  Joint::Reset();
}

//////////////////////////////////////////////////
LinkPtr RTQL8Joint::GetJointLink(int _index) const
{
  LinkPtr result;

  if (_index == 0)
  {
    RTQL8LinkPtr rtql8Link1
        = boost::shared_static_cast<RTQL8Link>(this->parentLink);

    if (rtql8Link1 != NULL)
      return this->parentLink;
  }

  if (_index == 1)
  {
    RTQL8LinkPtr rtql8Link2
        = boost::shared_static_cast<RTQL8Link>(this->childLink);

    if (rtql8Link2 != NULL)
      return this->childLink;
  }

  return result;
}

//////////////////////////////////////////////////
bool RTQL8Joint::AreConnected(LinkPtr _one, LinkPtr _two) const
{
//  RTQL8LinkPtr rtql8Link1 = boost::shared_dynamic_cast<RTQL8Link>(_one);
//  RTQL8LinkPtr rtql8Link2 = boost::shared_dynamic_cast<RTQL8Link>(_two);

//  if (rtql8Link1 == NULL || rtql8Link2 == NULL)
//    gzthrow("RTQL8Joint requires RTQL8 bodies\n");

//  return dAreConnected(odeLink1->GetODEId(), odeLink2->GetODEId());
  return (this->childLink.get() == _one.get() && this->parentLink.get() == _two.get())
      || (this->childLink.get() == _two.get() && this->parentLink.get() == _one.get());
}

//////////////////////////////////////////////////
void RTQL8Joint::Attach(LinkPtr _parent, LinkPtr _child)
{
   Joint::Attach(_parent, _child);

   RTQL8LinkPtr rtql8child = boost::shared_dynamic_cast<RTQL8Link>(this->childLink);
   RTQL8LinkPtr rtql8parent = boost::shared_dynamic_cast<RTQL8Link>(this->parentLink);

   if (rtql8child == NULL && rtql8parent == NULL)
     gzthrow("RTQL8Joint requires at least one RTQL8 link\n");

   // TODO: RTQL8's joint can't change their links connected.
   // For now, recreating the joint is the only way.

   // TODO: We need to add the functionality, attach/detaach, into rtql8's joint
   // class.
//   if (this->rtql8Joint)
//     delete this->rtql8Joint;

//   rtql8::kinematics::BodyNode* parentBodyNode = boost::shared_dynamic_cast<RTQL8Link>(
//         this->parentLink)->GetBodyNode();
//   rtql8::kinematics::BodyNode* childBodyNode = boost::shared_dynamic_cast<RTQL8Link>(
//         this->childLink)->GetBodyNode();

//   this->rtql8Joint = new rtql8::kinematics::Joint(parentBodyNode, childBodyNode);
}

//////////////////////////////////////////////////
void RTQL8Joint::Detach()
{
   this->childLink.reset();
   this->parentLink.reset();

  // TODO: RTQL8's joint can't change their links connected.
  // For now, recreating the joint is the only way.
//  if (this->rtql8Joint)
//    delete this->rtql8Joint;

//  kinematics::BodyNode* parentBodyNode = boost::shared_dynamic_cast<RTQL8Link>(
//        this->parentLink)->GetBodyNode();
//  kinematics::BodyNode* childBodyNode = boost::shared_dynamic_cast<RTQL8Link>(
//        this->childLink)->GetBodyNode();

//  this->rtql8Joint = new rtql8::kinematics::Joint(NULL, NULL);
}

//////////////////////////////////////////////////
void RTQL8Joint::SetHighStop(int _index, const math::Angle & /*_angle*/)
{
   switch (_index)
   {
     case 0:
//       this->SetParam(dParamHiStop, _angle.Radian());
       break;
     case 1:
//       this->SetParam(dParamHiStop2, _angle.Radian());
       break;
     case 2:
//       this->SetParam(dParamHiStop3, _angle.Radian());
       break;
     default:
       gzerr << "Invalid index[" << _index << "]\n";
       break;
   };
}

//////////////////////////////////////////////////
void RTQL8Joint::SetLowStop(int _index, const math::Angle & /*_angle*/)
{
   switch (_index)
   {
     case 0:
//       this->SetParam(dParamLoStop, _angle.Radian());
       break;
     case 1:
//       this->SetParam(dParamLoStop2, _angle.Radian());
       break;
     case 2:
//       this->SetParam(dParamLoStop3, _angle.Radian());
       break;
     default:
       gzerr << "Invalid index[" << _index << "]\n";
   };
}

//////////////////////////////////////////////////
math::Angle RTQL8Joint::GetHighStop(int /*_index*/)
{
//   switch (_index)
//   {
//     case 0:
//       return this->GetParam(dParamHiStop);
//     case 1:
//       return this->GetParam(dParamHiStop2);
//     case 2:
//       return this->GetParam(dParamHiStop3);
//     default:
//       gzerr << "Invalid index[" << _index << "]\n";
//   };

  return 0;
}

//////////////////////////////////////////////////
math::Angle RTQL8Joint::GetLowStop(int /*_index*/)
{
//   switch (_index)
//   {
//     case 0:
//       return this->GetParam(dParamLoStop);
//     case 1:
//       return this->GetParam(dParamLoStop2);
//     case 2:
//       return this->GetParam(dParamLoStop3);
//     default:
//       gzerr << "Invalid index[" << _index << "]\n";
//   };

  return 0;
}

//////////////////////////////////////////////////
math::Vector3 RTQL8Joint::GetLinkForce(unsigned int /*_index*/) const
{
  math::Vector3 result;
//   dJointFeedback *jointFeedback = dJointGetFeedback(this->jointId);
// 
//   if (_index == 0)
//     result.Set(jointFeedback->f1[0], jointFeedback->f1[1],
//                jointFeedback->f1[2]);
//   else
//     result.Set(jointFeedback->f2[0], jointFeedback->f2[1],
//                jointFeedback->f2[2]);

  return result;
}

//////////////////////////////////////////////////
math::Vector3 RTQL8Joint::GetLinkTorque(unsigned int /*_index*/) const
{
  math::Vector3 result;
//   dJointFeedback *jointFeedback = dJointGetFeedback(this->jointId);
// 
//   if (_index == 0)
//     result.Set(jointFeedback->t1[0], jointFeedback->t1[1],
//                jointFeedback->t1[2]);
//   else
//     result.Set(jointFeedback->t2[0], jointFeedback->t2[1],
//                jointFeedback->t2[2]);

  return result;
}

//////////////////////////////////////////////////
void RTQL8Joint::SetAttribute(Attribute /*_attr*/, int /*_index*/, double /*_value*/)
{
//   switch (_attr)
//   {
//     case FUDGE_FACTOR:
//       this->SetParam(dParamFudgeFactor, _value);
//       break;
//     case SUSPENSION_ERP:
//       this->SetParam(dParamSuspensionERP, _value);
//       break;
//     case SUSPENSION_CFM:
//       this->SetParam(dParamSuspensionCFM, _value);
//       break;
//     case STOP_ERP:
//       this->SetParam(dParamStopERP, _value);
//       break;
//     case STOP_CFM:
//       this->SetParam(dParamStopCFM, _value);
//       break;
//     case ERP:
//       this->SetParam(dParamERP, _value);
//       break;
//     case CFM:
//       this->SetParam(dParamCFM, _value);
//       break;
//     case FMAX:
//       this->SetParam(dParamFMax, _value);
//       break;
//     case VEL:
//       this->SetParam(dParamVel, _value);
//       break;
//     case HI_STOP:
//       this->SetParam(dParamHiStop, _value);
//       break;
//     case LO_STOP:
//       this->SetParam(dParamLoStop, _value);
//       break;
//     default:
//       gzerr << "Unable to handle joint attribute[" << _attr << "]\n";
//       break;
//   };
}

//////////////////////////////////////////////////
void RTQL8Joint::SetAttribute(const std::string &/*_key*/, int /*_index*/,
                            const boost::any &/*_value*/)
{
//   if (_key == "fudge_factor")
//   {
//     try
//     {
//       this->SetParam(dParamFudgeFactor, boost::any_cast<double>(_value));
//     }
//     catch(boost::bad_any_cast &e)
//     {
//       gzerr << "boost any_cast error:" << e.what() << "\n";
//     }
//   }
//   else if (_key == "suspension_erp")
//   {
//     try
//     {
//       this->SetParam(dParamSuspensionERP, boost::any_cast<double>(_value));
//     }
//     catch(boost::bad_any_cast &e)
//     {
//       gzerr << "boost any_cast error:" << e.what() << "\n";
//     }
//   }
//   else if (_key == "suspension_cfm")
//   {
//     try
//     {
//       this->SetParam(dParamSuspensionCFM, boost::any_cast<double>(_value));
//     }
//     catch(boost::bad_any_cast &e)
//     {
//       gzerr << "boost any_cast error:" << e.what() << "\n";
//     }
//   }
//   else if (_key == "stop_erp")
//   {
//     try
//     {
//       this->SetParam(dParamStopERP, boost::any_cast<double>(_value));
//     }
//     catch(boost::bad_any_cast &e)
//     {
//       gzerr << "boost any_cast error:" << e.what() << "\n";
//     }
//   }
//   else if (_key == "stop_cfm")
//   {
//     try
//     {
//       this->SetParam(dParamStopCFM, boost::any_cast<double>(_value));
//     }
//     catch(boost::bad_any_cast &e)
//     {
//       gzerr << "boost any_cast error:" << e.what() << "\n";
//     }
//   }
//   else if (_key == "erp")
//   {
//     try
//     {
//       this->SetParam(dParamERP, boost::any_cast<double>(_value));
//     }
//     catch(boost::bad_any_cast &e)
//     {
//       gzerr << "boost any_cast error:" << e.what() << "\n";
//     }
//   }
//   else if (_key == "cfm")
//   {
//     try
//     {
//       this->SetParam(dParamCFM, boost::any_cast<double>(_value));
//     }
//     catch(boost::bad_any_cast &e)
//     {
//       gzerr << "boost any_cast error:" << e.what() << "\n";
//     }
//   }
//   else if (_key == "fmax")
//   {
//     try
//     {
//       this->SetParam(dParamFMax, boost::any_cast<double>(_value));
//     }
//     catch(boost::bad_any_cast &e)
//     {
//       gzerr << "boost any_cast error:" << e.what() << "\n";
//     }
//   }
//   else if (_key == "vel")
//   {
//     try
//     {
//       this->SetParam(dParamVel, boost::any_cast<double>(_value));
//     }
//     catch(boost::bad_any_cast &e)
//     {
//       gzerr << "boost any_cast error:" << e.what() << "\n";
//     }
//   }
//   else if (_key == "hi_stop")
//   {
//     try
//     {
//       this->SetParam(dParamHiStop, boost::any_cast<double>(_value));
//     }
//     catch(boost::bad_any_cast &e)
//     {
//       gzerr << "boost any_cast error:" << e.what() << "\n";
//     }
//   }
//   else if (_key == "lo_stop")
//   {
//     try
//     {
//       this->SetParam(dParamLoStop, boost::any_cast<double>(_value));
//     }
//     catch(boost::bad_any_cast &e)
//     {
//       gzerr << "boost any_cast error:" << e.what() << "\n";
//     }
//   }
//   else if (_key == "thread_pitch")
//   {
//     ScrewJoint<RTQL8Joint>* screwJoint =
//       dynamic_cast<ScrewJoint<RTQL8Joint>* >(this);
//     if (screwJoint != NULL)
//     {
//       try
//       {
//         screwJoint->SetThreadPitch(0, boost::any_cast<double>(_value));
//       }
//       catch(boost::bad_any_cast &e)
//       {
//         gzerr << "boost any_cast error:" << e.what() << "\n";
//       }
//     }
//   }
//   else
//   {
//     try
//     {
//       gzerr << "Unable to handle joint attribute["
//             << boost::any_cast<std::string>(_value) << "]\n";
//     }
//     catch(boost::bad_any_cast &e)
//     {
//       gzerr << "boost any_cast error:" << e.what() << "\n";
//     }
//   }
}

JointWrench RTQL8Joint::GetForceTorque(int /*_index*/)
{
  JointWrench wrench;
//  // Note that:
//  // f2, t2 are the force torque measured on parent body's cg
//  // f1, t1 are the force torque measured on child body's cg
//  dJointFeedback* fb = this->GetFeedback();
//  if (fb)
//  {
//    wrench.body1Force.Set(fb->f1[0], fb->f1[1], fb->f1[2]);
//    wrench.body1Torque.Set(fb->t1[0], fb->t1[1], fb->t1[2]);
//    wrench.body2Force.Set(fb->f2[0], fb->f2[1], fb->f2[2]);
//    wrench.body2Torque.Set(fb->t2[0], fb->t2[1], fb->t2[2]);

//    if (this->childLink)
//    {
//      // convert torque from about child CG to joint anchor location
//      // cg position specified in child link frame
//      math::Vector3 cgPos = this->childLink->GetInertial()->GetPose().pos;
//      // moment arm rotated into world frame (given feedback is in world frame)
//      math::Vector3 childMomentArm =
//        this->childLink->GetWorldPose().rot.RotateVector(
//        this->anchorPos - cgPos);

//      // gzerr << "anchor [" << anchorPos
//      //       << "] iarm[" << this->childLink->GetInertial()->GetPose().pos
//      //       << "] childMomentArm[" << childMomentArm
//      //       << "] f1[" << wrench.body1Force
//      //       << "] t1[" << wrench.body1Torque
//      //       << "] fxp[" << wrench.body1Force.Cross(childMomentArm)
//      //       << "]\n";

//      wrench.body1Torque += wrench.body1Force.Cross(childMomentArm);
//    }

//    // convert torque from about parent CG to joint anchor location
//    if (this->parentLink)
//    {
//      // parent cg specified in child link frame
//      math::Vector3 cgPos = ((this->parentLink->GetInertial()->GetPose() +
//                            this->parentLink->GetWorldPose()) -
//                            this->childLink->GetWorldPose()).pos;

//      // rotate moement arms into world frame
//      math::Vector3 parentMomentArm =
//        this->childLink->GetWorldPose().rot.RotateVector(
//        this->anchorPos - cgPos);

//      wrench.body2Torque -= wrench.body2Force.Cross(parentMomentArm);

//      // A good check is that
//      // the computed body2Torque shoud in fact be opposite of body1Torque
//    }
//    else
//    {
//      // convert torque from about child CG to joint anchor location
//      // or simply use equal opposite force as body1 wrench
//      wrench.body2Force = -wrench.body1Force;
//      wrench.body2Torque = -wrench.body1Torque;
//    }
//  }
//  else
//  {
//    // forgot to set provide_feedback?
//    gzwarn << "GetForceTorque: forget to set <provide_feedback>?\n";
//  }

  return wrench;
}