bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
ekf_slam.h
Go to the documentation of this file.
1#ifndef BITSIM_ESTIMATION_EKF_SLAM_H
2#define BITSIM_ESTIMATION_EKF_SLAM_H
3
6
7#include <array>
8#include <map>
9#include <string>
10#include <vector>
11
12namespace bitsim
13{
14namespace estimation
15{
16
23{
24 public:
25 SlamState() : v_(DynVec<real_t>::Zero(3)) {}
26
27 DynVec<real_t> as_vector() const { return v_; }
28 index_t size() const noexcept { return v_.size(); }
29 real_t &operator[](index_t i) { return v_[i]; }
30 real_t operator[](index_t i) const { return v_[i]; }
31
35 real_t get(const std::string &name) const;
36
40 template <typename Derived> SlamState &operator+=(const Eigen::MatrixBase<Derived> &dx)
41 {
42 v_ += dx;
43 return *this;
44 }
45
49 void append(real_t x, real_t y);
50
51 private:
53};
54
71{
72 public:
75
76 struct Input
77 {
79 real_t turn{0.0};
80 };
82
86 state_type &evaluate(const Input &input);
87
88 state_type &get_state() { return state_; }
89 const state_type &get_state() const { return state_; }
90 real_t get_state_property(const std::string &name) const { return state_.get(name); }
91 const matrix_type &get_matrix(const std::string &name) const;
92
93 private:
94 state_type state_;
95 matrix_type F_;
96 matrix_type L_;
97};
98
145{
146 public:
156
157 struct Pose
158 {
159 real_t x{0.0};
160 real_t y{0.0};
162 };
163
168 {
169 uint_t id{0};
172 };
173
177 struct Sighting
178 {
181 };
182
189 {
192 };
193
198 {
199 enum class Kind
200 {
204 };
205 std::vector<Kind> kinds;
206 std::vector<uint_t> ids;
210 };
211
216 EKFSlam(const Pose &start, const Parameters &parameters);
217
218 EKFSlam(const EKFSlam &) = delete;
219 EKFSlam &operator=(const EKFSlam &) = delete;
220
225 void predict(real_t distance, real_t turn);
226
231 uint_t update(const std::vector<Observation> &sightings);
232
239 AssociationResult update_unknown(const std::vector<Sighting> &sightings);
240 AssociationResult update_unknown(const std::vector<Sighting> &sightings,
241 const Association &association);
242
247 real_t mahalanobis(uint_t id, const Sighting &sighting) const;
248
249 Pose pose() const;
250
255
256 uint_t n_landmarks() const noexcept { return static_cast<uint_t>(order_.size()); }
257 bool has_landmark(uint_t id) const { return index_.count(id) > 0; }
258
262 const std::vector<uint_t> &landmark_ids() const noexcept { return order_; }
263
267 std::array<real_t, 2> landmark(uint_t id) const;
268
273
277 const DynMat<real_t> &covariance() const { return ekf_["P"]; }
278
282 uint_t n_rejected() const noexcept { return n_rejected_; }
283
284 const Parameters &parameters() const noexcept { return p_; }
285
286 private:
287 index_t offset_(uint_t id) const;
288 void add_landmark_(const Observation &sighting);
289
290 Parameters p_;
291 SlamMotionModel model_;
293 std::map<uint_t, index_t> index_;
294 std::vector<uint_t> order_;
295 uint_t n_rejected_{0};
296};
297
298} // namespace estimation
299} // namespace bitsim
300
301#endif // BITSIM_ESTIMATION_EKF_SLAM_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
EKFSlam & operator=(const EKFSlam &)=delete
uint_t update(const std::vector< Observation > &sightings)
Correct with sightings taken at the same time. Returns how many were used: added as new landmarks,...
Definition ekf_slam.cpp:176
void predict(real_t distance, real_t turn)
Move the robot with the odometry of one step: it drove distance [m] and turned by turn [rad].
Definition ekf_slam.cpp:166
AssociationResult update_unknown(const std::vector< Sighting > &sightings)
Correct with sightings taken at the same time whose landmarks are not known: work out which landmark ...
Definition ekf_slam.cpp:226
const Parameters & parameters() const noexcept
Definition ekf_slam.h:284
bool has_landmark(uint_t id) const
Definition ekf_slam.h:257
std::array< real_t, 2 > landmark(uint_t id) const
Where landmark id is [m].
Definition ekf_slam.cpp:373
Pose pose() const
Definition ekf_slam.cpp:365
uint_t n_landmarks() const noexcept
Definition ekf_slam.h:256
const std::vector< uint_t > & landmark_ids() const noexcept
The ids of the landmarks, in the order they were mapped.
Definition ekf_slam.h:262
const DynMat< real_t > & covariance() const
The covariance of the whole state.
Definition ekf_slam.h:277
uint_t n_rejected() const noexcept
How many re-sightings the gate turned away.
Definition ekf_slam.h:282
EKFSlam(const Pose &start, const Parameters &parameters)
Definition ekf_slam.cpp:152
EKFSlam(const EKFSlam &)=delete
real_t mahalanobis(uint_t id, const Sighting &sighting) const
The squared Mahalanobis distance of a sighting from landmark id: how many variances of the innovation...
Definition ekf_slam.cpp:209
DynMat< real_t > landmark_covariance(uint_t id) const
The 2 x 2 covariance of landmark id.
Definition ekf_slam.cpp:380
DynMat< real_t > pose_covariance() const
The 3 x 3 covariance of (x, y, yaw).
Definition ekf_slam.cpp:371
The extended Kalman filter: it predicts the state with a non-linear motion model and corrects it with...
Definition extended_kalman_filter.h:68
The motion model of EKFSlam: odometry moves the robot and leaves the landmarks where they are.
Definition ekf_slam.h:71
DynMat< real_t > matrix_type
Definition ekf_slam.h:74
state_type & get_state()
Definition ekf_slam.h:88
SlamState state_type
Definition ekf_slam.h:73
const state_type & get_state() const
Definition ekf_slam.h:89
Input input_type
Definition ekf_slam.h:81
const matrix_type & get_matrix(const std::string &name) const
Definition ekf_slam.cpp:76
state_type & evaluate(const Input &input)
Move the robot by one step, and work out F and L at the state before it.
Definition ekf_slam.cpp:50
real_t get_state_property(const std::string &name) const
Definition ekf_slam.h:90
The state of EKFSlam: the robot's pose (x, y, yaw) followed by the position (x, y) of every landmark ...
Definition ekf_slam.h:23
index_t size() const noexcept
Definition ekf_slam.h:28
real_t operator[](index_t i) const
Definition ekf_slam.h:30
real_t get(const std::string &name) const
The pose by name: "x", "y" or "yaw".
Definition ekf_slam.cpp:21
DynVec< real_t > as_vector() const
Definition ekf_slam.h:27
SlamState & operator+=(const Eigen::MatrixBase< Derived > &dx)
Add a correction, as ExtendedKalmanFilter::update does.
Definition ekf_slam.h:40
void append(real_t x, real_t y)
Add a landmark at (x, y) at the end of the state.
Definition ekf_slam.cpp:38
real_t & operator[](index_t i)
Definition ekf_slam.h:29
SlamState()
Definition ekf_slam.h:25
Definition diff_drive_ekf.cpp:12
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
Eigen::Index index_t
The type Eigen indexes and sizes matrices and vectors with.
Definition bitsim_types.h:45
Eigen::MatrixX< T > DynMat
Dynamically sized matrix to use around the library.
Definition bitsim_types.h:51
double real_t
real_t
Definition bitsim_types.h:20
Eigen::RowVectorX< T > DynVec
Dynamically sized row vector.
Definition bitsim_types.h:101
What update_unknown() did with each sighting of a scan.
Definition ekf_slam.h:198
uint_t n_matched
Definition ekf_slam.h:207
uint_t n_discarded
Definition ekf_slam.h:209
std::vector< Kind > kinds
one for each sighting, in the order given
Definition ekf_slam.h:205
std::vector< uint_t > ids
the landmark of each; for a discarded one, unspecified
Definition ekf_slam.h:206
uint_t n_added
Definition ekf_slam.h:208
@ matched
of a mapped landmark, and used to correct the state
Definition ekf_slam.h:201
@ discarded
too far to be a mapped landmark, too close to be sure it is a new one
Definition ekf_slam.h:203
@ added
of a new landmark, added to the map
Definition ekf_slam.h:202
How update_unknown() associates, on the squared Mahalanobis distance. The defaults are the 99 % point...
Definition ekf_slam.h:189
real_t new_gate
beyond this from every landmark, a sighting is a new one
Definition ekf_slam.h:191
real_t match_gate
within this, a sighting can be of the landmark
Definition ekf_slam.h:190
A sighting of a landmark: which one, and where it is from the robot.
Definition ekf_slam.h:168
real_t bearing
from straight ahead, positive to the left [rad]
Definition ekf_slam.h:171
real_t range
[m]
Definition ekf_slam.h:170
real_t range_noise
of a sighting's range [m]
Definition ekf_slam.h:152
real_t distance_noise
of the odometry's distance [m per square root of m]
Definition ekf_slam.h:149
real_t bearing_noise
of its bearing [rad]
Definition ekf_slam.h:153
real_t gate
on the squared Mahalanobis distance of a re-sighting
Definition ekf_slam.h:154
real_t turn_drift
of its turn while driving [rad per square root of m]
Definition ekf_slam.h:151
real_t turn_noise
of its turn [rad per square root of rad turned]
Definition ekf_slam.h:150
Definition ekf_slam.h:158
real_t y
Definition ekf_slam.h:160
real_t x
Definition ekf_slam.h:159
real_t yaw
Definition ekf_slam.h:161
A sighting with no id: where something is from the robot, but not which landmark.
Definition ekf_slam.h:178
real_t range
[m]
Definition ekf_slam.h:179
real_t bearing
from straight ahead, positive to the left [rad]
Definition ekf_slam.h:180
real_t distance
driven over the step [m]
Definition ekf_slam.h:78
real_t turn
turned over the step [rad]
Definition ekf_slam.h:79