FloatingBaseObserver.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2019, CNRS-UM LIRMM
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <mc_rbdyn/rpy_utils.h>
29 
30 #include <cmath>
32 
33 namespace lipm_walking
34 {
35 
36 FloatingBaseObserver::FloatingBaseObserver(const mc_rbdyn::Robot & controlRobot) : controlRobot_(controlRobot) {}
37 
38 void FloatingBaseObserver::reset(const sva::PTransformd & X_0_fb)
39 {
40  orientation_ = X_0_fb.rotation();
41  position_ = X_0_fb.translation();
42 }
43 
44 void FloatingBaseObserver::run(const mc_rbdyn::Robot & realRobot)
45 {
46  estimateOrientation(realRobot);
47  estimatePosition(realRobot);
48 }
49 
50 void FloatingBaseObserver::estimateOrientation(const mc_rbdyn::Robot & realRobot)
51 {
52  // Prefixes:
53  // c for control-robot model
54  // r for real-robot model
55  // m for estimated/measured quantities
56  sva::PTransformd X_0_rBase = realRobot.posW();
57  sva::PTransformd X_0_rIMU = realRobot.bodyPosW(realRobot.bodySensor().parentBody());
58  sva::PTransformd X_rIMU_rBase = X_0_rBase * X_0_rIMU.inv();
59  Eigen::Matrix3d R_0_mIMU = imuOrientation().toRotationMatrix();
60  Eigen::Matrix3d R_0_cBase = controlRobot_.posW().rotation();
61  Eigen::Matrix3d R_0_mBase = X_rIMU_rBase.rotation() * R_0_mIMU;
62  Eigen::Vector3d cRPY = mc_rbdyn::rpyFromMat(R_0_cBase);
63  Eigen::Vector3d mRPY = mc_rbdyn::rpyFromMat(R_0_mBase);
64  orientation_ = mc_rbdyn::rpyToMat(mRPY(0), mRPY(1), cRPY(2));
65 }
66 
67 void FloatingBaseObserver::estimatePosition(const mc_rbdyn::Robot & realRobot)
68 {
69  sva::PTransformd X_0_c = getAnchorFrame(controlRobot_);
70  sva::PTransformd X_0_s = getAnchorFrame(realRobot);
71  const sva::PTransformd & X_0_real = realRobot.posW();
72  sva::PTransformd X_real_s = X_0_s * X_0_real.inv();
73  const Eigen::Vector3d & r_c_0 = X_0_c.translation();
74  const Eigen::Vector3d & r_s_real = X_real_s.translation();
75  position_ = r_c_0 - orientation_.transpose() * r_s_real;
76 }
77 
78 sva::PTransformd FloatingBaseObserver::getAnchorFrame(const mc_rbdyn::Robot & robot)
79 {
80  sva::PTransformd X_0_l = robot.surface("LeftFoot").X_0_s(robot);
81  sva::PTransformd X_0_r = robot.surface("RightFoot").X_0_s(robot);
82  return sva::interpolate(X_0_r, X_0_l, leftFootRatio_);
83 }
84 
85 void FloatingBaseObserver::updateRobot(mc_rbdyn::Robot & realRobot)
86 {
87  realRobot.posW(sva::PTransformd{orientation_, position_});
88 }
89 
90 } // namespace lipm_walking
EIGEN_MAKE_ALIGNED_OPERATOR_NEW FloatingBaseObserver(const mc_rbdyn::Robot &controlRobot)
Initialize floating base observer.
void reset(const sva::PTransformd &X_0_fb)
Reset floating base estimate.
void run(const mc_rbdyn::Robot &realRobot)
Update floating-base transform of real robot.
void updateRobot(mc_rbdyn::Robot &robot)
Write observed floating-base transform to the robot&#39;s configuration.
sva::PTransformd getAnchorFrame(const mc_rbdyn::Robot &robot)
Get anchor frame of a robot for a given contact state.
const Eigen::Quaterniond & imuOrientation()
Get the orientation of the IMU with respect to the inertial frame.
Main controller namespace.
Definition: build.dox:1