@@ -30,74 +30,114 @@ namespace ser94mor
30
30
{
31
31
32
32
/* *
33
- * A template class holding Kalman Filter equations.
33
+ * A base template class holding Kalman filter equations.
34
34
* The naming of vectors and matrices are taken from the
35
35
* "Thrun, S., Burgard, W. and Fox, D., 2005. Probabilistic robotics. MIT press."
36
36
*
37
37
* @tparam ProcessModel a class of the process model to use
38
38
* @tparam MeasurementModel a class of measurement model to use; notice that here it is not a template class
39
+ * @tparam DerivedKalmanFilter a concrete sub-template-class of the KalmanFilterBase;
40
+ * it is needed to invoke methods from that class
39
41
*/
40
- template <class ProcessModel , class MeasurementModel >
41
- class KalmanFilter
42
+ template <class ProcessModel , class MeasurementModel , template < class , class > class DerivedKalmanFilter >
43
+ class KalmanFilterBase
42
44
{
43
45
protected:
44
46
using Belief = typename ProcessModel::Belief_type;
45
47
using ControlVector = typename ProcessModel::ControlVector_type;
46
48
using Measurement = typename MeasurementModel::Measurement_type;
47
-
48
49
public:
50
+
49
51
/* *
50
52
* Prediction step of the Kalman filter. Predicts the object's state in dt time in the future in accordance with
51
53
* LINEAR process model and input control vector.
52
54
*
53
- * @param belief_posterior a current belief of the object's state
55
+ * @param bel a current belief of the object's state
54
56
* @param ut a control vector
55
57
* @param dt time interval between the previous and current measurements
56
58
* @param process_model an instance of the process model
57
59
*
58
60
* @return a prior belief, that is, after prediction but before incorporating the measurement
59
61
*/
60
- template <bool EnableBool = true >
61
- static Belief Predict (const Belief& belief_posterior,
62
- const ControlVector& ut,
63
- double dt,
64
- const std::enable_if_t <ProcessModel::IsLinear() && EnableBool, ProcessModel>& process_model)
62
+ template <bool enable = true >
63
+ static auto
64
+ Predict (const Belief& bel, const ControlVector& ut, double_t dt, const ProcessModel& process_model)
65
+ -> std::enable_if_t <ProcessModel::IsLinear() and enable, Belief>
65
66
{
66
67
auto At{process_model.A (dt)};
67
68
return {
68
- /* timestamp */ belief_posterior .t () + dt,
69
- /* state vector */ At * belief_posterior .mu () + process_model.B () * ut,
70
- /* state covariance matrix */ At * belief_posterior .Sigma () * At.transpose () + process_model.R (dt),
69
+ /* timestamp */ bel .t () + dt,
70
+ /* state vector */ At * bel .mu () + process_model.B () * ut,
71
+ /* state covariance matrix */ At * bel .Sigma () * At.transpose () + process_model.R (dt),
71
72
};
72
73
}
73
74
74
75
/* *
75
76
* Update step of the Kalman filter. Incorporates the sensor measurement into the given prior belief.
76
77
* Works only with linear measurement models.
77
78
*
78
- * @param belief_prior a belief after the prediction Kalman filter step
79
+ * @param bel a belief after the prediction Kalman filter step
79
80
* @param measurement a measurement from the sensor
80
81
* @param measurement_model an instance of the measurement model
81
82
*
82
83
* @return a posterior belief, that is, after the incorporation of the measurement
83
84
*/
84
- template <bool EnableBool = true >
85
- static Belief Update ( const Belief& belief_prior, const Measurement& measurement,
86
- const std:: enable_if_t <MeasurementModel::IsLinear() && EnableBool, MeasurementModel>&
87
- measurement_model)
85
+ template <bool enable = true >
86
+ static auto
87
+ Update ( const Belief& bel, const Measurement& measurement, const MeasurementModel& measurement_model)
88
+ -> std:: enable_if_t <MeasurementModel::IsLinear() and enable, Belief>
88
89
{
89
90
auto Ct{measurement_model.C ()};
90
- auto mu{belief_prior .mu ()};
91
- auto Sigma{belief_prior .Sigma ()};
91
+ auto mu{bel .mu ()};
92
+ auto Sigma{bel .Sigma ()};
92
93
auto Kt{Sigma * Ct.transpose () * (Ct * Sigma * Ct.transpose () + measurement_model.Q ()).inverse ()};
93
- auto I{Eigen::Matrix<double , ProcessModel::StateDims (), ProcessModel::StateDims ()>::Identity ()};
94
+ auto I{Eigen::Matrix<double_t , ProcessModel::StateDims (), ProcessModel::StateDims ()>::Identity ()};
94
95
95
96
return {
96
97
/* timestamp */ measurement.t (),
97
98
/* state vector */ mu + Kt * (measurement.z () - Ct * mu),
98
99
/* state covariance matrix */ (I - Kt * Ct) * Sigma,
99
100
};
100
101
}
102
+
103
+ /* *
104
+ * Combined Predict and Update steps of the derived Kalman filter.
105
+ * Predicts the object's state in dt time in the future in accordance with the process model
106
+ * and input control vector and then incorporates the sensor measurement into the belief.
107
+ *
108
+ * Notice that process model and mesurement model can be either linear and non-linear.
109
+ *
110
+ * @param bel a current belief of the object's state
111
+ * @param ut a control vector
112
+ * @param measurement a measurement from the sensor
113
+ * @param process_model an instance of the process model
114
+ * @param measurement_model an instance of the measurement model
115
+ * @return a posterior belief, that is, after the prediction and incorporation of the measurement
116
+ */
117
+ static Belief
118
+ PredictUpdate (const Belief& bel, const ControlVector& ut, const Measurement& measurement,
119
+ const ProcessModel& process_model, const MeasurementModel& measurement_model)
120
+ {
121
+ auto dt = measurement.t () - bel.t ();
122
+
123
+ auto belief_prior{DerivedKalmanFilter<ProcessModel, MeasurementModel>::Predict (bel, ut, dt, process_model)};
124
+
125
+ return DerivedKalmanFilter<ProcessModel, MeasurementModel>::
126
+ Update (belief_prior, measurement, measurement_model);
127
+ }
128
+ };
129
+
130
+
131
+ /* *
132
+ * A concrete class representing Kalman filter. All the equations are the same as in its base class.
133
+ *
134
+ * @tparam ProcessModel a class of the process model to use
135
+ * @tparam MeasurementModel a class of measurement model to use; notice that here it is not a template class
136
+ */
137
+ template <class ProcessModel , class MeasurementModel >
138
+ class KalmanFilter : public KalmanFilterBase <ProcessModel, MeasurementModel, KalmanFilter>
139
+ {
140
+
101
141
};
102
142
103
143
}
0 commit comments