residual-vel-collision.hxx
Go to the documentation of this file.
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2021, LAAS-CNRS, University of Edinburgh, INRIA
5 // Copyright note valid unless otherwise stated in individual files.
6 // All rights reserved.
8 
9 #include <crocoddyl/core/utils/exception.hpp>
10 #include <pinocchio/algorithm/frames-derivatives.hpp>
11 #include <pinocchio/algorithm/frames.hpp>
12 #include <pinocchio/algorithm/kinematics-derivatives.hpp>
13 
15 
16 namespace sobec {
17 
18 using namespace crocoddyl;
19 
20 template <typename Scalar>
21 ResidualModelVelCollisionTpl<Scalar>::ResidualModelVelCollisionTpl(
22  boost::shared_ptr<StateMultibody> state, const std::size_t nu,
23  boost::shared_ptr<GeometryModel> geom_model,
24  const pinocchio::PairIndex pair_id, const pinocchio::FrameIndex frame_id,
25  const pinocchio::ReferenceFrame type, const double beta)
26  : Base(state, 2, nu, true, false, false),
27  pin_model_(state->get_pinocchio()),
28  geom_model_(geom_model),
29  pair_id_(pair_id),
30  joint_id_(pin_model_->frames[frame_id].parent),
31  frame_id_(frame_id),
32  type_(type),
33  beta_(beta) {}
34 
35 template <typename Scalar>
36 ResidualModelVelCollisionTpl<Scalar>::~ResidualModelVelCollisionTpl() {}
37 
38 template <typename Scalar>
39 void ResidualModelVelCollisionTpl<Scalar>::calc(
40  const boost::shared_ptr<ResidualDataAbstract> &data,
41  const Eigen::Ref<const VectorXs> &x, const Eigen::Ref<const VectorXs> &) {
42  Data *d = static_cast<Data *>(data.get());
43 
44  const Eigen::VectorBlock<const Eigen::Ref<const VectorXs>, Eigen::Dynamic> q =
45  x.head(state_->get_nq());
46  pinocchio::updateGeometryPlacements(*pin_model_.get(), *d->pinocchio,
47  *geom_model_.get(), d->geometry, q);
48  pinocchio::computeDistance(*geom_model_.get(), d->geometry, pair_id_);
49 
50  // calculate residual
51  d->e = d->geometry.distanceResults[pair_id_].nearest_points[0] -
52  d->geometry.distanceResults[pair_id_].nearest_points[1];
53  d->V = (pinocchio::getFrameVelocity(*pin_model_.get(), *d->pinocchio,
54  frame_id_, type_))
55  .toVector()
56  .head(2);
57  d->n = d->e.norm() * d->e.norm();
58  data->r = d->V / (d->n + beta_);
59 }
60 
61 template <typename Scalar>
62 void ResidualModelVelCollisionTpl<Scalar>::calcDiff(
63  const boost::shared_ptr<ResidualDataAbstract> &data,
64  const Eigen::Ref<const VectorXs> &, const Eigen::Ref<const VectorXs> &) {
65  Data *d = static_cast<Data *>(data.get());
66 
67  const std::size_t nv = state_->get_nv();
68 
69  // Calculate the vector from the joint jointId to the collision p1, expressed
70  // in world frame
71  d->d = d->geometry.distanceResults[pair_id_].nearest_points[0] -
72  d->pinocchio->oMi[joint_id_].translation();
73  pinocchio::getJointJacobian(*pin_model_.get(), *d->pinocchio, joint_id_,
74  pinocchio::LOCAL_WORLD_ALIGNED, d->J);
75 
76  // Get the partial derivatives of the local frame velocity
77  pinocchio::getFrameVelocityDerivatives(*pin_model_.get(), *d->pinocchio,
78  frame_id_, type_, d->Vx.leftCols(nv),
79  d->Vx.rightCols(nv));
80 
81  // Calculate the Jacobian at p1
82  d->J.template topRows<3>().noalias() +=
83  pinocchio::skew(d->d).transpose() * (d->J.template bottomRows<3>());
84 
85  // --- Compute the residual derivatives ---
86  data->Rx.topLeftCorner(2, nv) = -2 / ((d->n + beta_) * (d->n + beta_)) *
87  d->V * d->e.transpose() *
88  d->J.template topRows<3>();
89  data->Rx.topLeftCorner(2, nv) +=
90  1 / (d->n + beta_) * d->Vx.leftCols(nv).topRows(2);
91  data->Rx.topRightCorner(2, nv) =
92  1 / (d->n + beta_) * d->Vx.rightCols(nv).topRows(2);
93 }
94 
95 template <typename Scalar>
96 boost::shared_ptr<ResidualDataAbstractTpl<Scalar> >
97 ResidualModelVelCollisionTpl<Scalar>::createData(
98  DataCollectorAbstract *const data) {
99  return boost::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this,
100  data);
101 }
102 
103 template <typename Scalar>
104 const pinocchio::GeometryModel &
105 ResidualModelVelCollisionTpl<Scalar>::get_geometry() const {
106  return *geom_model_.get();
107 }
108 
109 } // namespace sobec
sobec
Definition: activation-quad-ref.hpp:19
residual-vel-collision.hpp
sobec::newcontacts::x
@ x
Definition: contact1d.hpp:26