generic_quaternion_system_model.cpp
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2011, Johannes Meyer and Martin Nowara, 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 
31 
33 
34 namespace hector_pose_estimation {
35 
36 template class System_<GenericQuaternionSystemModel>;
37 
39 {
40  angular_acceleration_stddev_ = 360.0 * M_PI/180.0;
41  rate_stddev_ = 0.0;
43  velocity_stddev_ = 0.0;
44 
45 // parameters().addAlias("gravity", gravity_);
46  parameters().add("angular_acceleration_stddev", angular_acceleration_stddev_);
47  parameters().add("rate_stddev", rate_stddev_);
48  parameters().add("acceleration_stddev", acceleration_stddev_);
49  parameters().add("velocity_stddev", velocity_stddev_);
50 }
51 
53 {
54 }
55 
57 {
58  gravity_ = estimator.parameters().get("gravity_magnitude");
59 
60  imu_ = estimator.getInputType<ImuInput>("imu");
61  if (imu_ && state.orientation()) {
62  gyro_ = estimator.getSystem_<Gyro>("gyro");
63  if (!gyro_) {
64  gyro_.reset(new Gyro("gyro"));
65  estimator.addSystem(gyro_);
66  }
67  }
68  if (imu_ && state.velocity()) {
69  accelerometer_ = estimator.getSystem_<Accelerometer>("accelerometer");
70  if (!accelerometer_) {
71  accelerometer_.reset(new Accelerometer("accelerometer"));
72  estimator.addSystem(accelerometer_);
73  }
74 
75  }
76 
77 // // precalculate Q in order to just copy the parts that are active
78 // // Note: Q might be too small as we add other substates later.
79 // Q_ = State::Covariance::Zero(state.getCovarianceDimension(), state.getCovarianceDimension());
80 // if (state.orientation()) {
81 // if (!state.rate() && imu_ && gyro_) {
82 // gyro_->getModel()->getRateNoise(state.orientation()->block(Q_), state, true);
83 // }
84 // state.orientation()->block(Q_) += pow(rate_stddev_, 2) * SymmetricMatrix3::Identity();
85 // }
86 // if (state.rate()) {
87 // state.rate()->block(Q_) = pow(angular_acceleration_stddev_, 2) * SymmetricMatrix3::Identity();
88 // }
89 // if (state.position()) {
90 // state.position()->block(Q_) = pow(velocity_stddev_, 2) * SymmetricMatrix3::Identity();
91 // }
92 // if (state.velocity()) {
93 // if (!state.acceleration() && imu_ && accelerometer_) {
94 // accelerometer_->getModel()->getAccelerationNoise(state.velocity()->block(Q_), state, true);
95 // }
96 // state.velocity()->block(Q_) += pow(acceleration_stddev_, 2) * SymmetricMatrix3::Identity();
97 // }
98 
99  return true;
100 }
101 
103  if (state.orientation()) {
104  state.orientation()->P()(X,X) = 1.0;
105  state.orientation()->P()(Y,Y) = 1.0;
106  state.orientation()->P()(Z,Z) = 0.0;
107  }
108 
109  if (state.rate()) {
110  state.rate()->P()(X,X) = pow(0.0 * M_PI/180.0, 2);
111  state.rate()->P()(Y,Y) = pow(0.0 * M_PI/180.0, 2);
112  state.rate()->P()(Z,Z) = pow(0.0 * M_PI/180.0, 2);
113  }
114 
115  if (state.position()) {
116  state.position()->P()(X,X) = 0.0;
117  state.position()->P()(Y,Y) = 0.0;
118  state.position()->P()(Z,Z) = 0.0;
119  }
120 
121  if (state.velocity()) {
122  state.velocity()->P()(X,X) = 0.0;
123  state.velocity()->P()(Y,Y) = 0.0;
124  state.velocity()->P()(Z,Z) = 0.0;
125  }
126 }
127 
129 {
130  if (state.rate()) {
131  rate_nav_ = state.R() * state.getRate();
132  }
133  else if (rate_input_) {
134  rate_nav_ = state.R() * rate_input_->getVector();
135  }
136  else if (imu_) {
137  if (gyro_) {
138  rate_nav_ = state.R() * gyro_->getModel()->getRate(imu_->getRate(), state);
139  } else {
140  rate_nav_ = state.R() * imu_->getRate();
141  }
142  } else {
143  rate_nav_.setZero();
144  }
145 
146  if (state.acceleration()) {
147  acceleration_nav_ = state.R() * state.getAcceleration();
148  }
149  else if (force_input_) {
150  acceleration_nav_ = state.R() * force_input_->getVector();
151  }
152  else if (imu_) {
153  if (accelerometer_) {
154  acceleration_nav_ = state.R() * accelerometer_->getModel()->getAcceleration(imu_->getAcceleration(), state);
155  } else {
156  acceleration_nav_ = state.R() * imu_->getAcceleration();
157  }
158  } else {
159  acceleration_nav_.setZero();
160  }
161 
162  ROS_DEBUG_STREAM_NAMED("system", "rate_nav = [" << rate_nav_.transpose() << "]");
163  ROS_DEBUG_STREAM_NAMED("system", "acceleration_nav = [" << acceleration_nav_.transpose() << "]");
164  return true;
165 }
166 
167 void GenericQuaternionSystemModel::getDerivative(StateVector& x_dot, const State& state)
168 {
169  x_dot.setZero();
170 
171  if (state.rate()) {
172  if (torque_input_) {
173  state.rate()->segment(x_dot) = torque_input_->getVector();
174  }
175  }
176 
177  if (state.orientation()) {
178  state.orientation()->segment(x_dot).head(3) = rate_nav_;
179  if (!(state.getSystemStatus() & STATE_YAW) || (state.getSystemStatus() & STATUS_ALIGNMENT)) {
180  state.orientation()->segment(x_dot).z() = 0.0;
181  }
182  }
183 
184  if (state.velocity()) {
185  if ((state.getSystemStatus() & STATE_VELOCITY_XY) && !(state.getSystemStatus() & STATUS_ALIGNMENT)) {
186  state.velocity()->segment(x_dot)(X) = acceleration_nav_.x();
187  state.velocity()->segment(x_dot)(Y) = acceleration_nav_.y();
188  }
189  if ((state.getSystemStatus() & STATE_VELOCITY_Z) && !(state.getSystemStatus() & STATUS_ALIGNMENT)) {
190  state.velocity()->segment(x_dot)(Z) = acceleration_nav_.z();
191  if (imu_) {
192  state.velocity()->segment(x_dot)(Z) += gravity_;
193  }
194  }
195  }
196 
197  if (state.position()) {
199  if ((state.getSystemStatus() & STATE_POSITION_XY) && !(state.getSystemStatus() & STATUS_ALIGNMENT)) {
200  state.position()->segment(x_dot)(X) = v.x();
201  state.position()->segment(x_dot)(Y) = v.y();
202  }
203  if ((state.getSystemStatus() & STATE_POSITION_Z) && !(state.getSystemStatus() & STATUS_ALIGNMENT)) {
204  state.position()->segment(x_dot)(Z) = v.z();
205  }
206  }
207 }
208 
209 void GenericQuaternionSystemModel::getSystemNoise(NoiseVariance& Q, const State& state, bool init)
210 {
211 // if (init) Q.setZero();
212 // Q.topLeftCorner(Q_.rows(), Q_.cols()) = Q_;
213 
214 // if (state.orientation()) {
215 // if (!(state.getSystemStatus() & STATE_YAW) || (state.getSystemStatus() & STATUS_ALIGNMENT)) {
216 // state.orientation()->block(Q)(Z,Z) = 0.0;
217 // }
218 // }
219 
220 // if (state.velocity()) {
221 // if (!(state.getSystemStatus() & STATE_VELOCITY_XY) || (state.getSystemStatus() & STATUS_ALIGNMENT)) {
222 // state.velocity()->block(Q)(X,X) = 0.0;
223 // state.velocity()->block(Q)(Y,Y) = 0.0;
224 // }
225 // if (!(state.getSystemStatus() & STATE_VELOCITY_Z) || (state.getSystemStatus() & STATUS_ALIGNMENT)) {
226 // state.velocity()->block(Q)(Z,Z) = 0.0;
227 // }
228 // }
229 
230 // if (state.position()) {
231 // if (!(state.getSystemStatus() & STATE_POSITION_XY) || (state.getSystemStatus() & STATUS_ALIGNMENT)) {
232 // state.position()->block(Q)(X,X) = 0.0;
233 // state.position()->block(Q)(Y,Y) = 0.0;
234 // }
235 // if (!(state.getSystemStatus() & STATE_POSITION_Z) || (state.getSystemStatus() & STATUS_ALIGNMENT)) {
236 // state.position()->block(Q)(Z,Z) = 0.0;
237 // }
238 // }
239 
240  if (!init) return;
241 
242  Q.setZero();
243  if (state.orientation()) {
244  if (!state.rate() && imu_ && gyro_) {
245  gyro_->getModel()->getRateNoise(state.orientation()->block(Q), state, init);
246  }
247  state.orientation()->block(Q) += pow(rate_stddev_, 2) * SymmetricMatrix3::Identity();
248  }
249  if (state.rate()) {
250  state.rate()->block(Q) = pow(angular_acceleration_stddev_, 2) * SymmetricMatrix3::Identity();
251  }
252  if (state.position()) {
253  state.position()->block(Q) = pow(velocity_stddev_, 2) * SymmetricMatrix3::Identity();
254  }
255  if (state.velocity()) {
256  if (!state.acceleration() && imu_ && accelerometer_) {
257  accelerometer_->getModel()->getAccelerationNoise(state.velocity()->block(Q), state, init);
258  }
259  state.velocity()->block(Q) += pow(acceleration_stddev_, 2) * SymmetricMatrix3::Identity();
260  }
261 }
262 
263 void GenericQuaternionSystemModel::getStateJacobian(SystemMatrix& A, const State& state, bool)
264 {
265  const State::RotationMatrix &R = state.R();
266  A.setZero();
267 
268  if (state.orientation()) {
269  if (state.rate()) {
270  state.orientation()->block(A, *state.rate()) = R;
271  } else if (imu_ && gyro_) {
272  GyroModel::SystemMatrixBlock A_orientation = state.orientation()->rows(A);
273  gyro_->getModel()->getRateJacobian(A_orientation, state);
274  state.orientation()->rows(A) = R * A_orientation;
275  }
276 
277  state.orientation()->block(A) += SkewSymmetricMatrix(-rate_nav_);
278 
279  if (!(state.getSystemStatus() & STATE_YAW) || (state.getSystemStatus() & STATUS_ALIGNMENT)) {
280  state.orientation()->rows(A).row(2).setZero();
281  state.orientation()->block(A).col(2).setZero();
282  }
283  }
284 
285  if (state.velocity()) {
286  if (state.acceleration()) {
287  state.velocity()->block(A, *state.acceleration()) = R;
288  } else if (imu_ && accelerometer_) {
289  AccelerometerModel::SystemMatrixBlock A_velocity = state.velocity()->rows(A);
290  accelerometer_->getModel()->getAccelerationJacobian(A_velocity, state);
291  state.velocity()->rows(A) = R * A_velocity;
292  }
293 
294  if (state.orientation()) {
295  state.velocity()->block(A, *state.orientation()) += SkewSymmetricMatrix(-acceleration_nav_);
296  }
297 
298  if (!(state.getSystemStatus() & STATE_VELOCITY_XY) || (state.getSystemStatus() & STATUS_ALIGNMENT)) {
299  state.velocity()->rows(A).topRows(2).setZero();
300  }
301 
302  if (!(state.getSystemStatus() & STATE_VELOCITY_Z) || (state.getSystemStatus() & STATUS_ALIGNMENT)) {
303  state.velocity()->rows(A).row(2).setZero();
304  }
305  }
306 
307  if (state.position() && state.velocity()) {
308  state.position()->block(A, *state.velocity()).setIdentity();
309 
310  if ((state.getSystemStatus() & STATE_POSITION_XY) && !(state.getSystemStatus() & STATUS_ALIGNMENT)) {
311  state.position()->block(A, *state.velocity())(X,X) = 1.0;
312  state.position()->block(A, *state.velocity())(Y,Y) = 1.0;
313  }
314  if ((state.getSystemStatus() & STATE_POSITION_Z) && !(state.getSystemStatus() & STATUS_ALIGNMENT)) {
315  state.position()->block(A, *state.velocity())(Z,Z) = 1.0;
316  }
317  }
318 
319 }
320 
322 {
323  SystemStatus flags = state.getMeasurementStatus();
324  if (flags & STATE_POSITION_XY) flags |= STATE_VELOCITY_XY;
325  if (flags & STATE_POSITION_Z) flags |= STATE_VELOCITY_Z;
326  if (imu_) {
327  if (flags & STATE_VELOCITY_XY) flags |= STATE_ROLLPITCH;
328  if (flags & STATE_ROLLPITCH) flags |= STATE_RATE_XY;
329  if (flags & STATE_PSEUDO_ROLLPITCH) flags |= STATE_PSEUDO_RATE_XY;
330  if (flags & STATE_YAW) flags |= STATE_RATE_Z;
331  if (flags & STATE_PSEUDO_YAW) flags |= STATE_PSEUDO_RATE_Z;
332  }
333  return flags & STATE_MASK;
334 }
335 
336 } // namespace hector_pose_estimation
const SystemPtr & addSystem(const SystemPtr &system, const std::string &name="system")
void setIdentity(geometry_msgs::Transform &tx)
virtual void getDerivative(StateVector &x_dot, const State &state)
#define ROS_DEBUG_STREAM_NAMED(name, args)
boost::shared_ptr< SystemType > getSystem_(const std::string &name) const
virtual void getSystemNoise(NoiseVariance &Q, const State &state, bool init=true)
ParameterPtr const & get(const std::string &key) const
Definition: parameters.cpp:77
virtual const boost::shared_ptr< OrientationStateType > & orientation() const
Definition: state.h:112
virtual ParameterList & parameters()
Matrix_< 3, 3 >::type RotationMatrix
Definition: state.h:71
System_< GyroModel > Gyro
Definition: imu_model.h:91
virtual ConstRateType getRate() const
Definition: state.cpp:117
const State::RotationMatrix & R() const
Definition: state.cpp:131
virtual ConstVelocityType getVelocity() const
Definition: state.cpp:119
virtual const boost::shared_ptr< PositionStateType > & position() const
Definition: state.h:114
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, System &system, State &state)
unsigned int SystemStatus
Definition: types.h:70
boost::shared_ptr< InputType > getInputType(const std::string &name) const
virtual const boost::shared_ptr< VelocityStateType > & velocity() const
Definition: state.h:115
virtual SystemStatus getSystemStatus() const
Definition: state.h:100
virtual ConstAccelerationType getAcceleration() const
Definition: state.cpp:120
System_< AccelerometerModel > Accelerometer
Definition: imu_model.h:94
virtual const boost::shared_ptr< RateStateType > & rate() const
Definition: state.h:113
virtual void getStateJacobian(SystemMatrix &A, const State &state, bool init=true)
virtual SystemStatus getMeasurementStatus() const
Definition: state.h:101
VectorBlock< const Vector, 3 > ConstVelocityType
Definition: state.h:64
virtual const boost::shared_ptr< AccelerationStateType > & acceleration() const
Definition: state.h:116
static Matrix3 SkewSymmetricMatrix(const Eigen::MatrixBase< OtherDerived > &other)
Definition: matrix.h:91


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