gravity.cpp
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2011, Johannes Meyer, TU Darmstadt
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 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
32 
33 namespace hector_pose_estimation {
34 
35 template class Measurement_<GravityModel>;
36 
38  : gravity_(MeasurementVector::Zero())
39 {
40  parameters().add("stddev", stddev_, 1.0);
41  parameters().add("use_bias", use_bias_, std::string("accelerometer_bias"));
42 }
43 
45 
46 bool GravityModel::init(PoseEstimation &estimator, Measurement &measurement, State &state) {
47  if (!use_bias_.empty()) {
48  bias_ = state.getSubState<3,3>(use_bias_);
49  if (!bias_) {
50  ROS_ERROR("Could not find bias substate '%s' during initialization of gravity measurement '%s'.", use_bias_.c_str(), measurement.getName().c_str());
51  return false;
52  }
53  } else {
54  bias_.reset();
55  }
56 
57  setGravity(estimator.parameters().getAs<double>("gravity_magnitude"));
58  return true;
59 }
60 
61 
62 void GravityModel::getMeasurementNoise(NoiseVariance& R, const State&, bool init)
63 {
64  if (init) {
65  R(0,0) = R(1,1) = R(2,2) = pow(stddev_, 2);
66  }
67 }
68 
69 void GravityModel::getExpectedValue(MeasurementVector& y_pred, const State& state)
70 {
71  const State::RotationMatrix &R = state.R();
72  y_pred = -R.row(2).transpose() * gravity_.z();
73  if (bias_) {
74  y_pred += bias_->getVector();
75  }
76 }
77 
78 void GravityModel::getStateJacobian(MeasurementMatrix& C, const State& state, bool)
79 {
80  const State::RotationMatrix &R = state.R();
81  if (state.orientation()) {
82 // C(0,state.getOrientationCovarianceIndex() + W) = gravity_.z()*2*q.y();
83 // C(0,state.getOrientationCovarianceIndex() + X) = -gravity_.z()*2*q.z();
84 // C(0,state.getOrientationCovarianceIndex() + Y) = gravity_.z()*2*q.w();
85 // C(0,state.getOrientationCovarianceIndex() + Z) = -gravity_.z()*2*q.x();
86 // C(1,state.getOrientationCovarianceIndex() + W) = -gravity_.z()*2*q.x();
87 // C(1,state.getOrientationCovarianceIndex() + X) = -gravity_.z()*2*q.w();
88 // C(1,state.getOrientationCovarianceIndex() + Y) = -gravity_.z()*2*q.z();
89 // C(1,state.getOrientationCovarianceIndex() + Z) = -gravity_.z()*2*q.y();
90 // C(2,state.getOrientationCovarianceIndex() + W) = -gravity_.z()*2*q.w();
91 // C(2,state.getOrientationCovarianceIndex() + X) = gravity_.z()*2*q.x();
92 // C(2,state.getOrientationCovarianceIndex() + Y) = gravity_.z()*2*q.y();
93 // C(2,state.getOrientationCovarianceIndex() + Z) = -gravity_.z()*2*q.z();
94 
95  state.orientation()->cols(C)(X,X) = -gravity_.z() * R(1,0);
96  state.orientation()->cols(C)(X,Y) = gravity_.z() * R(0,0);
97  state.orientation()->cols(C)(Y,X) = -gravity_.z() * R(1,1);
98  state.orientation()->cols(C)(Y,Y) = gravity_.z() * R(0,1);
99  state.orientation()->cols(C)(Z,X) = -gravity_.z() * R(1,2);
100  state.orientation()->cols(C)(Z,Y) = gravity_.z() * R(0,2);
101  }
102 
103 // Only the bias component in direction of the gravity is observable, under the assumption that we do not accelerate vertically.
104  if (bias_) {
105  bias_->cols(C) = R.row(2).transpose() * R.row(2);
106  }
107 }
108 
109 } // namespace hector_pose_estimation
virtual void getMeasurementNoise(NoiseVariance &R, const State &, bool init)
Definition: gravity.cpp:62
boost::shared_ptr< SubState_< SubVectorDimension, SubCovarianceDimension > > getSubState(const Model *model) const
SubState_< 3 >::Ptr bias_
Definition: gravity.h:62
virtual const boost::shared_ptr< OrientationStateType > & orientation() const
Definition: state.h:112
virtual ParameterList & parameters()
Matrix_< 3, 3 >::type RotationMatrix
Definition: state.h:71
virtual void getStateJacobian(MeasurementMatrix &C, const State &state, bool init)
Definition: gravity.cpp:78
T & getAs(const std::string &key) const
Definition: parameters.h:168
ConstVectorSegment getVector() const
Definition: substate.h:116
virtual const std::string & getName() const
Definition: measurement.h:50
Block< MatrixType, MatrixType::RowsAtCompileTime, CovarianceDimension > cols(MatrixType &matrix)
Definition: substate.h:129
const State::RotationMatrix & R() const
Definition: state.cpp:131
virtual void setGravity(double gravity)
Definition: gravity.h:48
ParameterList & add(const std::string &key, T &value, const T &default_value)
Definition: parameters.h:148
ParameterList & parameters()
Definition: model.h:47
virtual bool init(PoseEstimation &estimator, Measurement &measurement, State &state)
Definition: gravity.cpp:46
virtual void getExpectedValue(MeasurementVector &y_pred, const State &state)
Definition: gravity.cpp:69
#define ROS_ERROR(...)


hector_pose_estimation_core
Author(s): Johannes Meyer
autogenerated on Thu Feb 18 2021 03:29:30