- 3.0.2 rigid body dynamics module.
FixBaseFDSystemSymplectic.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 
8 #include "FixBaseSystemBase.h"
10 
11 
12 #define ACTUATOR_DYNAMICS_ENABLED \
13  template <size_t ACT_STATE_DIMS> \
14  typename std::enable_if<(ACT_STATE_DIMS > 0), void>::type
15 #define ACTUATOR_DYNAMICS_DISABLED \
16  template <size_t ACT_STATE_DIMS> \
17  typename std::enable_if<(ACT_STATE_DIMS <= 0), void>::type
18 
19 namespace ct {
20 namespace rbd {
21 
40 template <class RBDDynamics, size_t ACT_STATE_DIM = 0, bool EE_ARE_CONTROL_INPUTS = false>
42  : public RBDSystem<RBDDynamics, false>,
43  public core::SymplecticSystem<RBDDynamics::NJOINTS + ACT_STATE_DIM / 2, // p-dim of combined system
44  RBDDynamics::NJOINTS + ACT_STATE_DIM / 2, // v-dim of combined system
45  RBDDynamics::NJOINTS + EE_ARE_CONTROL_INPUTS * RBDDynamics::N_EE * 3, // input-dim of combined system
46  typename RBDDynamics::SCALAR>
47 {
48 public:
49  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
50 
51  static const size_t N_EE = RBDDynamics::N_EE; // number of end-effectors
52  static const size_t STATE_DIM = RBDDynamics::NSTATE + ACT_STATE_DIM; // RBD state-dim plus actuator state-dim
53  static const size_t CONTROL_DIM =
54  RBDDynamics::NJOINTS + EE_ARE_CONTROL_INPUTS * RBDDynamics::N_EE * 3; // torques plus EE-forces
55  static const size_t ACTUATOR_STATE_DIM = ACT_STATE_DIM;
56  static const size_t NJOINTS = RBDDynamics::NJOINTS;
57 
58  using Dynamics = RBDDynamics;
59  using SCALAR = typename RBDDynamics::SCALAR;
60  using Base = core::SymplecticSystem<STATE_DIM / 2, STATE_DIM / 2, CONTROL_DIM, SCALAR>;
62 
63  // typedefs state and controls on all occurring dimensions
68  using p_vector_full_t = core::StateVector<STATE_DIM / 2, SCALAR>;
69  using v_vector_full_t = core::StateVector<STATE_DIM / 2, SCALAR>;
70  using p_vector_rbd_t = core::StateVector<RBDDynamics::NSTATE / 2, SCALAR>;
71  using v_vector_rbd_t = core::StateVector<RBDDynamics::NSTATE / 2, SCALAR>;
72  using p_vector_act_t = core::StateVector<ACTUATOR_STATE_DIM / 2, SCALAR>;
73  using v_vector_act_t = core::StateVector<ACTUATOR_STATE_DIM / 2, SCALAR>;
74 
75 
78  : Base(core::SYSTEM_TYPE::SECOND_ORDER), dynamics_(RBDDynamics()), actuatorDynamics_(nullptr)
79  {
80  basePose_.setIdentity();
81  }
82 
84  FixBaseFDSystemSymplectic(std::shared_ptr<ActuatorDynamics_t> actuatorDynamics)
85  : Base(core::SYSTEM_TYPE::SECOND_ORDER), dynamics_(RBDDynamics()), actuatorDynamics_(actuatorDynamics)
86  {
87  basePose_.setIdentity();
88  }
89 
98  : Base(arg), basePose_(arg.basePose_), dynamics_(RBDDynamics())
99  {
100  if (arg.actuatorDynamics_)
101  {
102  actuatorDynamics_ = std::shared_ptr<ActuatorDynamics_t>(arg.actuatorDynamics_->clone());
103  }
104  }
105 
109  virtual RBDDynamics& dynamics() override { return dynamics_; }
111  virtual const RBDDynamics& dynamics() const override { return dynamics_; }
113  virtual void computePdot(const state_vector_full_t& x,
114  const v_vector_full_t& v,
115  const control_vector_full_t& controlIn,
116  p_vector_full_t& pDot) override
117  {
118  // the top rows hold the RBD velocities ...
119  pDot.template topRows<RBDDynamics::NJOINTS>() = v.template topRows<RBDDynamics::NJOINTS>();
120 
121  // full joint state vector
122  typename RBDDynamics::JointState_t jState = jointStateFromVector(x);
123 
124  // compute he pDot for the actuator part
125  computeActuatorPdot<ACT_STATE_DIM>(jState, x, v, controlIn, pDot);
126  }
127 
128 
129  ACTUATOR_DYNAMICS_ENABLED computeActuatorPdot(const typename RBDDynamics::JointState_t& jState,
130  const state_vector_full_t& x,
131  const v_vector_full_t& v,
132  const control_vector_full_t& controlIn,
133  p_vector_full_t& pDot)
134  {
135  // get const references to the current actuator position, velocity, and actuator input
136  const Eigen::Ref<const typename p_vector_act_t::Base> actPos =
137  x.template segment<ACT_STATE_DIM / 2>(RBDDynamics::NJOINTS);
138  const Eigen::Ref<const typename v_vector_act_t::Base> actVel = v.template bottomRows<ACT_STATE_DIM / 2>();
139  const Eigen::Ref<const typename control_vector_full_t::Base> actControlIn =
140  controlIn.template topRows<RBDDynamics::NJOINTS>();
141 
142  // assemble temporary actuator state
143  state_vector_act_t actState;
144  actState << actPos, actVel;
145 
146  p_vector_act_t actPdot;
147  actuatorDynamics_->computePdot(jState, actState, actVel, actControlIn, actPdot);
148 
149  // ... the bottom rows hold the actuator dynamics
150  pDot.template bottomRows<ACT_STATE_DIM / 2>() = actPdot;
151  }
152 
153 
154  ACTUATOR_DYNAMICS_DISABLED computeActuatorPdot(const typename RBDDynamics::JointState_t& jState,
155  const state_vector_full_t& x,
156  const v_vector_full_t& v,
157  const control_vector_full_t& controlIn,
158  p_vector_full_t& pDot)
159  {
160  }
161 
162 
164  virtual void computeVdot(const state_vector_full_t& x,
165  const p_vector_full_t& p,
166  const control_vector_full_t& controlIn,
167  v_vector_full_t& vDot) override
168  {
169  // temporary variable for the control (will get modified by the actuator dynamics, if applicable)
170  control_vector_full_t control = controlIn;
171 
172  // extract the current RBD joint state from the state vector // todo make method to get joint state
173  typename RBDDynamics::JointState_t jState = jointStateFromVector(x);
174 
175  computeActuatorVdot<ACT_STATE_DIM>(jState, x, p, controlIn, vDot, control);
176 
177  // Cache updated rbd state
178  typename RBDDynamics::ExtLinkForces_t linkForces(Eigen::Matrix<SCALAR, 6, 1>::Zero());
179 
180  // add end effector forces as control inputs (if applicable)
181  if (EE_ARE_CONTROL_INPUTS == true)
182  {
183  for (size_t i = 0; i < RBDDynamics::N_EE; i++)
184  {
185  auto endEffector = dynamics_.kinematics().getEndEffector(i);
186  size_t linkId = endEffector.getLinkId();
187  linkForces[static_cast<typename RBDDynamics::ROBCOGEN::LinkIdentifiers>(linkId)] =
188  dynamics_.kinematics().mapForceFromWorldToLink3d(
189  control.template segment<3>(RBDDynamics::NJOINTS + i * 3), basePose_, jState.getPositions(), i);
190  }
191  }
192 
193  typename RBDDynamics::JointAcceleration_t jAcc;
194 
195  dynamics_.FixBaseForwardDynamics(jState, control.template head<RBDDynamics::NJOINTS>(), linkForces, jAcc);
196 
197  vDot.template topRows<RBDDynamics::NJOINTS>() = jAcc.getAcceleration();
198  }
199 
200 
201  ACTUATOR_DYNAMICS_ENABLED computeActuatorVdot(const typename RBDDynamics::JointState_t& jState,
202  const state_vector_full_t& x,
203  const p_vector_full_t& p,
204  const control_vector_full_t& controlIn,
205  v_vector_full_t& vDot,
206  control_vector_full_t& controlOut)
207  {
208  // get references to the current actuator position, velocity and input
209  const Eigen::Ref<const typename p_vector_act_t::Base> actPos =
210  p.template segment<ACT_STATE_DIM / 2>(RBDDynamics::NJOINTS);
211  const Eigen::Ref<const typename v_vector_act_t::Base> actVel = x.template bottomRows<ACT_STATE_DIM / 2>();
212  const Eigen::Ref<const typename control_vector_full_t::Base> actControlIn =
213  controlIn.template topRows<RBDDynamics::NJOINTS>();
214 
215  // assemble temporary actuator state
216  state_vector_act_t actState;
217  actState << actPos, actVel;
218 
219  // ... the bottom rows hold the actuator dynamics
220  v_vector_act_t actVdot;
221  actuatorDynamics_->computeVdot(jState, actState, actPos, actControlIn, actVdot);
222  vDot.template bottomRows<ACT_STATE_DIM / 2>() = actVdot;
223 
224  // overwrite control with actuator control output as a function of current robot and actuator states
225  controlOut = actuatorDynamics_->computeControlOutput(jState, actState);
226  }
227 
228 
229  ACTUATOR_DYNAMICS_DISABLED computeActuatorVdot(const typename RBDDynamics::JointState_t& jState,
230  const state_vector_full_t& x,
231  const p_vector_full_t& p,
232  const control_vector_full_t& controlIn,
233  v_vector_full_t& vDot,
234  control_vector_full_t& controlOut)
235  {
236  }
237 
238 
241  {
243  }
244 
246  static const typename RBDDynamics::JointState_t jointStateFromVector(const state_vector_full_t& x_full)
247  {
248  typename RBDDynamics::JointState_t jointState;
249  jointState.getPositions() = x_full.template head<RBDDynamics::NJOINTS>();
250  jointState.getVelocities() = x_full.template segment<RBDDynamics::NJOINTS>(STATE_DIM / 2);
251  return jointState;
252  }
253 
255  static const typename RBDDynamics::RBDState_t RBDStateFromVector(const state_vector_full_t& x_full,
257  {
258  typename RBDDynamics::RBDState_t rbdState;
259  rbdState.setZero();
260  rbdState.basePose() = basePose;
261  rbdState.joints() = jointStateFromVector(x_full);
262  return rbdState;
263  }
264 
267  {
268  state_vector_rbd_t x_small;
269  x_small.template head<NJOINTS>() = x_full.template head<NJOINTS>();
270  x_small.template tail<NJOINTS>() = x_full.template segment<NJOINTS>(STATE_DIM / 2);
271  return x_small;
272  }
273 
276  {
277  state_vector_act_t actState;
278  actState.template topRows<ACT_STATE_DIM / 2>() =
279  state.template segment<ACT_STATE_DIM / 2>(RBDDynamics::NSTATE / 2);
280  actState.template bottomRows<ACT_STATE_DIM / 2>() = state.template bottomRows<ACT_STATE_DIM / 2>();
281  return actState;
282  }
283 
286  const state_vector_act_t& x_act = state_vector_act_t::Zero())
287  {
288  state_vector_full_t fullState;
289  fullState.template head<NJOINTS>() = x_rbd.template head<NJOINTS>();
290  fullState.template segment<ACTUATOR_STATE_DIM / 2>(NJOINTS) = x_act.template head<ACTUATOR_STATE_DIM / 2>();
291  fullState.template segment<NJOINTS>(STATE_DIM / 2) = x_rbd.template tail<NJOINTS>();
292  fullState.template tail<ACTUATOR_STATE_DIM / 2>() = x_act.template tail<ACTUATOR_STATE_DIM / 2>();
293  return fullState;
294  }
295 
296 private:
298  tpl::RigidBodyPose<SCALAR> basePose_;
299 
301  RBDDynamics dynamics_;
302 
304  std::shared_ptr<ActuatorDynamics_t> actuatorDynamics_;
305 };
306 
307 } // namespace rbd
308 } // namespace ct
309 
310 #undef ACTUATOR_DYNAMICS_ENABLED
311 #undef ACTUATOR_DYNAMICS_DISABLED
static const size_t STATE_DIM
Definition: FixBaseFDSystemSymplectic.h:52
static const state_vector_act_t actuatorStateFromVector(const state_vector_full_t &state)
transform control systems state vector to the pure actuator state
Definition: FixBaseFDSystemSymplectic.h:275
Pose of a single rigid body.
Definition: RigidBodyPose.h:29
#define ACTUATOR_DYNAMICS_ENABLED
Definition: FixBaseFDSystemSymplectic.h:12
RBDDynamics Dynamics
Definition: FixBaseFDSystemSymplectic.h:58
#define ACTUATOR_DYNAMICS_DISABLED
Definition: FixBaseFDSystemSymplectic.h:15
ACTUATOR_DYNAMICS_DISABLED computeActuatorVdot(const typename RBDDynamics::JointState_t &jState, const state_vector_full_t &x, const p_vector_full_t &p, const control_vector_full_t &controlIn, v_vector_full_t &vDot, control_vector_full_t &controlOut)
Definition: FixBaseFDSystemSymplectic.h:229
SECOND_ORDER
virtual ~FixBaseFDSystemSymplectic()
destructor
Definition: FixBaseFDSystemSymplectic.h:107
FixBaseFDSystemSymplectic(std::shared_ptr< ActuatorDynamics_t > actuatorDynamics)
constructor including actuator dynamics
Definition: FixBaseFDSystemSymplectic.h:84
Definition: ActuatorDynamicsSymplectic.h:20
virtual FixBaseFDSystemSymplectic< RBDDynamics, ACT_STATE_DIM, EE_ARE_CONTROL_INPUTS > * clone() const override
deep cloning
Definition: FixBaseFDSystemSymplectic.h:240
virtual void computePdot(const state_vector_full_t &x, const v_vector_full_t &v, const control_vector_full_t &controlIn, p_vector_full_t &pDot) override
compute position derivatives, for both RBD system and actuator dynamics, if applicable ...
Definition: FixBaseFDSystemSymplectic.h:113
LinkIdentifiers
Definition: declarations.h:40
virtual const RBDDynamics & dynamics() const override
get dynamics (const)
Definition: FixBaseFDSystemSymplectic.h:111
This is a common interface class for an RBDSystem.
Definition: RBDSystem.h:15
static const size_t NJOINTS
Definition: FixBaseFDSystemSymplectic.h:56
for i
FixBaseFDSystemSymplectic(const FixBaseFDSystemSymplectic &arg)
copy constructor
Definition: FixBaseFDSystemSymplectic.h:97
ACTUATOR_DYNAMICS_DISABLED computeActuatorPdot(const typename RBDDynamics::JointState_t &jState, const state_vector_full_t &x, const v_vector_full_t &v, const control_vector_full_t &controlIn, p_vector_full_t &pDot)
Definition: FixBaseFDSystemSymplectic.h:154
static const size_t CONTROL_DIM
Definition: FixBaseFDSystemSymplectic.h:53
FixBaseFDSystemSymplectic()
constructor
Definition: FixBaseFDSystemSymplectic.h:77
SYSTEM_TYPE
ACTUATOR_DYNAMICS_ENABLED computeActuatorVdot(const typename RBDDynamics::JointState_t &jState, const state_vector_full_t &x, const p_vector_full_t &p, const control_vector_full_t &controlIn, v_vector_full_t &vDot, control_vector_full_t &controlOut)
Definition: FixBaseFDSystemSymplectic.h:201
static const RBDDynamics::RBDState_t RBDStateFromVector(const state_vector_full_t &x_full, const tpl::RigidBodyPose< SCALAR > &basePose=tpl::RigidBodyPose< SCALAR >())
transform control systems state vector to a RBDState
Definition: FixBaseFDSystemSymplectic.h:255
virtual RBDDynamics & dynamics() override
get dynamics
Definition: FixBaseFDSystemSymplectic.h:109
ACTUATOR_DYNAMICS_ENABLED computeActuatorPdot(const typename RBDDynamics::JointState_t &jState, const state_vector_full_t &x, const v_vector_full_t &v, const control_vector_full_t &controlIn, p_vector_full_t &pDot)
Definition: FixBaseFDSystemSymplectic.h:129
static const state_vector_full_t toFullState(const state_vector_rbd_t &x_rbd, const state_vector_act_t &x_act=state_vector_act_t::Zero())
transform from "reduced" to full coordinates including actuator dynamics
Definition: FixBaseFDSystemSymplectic.h:285
A fix base rigid body system that uses forward dynamics.
Definition: FixBaseFDSystemSymplectic.h:41
typename RBDDynamics::SCALAR SCALAR
Definition: FixBaseFDSystemSymplectic.h:59
static const size_t ACTUATOR_STATE_DIM
Definition: FixBaseFDSystemSymplectic.h:55
virtual void computeVdot(const state_vector_full_t &x, const p_vector_full_t &p, const control_vector_full_t &controlIn, v_vector_full_t &vDot) override
compute velocity derivatives, for both RBD system and actuator dynamics
Definition: FixBaseFDSystemSymplectic.h:164
static EIGEN_MAKE_ALIGNED_OPERATOR_NEW const size_t N_EE
Definition: FixBaseFDSystemSymplectic.h:51
static const state_vector_rbd_t rbdStateFromVector(const state_vector_full_t &x_full)
transform control systems state vector to a RBDState
Definition: FixBaseFDSystemSymplectic.h:266
static const RBDDynamics::JointState_t jointStateFromVector(const state_vector_full_t &x_full)
full vector to ct JointState
Definition: FixBaseFDSystemSymplectic.h:246