easyPoint2KalmanFilter.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* ----------------------------------------------------------------------------
  2. * GTSAM Copyright 2010, Georgia Tech Research Corporation,
  3. * Atlanta, Georgia 30332-0415
  4. * All Rights Reserved
  5. * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
  6. * See LICENSE for the license information
  7. * -------------------------------------------------------------------------- */
  8. /**
  9. * @file easyPoint2KalmanFilter.cpp
  10. *
  11. * simple linear Kalman filter on a moving 2D point, but done using factor graphs
  12. * This example uses the templated ExtendedKalmanFilter class to perform the same
  13. * operations as in elaboratePoint2KalmanFilter
  14. *
  15. * @date Aug 19, 2011
  16. * @author Frank Dellaert
  17. * @author Stephen Williams
  18. */
  19. #include <gtsam/nonlinear/ExtendedKalmanFilter.h>
  20. #include <gtsam/inference/Symbol.h>
  21. #include <gtsam/nonlinear/PriorFactor.h>
  22. #include <gtsam/slam/BetweenFactor.h>
  23. #include <gtsam/geometry/Point2.h>
  24. using namespace std;
  25. using namespace gtsam;
  26. // Define Types for Linear System Test
  27. typedef Point2 LinearMeasurement;
  28. int main() {
  29. // Create the Kalman Filter initialization point
  30. Point2 x_initial(0.0, 0.0);
  31. SharedDiagonal P_initial = noiseModel::Diagonal::Sigmas(Vector2(0.1, 0.1));
  32. // Create Key for initial pose
  33. Symbol x0('x',0);
  34. // Create an ExtendedKalmanFilter object
  35. ExtendedKalmanFilter<Point2> ekf(x0, x_initial, P_initial);
  36. // Now predict the state at t=1, i.e. argmax_{x1} P(x1) = P(x1|x0) P(x0)
  37. // In Kalman Filter notation, this is x_{t+1|t} and P_{t+1|t}
  38. // For the Kalman Filter, this requires a motion model, f(x_{t}) = x_{t+1|t)
  39. // Assuming the system is linear, this will be of the form f(x_{t}) = F*x_{t} + B*u_{t} + w
  40. // where F is the state transition model/matrix, B is the control input model,
  41. // and w is zero-mean, Gaussian white noise with covariance Q
  42. // Note, in some models, Q is actually derived as G*w*G^T where w models uncertainty of some
  43. // physical property, such as velocity or acceleration, and G is derived from physics
  44. //
  45. // For the purposes of this example, let us assume we are using a constant-position model and
  46. // the controls are driving the point to the right at 1 m/s. Then, F = [1 0 ; 0 1], B = [1 0 ; 0 1]
  47. // and u = [1 ; 0]. Let us also assume that the process noise Q = [0.1 0 ; 0 0.1].
  48. Vector u = Vector2(1.0, 0.0);
  49. SharedDiagonal Q = noiseModel::Diagonal::Sigmas(Vector2(0.1, 0.1), true);
  50. // This simple motion can be modeled with a BetweenFactor
  51. // Create Key for next pose
  52. Symbol x1('x',1);
  53. // Predict delta based on controls
  54. Point2 difference(1,0);
  55. // Create Factor
  56. BetweenFactor<Point2> factor1(x0, x1, difference, Q);
  57. // Predict the new value with the EKF class
  58. Point2 x1_predict = ekf.predict(factor1);
  59. traits<Point2>::Print(x1_predict, "X1 Predict");
  60. // Now, a measurement, z1, has been received, and the Kalman Filter should be "Updated"/"Corrected"
  61. // This is equivalent to saying P(x1|z1) ~ P(z1|x1)*P(x1)
  62. // For the Kalman Filter, this requires a measurement model h(x_{t}) = \hat{z}_{t}
  63. // Assuming the system is linear, this will be of the form h(x_{t}) = H*x_{t} + v
  64. // where H is the observation model/matrix, and v is zero-mean, Gaussian white noise with covariance R
  65. //
  66. // For the purposes of this example, let us assume we have something like a GPS that returns
  67. // the current position of the robot. Then H = [1 0 ; 0 1]. Let us also assume that the measurement noise
  68. // R = [0.25 0 ; 0 0.25].
  69. SharedDiagonal R = noiseModel::Diagonal::Sigmas(Vector2(0.25, 0.25), true);
  70. // This simple measurement can be modeled with a PriorFactor
  71. Point2 z1(1.0, 0.0);
  72. PriorFactor<Point2> factor2(x1, z1, R);
  73. // Update the Kalman Filter with the measurement
  74. Point2 x1_update = ekf.update(factor2);
  75. traits<Point2>::Print(x1_update, "X1 Update");
  76. // Do the same thing two more times...
  77. // Predict
  78. Symbol x2('x',2);
  79. difference = Point2(1,0);
  80. BetweenFactor<Point2> factor3(x1, x2, difference, Q);
  81. Point2 x2_predict = ekf.predict(factor1);
  82. traits<Point2>::Print(x2_predict, "X2 Predict");
  83. // Update
  84. Point2 z2(2.0, 0.0);
  85. PriorFactor<Point2> factor4(x2, z2, R);
  86. Point2 x2_update = ekf.update(factor4);
  87. traits<Point2>::Print(x2_update, "X2 Update");
  88. // Do the same thing one more time...
  89. // Predict
  90. Symbol x3('x',3);
  91. difference = Point2(1,0);
  92. BetweenFactor<Point2> factor5(x2, x3, difference, Q);
  93. Point2 x3_predict = ekf.predict(factor5);
  94. traits<Point2>::Print(x3_predict, "X3 Predict");
  95. // Update
  96. Point2 z3(3.0, 0.0);
  97. PriorFactor<Point2> factor6(x3, z3, R);
  98. Point2 x3_update = ekf.update(factor6);
  99. traits<Point2>::Print(x3_update, "X3 Update");
  100. return 0;
  101. }