9 #ifndef CROCODDYL_MULTIBODY_ACTIONS_CONTACT_INVDYN_HPP_
10 #define CROCODDYL_MULTIBODY_ACTIONS_CONTACT_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/multibody/contacts/multiple-contacts.hpp"
17 #include "crocoddyl/multibody/data/contacts.hpp"
18 #include "crocoddyl/multibody/fwd.hpp"
19 #include "crocoddyl/multibody/states/multibody.hpp"
43 template <
typename _Scalar>
47 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
49 typedef _Scalar Scalar;
62 typedef typename MathBase::VectorXs VectorXs;
63 typedef typename MathBase::MatrixXs MatrixXs;
77 boost::shared_ptr<StateMultibody> state,
78 boost::shared_ptr<ActuationModelAbstract> actuation,
79 boost::shared_ptr<ContactModelMultiple> contacts,
80 boost::shared_ptr<CostModelSum> costs);
92 boost::shared_ptr<StateMultibody> state,
93 boost::shared_ptr<ActuationModelAbstract> actuation,
94 boost::shared_ptr<ContactModelMultiple> contacts,
95 boost::shared_ptr<CostModelSum> costs,
96 boost::shared_ptr<ConstraintModelManager> constraints);
110 const boost::shared_ptr<DifferentialActionDataAbstract>& data,
111 const Eigen::Ref<const VectorXs>& x,
const Eigen::Ref<const VectorXs>& u);
119 const boost::shared_ptr<DifferentialActionDataAbstract>& data,
120 const Eigen::Ref<const VectorXs>& x);
136 const boost::shared_ptr<DifferentialActionDataAbstract>& data,
137 const Eigen::Ref<const VectorXs>& x,
const Eigen::Ref<const VectorXs>& u);
145 const boost::shared_ptr<DifferentialActionDataAbstract>& data,
146 const Eigen::Ref<const VectorXs>& x);
153 virtual boost::shared_ptr<DifferentialActionDataAbstract>
createData();
160 const boost::shared_ptr<DifferentialActionDataAbstract>& data);
176 const boost::shared_ptr<DifferentialActionDataAbstract>& data,
177 Eigen::Ref<VectorXs> u,
const Eigen::Ref<const VectorXs>& x,
178 const std::size_t maxiter = 100,
const Scalar tol = Scalar(1e-9));
213 const boost::shared_ptr<CostModelSum>&
get_costs()
const;
229 virtual void print(std::ostream& os)
const;
240 void init(
const boost::shared_ptr<StateMultibody>& state);
241 boost::shared_ptr<ActuationModelAbstract> actuation_;
242 boost::shared_ptr<ContactModelMultiple> contacts_;
243 boost::shared_ptr<CostModelSum> costs_;
244 boost::shared_ptr<ConstraintModelManager> constraints_;
245 pinocchio::ModelTpl<Scalar>& pinocchio_;
263 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
265 typedef _Scalar Scalar;
271 typedef typename MathBase::VectorXs VectorXs;
281 const std::size_t nu,
const std::size_t nc)
282 :
Base(state, state->get_nv() - nu, state->get_nv() + nc, true, true,
295 virtual void calc(
const boost::shared_ptr<ResidualDataAbstract>& data,
296 const Eigen::Ref<const VectorXs>&,
297 const Eigen::Ref<const VectorXs>&) {
301 std::size_t nrow = 0;
302 for (std::size_t k = 0;
303 k < static_cast<std::size_t>(d->
actuation->tau_set.size()); ++k) {
315 virtual void calc(
const boost::shared_ptr<ResidualDataAbstract>& data,
316 const Eigen::Ref<const VectorXs>&) {
327 virtual void calcDiff(
const boost::shared_ptr<ResidualDataAbstract>& data,
328 const Eigen::Ref<const VectorXs>&,
329 const Eigen::Ref<const VectorXs>&) {
332 std::size_t nrow = 0;
333 const std::size_t nv =
state_->get_nv();
334 d->dtau_dx.leftCols(nv) = d->
pinocchio->dtau_dq;
335 d->dtau_dx.rightCols(nv) = d->
pinocchio->dtau_dv;
337 d->dtau_du.leftCols(nv) = d->
pinocchio->M;
338 d->dtau_du.rightCols(nc_) = -d->
contact->Jc.transpose();
339 for (std::size_t k = 0;
340 k < static_cast<std::size_t>(d->
actuation->tau_set.size()); ++k) {
342 d->
Rx.row(nrow) = d->dtau_dx.row(k);
343 d->
Ru.row(nrow) = d->dtau_du.row(k);
354 virtual void calcDiff(
const boost::shared_ptr<ResidualDataAbstract>& data,
355 const Eigen::Ref<const VectorXs>&) {
367 return boost::allocate_shared<typename Data::ResidualDataActuation>(
368 Eigen::aligned_allocator<typename Data::ResidualDataActuation>(),
377 virtual void print(std::ostream& os)
const {
378 os <<
"ResidualModelActuation {nx=" <<
state_->get_nx()
379 <<
", ndx=" <<
state_->get_ndx() <<
", nu=" <<
nu_ <<
", na=" << na_
410 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
412 typedef _Scalar Scalar;
418 typedef typename MathBase::VectorXs VectorXs;
419 typedef typename MathBase::MatrixXs MatrixXs;
430 const pinocchio::FrameIndex
id,
const std::size_t nr,
431 const std::size_t nc)
432 :
Base(state, nr, state->get_nv() + nc, true, true, true),
444 void calc(
const boost::shared_ptr<ResidualDataAbstract>& data,
445 const Eigen::Ref<const VectorXs>&,
446 const Eigen::Ref<const VectorXs>&) {
456 virtual void calc(
const boost::shared_ptr<ResidualDataAbstract>& data,
457 const Eigen::Ref<const VectorXs>&) {
468 void calcDiff(
const boost::shared_ptr<ResidualDataAbstract>& data,
469 const Eigen::Ref<const VectorXs>&,
470 const Eigen::Ref<const VectorXs>&) {
482 virtual void calcDiff(
const boost::shared_ptr<ResidualDataAbstract>& data,
483 const Eigen::Ref<const VectorXs>&) {
495 return boost::allocate_shared<typename Data::ResidualDataContact>(
496 Eigen::aligned_allocator<typename Data::ResidualDataContact>(),
this,
506 virtual void print(std::ostream& os)
const {
507 os <<
"ResidualModelContact {frame=" << frame_name_ <<
", nr=" <<
nr_
517 pinocchio::FrameIndex id_;
518 std::string frame_name_;
521 template <
typename _Scalar>
524 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
525 typedef _Scalar Scalar;
534 typedef typename MathBase::VectorXs VectorXs;
535 typedef typename MathBase::MatrixXs MatrixXs;
537 template <
template <
typename Scalar>
class Model>
539 Model<Scalar>*
const model)
541 pinocchio(pinocchio::DataTpl<Scalar>(model->get_pinocchio())),
543 &
pinocchio, model->get_actuation()->createData(),
544 boost::make_shared<JointDataAbstract>(
545 model->get_state(), model->get_actuation(), model->get_nu()),
546 model->get_contacts()->createData(&
pinocchio)),
549 model->get_contacts()->get_nc()),
551 model->get_actuation()->get_nu() +
552 model->get_contacts()->get_nc()) {
554 const std::size_t nv = model->get_state()->get_nv();
555 const std::size_t nc = model->get_contacts()->get_nc_total();
556 Fu.leftCols(nv).diagonal().setOnes();
557 multibody.joint->da_du.leftCols(nv).diagonal().setOnes();
558 MatrixXs df_dx = MatrixXs::Zero(nc, model->get_state()->get_ndx());
559 MatrixXs df_du = MatrixXs::Zero(nc, model->get_nu());
561 for (
typename ContactModelMultiple::ContactModelContainer::const_iterator
562 it = model->get_contacts()->get_contacts().begin();
563 it != model->get_contacts()->get_contacts().end(); ++it) {
564 const std::size_t nc = it->second->contact->get_nc();
565 df_du.block(fid, nv + fid, nc, nc).diagonal().setOnes();
568 model->get_contacts()->updateForceDiff(
multibody.contacts, df_dx, df_du);
571 costs->shareMemory(
this);
581 boost::shared_ptr<CostDataSum>
costs;
601 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
603 typedef _Scalar Scalar;
611 typedef typename MathBase::MatrixXs MatrixXs;
613 template <
template <
typename Scalar>
class Model>
617 dtau_dx(model->get_state()->get_nv(), model->get_state()->get_ndx()),
618 dtau_du(model->get_state()->get_nv(), model->get_nu()) {
624 "Invalid argument: the shared data should be derived from "
625 "DataCollectorActMultibodyInContact");
637 boost::shared_ptr<ContactDataMultiple>
contact;
647 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
649 typedef _Scalar Scalar;
657 template <
template <
typename Scalar>
class Model>
660 :
Base(model, data) {
665 "Invalid argument: the shared data should be derived from "
666 "DataCollectorMultibodyInContact");
668 typename ContactModelMultiple::ContactDataContainer::iterator it, end;
669 for (it = d->contacts->contacts.begin(),
670 end = d->contacts->contacts.end();
672 if (
id == it->second->frame) {
692 #include <crocoddyl/multibody/actions/contact-invdyn.hxx>
Abstract class for the actuation-mapping model.
Abstract class for differential action model.
VectorXs g_ub_
Lower bound of the inequality constraints.
boost::shared_ptr< StateAbstract > state_
Model of the state.
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.
Abstract class for residual models.
boost::shared_ptr< StateAbstract > state_
State description.
std::size_t nu_
Control dimension.
std::size_t nr_
Residual vector 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.
DataCollectorAbstract * shared
Shared data allocated by the action model.
VectorXs r
Residual vector.