Crocoddyl
floating-base-thrusters.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2014-2024, Heriot-Watt University
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
8 
9 #ifndef CROCODDYL_MULTIBODY_ACTUATIONS_PROPELLERS_HPP_
10 #define CROCODDYL_MULTIBODY_ACTUATIONS_PROPELLERS_HPP_
11 
12 #include <pinocchio/multibody/fwd.hpp>
13 #include <pinocchio/spatial/se3.hpp>
14 #include <vector>
15 
16 #include "crocoddyl/core/actuation-base.hpp"
17 #include "crocoddyl/core/utils/exception.hpp"
18 #include "crocoddyl/multibody/states/multibody.hpp"
19 
20 namespace crocoddyl {
21 
22 enum ThrusterType { CW = 0, CCW };
23 
24 template <typename _Scalar>
25 struct ThrusterTpl {
26  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
27 
28  typedef _Scalar Scalar;
29  typedef pinocchio::SE3Tpl<Scalar> SE3;
31 
42  ThrusterTpl(const SE3& pose, const Scalar ctorque,
43  const ThrusterType type = CW,
44  const Scalar min_thrust = Scalar(0.),
45  const Scalar max_thrust = std::numeric_limits<Scalar>::infinity())
46  : pose(pose),
48  type(type),
51 
62  ThrusterTpl(const Scalar ctorque, const ThrusterType type = CW,
63  const Scalar min_thrust = Scalar(0.),
64  const Scalar max_thrust = std::numeric_limits<Scalar>::infinity())
65  : pose(SE3::Identity()),
67  type(type),
70  ThrusterTpl(const ThrusterTpl<Scalar>& clone)
71  : pose(clone.pose),
72  ctorque(clone.ctorque),
73  type(clone.type),
74  min_thrust(clone.min_thrust),
75  max_thrust(clone.max_thrust) {}
76 
77  ThrusterTpl& operator=(const ThrusterTpl<Scalar>& other) {
78  if (this != &other) {
79  pose = other.pose;
80  ctorque = other.ctorque;
81  type = other.type;
82  min_thrust = other.min_thrust;
83  max_thrust = other.max_thrust;
84  }
85  return *this;
86  }
87 
88  template <typename OtherScalar>
89  bool operator==(const ThrusterTpl<OtherScalar>& other) const {
90  return (pose == other.pose && ctorque == other.ctorque &&
91  type == other.type && min_thrust == other.min_thrust &&
92  max_thrust == other.max_thrust);
93  }
94 
95  friend std::ostream& operator<<(std::ostream& os,
96  const ThrusterTpl<Scalar>& X) {
97  os << " pose:" << std::endl
98  << X.pose << " ctorque: " << X.ctorque << std::endl
99  << " type: " << X.type << std::endl
100  << "min_thrust: " << X.min_thrust << std::endl
101  << "max_thrust: " << X.max_thrust << std::endl;
102  return os;
103  }
104 
105  SE3 pose;
106  Scalar ctorque;
107  ThrusterType type;
109  Scalar min_thrust;
110  Scalar max_thrust;
111 };
112 
131 template <typename _Scalar>
133  : public ActuationModelAbstractTpl<_Scalar> {
134  public:
135  typedef _Scalar Scalar;
141  typedef typename MathBase::Vector3s Vector3s;
142  typedef typename MathBase::VectorXs VectorXs;
143  typedef typename MathBase::MatrixXs MatrixXs;
144 
153  boost::shared_ptr<StateMultibody> state,
154  const std::vector<Thruster>& thrusters)
155  : Base(state,
156  state->get_nv() -
157  state->get_pinocchio()
158  ->joints[(
159  state->get_pinocchio()->existJointName("root_joint")
160  ? state->get_pinocchio()->getJointId("root_joint")
161  : 0)]
162  .nv() +
163  thrusters.size()),
164  thrusters_(thrusters),
165  n_thrusters_(thrusters.size()),
166  W_thrust_(state_->get_nv(), nu_),
167  update_data_(true) {
168  if (!state->get_pinocchio()->existJointName("root_joint")) {
169  throw_pretty(
170  "Invalid argument: "
171  << "the first joint has to be a root one (e.g., free-flyer joint)");
172  }
173  // Update the joint actuation part
174  W_thrust_.setZero();
175  if (nu_ > n_thrusters_) {
176  W_thrust_.bottomRightCorner(nu_ - n_thrusters_, nu_ - n_thrusters_)
177  .diagonal()
178  .setOnes();
179  }
180  // Update the floating base actuation part
182  }
184 
193  virtual void calc(const boost::shared_ptr<Data>& data,
194  const Eigen::Ref<const VectorXs>&,
195  const Eigen::Ref<const VectorXs>& u) {
196  if (static_cast<std::size_t>(u.size()) != nu_) {
197  throw_pretty("Invalid argument: "
198  << "u has wrong dimension (it should be " +
199  std::to_string(nu_) + ")");
200  }
201  if (update_data_) {
202  updateData(data);
203  }
204  data->tau.noalias() = data->dtau_du * u;
205  }
206 
215 #ifndef NDEBUG
216  virtual void calcDiff(const boost::shared_ptr<Data>& data,
217  const Eigen::Ref<const VectorXs>&,
218  const Eigen::Ref<const VectorXs>&) {
219 #else
220  virtual void calcDiff(const boost::shared_ptr<Data>&,
221  const Eigen::Ref<const VectorXs>&,
222  const Eigen::Ref<const VectorXs>&) {
223 #endif
224  // The derivatives has constant values which were set in createData.
225  assert_pretty(MatrixXs(data->dtau_du).isApprox(W_thrust_),
226  "dtau_du has wrong value");
227  }
228 
229  virtual void commands(const boost::shared_ptr<Data>& data,
230  const Eigen::Ref<const VectorXs>&,
231  const Eigen::Ref<const VectorXs>& tau) {
232  data->u.noalias() = data->Mtau * tau;
233  }
234 
235 #ifndef NDEBUG
236  virtual void torqueTransform(const boost::shared_ptr<Data>& data,
237  const Eigen::Ref<const VectorXs>&,
238  const Eigen::Ref<const VectorXs>&) {
239 #else
240  virtual void torqueTransform(const boost::shared_ptr<Data>&,
241  const Eigen::Ref<const VectorXs>&,
242  const Eigen::Ref<const VectorXs>&) {
243 #endif
244  // The torque transform has constant values which were set in createData.
245  assert_pretty(MatrixXs(data->Mtau).isApprox(Mtau_), "Mtau has wrong value");
246  }
247 
253  virtual boost::shared_ptr<Data> createData() {
254  boost::shared_ptr<Data> data =
255  boost::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this);
256  updateData(data);
257  return data;
258  }
259 
263  const std::vector<Thruster>& get_thrusters() const { return thrusters_; }
264 
268  std::size_t get_nthrusters() const { return n_thrusters_; }
269 
278  void set_thrusters(const std::vector<Thruster>& thrusters) {
279  if (static_cast<std::size_t>(thrusters.size()) != n_thrusters_) {
280  throw_pretty("Invalid argument: "
281  << "the number of thrusters is wrong (it should be " +
282  std::to_string(n_thrusters_) + ")");
283  }
284  thrusters_ = thrusters;
285  // Update the mapping matrix from thrusters thrust to body force/moments
286  for (std::size_t i = 0; i < n_thrusters_; ++i) {
287  const Thruster& p = thrusters_[i];
288  const Vector3s& f_z = p.pose.rotation() * Vector3s::UnitZ();
289  W_thrust_.template topRows<3>().col(i) += f_z;
290  W_thrust_.template middleRows<3>(3).col(i).noalias() +=
291  p.pose.translation().cross(Vector3s::UnitZ());
292  switch (p.type) {
293  case CW:
294  W_thrust_.template middleRows<3>(3).col(i) += p.ctorque * f_z;
295  break;
296  case CCW:
297  W_thrust_.template middleRows<3>(3).col(i) -= p.ctorque * f_z;
298  break;
299  }
300  }
301  // Compute the torque transform matrix from generalized torques to joint
302  // torque inputs
303  Mtau_ = pseudoInverse(MatrixXs(W_thrust_));
304  S_.noalias() = W_thrust_ * Mtau_;
305  update_data_ = true;
306  }
307 
308  const MatrixXs& get_Wthrust() const { return W_thrust_; }
309 
310  const MatrixXs& get_S() const { return S_; }
311 
312  void print(std::ostream& os) const {
313  os << "ActuationModelFloatingBaseThrusters {nu=" << nu_
314  << ", nthrusters=" << n_thrusters_ << ", thrusters=" << std::endl;
315  for (std::size_t i = 0; i < n_thrusters_; ++i) {
316  os << std::to_string(i) << ": " << thrusters_[i];
317  }
318  os << "}";
319  }
320 
321  protected:
322  std::vector<Thruster> thrusters_;
323  std::size_t n_thrusters_;
324  MatrixXs W_thrust_;
325  MatrixXs Mtau_;
327  MatrixXs S_;
328 
329  bool update_data_;
330  using Base::nu_;
331  using Base::state_;
332 
333  private:
334  void updateData(const boost::shared_ptr<Data>& data) {
335  data->dtau_du = W_thrust_;
336  data->Mtau = Mtau_;
337  const std::size_t nv = state_->get_nv();
338  for (std::size_t k = 0; k < nv; ++k) {
339  if (fabs(S_(k, k)) < std::numeric_limits<Scalar>::epsilon()) {
340  data->tau_set[k] = false;
341  } else {
342  data->tau_set[k] = true;
343  }
344  }
345  update_data_ = false;
346  }
347 };
348 
349 } // namespace crocoddyl
350 
351 #endif // CROCODDYL_MULTIBODY_ACTUATIONS_PROPELLERS_HPP_
Abstract class for the actuation-mapping model.
boost::shared_ptr< StateAbstract > state_
Model of the state.
std::size_t nu_
Dimension of joint torque inputs.
Actuation models for floating base systems actuated with thrusters.
MatrixXs S_
Selection matrix for under-actuation part.
const std::vector< Thruster > & get_thrusters() const
Return the vector of thrusters.
std::vector< Thruster > thrusters_
Vector of thrusters.
boost::shared_ptr< StateAbstract > state_
Model of the state.
std::size_t nu_
Dimension of joint torque inputs.
ActuationModelFloatingBaseThrustersTpl(boost::shared_ptr< StateMultibody > state, const std::vector< Thruster > &thrusters)
Initialize the floating base actuation model equipped with thrusters.
std::size_t get_nthrusters() const
Return the number of thrusters.
virtual void torqueTransform(const boost::shared_ptr< Data > &data, const Eigen::Ref< const VectorXs > &, const Eigen::Ref< const VectorXs > &)
Compute the torque transform from generalized torques to joint torque inputs.
virtual void calc(const boost::shared_ptr< Data > &data, const Eigen::Ref< const VectorXs > &, const Eigen::Ref< const VectorXs > &u)
Compute the actuation signal and actuation set from its thrust and joint torque inputs .
MatrixXs W_thrust_
Matrix from thrusters thrusts to body wrench.
virtual void commands(const boost::shared_ptr< Data > &data, const Eigen::Ref< const VectorXs > &, const Eigen::Ref< const VectorXs > &tau)
Compute the joint torque input from the generalized torques.
virtual void calcDiff(const boost::shared_ptr< Data > &data, const Eigen::Ref< const VectorXs > &, const Eigen::Ref< const VectorXs > &)
Compute the Jacobians of the floating base thruster actuation function.
void print(std::ostream &os) const
Print relevant information of the residual model.
void set_thrusters(const std::vector< Thruster > &thrusters)
Modify the vector of thrusters.
virtual boost::shared_ptr< Data > createData()
Create the floating base thruster actuation data.
State multibody representation.
Definition: multibody.hpp:35
Scalar min_thrust
Minimum thrust.
ThrusterTpl(const SE3 &pose, const Scalar ctorque, const ThrusterType type=CW, const Scalar min_thrust=Scalar(0.), const Scalar max_thrust=std::numeric_limits< Scalar >::infinity())
Initialize the thruster in a give pose from the root joint.
Scalar max_thrust
Minimum thrust.
ThrusterTpl(const Scalar ctorque, const ThrusterType type=CW, const Scalar min_thrust=Scalar(0.), const Scalar max_thrust=std::numeric_limits< Scalar >::infinity())
Initialize the thruster in a pose in the origin of the root joint.
Scalar ctorque
Coefficient of generated torque per thrust.