state-observation 1.8.0
General implementation of observers.
Loading...
Searching...
No Matches
legged-odometry-manager.hpp
1#ifndef LEGGEDODOMETRYMANAGERHPP
2#define LEGGEDODOMETRYMANAGERHPP
3
4#include <set>
5#include <state-observation/api.h>
6#include <state-observation/tools/measurements-manager/ContactsManager.hpp>
7#include <type_traits>
8#include <unordered_map>
9
12
13namespace stateObservation
14{
15namespace odometry
16{
17
18// allowed odometry types
19enum class OdometryType
20{
21 Odometry6d,
22 Flat,
23 None
24};
25namespace internal
26{
27// map allowing to get the OdometryType value associated to the given string
28inline static const std::unordered_map<std::string, OdometryType> strToOdometryType_ = {
29 {"6D", OdometryType::Odometry6d},
30 {"Flat", OdometryType::Flat},
31 {"None", OdometryType::None}};
32// map allowing to get the string value associated to the given OdometryType object
33inline static const std::unordered_map<OdometryType, std::string> odometryTypToStr_ = {{OdometryType::Odometry6d, "6D"},
34 {OdometryType::Flat, "Flat"},
35 {OdometryType::None, "None"}};
36} // namespace internal
37
42inline static OdometryType stringToOdometryType(const std::string & str)
43{
44 auto it = internal::strToOdometryType_.find(str);
45 BOOST_ASSERT_MSG(it != internal::strToOdometryType_.end(),
46 (": No known OdometryType value for " + str + ".").c_str());
47
48 return it->second;
49}
50
56inline static std::string odometryTypeToString(OdometryType odometryType)
57{
58 return internal::odometryTypToStr_.at(odometryType);
59}
60
61using kine::Kinematics;
62using kine::LocalKinematics;
63typedef Eigen::Matrix<double, 7, 1> Vector7;
64
79
83// Enhancement of the class Contact for legged odometry.
85{
86 using measurements::Contact::Contact;
87
88public:
89 inline void resetContact() noexcept override
90 {
91 measurements::Contact::resetContact();
92 lifeTime_ = 0.0;
93 }
94
95 inline void lambda(double lambda)
96 {
97 lambda_ = lambda;
98 }
99
100 inline void resetLifeTime()
101 {
102 lifeTime_ = 0.0;
103 }
104 inline void lifeTimeIncrement(double dt)
105 {
106 lifeTime_ += dt;
107 }
108 inline void correctionWeightingCoeff(double weightingCoeff)
109 {
110 correctionWeightingCoeff_ = weightingCoeff;
111 }
112
113 inline double lambda() const noexcept
114 {
115 return lambda_;
116 }
117
118 inline double lifeTime() const noexcept
119 {
120 return lifeTime_;
121 }
122 inline double correctionWeightingCoeff() const noexcept
123 {
124 return correctionWeightingCoeff_;
125 }
126
127public:
128 // reference of the contact in the world
129 Kinematics worldRefKine_;
130 // reference of the contact in the world before correction
131 Kinematics worldRefKineBeforeCorrection_;
132 // new incoming ref kine for the correction
133 Kinematics newIncomingWorldRefKine_;
134 // indicates whether the contact can be used for the orientation odometry or not
135 bool useForOrientation_ = false;
136 // current estimation of the kinematics of the body in the world, obtained from the reference pose of the
137 // contact in the world
138 Kinematics worldBodyKineFromRef_;
139 // current estimation of the kinematics of the contact in the world. Avoids recomputations.
140 Kinematics currentWorldKine_;
141 // kinematics of the frame of the body in the frame of the contact, obtained by forward kinematics.
142 // Kinematics contactBodyKine_;
143 Kinematics bodyContactKine_;
144
145 // weighing coefficient for the anchor point computation
146 double lambda_ = 0.0;
147 // time ellapsed since the creation of the contact.
148 double lifeTime_ = 0.0;
149 // defines the weighting of the contribution of the newly "measured" reference pose of the contact and the current one
150 double correctionWeightingCoeff_;
151};
152
156struct STATE_OBSERVATION_DLLAPI LeggedOdometryManager
157{
158public:
159 using ContactsManager = measurements::ContactsManager<LoContact>;
160
161 struct KineParams
162 {
164
171
172 KineParams & positionMeas(const Eigen::Vector3d & worldPosMeas)
173 {
174 this->worldPosMeas = &worldPosMeas;
175 return *this;
176 }
177
178 KineParams & tiltMeasurement(const Eigen::Vector3d & tilt)
179 {
180 BOOST_ASSERT_MSG(attitudeMeas == nullptr, "An input attitude is already set");
181 tiltMeas = &tilt;
182 return *this;
183 }
184
185 KineParams & attitudeMeasurement(const Eigen::Matrix3d & oriMeas)
186 {
187 BOOST_ASSERT_MSG(tiltMeas == nullptr, "An input tilt is already set");
188 attitudeMeas = &oriMeas;
189 return *this;
190 }
191
192 static KineParams fromOther(const KineParams & other)
193 {
194 KineParams out(*other.kineToUpdate);
195 out.attitudeMeas = other.attitudeMeas;
196 out.tiltMeas = other.tiltMeas;
197 out.worldPosMeas = other.worldPosMeas;
198 return out;
199 }
200
201 explicit KineParams(Kinematics & pose)
202 {
203 kineToUpdate = &pose;
204 }
205
206 /* Variables to update */
207 // Kinematics of the body of the robot in the world that we want to update with the odometry.
208 Kinematics * kineToUpdate;
209 /* Inputs */
210
211 // Input position of the body in the world, used to perform the
212 // legged odometry.
213 const Vector3 * worldPosMeas = nullptr;
214 // Input orientation of the body in the world, used to perform the
215 // legged odometry. If only a tilt is provided, the yaw will come from the yaw of the contacts.
216 const Matrix3 * attitudeMeas = nullptr;
217 // Input orientation of the body in the world, used to perform the
218 // legged odometry. If only a tilt is provided, the yaw will come from the yaw of the contacts.
219 const Vector3 * tiltMeas = nullptr;
220 };
221
222 struct ContactInputData
223 {
224 ContactInputData(const Kinematics & bodyContactKine, double lambda)
225 : bodyContactKine_(bodyContactKine), lambda_(lambda)
226 {
227 }
228
229 // Kinematics contactBodyKine_;
230 Kinematics bodyContactKine_;
231 double lambda_;
232 };
233
234 template<typename OnNewContactObserver = std::nullptr_t,
235 typename OnMaintainedContactObserver = std::nullptr_t,
236 typename OnRemovedContactObserver = std::nullptr_t,
237 typename OnAddedContactObserver = std::nullptr_t>
238 struct ContactUpdateFunctions
239 {
241
251
252 explicit ContactUpdateFunctions() {}
253
254 template<typename OnNewContactOther>
255 ContactUpdateFunctions<OnNewContactOther,
256 OnMaintainedContactObserver,
257 OnRemovedContactObserver,
258 OnAddedContactObserver>
259 onNewContact(OnNewContactOther & onNewContact)
260 {
261 auto out = ContactUpdateFunctions<OnNewContactOther, OnMaintainedContactObserver, OnRemovedContactObserver,
262 OnAddedContactObserver>::fromOther(*this);
263
264 out.onNewContactFn = &onNewContact;
265 return out;
266 }
267
268 template<typename OnMaintainedContactOther>
269 ContactUpdateFunctions<OnNewContactObserver,
270 OnMaintainedContactOther,
271 OnRemovedContactObserver,
272 OnAddedContactObserver>
273 onMaintainedContact(OnMaintainedContactOther & onMaintainedContact)
274 {
275 auto out = ContactUpdateFunctions<OnNewContactObserver, OnMaintainedContactOther, OnRemovedContactObserver,
276 OnAddedContactObserver>::fromOther(*this);
277 out.onMaintainedContactFn = &onMaintainedContact;
278 return out;
279 }
280
281 template<typename OnRemovedContactOther>
282 ContactUpdateFunctions<OnNewContactObserver,
283 OnMaintainedContactObserver,
284 OnRemovedContactOther,
285 OnAddedContactObserver>
286 onRemovedContact(OnRemovedContactOther & onRemovedContact)
287 {
288 auto out = ContactUpdateFunctions<OnNewContactObserver, OnMaintainedContactObserver, OnRemovedContactOther,
289 OnAddedContactObserver>::fromOther(*this);
290 out.onRemovedContactFn = &onRemovedContact;
291 return out;
292 }
293
294 template<typename OnAddedContactOther>
295 ContactUpdateFunctions<OnNewContactObserver,
296 OnMaintainedContactObserver,
297 OnRemovedContactObserver,
298 OnAddedContactOther>
299 onAddedContact(OnAddedContactOther & onAddedontact)
300 {
301 auto out = ContactUpdateFunctions<OnNewContactObserver, OnMaintainedContactObserver, OnRemovedContactObserver,
302 OnAddedContactOther>::fromOther(*this);
303 out.onAddedContactFn = &onAddedontact;
304 return out;
305 }
306
307 template<typename OnNewContactOther,
308 typename OnMaintainedContactOther,
309 typename OnRemovedContactOther,
310 typename OnAddedContactOther>
311 static ContactUpdateFunctions fromOther(const ContactUpdateFunctions<OnNewContactOther,
312 OnMaintainedContactOther,
313 OnRemovedContactOther,
314 OnAddedContactOther> & other)
315 {
316 ContactUpdateFunctions out;
317 if constexpr(std::is_same_v<OnNewContactOther, OnNewContactObserver>)
318 {
319 out.onNewContactFn = other.onNewContactFn;
320 }
321 if constexpr(std::is_same_v<OnMaintainedContactOther, OnMaintainedContactObserver>)
322 {
323 out.onMaintainedContactFn = other.onMaintainedContactFn;
324 }
325 if constexpr(std::is_same_v<OnRemovedContactOther, OnRemovedContactObserver>)
326 {
327 out.onRemovedContactFn = other.onRemovedContactFn;
328 }
329 if constexpr(std::is_same_v<OnAddedContactOther, OnAddedContactObserver>)
330 {
331 out.onAddedContactFn = other.onAddedContactFn;
332 }
333 return out;
334 }
335
336 // Function defined in the observer using the legged odometry that must be called when a contact is newly detected.
337 OnNewContactObserver * onNewContactFn = nullptr;
338 /* Function defined in the observer using the legged odometry that must be called on all the contacts maintained
339 * with the environment. */
340 OnMaintainedContactObserver * onMaintainedContactFn = nullptr;
341 /* Function defined in the observer using the legged odometry that must be called when a contact is broken. */
342 OnRemovedContactObserver * onRemovedContactFn = nullptr;
343 /* Function defined in the observer using the legged odometry that must be called when a contact is newly added to
344 * the manager (used to add it to the gui, to logs that must be written since its first detection, etc.) */
345 OnAddedContactObserver * onAddedContactFn = nullptr;
346 };
347
348protected:
352
354 struct LeggedOdometryContactsManager : public ContactsManager
355 {
356 protected:
357 // comparison function that sorts the contacts based on their lambda. Contacts with the same lambda are sorted by id
358 // so that they are not considered as duplicates by the set.
360 {
361 inline bool operator()(const LoContact & contact1, const LoContact & contact2) const noexcept
362 {
363 if(contact1.lambda() != contact2.lambda())
364 {
365 return (contact1.lambda() < contact2.lambda());
366 }
367 return (contact1.id() < contact2.id());
368 }
369 };
370
371 public:
372 // list of contacts used for the orientation odometry. At most two contacts can be used for this estimation, and
373 // contacts at hands are not considered. The contacts with the highest lambda are used.
374 std::set<std::reference_wrapper<LoContact>, sortByLambda> oriOdometryContacts_;
375 };
376
377public:
381
385 {
389 inline Configuration(const std::string & odometryTypeString) noexcept
390 {
391 odometryType_ = stringToOdometryType(odometryTypeString);
392 BOOST_ASSERT_MSG(
393 odometryType_ == OdometryType::Flat || odometryType_ == OdometryType::Odometry6d,
394 "Odometry type not allowed. Please pick among : [Odometry6d, Flat] or use the other Configuration "
395 "constructor for an estimator that can run without odometry.");
396 }
397
400 inline Configuration(OdometryType odometryType) noexcept : odometryType_(odometryType) {}
401
402 // Desired kind of odometry (6D or flat)
403 OdometryType odometryType_;
404
405 // Indicates if the orientation must be estimated by this odometry.
406 bool withYaw_ = true;
407 // Indicates if the reference pose of the contacts must be corrected at the end of each iteration.
408 bool correctContacts_ = true;
409
410 inline Configuration & withYawEstimation(bool withYaw) noexcept
411 {
412 withYaw_ = withYaw;
413 return *this;
414 }
415 inline Configuration & correctContacts(bool correctContacts) noexcept
416 {
417 correctContacts_ = correctContacts;
418 return *this;
419 }
420 };
421
427 inline const std::vector<LoContact *> & newContacts()
428 {
429 return newContacts_;
430 }
431
436 inline const std::vector<LoContact *> & maintainedContacts() const
437 {
438 return maintainedContacts_;
439 }
440
444 void init(const Configuration & odomConfig, const Vector7 & initPose);
445
452 template<typename OnNewContactObserver = std::nullptr_t,
453 typename OnMaintainedContactObserver = std::nullptr_t,
454 typename OnRemovedContactObserver = std::nullptr_t,
455 typename OnAddedContactObserver = std::nullptr_t>
456 bool initLoop(const std::set<std::string> & latestContactList,
457 const ContactUpdateFunctions<OnNewContactObserver,
458 OnMaintainedContactObserver,
459 OnRemovedContactObserver,
460 OnAddedContactObserver> & updateFunctions,
461 const Vector3 * linVel = nullptr,
462 const Vector3 * angVel = nullptr);
463
468 void run(KineParams & kineParams);
469
475 void replaceOdomBodyPose(const Vector7 & newPose);
476
482
486
493
499 Kinematics getWorldBodyKineFromAnchor(bool withPos, bool withOri);
500
507
511 void setOdometryType(OdometryType newOdometryType);
512
513 void setSamplingTime(double dt)
514 {
515 ctl_dt_ = dt;
516 }
517
518 inline void kappa(double kappa) noexcept
519 {
520 kappa_ = kappa;
521 }
522 inline void lambdaInf(double lambdaInf) noexcept
523 {
524 lambdaInf_ = lambdaInf;
525 }
526
529 {
530 return contactsManager_;
531 }
532
533private:
537 template<typename OnNewContactObserver = std::nullptr_t,
538 typename OnMaintainedContactObserver = std::nullptr_t,
539 typename OnRemovedContactObserver = std::nullptr_t,
540 typename OnAddedContactObserver = std::nullptr_t>
541 bool updateContacts(const std::set<std::string> & latestContactList,
542 const ContactUpdateFunctions<OnNewContactObserver,
543 OnMaintainedContactObserver,
544 OnRemovedContactObserver,
545 OnAddedContactObserver> & updateFunctions);
546
549 void updateBodyKinematicsPvt(Kinematics & pose);
550
553 void updateBodyAndContacts(const KineParams & params);
554
557 void correctContactsRef();
558
561 void setNewContact(LoContact & contact);
562
567 const Kinematics & getContactKinematics(LoContact & contact);
568
573 Kinematics getContactKineIn(LoContact & contact, Kinematics & bodyTargetKine);
574
579 double selectForOrientationOdometry();
580
581protected:
582 // category to plot the odometry in
583 std::string category_;
584
585 // contacts manager used by this odometry manager
586 LeggedOdometryContactsManager contactsManager_;
587
588public:
589 // kinematics of the tracked body
590 Kinematics bodyKine_;
591
592protected:
593 // contacts created on the current iteration
594 std::vector<LoContact *> newContacts_;
595 // contacts maintained during the current iteration
596 std::vector<LoContact *> maintainedContacts_;
597 // time constant defining how fast the contact reference poses are corrected by the one of the body
598 double kappa_ = 10;
599 // gain allowing for the contribution of the contact pose measurement into the reference pose even after a long
600 // contact's lifetime.
601 double lambdaInf_ = 0.001;
602 // timestep used in the controller
603 double ctl_dt_ = 0.0;
604
605 // indicates whether we want to update the yaw using this method or not
606 bool withYawEstimation_;
607 // Indicates if the reference pose of the contacts must be corrected at the end of each iteration.
608 bool correctContacts_ = true;
609
610 // position of the anchor point of the robot in the world, obtained from the contact references.
611 Vector3 worldRefAnchorPosition_;
612 // position of the anchor point in the frame of the body.
613 Vector3 bodyAnchorPos_;
614
615 // Indicates if the previous anchor point was obtained using contacts
616 bool prevAnchorFromContacts_ = true;
617 // Indicates if the current anchor point was obtained using contacts
618 bool currAnchorFromContacts_ = true;
619 // indicated if the position can be updated using contacts. True if a contact is currently set.
620 bool posUpdatable_ = false;
621
622 // time stamp, incremented on the intiialization of each iteration.
623 TimeIndex k_iter_ = 0;
624 // time stamp, incremented once the reading of the joint encoders and the contacts are updated
625 TimeIndex k_data_ = 0;
626 // time stamp, incremented once the kinematics of the odometry robot have been updated.
627 TimeIndex k_est_ = 0;
628 // time stamp, incremented once the contact references have been corrected.
629 TimeIndex k_correct_ = 0;
630 // time stamp, incremented once the anchor frame has been computed.
631 TimeIndex k_anchor_ = 0;
632
633public:
634 // Indicates if the desired odometry must be a flat or a 6D odometry.
635 OdometryType odometryType_ = OdometryType::None;
636};
637
638} // namespace odometry
639} // namespace stateObservation
640
641#include <state-observation/tools/odometry/legged-odometry-manager.hxx>
642
643#endif // LEGGEDODOMETRYMANAGERHPP
---------------------------—Contacts--------------------------—
Definitions of types and some structures.
Filtering of divergent component of motion (DCM) and estimation of a bias betweeen the DCM and the co...
Eigen::Vector3d Vector3
3D vector
Eigen::Matrix3d Matrix3
3x3 Scalar Matrix
Implements integrators for the kinematics, in terms or rotations and translations.
Class facilitating the manipulation of the kinematics of a frame within another and the associated op...
Class facilitating the manipulation of the local kinematics of a frame within another and the associa...
Structure that implements all the necessary functions to manage the map of contacts....
---------------------—Configuration------------------------—
Configuration(const std::string &odometryTypeString) noexcept
Configuration's constructor.
Configuration(OdometryType odometryType) noexcept
Configuration's constructor.
OnNewContactObserver * onNewContactFn
Structure containing all the functions required to update the contact.
---------------------—Contacts Manager------------------------—
Structure that implements all the necessary functions to perform legged odometry.
Kinematics getAnchorKineIn(Kinematics &bodyTargetKine)
Gives the kinematics (position and linear velocity) of the anchor point in the desired frame.
const std::vector< LoContact * > & newContacts()
Returns a list of pointers to the contacts created on the current iteration.
const Vector3 & getWorldRefAnchorPos()
Returns the position of the anchor point in the world from the current contacts reference position.
void init(const Configuration &odomConfig, const Vector7 &initPose)
Initializer for the odometry manager.
LeggedOdometryContactsManager & contactsManager()
Getter for the contacts manager.
void replaceOdomBodyPose(const Vector7 &newPose)
Replaces the current pose of the odometry robot (that of the body used for the odometry !...
bool initLoop(const std::set< std::string > &latestContactList, const ContactUpdateFunctions< OnNewContactObserver, OnMaintainedContactObserver, OnRemovedContactObserver, OnAddedContactObserver > &updateFunctions, const Vector3 *linVel=nullptr, const Vector3 *angVel=nullptr)
Function that initializes the loop of the legged odometry. To be called at the beginning of each iter...
Kinematics getWorldBodyKineFromAnchor(bool withPos, bool withOri)
Updates the kinematics of the body in the world.
void setOdometryType(OdometryType newOdometryType)
Changes the type of the odometry.
LocalKinematics getWorldBodyLocalKineFromAnchor()
Updates the local kinematics of the body in the world.
Kinematics getAnchorKineInBody(bool withVel)
Gives the kinematics (position and linear velocity) of the anchor point in the body frame.
const std::vector< LoContact * > & maintainedContacts() const
Returns a list of pointers to the contacts maintained during the current iteration.
void run(KineParams &kineParams)
Function that runs the legged odometry loop. Using the input orientation and the current contacts,...