GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/multibody/contact-base.hpp
Date: 2025-02-24 23:41:29
Exec Total Coverage
Lines: 10 10 100.0%
Branches: 8 16 50.0%

Line Branch Exec Source
1 ///////////////////////////////////////////////////////////////////////////////
2 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2023, LAAS-CNRS, University of Edinburgh,
5 // Heriot-Watt University
6 // Copyright note valid unless otherwise stated in individual files.
7 // All rights reserved.
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #ifndef CROCODDYL_MULTIBODY_CONTACT_BASE_HPP_
11 #define CROCODDYL_MULTIBODY_CONTACT_BASE_HPP_
12
13 #include <pinocchio/multibody/fwd.hpp>
14
15 #include "crocoddyl/core/mathbase.hpp"
16 #include "crocoddyl/core/utils/deprecate.hpp"
17 #include "crocoddyl/multibody/force-base.hpp"
18 #include "crocoddyl/multibody/fwd.hpp"
19 #include "crocoddyl/multibody/states/multibody.hpp"
20
21 namespace crocoddyl {
22
23 template <typename _Scalar>
24 class ContactModelAbstractTpl {
25 public:
26 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
27
28 typedef _Scalar Scalar;
29 typedef MathBaseTpl<Scalar> MathBase;
30 typedef ContactDataAbstractTpl<Scalar> ContactDataAbstract;
31 typedef StateMultibodyTpl<Scalar> StateMultibody;
32 typedef typename MathBase::VectorXs VectorXs;
33 typedef typename MathBase::MatrixXs MatrixXs;
34
35 /**
36 * @brief Initialize the contact abstraction
37 *
38 * @param[in] state State of the multibody system
39 * @param[in] type Type of contact
40 * @param[in] nc Dimension of the contact model
41 * @param[in] nu Dimension of the control vector
42 */
43 ContactModelAbstractTpl(std::shared_ptr<StateMultibody> state,
44 const pinocchio::ReferenceFrame type,
45 const std::size_t nc, const std::size_t nu);
46 ContactModelAbstractTpl(std::shared_ptr<StateMultibody> state,
47 const pinocchio::ReferenceFrame type,
48 const std::size_t nc);
49
50 DEPRECATED(
51 "Use constructor that passes the type type of contact, this assumes is "
52 "pinocchio::LOCAL",
53 ContactModelAbstractTpl(std::shared_ptr<StateMultibody> state,
54 const std::size_t nc, const std::size_t nu);)
55 DEPRECATED(
56 "Use constructor that passes the type type of contact, this assumes is "
57 "pinocchio::LOCAL",
58 ContactModelAbstractTpl(std::shared_ptr<StateMultibody> state,
59 const std::size_t nc);)
60 virtual ~ContactModelAbstractTpl();
61
62 /**
63 * @brief Compute the contact Jacobian and acceleration drift
64 *
65 * @param[in] data Contact data
66 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
67 * @param[in] u Control input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
68 */
69 virtual void calc(const std::shared_ptr<ContactDataAbstract>& data,
70 const Eigen::Ref<const VectorXs>& x) = 0;
71
72 /**
73 * @brief Compute the derivatives of the acceleration-based contact
74 *
75 * @param[in] data Contact data
76 * @param[in] x State point \f$\mathbf{x}\in\mathbb{R}^{ndx}\f$
77 * @param[in] u Control input \f$\mathbf{u}\in\mathbb{R}^{nu}\f$
78 */
79 virtual void calcDiff(const std::shared_ptr<ContactDataAbstract>& data,
80 const Eigen::Ref<const VectorXs>& x) = 0;
81
82 /**
83 * @brief Convert the force into a stack of spatial forces
84 *
85 * @param[in] data Contact data
86 * @param[in] force Contact force
87 */
88 virtual void updateForce(const std::shared_ptr<ContactDataAbstract>& data,
89 const VectorXs& force) = 0;
90
91 /**
92 * @brief Convert the force into a stack of spatial forces
93 *
94 * @param[in] data Contact data
95 * @param[in] force Contact force
96 */
97 void updateForceDiff(const std::shared_ptr<ContactDataAbstract>& data,
98 const MatrixXs& df_dx, const MatrixXs& df_du) const;
99
100 /**
101 * @brief Set the stack of spatial forces to zero
102 *
103 * @param[in] data Contact data
104 */
105 void setZeroForce(const std::shared_ptr<ContactDataAbstract>& data) const;
106
107 /**
108 * @brief Set the stack of spatial forces Jacobians to zero
109 *
110 * @param[in] data Contact data
111 */
112 void setZeroForceDiff(const std::shared_ptr<ContactDataAbstract>& data) const;
113
114 /**
115 * @brief Create the contact data
116 */
117 virtual std::shared_ptr<ContactDataAbstract> createData(
118 pinocchio::DataTpl<Scalar>* const data);
119
120 /**
121 * @brief Return the state
122 */
123 const std::shared_ptr<StateMultibody>& get_state() const;
124
125 /**
126 * @brief Return the dimension of the contact
127 */
128 std::size_t get_nc() const;
129
130 /**
131 * @brief Return the dimension of the control vector
132 */
133 std::size_t get_nu() const;
134
135 /**
136 * @brief Return the reference frame id
137 */
138 pinocchio::FrameIndex get_id() const;
139
140 /**
141 * @brief Modify the reference frame id
142 */
143 void set_id(const pinocchio::FrameIndex id);
144
145 /**
146 * @brief Modify the type of contact
147 */
148 void set_type(const pinocchio::ReferenceFrame type);
149
150 /**
151 * @brief Return the type of contact
152 */
153 pinocchio::ReferenceFrame get_type() const;
154
155 /**
156 * @brief Print information on the contact model
157 */
158 template <class Scalar>
159 friend std::ostream& operator<<(std::ostream& os,
160 const ContactModelAbstractTpl<Scalar>& model);
161
162 /**
163 * @brief Print relevant information of the contact model
164 *
165 * @param[out] os Output stream object
166 */
167 virtual void print(std::ostream& os) const;
168
169 protected:
170 std::shared_ptr<StateMultibody> state_;
171 std::size_t nc_;
172 std::size_t nu_;
173 pinocchio::FrameIndex id_; //!< Reference frame id of the contact
174 pinocchio::ReferenceFrame type_; //!< Type of contact
175 };
176
177 template <typename _Scalar>
178 struct ContactDataAbstractTpl : public ForceDataAbstractTpl<_Scalar> {
179 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
180
181 typedef _Scalar Scalar;
182 typedef MathBaseTpl<Scalar> MathBase;
183 typedef ForceDataAbstractTpl<Scalar> Base;
184 typedef typename MathBase::VectorXs VectorXs;
185 typedef typename MathBase::MatrixXs MatrixXs;
186 typedef typename pinocchio::SE3Tpl<Scalar> SE3;
187
188 template <template <typename Scalar> class Model>
189 326646 ContactDataAbstractTpl(Model<Scalar>* const model,
190 pinocchio::DataTpl<Scalar>* const data)
191 : Base(model, data),
192
1/2
✓ Branch 1 taken 163341 times.
✗ Branch 2 not taken.
326646 fXj(jMf.inverse().toActionMatrix()),
193
1/2
✓ Branch 2 taken 163341 times.
✗ Branch 3 not taken.
326646 a0(model->get_nc()),
194
1/2
✓ Branch 5 taken 163341 times.
✗ Branch 6 not taken.
326646 da0_dx(model->get_nc(), model->get_state()->get_ndx()),
195
2/4
✓ Branch 2 taken 163341 times.
✗ Branch 3 not taken.
✓ Branch 11 taken 163341 times.
✗ Branch 12 not taken.
653292 dtau_dq(model->get_state()->get_nv(), model->get_state()->get_nv()) {
196
1/2
✓ Branch 1 taken 163341 times.
✗ Branch 2 not taken.
326646 a0.setZero();
197
1/2
✓ Branch 1 taken 163341 times.
✗ Branch 2 not taken.
326646 da0_dx.setZero();
198
1/2
✓ Branch 1 taken 163341 times.
✗ Branch 2 not taken.
326646 dtau_dq.setZero();
199 326646 }
200 163462 virtual ~ContactDataAbstractTpl() {}
201
202 using Base::df_du;
203 using Base::df_dx;
204 using Base::f;
205 using Base::frame;
206 using Base::Jc;
207 using Base::jMf;
208 using Base::pinocchio;
209
210 typename SE3::ActionMatrixType fXj;
211 VectorXs a0;
212 MatrixXs da0_dx;
213 MatrixXs dtau_dq;
214 };
215
216 } // namespace crocoddyl
217
218 /* --- Details -------------------------------------------------------------- */
219 /* --- Details -------------------------------------------------------------- */
220 /* --- Details -------------------------------------------------------------- */
221 #include "crocoddyl/multibody/contact-base.hxx"
222
223 #endif // CROCODDYL_MULTIBODY_CONTACT_BASE_HPP_
224