- 3.0.2 rigid body dynamics module.
ConstraintJacobian.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
10 
11 #include "FrameJacobian.h"
12 
16 namespace ct {
17 namespace rbd {
18 namespace tpl {
19 
20 template <typename Kinematics, size_t OUTPUTS, size_t NJOINTS, typename SCALAR>
21 class ConstraintJacobian : public OperationalJacobianBase<OUTPUTS, NJOINTS, SCALAR>
22 {
23 public:
24  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
25 
28  typedef Eigen::Matrix<SCALAR, 3, 3> Matrix3s;
29 
31 
32  virtual ~ConstraintJacobian(){};
33 
34  void SetBlockZero(const int& row)
35  {
36  // todo: check if needed
37  this->J();
38  this->J_.template block<3, NJOINTS + 6>(row, 0).setZero();
39  }
40 
41  static const size_t BASE_DOF = 6;
42 
43  size_t c_size_ = 0;
44  std::vector<int> ee_indices_;
45  std::array<bool, Kinematics::NUM_EE> eeInContact_;
46 
47 
48  void getJacobianOriginDerivativeNumdiff(const RBDState_t& state, jacobian_t& dJdt)
49  {
50  /*
51  * Takes the numdiff (single sided) of getContactJacobian. derivative is dJ/dt = dJ/dq * dq/dt
52  * Because the contactJacobian (defined in the base frame) is independent of floating base coordinates
53  * -> only numdiff against joints
54  */
55  jacobian_t Jc0, Jc1;
56  RBDState_t state1 = state;
57  getJacobianOrigin(state, Jc0);
58 
59  SCALAR eps_ = sqrt(Eigen::NumTraits<SCALAR>::epsilon());
60 
61  dJdt.setZero();
62  for (size_t i = 0; i < NJOINTS; i++) //
63  {
64  SCALAR h = eps_ * std::max(core::tpl::TraitSelector<SCALAR>::Trait::fabs(state.joints().getPositions()(i)),
65  SCALAR(1.0)); // h = eps_ * max(abs(qj(i)), 1.0)
66  state1.joints().getPositions()(i) += h;
67  getJacobianOrigin(state1, Jc1);
68  dJdt += (Jc1 - Jc0) / h * state.joints().getVelocities()(i);
69  state1.joints().getPositions()(i) = state.joints().getPositions()(i);
70  }
71  }
72 
73  virtual void getJacobianOriginDerivative(const RBDState_t& state, jacobian_t& dJdt) override
74  {
75  /*
76  * Analytic approach for Jacobian derivative
77  * Derived from https://doi.org/10.1007/s11044-012-9334-7
78  */
79  // Temp objects
80  Eigen::Matrix<SCALAR, 3, NJOINTS + 6> dJdt_eeId;
81  Eigen::Matrix<SCALAR, 6, NJOINTS> Jc_geometric;
82  Eigen::Matrix<SCALAR, 3, NJOINTS> Jc_Rotational, Jc_Translational, dJdt_joints;
83  Eigen::Matrix<SCALAR, 3, 1> dJidqj;
84  Eigen::Matrix<SCALAR, 3, 6> dJdt_base;
85  Eigen::Matrix<SCALAR, 3, 1> eeVelocityWrtBase; // Time derivative of the postion vector from base to EE
86 
87  dJdt_base.setZero();
88  dJdt.setZero();
89  for (size_t ee = 0; ee < ee_indices_.size(); ee++)
90  {
91  if (eeInContact_[ee_indices_[ee]])
92  {
93  // Collect current contact Jacobians
94  Jc_geometric =
95  kinematics_.robcogen().getJacobianBaseEEbyId(ee_indices_[ee], state.joints().getPositions());
96  Jc_Rotational = Jc_geometric.template topRows<3>();
97  Jc_Translational = Jc_geometric.template bottomRows<3>();
98 
99  // Compute dJdt for the joint columns
100  dJdt_joints.setZero();
101  for (size_t i = 0; i < NJOINTS; i++)
102  { // Loop over columns i of dJdt_joints
103  for (size_t j = 0; j < NJOINTS; j++)
104  { // Loop over derivative w.r.t each joint j and sum
105  if (i >= j)
106  {
107  dJidqj = (Jc_Rotational.template block<3, 1>(0, j))
108  .template cross(Jc_Translational.template block<3, 1>(0, i));
109  }
110  else
111  { // i < j
112  dJidqj = (Jc_Rotational.template block<3, 1>(0, i))
113  .template cross(Jc_Translational.template block<3, 1>(0, j));
114  }
115  dJdt_joints.template block<3, 1>(0, i) += dJidqj * state.joints().getVelocities()(j);
116  }
117  }
118 
119  // Add the base columns: [-skew(v), 0_(3x3)]
120  eeVelocityWrtBase = Jc_Translational * state.joints().getVelocities();
121  dJdt_base(1, 2) = eeVelocityWrtBase(0); // x
122  dJdt_base(2, 1) = -eeVelocityWrtBase(0);
123  dJdt_base(0, 2) = -eeVelocityWrtBase(1); // y
124  dJdt_base(2, 0) = eeVelocityWrtBase(1);
125  dJdt_base(0, 1) = eeVelocityWrtBase(2); // z
126  dJdt_base(1, 0) = -eeVelocityWrtBase(2);
127 
128  // Fill in the rows of the full dJdt
129  dJdt.template block<3, 6>(ee_indices_[ee] * 3, 0) = dJdt_base;
130  dJdt.template block<3, NJOINTS>(ee_indices_[ee] * 3, 6) = dJdt_joints;
131  }
132  }
133  }
134 
135  virtual void getJacobianOrigin(const RBDState_t& state, jacobian_t& Jc) override
136  {
137  Jc.setZero();
138  for (size_t ee = 0; ee < ee_indices_.size(); ee++)
139  {
140  if (eeInContact_[ee_indices_[ee]])
141  {
142  Eigen::Matrix<SCALAR, 3, NJOINTS + 6> J_eeId;
143  kindr::Position<SCALAR, 3> eePosition =
144  kinematics_.getEEPositionInBase(ee_indices_[ee], state.joints().getPositions());
145  Eigen::Matrix<SCALAR, 3, NJOINTS> J_single =
146  kinematics_.robcogen()
147  .getJacobianBaseEEbyId(ee_indices_[ee], state.joints().getPositions())
148  .template bottomRows<3>();
150  Matrix3s::Identity(), eePosition.toImplementation(), J_single, J_eeId);
151  Jc.template block<3, NJOINTS + 6>(ee_indices_[ee] * 3, 0) = J_eeId;
152  }
153  }
154  }
155 
156 
157 private:
158  Kinematics kinematics_;
159 };
160 
161 } // namespace tpl
162 
163 template <typename Kinematics, size_t OUTPUTS, size_t NJOINTS>
165 
166 
167 } /* namespace rbd*/
168 } /* namespace ct*/
Definition: ConstraintJacobian.h:21
std::vector< int > ee_indices_
Definition: ConstraintJacobian.h:44
RBD & robcogen()
Definition: Kinematics.h:360
Definition: OperationalJacobianBase.h:20
static const size_t BASE_DOF
Definition: ConstraintJacobian.h:41
joint states and base states
Definition: RBDState.h:27
virtual void getJacobianOriginDerivative(const RBDState_t &state, jacobian_t &dJdt) override
Definition: ConstraintJacobian.h:73
Eigen::Matrix< SCALAR, 3, 3 > Matrix3s
Definition: ConstraintJacobian.h:28
void SetBlockZero(const int &row)
Definition: ConstraintJacobian.h:34
virtual ~ConstraintJacobian()
Definition: ConstraintJacobian.h:32
Position3Tpl getEEPositionInBase(size_t eeID, const typename JointState_t::Position &jointPosition)
Definition: Kinematics.h:145
virtual void getJacobianOrigin(const RBDState_t &state, jacobian_t &Jc) override
Definition: ConstraintJacobian.h:135
CppAD::AD< CppAD::cg::CG< double > > SCALAR
const jacobian_t & dJdt()
This method gets the time derivative of the floating-base Jacobian.
Definition: OperationalJacobianBase.h:60
for i
std::array< bool, Kinematics::NUM_EE > eeInContact_
Definition: ConstraintJacobian.h:45
OperationalJacobianBase< OUTPUTS, NJOINTS, SCALAR >::jacobian_t jacobian_t
Definition: ConstraintJacobian.h:27
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef RBDState< NJOINTS, SCALAR > RBDState_t
Definition: ConstraintJacobian.h:26
static void FromBaseJacToInertiaJacTranslation(const Matrix3s &i_R_b, const Vector3s &b_r_point, const Eigen::Matrix< SCALAR, 3, NUM_JOINTS > &b_J_point, Eigen::Matrix< SCALAR, 3, NUM_JOINTS+6 > &i_J_point)
Definition: FrameJacobian.h:77
void getJacobianOriginDerivativeNumdiff(const RBDState_t &state, jacobian_t &dJdt)
Definition: ConstraintJacobian.h:48
size_t c_size_
Definition: ConstraintJacobian.h:43
ConstraintJacobian()
Definition: ConstraintJacobian.h:30
A general class for computing Kinematic properties.
Definition: Kinematics.h:27
jacobian_t J_
Definition: OperationalJacobianBase.h:151
const jacobian_t & J()
This method gets the floating-base Jacobian.
Definition: OperationalJacobianBase.h:49