residual-fly-angle.hxx
Go to the documentation of this file.
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2022, LAAS-CNRS
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 
14 
15 namespace sobec {
16 using namespace crocoddyl;
17 template <typename Scalar>
19  boost::shared_ptr<StateMultibody> state,
20  const pinocchio::FrameIndex frame_id, const Scalar slope,
21  const Scalar height, const Scalar dist, const Scalar width,
22  const std::size_t nu)
23  : Base(state, 2, nu, true, true, false),
24  frame_id(frame_id),
25  slope(slope),
26  height(height),
27  dist(dist),
28  width(width),
29  pin_model_(*state->get_pinocchio()) {}
30 
31 template <typename Scalar>
33  boost::shared_ptr<StateMultibody> state,
34  const pinocchio::FrameIndex frame_id, const Scalar slope,
35  const Scalar height, const Scalar dist, const Scalar width)
36  : Base(state, 2, true, true, false),
37  frame_id(frame_id),
38  slope(slope),
39  height(height),
40  dist(dist),
41  width(width),
42  pin_model_(*state->get_pinocchio()) {}
43 
44 template <typename Scalar>
46 
47 template <typename Scalar>
49  const boost::shared_ptr<ResidualDataAbstract>& data,
50  const Eigen::Ref<const VectorXs>& /*x*/,
51  const Eigen::Ref<const VectorXs>&) {
52  // Compute the residual residual give the reference CoM velocity
53 
54  Data* d = static_cast<Data*>(data.get());
55 
56  pinocchio::updateFramePlacement(pin_model_, *d->pinocchio, frame_id);
57 
58  d->sig =
59  1 /
60  (1 + exp(-width * (d->pinocchio->oMf[frame_id].translation()[0] - dist)));
61  d->sig_dt = width * d->sig * (1 - d->sig);
62  d->alpha = atan(height * d->sig_dt);
63  // d->alpha = d->sig_dt;
64  d->rotation_alpha.row(0) << cos(d->alpha), 0, sin(d->alpha);
65  d->rotation_alpha.row(1) << 0, 1, 0;
66  d->rotation_alpha.row(2) << -sin(d->alpha), 0, cos(d->alpha);
67  d->ez = exp(-slope *
68  (d->pinocchio->oMf[frame_id].translation()[2] - height * d->sig));
69 
70  data->r = (d->rotation_alpha *
71  pinocchio::getFrameVelocity(pin_model_, *d->pinocchio, frame_id,
72  pinocchio::LOCAL_WORLD_ALIGNED)
73  .linear())
74  .head(2);
75  // data->r = pinocchio::getFrameVelocity(pin_model_, *d->pinocchio, frame_id,
76  // pinocchio::LOCAL_WORLD_ALIGNED).linear().head(2);
77  data->r *= d->ez;
78 }
79 
80 template <typename Scalar>
82  const boost::shared_ptr<ResidualDataAbstract>& data,
83  const Eigen::Ref<const VectorXs>& /*x*/,
84  const Eigen::Ref<const VectorXs>&) {
85  Data* d = static_cast<Data*>(data.get());
86  const std::size_t nv = state_->get_nv();
87 
88  /* Let' s do a little bit of maths ...
89  * r = v/e with e=exp(z/2)
90  * r' = v'/e - v/e z'/2 = v'/e - r/2 z'
91  *
92  * Wrong, we should consider l_v the local velocity, with o_v = oRl l_v
93  * Then v=R l_v
94  * v' = R l_v' + R' l_v = R l_v' - l_v x Jr
95  * Then r' = v'/e - r/2 z' = R l_v'/e - l_v x Jr/e - r/2 z'
96  */
97 
98  pinocchio::getFrameVelocityDerivatives(pin_model_, *d->pinocchio, frame_id,
99  pinocchio::LOCAL, d->l_dnu_dq,
100  d->l_dnu_dv);
101  const Vector3s& v = pinocchio::getFrameVelocity(pin_model_, *d->pinocchio,
102  frame_id, pinocchio::LOCAL)
103  .linear();
104  const Matrix3s& R = d->pinocchio->oMf[frame_id].rotation();
105 
106  d->sig_ddt = width * d->sig_dt * (1 - 2 * d->sig);
107  d->alpha_dt =
108  height * d->sig_ddt / (1 + (height * d->sig_dt) * (height * d->sig_dt));
109  d->rotation_alpha_dt.row(0) << -sin(d->alpha), 0, cos(d->alpha);
110  d->rotation_alpha_dt.row(1) << 0, 0, 0;
111  d->rotation_alpha_dt.row(2) << -cos(d->alpha), 0, -sin(d->alpha);
112  // First compute LWA derivatives of the velocity
113  d->vxJ.noalias() = pinocchio::skew(-v) * d->l_dnu_dv.template bottomRows<3>();
114  d->vxJ += d->l_dnu_dq.template topRows<3>();
115  d->o_dv_dq = R * d->vxJ;
116  d->o_dv_dv = R * d->l_dnu_dv.template topRows<3>();
117 
118  // First term with derivative of v
119  data->Rx.leftCols(nv) =
120  (d->rotation_alpha * d->o_dv_dq).template topRows<2>();
121  data->Rx.rightCols(nv) =
122  (d->rotation_alpha * d->o_dv_dv).template topRows<2>();
123  // data->Rx.leftCols(nv) = d->o_dv_dq.template topRows<2>();
124  // data->Rx.rightCols(nv) = d->o_dv_dv.template topRows<2>();
125  data->Rx *= d->ez;
126 
127  // Second term with derivative of z
128  data->Rx.leftCols(nv).row(0) -=
129  data->r[0] * slope *
130  (d->o_dv_dv.row(2) - height * d->sig_dt * d->o_dv_dv.row(0));
131  data->Rx.leftCols(nv).row(1) -=
132  data->r[1] * slope *
133  (d->o_dv_dv.row(2) - height * d->sig_dt * d->o_dv_dv.row(0));
134 
135  data->Rx.leftCols(nv).row(0) +=
136  (d->rotation_alpha_dt *
137  pinocchio::getFrameVelocity(pin_model_, *d->pinocchio, frame_id,
138  pinocchio::LOCAL_WORLD_ALIGNED)
139  .linear())[0] *
140  d->ez * d->alpha_dt * d->o_dv_dv.row(0);
141  data->Rx.leftCols(nv).row(1) +=
142  (d->rotation_alpha_dt *
143  pinocchio::getFrameVelocity(pin_model_, *d->pinocchio, frame_id,
144  pinocchio::LOCAL_WORLD_ALIGNED)
145  .linear())[1] *
146  d->ez * d->alpha_dt * d->o_dv_dv.row(0);
147 }
148 
149 template <typename Scalar>
150 boost::shared_ptr<ResidualDataAbstractTpl<Scalar> >
152  DataCollectorAbstract* const data) {
153  return boost::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this,
154  data);
155 }
156 
157 template <typename Scalar>
158 const typename pinocchio::FrameIndex&
160  return frame_id;
161 }
162 
163 template <typename Scalar>
165  const pinocchio::FrameIndex& fid) {
166  frame_id = fid;
167 }
168 
169 } // namespace sobec
sobec::ResidualDataFlyAngleTpl::rotation_alpha_dt
Matrix3s rotation_alpha_dt
Definition: residual-fly-angle.hpp:195
sobec::ResidualModelFlyAngleTpl::Base
ResidualModelAbstractTpl< Scalar > Base
Definition: residual-fly-angle.hpp:38
sobec::ResidualModelFlyAngleTpl::calcDiff
virtual void calcDiff(const boost::shared_ptr< ResidualDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the derivatives of the CoM velocity residual.
Definition: residual-fly-angle.hxx:81
sobec::ResidualDataFlyAngleTpl::ez
Scalar ez
Definition: residual-fly-angle.hpp:188
sobec::ResidualModelFlyAngleTpl::calc
virtual void calc(const boost::shared_ptr< ResidualDataAbstract > &data, const Eigen::Ref< const VectorXs > &x, const Eigen::Ref< const VectorXs > &u)
Compute the residual.
Definition: residual-fly-angle.hxx:48
sobec::ResidualDataFlyAngleTpl::alpha
Scalar alpha
Definition: residual-fly-angle.hpp:190
sobec::ResidualDataFlyAngleTpl::sig_ddt
Scalar sig_ddt
Definition: residual-fly-angle.hpp:192
sobec::ResidualDataFlyAngleTpl::sig
Scalar sig
Definition: residual-fly-angle.hpp:189
sobec::ResidualModelFlyAngleTpl::DataCollectorAbstract
DataCollectorAbstractTpl< Scalar > DataCollectorAbstract
Definition: residual-fly-angle.hpp:42
sobec::ResidualModelFlyAngleTpl::set_frame_id
void set_frame_id(const pinocchio::FrameIndex &fid)
Modify the frame index.
Definition: residual-fly-angle.hxx:164
sobec::ResidualDataFlyAngleTpl::l_dnu_dq
Matrix6xs l_dnu_dq
Definition: residual-fly-angle.hpp:185
sobec::ResidualModelFlyAngleTpl::ResidualModelFlyAngleTpl
ResidualModelFlyAngleTpl(boost::shared_ptr< StateMultibody > state, const pinocchio::FrameIndex frame_id, const Scalar slope, const Scalar height, const Scalar dist, const Scalar width, const std::size_t nu)
Initialize the residual model.
Definition: residual-fly-angle.hxx:18
sobec::ResidualModelFlyAngleTpl::Matrix3s
MathBase::Matrix3s Matrix3s
Definition: residual-fly-angle.hpp:46
sobec::ResidualDataFlyAngleTpl::vxJ
Matrix3xs vxJ
Definition: residual-fly-angle.hpp:186
sobec::ResidualModelFlyAngleTpl::~ResidualModelFlyAngleTpl
virtual ~ResidualModelFlyAngleTpl()
Definition: residual-fly-angle.hxx:45
sobec
Definition: activation-quad-ref.hpp:19
sobec::ResidualDataFlyAngleTpl::alpha_dt
Scalar alpha_dt
Definition: residual-fly-angle.hpp:193
sobec::ResidualDataFlyAngleTpl::o_dv_dq
Matrix3xs o_dv_dq
Definition: residual-fly-angle.hpp:186
sobec::ResidualDataFlyAngleTpl::l_dnu_dv
Matrix6xs l_dnu_dv
Definition: residual-fly-angle.hpp:185
sobec::ResidualDataFlyAngleTpl
Definition: residual-fly-angle.hpp:134
sobec::ResidualModelFlyAngleTpl::Vector3s
MathBase::Vector3s Vector3s
Definition: residual-fly-angle.hpp:43
sobec::ResidualDataFlyAngleTpl::pinocchio
pinocchio::DataTpl< Scalar > * pinocchio
Pinocchio data.
Definition: residual-fly-angle.hpp:183
sobec::ResidualModelFlyAngleTpl::get_frame_id
const pinocchio::FrameIndex & get_frame_id() const
Return the frame index.
Definition: residual-fly-angle.hxx:159
sobec::ResidualDataFlyAngleTpl::o_dv_dv
Matrix3xs o_dv_dv
Definition: residual-fly-angle.hpp:186
sobec::ResidualModelFlyAngleTpl::createData
virtual boost::shared_ptr< ResidualDataAbstract > createData(DataCollectorAbstract *const data)
Definition: residual-fly-angle.hxx:151
sobec::ResidualDataFlyAngleTpl::sig_dt
Scalar sig_dt
Definition: residual-fly-angle.hpp:191
sobec::ResidualDataFlyAngleTpl::rotation_alpha
Matrix3s rotation_alpha
Definition: residual-fly-angle.hpp:194
residual-fly-angle.hpp