GCC Code Coverage Report


Directory: ./
File: include/crocoddyl/multibody/contact-base.hpp
Date: 2025-03-26 19:23:43
Exec Total Coverage
Lines: 12 14 85.7%
Branches: 16 36 44.4%

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