9 #ifndef CROCODDYL_MULTIBODY_ACTIONS_FREE_INVDYN_HPP_
10 #define CROCODDYL_MULTIBODY_ACTIONS_FREE_INVDYN_HPP_
12 #include "crocoddyl/core/actuation-base.hpp"
13 #include "crocoddyl/core/constraints/constraint-manager.hpp"
14 #include "crocoddyl/core/costs/cost-sum.hpp"
15 #include "crocoddyl/core/diff-action-base.hpp"
16 #include "crocoddyl/core/residual-base.hpp"
17 #include "crocoddyl/multibody/data/multibody.hpp"
18 #include "crocoddyl/multibody/fwd.hpp"
19 #include "crocoddyl/multibody/states/multibody.hpp"
38 template <
typename _Scalar>
42 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
46 typedef _Scalar Scalar;
57 typedef typename MathBase::VectorXs VectorXs;
70 std::shared_ptr<StateMultibody> state,
71 std::shared_ptr<ActuationModelAbstract> actuation,
72 std::shared_ptr<CostModelSum> costs);
83 std::shared_ptr<StateMultibody> state,
84 std::shared_ptr<ActuationModelAbstract> actuation,
85 std::shared_ptr<CostModelSum> costs,
86 std::shared_ptr<ConstraintModelManager> constraints);
99 virtual void calc(
const std::shared_ptr<DifferentialActionDataAbstract>& data,
100 const Eigen::Ref<const VectorXs>& x,
101 const Eigen::Ref<const VectorXs>& u)
override;
108 virtual void calc(
const std::shared_ptr<DifferentialActionDataAbstract>& data,
109 const Eigen::Ref<const VectorXs>& x)
override;
125 const std::shared_ptr<DifferentialActionDataAbstract>& data,
126 const Eigen::Ref<const VectorXs>& x,
127 const Eigen::Ref<const VectorXs>& u)
override;
135 const std::shared_ptr<DifferentialActionDataAbstract>& data,
136 const Eigen::Ref<const VectorXs>& x)
override;
143 virtual std::shared_ptr<DifferentialActionDataAbstract>
createData()
override;
154 template <
typename NewScalar>
162 const std::shared_ptr<DifferentialActionDataAbstract>& data)
override;
178 const std::shared_ptr<DifferentialActionDataAbstract>& data,
179 Eigen::Ref<VectorXs> u,
const Eigen::Ref<const VectorXs>& x,
180 const std::size_t maxiter = 100,
181 const Scalar tol = Scalar(1e-9))
override;
186 virtual std::size_t
get_ng()
const override;
191 virtual std::size_t
get_nh()
const override;
238 virtual void print(std::ostream& os)
const override;
249 void init(
const std::shared_ptr<StateMultibody>& state);
250 std::shared_ptr<ActuationModelAbstract> actuation_;
251 std::shared_ptr<CostModelSum> costs_;
252 std::shared_ptr<ConstraintModelManager> constraints_;
253 pinocchio::ModelTpl<Scalar>* pinocchio_;
270 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
275 typedef _Scalar Scalar;
282 typedef typename MathBase::VectorXs VectorXs;
283 typedef typename MathBase::MatrixXs MatrixXs;
292 const std::size_t nu)
293 :
Base(state, state->get_nv() - nu, state->get_nv(), true, true, true),
304 virtual void calc(
const std::shared_ptr<ResidualDataAbstract>& data,
305 const Eigen::Ref<const VectorXs>&,
306 const Eigen::Ref<const VectorXs>&)
override {
310 std::size_t nrow = 0;
311 for (std::size_t k = 0;
312 k < static_cast<std::size_t>(d->
actuation->tau_set.size()); ++k) {
324 virtual void calc(
const std::shared_ptr<ResidualDataAbstract>& data,
325 const Eigen::Ref<const VectorXs>&)
override {
336 virtual void calcDiff(
const std::shared_ptr<ResidualDataAbstract>& data,
337 const Eigen::Ref<const VectorXs>&,
338 const Eigen::Ref<const VectorXs>&)
override {
341 std::size_t nrow = 0;
342 const std::size_t nv =
state_->get_nv();
343 d->dtau_dx.leftCols(nv) = d->
pinocchio->dtau_dq;
344 d->dtau_dx.rightCols(nv) = d->
pinocchio->dtau_dv;
346 for (std::size_t k = 0;
347 k < static_cast<std::size_t>(d->
actuation->tau_set.size()); ++k) {
349 d->
Rx.row(nrow) = d->dtau_dx.row(k);
361 virtual void calcDiff(
const std::shared_ptr<ResidualDataAbstract>& data,
362 const Eigen::Ref<const VectorXs>&)
override {
374 return std::allocate_shared<typename Data::ResidualDataActuation>(
375 Eigen::aligned_allocator<typename Data::ResidualDataActuation>(),
390 template <
typename NewScalar>
397 ReturnType ret(std::static_pointer_cast<StateType>(
398 state_->template cast<NewScalar>()),
408 virtual void print(std::ostream& os)
const override {
409 os <<
"ResidualModelActuation {nx=" <<
state_->get_nx()
410 <<
", ndx=" <<
state_->get_ndx() <<
", nu=" <<
nu_ <<
", na=" <<
na_
421 template <
typename _Scalar>
424 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
425 typedef _Scalar Scalar;
433 typedef typename MathBase::VectorXs VectorXs;
435 template <
template <
typename Scalar>
class Model>
438 pinocchio(pinocchio::DataTpl<Scalar>(model->get_pinocchio())),
440 &
pinocchio, model->get_actuation()->createData(),
441 std::make_shared<JointDataAbstract>(
442 model->get_state(), model->get_actuation(), model->get_nu())),
446 const std::size_t nv = model->get_state()->get_nv();
447 Fu.leftCols(nv).diagonal().setOnes();
448 multibody.joint->da_du.leftCols(nv).diagonal().setOnes();
449 costs->shareMemory(
this);
473 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
475 typedef _Scalar Scalar;
481 typedef typename MathBase::MatrixXs MatrixXs;
483 template <
template <
typename Scalar>
class Model>
487 dtau_dx(model->get_state()->get_nv(), model->get_state()->get_ndx()) {
494 "Invalid argument: the shared data should be derived from "
495 "DataCollectorActMultibody");
518 #include <crocoddyl/multibody/actions/free-invdyn.hxx>
520 CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(
522 CROCODDYL_DECLARE_EXTERN_TEMPLATE_STRUCT(
Abstract class for the actuation-mapping model.
Residual-based constraint.
Abstract class for differential action model.
std::shared_ptr< StateAbstract > state_
Model of the state.
VectorXs g_ub_
Lower bound of the inequality constraints.
std::size_t nu_
Control dimension.
std::size_t nh_
Number of equality constraints.
VectorXs g_lb_
Lower bound of the inequality constraints.
std::size_t ng_
Number of inequality constraints.
virtual void calcDiff(const std::shared_ptr< ResidualDataAbstract > &data, const Eigen::Ref< const VectorXs > &) override
std::size_t na_
Dimension of the joint torques.
ResidualModelActuation(std::shared_ptr< StateMultibody > state, const std::size_t nu)
Initialize the actuation residual model.
virtual void calc(const std::shared_ptr< ResidualDataAbstract > &data, const Eigen::Ref< const VectorXs > &) override
std::shared_ptr< StateAbstract > state_
State description.
virtual void calcDiff(const std::shared_ptr< ResidualDataAbstract > &data, const Eigen::Ref< const VectorXs > &, const Eigen::Ref< const VectorXs > &) override
Compute the derivatives of the actuation residual.
virtual void calc(const std::shared_ptr< ResidualDataAbstract > &data, const Eigen::Ref< const VectorXs > &, const Eigen::Ref< const VectorXs > &) override
Compute the actuation residual.
DifferentialActionModelFreeInvDynamicsTpl< NewScalar >::ResidualModelActuation cast() const
Cast the actuation-residual model to a different scalar type.
std::size_t nu_
Control dimension.
virtual void print(std::ostream &os) const override
Print relevant information of the actuation residual model.
virtual std::shared_ptr< ResidualDataAbstract > createData(DataCollectorAbstract *const data) override
Create the actuation residual data.
Differential action model for free inverse dynamics in multibody systems.
virtual bool checkData(const std::shared_ptr< DifferentialActionDataAbstract > &data) override
Checks that a specific data belongs to the free inverse-dynamics model.
pinocchio::ModelTpl< Scalar > & get_pinocchio() const
Return the Pinocchio model.
virtual void calcDiff(const std::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x) override
virtual void calc(const std::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u) override
Compute the system acceleration, cost value and constraint residuals.
const std::shared_ptr< ActuationModelAbstract > & get_actuation() const
Return the actuation model.
virtual std::shared_ptr< DifferentialActionDataAbstract > createData() override
Create the free inverse-dynamics data.
virtual std::size_t get_ng() const override
Return the number of inequality constraints.
virtual const VectorXs & get_g_ub() const override
Return the upper bound of the inequality constraints.
const std::shared_ptr< ConstraintModelManager > & get_constraints() const
Return the constraint model manager.
virtual std::size_t get_nh_T() const override
Return the number of equality terminal constraints.
DifferentialActionModelFreeInvDynamicsTpl(std::shared_ptr< StateMultibody > state, std::shared_ptr< ActuationModelAbstract > actuation, std::shared_ptr< CostModelSum > costs)
Initialize the free inverse-dynamics action model.
virtual std::size_t get_nh() const override
Return the number of equality constraints.
DifferentialActionModelFreeInvDynamicsTpl< NewScalar > cast() const
Cast the free-invdyn model to a different scalar type.
DifferentialActionModelFreeInvDynamicsTpl(std::shared_ptr< StateMultibody > state, std::shared_ptr< ActuationModelAbstract > actuation, std::shared_ptr< CostModelSum > costs, std::shared_ptr< ConstraintModelManager > constraints)
Initialize the free inverse-dynamics action model.
virtual std::size_t get_ng_T() const override
Return the number of equality terminal constraints.
virtual void calc(const std::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x) override
virtual void quasiStatic(const std::shared_ptr< DifferentialActionDataAbstract > &data, Eigen::Ref< VectorXs > u, const Eigen::Ref< const VectorXs > &x, const std::size_t maxiter=100, const Scalar tol=Scalar(1e-9)) override
Computes the quasic static commands.
const std::shared_ptr< CostModelSum > & get_costs() const
Return the cost model.
virtual void print(std::ostream &os) const override
Print relevant information of the free inverse-dynamics model.
virtual void calcDiff(const std::shared_ptr< DifferentialActionDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u) override
Compute the derivatives of the dynamics, cost and constraint functions.
virtual const VectorXs & get_g_lb() const override
Return the lower bound of the inequality constraints.
Abstract class for residual models.
std::shared_ptr< StateAbstract > state_
State description.
std::size_t nu_
Control dimension.
State multibody representation.
MatrixXs Fx
Jacobian of the dynamics w.r.t. the state .
MatrixXs Fu
Jacobian of the dynamics w.r.t. the control .
MatrixXs Luu
Hessian of the cost w.r.t. the control .
VectorXs Lx
Jacobian of the cost w.r.t. the state .
MatrixXs Lxx
Hessian of the cost w.r.t. the state .
VectorXs Lu
Jacobian of the cost w.r.t. the control .
VectorXs xout
evolution state
MatrixXs Ru
Jacobian of the residual vector with respect the control.
MatrixXs Rx
Jacobian of the residual vector with respect the state.
std::shared_ptr< ActuationDataAbstract > actuation
Actuation data.
DataCollectorAbstract * shared
Shared data allocated by the action model.
VectorXs r
Residual vector.
pinocchio::DataTpl< Scalar > * pinocchio
Pinocchio data.
MatrixXs Fu
Jacobian of the dynamics w.r.t. the control .
std::shared_ptr< ConstraintDataManager > constraints
Constraints data.
VectorXs tmp_xstatic
State point used for computing the quasi-static input.
DataCollectorJointActMultibody multibody
Multibody data.
pinocchio::DataTpl< Scalar > pinocchio
Pinocchio data.
std::shared_ptr< CostDataSum > costs
Costs data.
MatrixXs Ru
Jacobian of the residual vector with respect the control.
MatrixXs Rx
Jacobian of the residual vector with respect the state.
DataCollectorAbstract * shared
Shared data allocated by the action model.
VectorXs r
Residual vector.