- 3.0.2 rigid body dynamics module.
FixBaseFDSystem.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"
11 
12 namespace ct {
13 namespace rbd {
14 
29 template <class RBDDynamics, size_t ACT_STATE_DIM = 0, bool EE_ARE_CONTROL_INPUTS = false>
30 class FixBaseFDSystem : public FixBaseSystemBase<RBDDynamics,
31  RBDDynamics::NSTATE + ACT_STATE_DIM, // state dim
32  RBDDynamics::NJOINTS + EE_ARE_CONTROL_INPUTS * RBDDynamics::N_EE * 3> // control dim
33 {
34 public:
35  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
36 
37  static const size_t N_EE = RBDDynamics::N_EE;
38  static const size_t STATE_DIM = RBDDynamics::NSTATE + ACT_STATE_DIM; // combined state dim
39  static const size_t CONTROL_DIM = RBDDynamics::NJOINTS + EE_ARE_CONTROL_INPUTS * N_EE * 3; // combined control dim
40  static const size_t ACTUATOR_STATE_DIM = ACT_STATE_DIM;
41 
43  using SCALAR = typename BASE::SCALAR;
48 
52 
53 
58  FixBaseFDSystem(const RigidBodyPose_t& basePose = RigidBodyPose_t()) : BASE(basePose), actuatorDynamics_(nullptr) {}
63  FixBaseFDSystem(std::shared_ptr<ActuatorDynamics_t> actuatorDynamics,
64  const RigidBodyPose_t& basePose = RigidBodyPose_t())
65  : BASE(basePose), actuatorDynamics_(actuatorDynamics)
66  {
67  }
68 
77  {
78  if (arg.actuatorDynamics_)
79  {
80  actuatorDynamics_ = std::shared_ptr<ActuatorDynamics_t>(arg.actuatorDynamics_->clone());
81  }
82  }
83 
85  virtual ~FixBaseFDSystem() = default;
86 
89  const SCALAR& t,
90  const control_vector_t& controlIn,
91  state_vector_t& derivative) override
92  {
93  FixBaseRobotState_t robotState(state);
94 
95  // map the joint velocities (unchanged, no damping)
96  derivative.template topRows<BASE::NJOINTS>() = robotState.joints().getVelocities();
97 
98  // temporary variable for the control (will get modified by the actuator dynamics, if applicable)
99  control_vector_t control = controlIn;
100 
101  // compute actuator dynamics and their control output
102  computeActuatorDynamics(robotState, t, controlIn, derivative, control);
103 
104  // Cache updated rbd state
105  typename RBDDynamics::ExtLinkForces_t linkForces(Eigen::Matrix<SCALAR, 6, 1>::Zero());
106 
107  // add end effector forces as control inputs (if applicable)
108  if (EE_ARE_CONTROL_INPUTS == true)
109  {
110  for (size_t i = 0; i < RBDDynamics::N_EE; i++)
111  {
112  auto endEffector = this->dynamics_.kinematics().getEndEffector(i);
113  size_t linkId = endEffector.getLinkId();
114  linkForces[static_cast<typename RBDDynamics::ROBCOGEN::LinkIdentifiers>(linkId)] =
115  this->dynamics_.kinematics().mapForceFromWorldToLink3d(
116  control.template segment<3>(BASE::NJOINTS + i * 3), this->basePose_,
117  robotState.joints().getPositions(), i);
118  }
119  }
120 
121  typename RBDDynamics::JointAcceleration_t jAcc;
122 
123  this->dynamics_.FixBaseForwardDynamics(
124  robotState.joints(), control.template head<BASE::NJOINTS>(), linkForces, jAcc);
125 
126  derivative.template segment<BASE::NJOINTS>(BASE::NJOINTS) = jAcc.getAcceleration();
127  }
128 
130  template <typename T = void>
131  typename std::enable_if<(ACT_STATE_DIM > 0), T>::type computeActuatorDynamics(const FixBaseRobotState_t& robotState,
132  const SCALAR& t,
133  const control_vector_t& controlIn,
134  state_vector_t& stateDerivative,
135  control_vector_t& controlOut)
136  {
137  // get references to the current actuator position, velocity and input
138  const Eigen::Ref<const typename control_vector_t::Base> actControlIn =
139  controlIn.template topRows<BASE::NJOINTS>();
140 
141  actuator_state_vector_t actStateDerivative; // todo use vector block for this?
142  actuatorDynamics_->computeActuatorDynamics(
143  robotState.joints(), robotState.actuatorState(), t, actControlIn, actStateDerivative);
144 
145  stateDerivative.template tail<ACTUATOR_STATE_DIM>() = actStateDerivative;
146 
147  // overwrite control with actuator control output as a function of current robot and actuator states
148  controlOut = actuatorDynamics_->computeControlOutput(robotState.joints(), robotState.actuatorState());
149  }
150 
152  template <typename T = void>
153  typename std::enable_if<(ACT_STATE_DIM == 0), T>::type computeActuatorDynamics(
154  const FixBaseRobotState_t& robotState,
155  const SCALAR& t,
156  const control_vector_t& controlIn,
157  state_vector_t& stateDerivative,
158  control_vector_t& controlOut)
159  {
160  }
161 
164  {
166  }
167 
169  std::shared_ptr<ActuatorDynamics_t> getActuatorDynamics() { return actuatorDynamics_; }
171  template <typename T = typename FixBaseRobotState_t::actuator_state_vector_t>
172  typename std::enable_if<(ACT_STATE_DIM > 0), T>::type computeConsistentActuatorState(
173  const JointState<BASE::NJOINTS, SCALAR>& jStateRef,
175  {
176  return actuatorDynamics_->computeStateFromOutput(jStateRef, torqueRef);
177  }
178 
180  template <typename T = typename FixBaseRobotState_t::actuator_state_vector_t>
181  typename std::enable_if<(ACT_STATE_DIM > 0), T>::type computeConsistentActuatorState(
182  const JointState<BASE::NJOINTS, SCALAR>& jStateRef)
183  {
184  const ct::core::ControlVector<BASE::NJOINTS> torqueRef = computeIDTorques(jStateRef);
185  return computeConsistentActuatorState(jStateRef, torqueRef);
186  }
187 
188 
189 private:
191  std::shared_ptr<ActuatorDynamics_t> actuatorDynamics_;
192 };
193 
194 } // namespace rbd
195 } // namespace ct
static const size_t CONTROL_DIM
Definition: FixBaseFDSystem.h:39
static const size_t ACTUATOR_STATE_DIM
Definition: FixBaseFDSystem.h:40
FixBaseFDSystem(const RigidBodyPose_t &basePose=RigidBodyPose_t())
constructor
Definition: FixBaseFDSystem.h:58
A fix base rigid body system that uses forward dynamics.
Definition: FixBaseFDSystem.h:30
static const size_t STATE_DIM
number of end-effectors
Definition: FixBaseFDSystem.h:38
joint state and joint velocity
Definition: JointState.h:21
virtual ~FixBaseFDSystem()=default
destructor
std::shared_ptr< ActuatorDynamics_t > getActuatorDynamics()
get pointer to actuator dynamics
Definition: FixBaseFDSystem.h:169
typename BASE::control_vector_t control_vector_t
Definition: FixBaseFDSystem.h:45
typename BASE::state_vector_t state_vector_t
Definition: FixBaseFDSystem.h:44
ct::core::ControlVector< NJOINTS > computeIDTorques(const JointState< NJOINTS, SCALAR > &jState, const JointAcceleration_t &jAcc=JointAcceleration_t(Eigen::Matrix< SCALAR, NJOINTS, 1 >::Zero()))
compute inverse dynamics torques
Definition: FixBaseSystemBase.h:67
std::enable_if<(ACT_STATE_DIM > 0), T >::type computeActuatorDynamics(const FixBaseRobotState_t &robotState, const SCALAR &t, const control_vector_t &controlIn, state_vector_t &stateDerivative, control_vector_t &controlOut)
evaluate the actuator dynamics if actuators are enabled
Definition: FixBaseFDSystem.h:131
FixBaseFDSystem(std::shared_ptr< ActuatorDynamics_t > actuatorDynamics, const RigidBodyPose_t &basePose=RigidBodyPose_t())
constructor including actuator dynamics
Definition: FixBaseFDSystem.h:63
Base class for fix-base robot systems.
Definition: FixBaseSystemBase.h:19
FixBaseFDSystem(const FixBaseFDSystem &arg)
copy constructor
Definition: FixBaseFDSystem.h:76
void computeControlledDynamics(const state_vector_t &state, const SCALAR &t, const control_vector_t &controlIn, state_vector_t &derivative) override
compute the controlled dynamics of the fixed base robotic system
Definition: FixBaseFDSystem.h:88
actuator_state_vector_t & actuatorState()
accessor to actuator state
Definition: FixBaseRobotState.h:94
static EIGEN_MAKE_ALIGNED_OPERATOR_NEW const size_t N_EE
Definition: FixBaseFDSystem.h:37
JointPositionBlock getVelocities()
get joint velocity
Definition: JointState.h:131
LinkIdentifiers
Definition: declarations.h:40
for i
typename BASE::RigidBodyPose_t RigidBodyPose_t
Definition: FixBaseFDSystem.h:47
virtual FixBaseFDSystem< RBDDynamics, ACT_STATE_DIM, EE_ARE_CONTROL_INPUTS > * clone() const override
deep cloning
Definition: FixBaseFDSystem.h:163
core::ControlVector< CONTROL_D, SCALAR > control_vector_t
Definition: FixBaseSystemBase.h:37
typename BASE::JointAcceleration_t JointAcceleration_t
Definition: FixBaseFDSystem.h:46
JointAcceleration< NJOINTS, SCALAR > JointAcceleration_t
Definition: FixBaseSystemBase.h:38
static const size_t NJOINTS
number of joints
Definition: FixBaseSystemBase.h:28
typename BASE::SCALAR SCALAR
Definition: FixBaseFDSystem.h:43
Definition: ActuatorDynamics.h:25
typename FixBaseRobotState_t::actuator_state_vector_t actuator_state_vector_t
Definition: FixBaseFDSystem.h:51
std::enable_if<(ACT_STATE_DIM > 0), T >::type computeConsistentActuatorState(const JointState< BASE::NJOINTS, SCALAR > &jStateRef)
if actuator dynamics enabled, this method allows to design a consistent actuator state ...
Definition: FixBaseFDSystem.h:181
tpl::RigidBodyPose< SCALAR > RigidBodyPose_t
Definition: FixBaseSystemBase.h:33
std::enable_if<(ACT_STATE_DIM > 0), T >::type computeConsistentActuatorState(const JointState< BASE::NJOINTS, SCALAR > &jStateRef, const ct::core::ControlVector< BASE::NJOINTS > &torqueRef)
if actuator dynamics enabled, this method allows to design a consistent actuator state ...
Definition: FixBaseFDSystem.h:172
whole fix base robot state, i.e. Joint state, Actuator Dynamics (and fix-base pose) ...
Definition: FixBaseRobotState.h:26
typename RBDDynamics::SCALAR SCALAR
Definition: FixBaseSystemBase.h:31
JointPositionBlock getPositions()
get joint state
Definition: JointState.h:76
JointState_t & joints()
accessor to joint state
Definition: FixBaseRobotState.h:102
std::enable_if<(ACT_STATE_DIM==0), T >::type computeActuatorDynamics(const FixBaseRobotState_t &robotState, const SCALAR &t, const control_vector_t &controlIn, state_vector_t &stateDerivative, control_vector_t &controlOut)
do nothing if actuators disabled
Definition: FixBaseFDSystem.h:153
ct::core::StateVector< STATE_D, SCALAR > state_vector_t
Definition: FixBaseSystemBase.h:36
ct::core::StateVector< ACT_STATE_DIM, SCALAR > actuator_state_vector_t
Definition: FixBaseRobotState.h:38