filter_base.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, 2015, 2016, Charles River Analytics, Inc.
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
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following
13  * disclaimer in the documentation and/or other materials provided
14  * with the distribution.
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef ROBOT_LOCALIZATION_FILTER_BASE_H
34 #define ROBOT_LOCALIZATION_FILTER_BASE_H
35 
38 
39 #include <Eigen/Dense>
40 
41 #include <algorithm>
42 #include <limits>
43 #include <map>
44 #include <ostream>
45 #include <queue>
46 #include <set>
47 #include <string>
48 #include <vector>
49 
50 #include <boost/shared_ptr.hpp>
51 
52 namespace RobotLocalization
53 {
54 
62 {
63  // The topic name for this measurement. Needed
64  // for capturing previous state values for new
65  // measurements.
66  std::string topicName_;
67 
68  // The measurement and its associated covariance
69  Eigen::VectorXd measurement_;
70  Eigen::MatrixXd covariance_;
71 
72  // This defines which variables within this measurement
73  // actually get passed into the filter. std::vector<bool>
74  // is generally frowned upon, so we use ints.
75  std::vector<int> updateVector_;
76 
77  // The real-valued time, in seconds, since some epoch
78  // (presumably the start of execution, but any will do)
79  double time_;
80 
81  // The Mahalanobis distance threshold in number of sigmas
83 
84  // The most recent control vector (needed for lagged data)
85  Eigen::VectorXd latestControl_;
86 
87  // The time stamp of the most recent control term (needed for lagged data)
89 
90  // We want earlier times to have greater priority
92  {
93  return (*this)(*(a.get()), *(b.get()));
94  }
95 
96  bool operator()(const Measurement &a, const Measurement &b)
97  {
98  return a.time_ > b.time_;
99  }
100 
102  topicName_(""),
103  latestControl_(),
104  latestControlTime_(0.0),
105  time_(0.0),
106  mahalanobisThresh_(std::numeric_limits<double>::max())
107  {
108  }
109 };
111 
119 {
120  // The filter state vector
121  Eigen::VectorXd state_;
122 
123  // The filter error covariance matrix
124  Eigen::MatrixXd estimateErrorCovariance_;
125 
126  // The most recent control vector
127  Eigen::VectorXd latestControl_;
128 
129  // The time stamp of the most recent measurement for the filter
131 
132  // The time stamp of the most recent control term
134 
135  // We want the queue to be sorted from latest to earliest timestamps.
136  bool operator()(const FilterState &a, const FilterState &b)
137  {
139  }
140 
142  state_(),
143  estimateErrorCovariance_(),
144  latestControl_(),
145  lastMeasurementTime_(0.0),
146  latestControlTime_(0.0)
147  {}
148 };
150 
152 {
153  public:
156  FilterBase();
157 
160  virtual ~FilterBase();
161 
164  void reset();
165 
172  void computeDynamicProcessNoiseCovariance(const Eigen::VectorXd &state, const double delta);
173 
179  virtual void correct(const Measurement &measurement) = 0;
180 
185  const Eigen::VectorXd& getControl();
186 
191  double getControlTime();
192 
197  bool getDebug();
198 
203  const Eigen::MatrixXd& getEstimateErrorCovariance();
204 
209  bool getInitializedStatus();
210 
215  double getLastMeasurementTime();
216 
222  const Eigen::VectorXd& getPredictedState();
223 
228  const Eigen::MatrixXd& getProcessNoiseCovariance();
229 
234  double getSensorTimeout();
235 
240  const Eigen::VectorXd& getState();
241 
249  virtual void predict(const double referenceTime, const double delta) = 0;
250 
255  virtual void processMeasurement(const Measurement &measurement);
256 
262  void setControl(const Eigen::VectorXd &control, const double controlTime);
263 
273  void setControlParams(const std::vector<int> &updateVector, const double controlTimeout,
274  const std::vector<double> &accelerationLimits, const std::vector<double> &accelerationGains,
275  const std::vector<double> &decelerationLimits, const std::vector<double> &decelerationGains);
276 
287  void setDebug(const bool debug, std::ostream *outStream = NULL);
288 
293  void setUseDynamicProcessNoiseCovariance(const bool dynamicProcessNoiseCovariance);
294 
299  void setEstimateErrorCovariance(const Eigen::MatrixXd &estimateErrorCovariance);
300 
305  void setLastMeasurementTime(const double lastMeasurementTime);
306 
315  void setProcessNoiseCovariance(const Eigen::MatrixXd &processNoiseCovariance);
316 
322  void setSensorTimeout(const double sensorTimeout);
323 
328  void setState(const Eigen::VectorXd &state);
329 
334  void validateDelta(double &delta);
335 
336  protected:
346  inline double computeControlAcceleration(const double state, const double control, const double accelerationLimit,
347  const double accelerationGain, const double decelerationLimit, const double decelerationGain)
348  {
349  FB_DEBUG("---------- FilterBase::computeControlAcceleration ----------\n");
350 
351  const double error = control - state;
352  const bool sameSign = (::fabs(error) <= ::fabs(control) + 0.01);
353  const double setPoint = (sameSign ? control : 0.0);
354  const bool decelerating = ::fabs(setPoint) < ::fabs(state);
355  double limit = accelerationLimit;
356  double gain = accelerationGain;
357 
358  if (decelerating)
359  {
360  limit = decelerationLimit;
361  gain = decelerationGain;
362  }
363 
364  const double finalAccel = std::min(std::max(gain * error, -limit), limit);
365 
366  FB_DEBUG("Control value: " << control << "\n" <<
367  "State value: " << state << "\n" <<
368  "Error: " << error << "\n" <<
369  "Same sign: " << (sameSign ? "true" : "false") << "\n" <<
370  "Set point: " << setPoint << "\n" <<
371  "Decelerating: " << (decelerating ? "true" : "false") << "\n" <<
372  "Limit: " << limit << "\n" <<
373  "Gain: " << gain << "\n" <<
374  "Final is " << finalAccel << "\n");
375 
376  return finalAccel;
377  }
378 
381  virtual void wrapStateAngles();
382 
388  virtual bool checkMahalanobisThreshold(const Eigen::VectorXd &innovation,
389  const Eigen::MatrixXd &invCovariance,
390  const double nsigmas);
391 
396  void prepareControl(const double referenceTime, const double predictionDelta);
397 
400  std::vector<double> accelerationGains_;
401 
404  std::vector<double> accelerationLimits_;
405 
408  Eigen::VectorXd controlAcceleration_;
409 
412  std::vector<double> decelerationGains_;
413 
416  std::vector<double> decelerationLimits_;
417 
420  Eigen::VectorXd latestControl_;
421 
424  std::vector<int> controlUpdateVector_;
425 
429 
434  Eigen::MatrixXd covarianceEpsilon_;
435 
438  std::ostream *debugStream_;
439 
443 
447  Eigen::MatrixXd estimateErrorCovariance_;
448 
451  Eigen::MatrixXd identity_;
452 
456 
463 
467 
470  Eigen::VectorXd predictedState_;
471 
482  Eigen::MatrixXd processNoiseCovariance_;
483 
490 
494  Eigen::VectorXd state_;
495 
509  Eigen::MatrixXd transferFunction_;
510 
519  Eigen::MatrixXd transferFunctionJacobian_;
520 
524 
529 
530  private:
533  bool debug_;
534 };
535 
536 } // namespace RobotLocalization
537 
538 #endif // ROBOT_LOCALIZATION_FILTER_BASE_H
std::vector< double > decelerationGains_
Gains applied to deceleration derived from control term.
Definition: filter_base.h:412
Eigen::MatrixXd transferFunctionJacobian_
The Kalman filter transfer function Jacobian.
Definition: filter_base.h:519
double controlTimeout_
Timeout value, in seconds, after which a control is considered stale.
Definition: filter_base.h:428
std::vector< int > controlUpdateVector_
Which control variables are being used (e.g., not every vehicle is controllable in Y or Z) ...
Definition: filter_base.h:424
boost::shared_ptr< FilterState > FilterStatePtr
Definition: filter_base.h:149
Eigen::MatrixXd estimateErrorCovariance_
This matrix stores the total error in our position estimate (the state_ variable).
Definition: filter_base.h:447
bool operator()(const Measurement &a, const Measurement &b)
Definition: filter_base.h:96
Structure used for storing and comparing measurements (for priority queues)
Definition: filter_base.h:61
Eigen::VectorXd predictedState_
Holds the last predicted state of the filter.
Definition: filter_base.h:470
Eigen::MatrixXd transferFunction_
The Kalman filter transfer function.
Definition: filter_base.h:509
Eigen::MatrixXd covarianceEpsilon_
Covariance matrices can be incredibly unstable. We can add a small value to it at each iteration to h...
Definition: filter_base.h:434
Eigen::VectorXd latestControl_
Definition: filter_base.h:85
Eigen::MatrixXd estimateErrorCovariance_
Definition: filter_base.h:124
#define FB_DEBUG(msg)
Eigen::VectorXd latestControl_
Latest control term.
Definition: filter_base.h:420
Eigen::MatrixXd identity_
We need the identity for a few operations. Better to store it.
Definition: filter_base.h:451
double computeControlAcceleration(const double state, const double control, const double accelerationLimit, const double accelerationGain, const double decelerationLimit, const double decelerationGain)
Method for settings bounds on acceleration values derived from controls.
Definition: filter_base.h:346
Eigen::VectorXd latestControl_
Definition: filter_base.h:127
boost::shared_ptr< Measurement > MeasurementPtr
Definition: filter_base.h:110
double latestControlTime_
The time of reception of the most recent control term.
Definition: filter_base.h:466
bool debug_
Whether or not the filter is in debug mode.
Definition: filter_base.h:533
Eigen::MatrixXd dynamicProcessNoiseCovariance_
Gets updated when useDynamicProcessNoise_ is true.
Definition: filter_base.h:442
double sensorTimeout_
The updates to the filter - both predict and correct - are driven by measurements. If we get a gap in measurements for some reason, we want the filter to continue estimating. When this gap occurs, as specified by this timeout, we will continue to call predict() at the filter&#39;s frequency.
Definition: filter_base.h:489
bool useDynamicProcessNoiseCovariance_
If true, uses the robot&#39;s vehicle state and the static process noise covariance matrix to generate a ...
Definition: filter_base.h:528
Eigen::VectorXd measurement_
Definition: filter_base.h:69
bool operator()(const FilterState &a, const FilterState &b)
Definition: filter_base.h:136
std::vector< double > accelerationGains_
Gains applied to acceleration derived from control term.
Definition: filter_base.h:400
double lastMeasurementTime_
Tracks the time the filter was last updated using a measurement.
Definition: filter_base.h:462
bool initialized_
Whether or not we&#39;ve received any measurements.
Definition: filter_base.h:455
Structure used for storing and comparing filter states.
Definition: filter_base.h:118
Eigen::VectorXd controlAcceleration_
Variable that gets updated every time we process a measurement and we have a valid control...
Definition: filter_base.h:408
Eigen::VectorXd state_
This is the robot&#39;s state vector, which is what we are trying to filter. The values in this vector ar...
Definition: filter_base.h:494
std::vector< double > accelerationLimits_
Caps the acceleration we apply from control input.
Definition: filter_base.h:404
Eigen::MatrixXd processNoiseCovariance_
As we move through the world, we follow a predict/update cycle. If one were to imagine a scenario whe...
Definition: filter_base.h:482
std::vector< double > decelerationLimits_
Caps the deceleration we apply from control input.
Definition: filter_base.h:416
double max(double a, double b)
std::vector< int > updateVector_
Definition: filter_base.h:75
std::ostream * debugStream_
Used for outputting debug messages.
Definition: filter_base.h:438
bool useControl_
Whether or not we apply the control term.
Definition: filter_base.h:523
bool operator()(const boost::shared_ptr< Measurement > &a, const boost::shared_ptr< Measurement > &b)
Definition: filter_base.h:91


robot_localization
Author(s): Tom Moore
autogenerated on Wed Feb 3 2021 03:34:02