bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
bitsim::estimation::EKFSlam Class Reference

Simultaneous localisation and mapping with an extended Kalman filter: a robot works out where it is and where the landmarks around it are, at the same time, from odometry and range-and-bearing sightings of the landmarks. More...

#include <ekf_slam.h>

Classes

struct  Parameters
struct  Pose
struct  Observation
 A sighting of a landmark: which one, and where it is from the robot. More...
struct  Sighting
 A sighting with no id: where something is from the robot, but not which landmark. More...
struct  Association
 How update_unknown() associates, on the squared Mahalanobis distance. The defaults are the 99 % point of the chi-squared distribution with 2 degrees of freedom, and the distance that a sighting of a mapped landmark is beyond once in about 270 000 times. More...
struct  AssociationResult
 What update_unknown() did with each sighting of a scan. More...

Public Member Functions

 EKFSlam (const Pose &start, const Parameters &parameters)
 EKFSlam (const EKFSlam &)=delete
EKFSlam & operator= (const EKFSlam &)=delete
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].
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, or accepted by the gate.
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 each is of, or whether it is a new one, see the class. Do not mix with update() on the same filter unless the ids cannot collide: new landmarks take the next id after the largest in the map.
AssociationResult update_unknown (const std::vector< Sighting > &sightings, const Association &association)
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 apart what was seen and what the estimate expects are.
Pose pose () const
DynMat< real_t > pose_covariance () const
 The 3 x 3 covariance of (x, y, yaw).
uint_t n_landmarks () const noexcept
bool has_landmark (uint_t id) const
const std::vector< uint_t > & landmark_ids () const noexcept
 The ids of the landmarks, in the order they were mapped.
std::array< real_t, 2 > landmark (uint_t id) const
 Where landmark id is [m].
DynMat< real_t > landmark_covariance (uint_t id) const
 The 2 x 2 covariance of landmark id.
const DynMat< real_t > & covariance () const
 The covariance of the whole state.
uint_t n_rejected () const noexcept
 How many re-sightings the gate turned away.
const Parameters & parameters () const noexcept

Detailed Description

Simultaneous localisation and mapping with an extended Kalman filter: a robot works out where it is and where the landmarks around it are, at the same time, from odometry and range-and-bearing sightings of the landmarks.

Odometry alone drifts: every step adds a little error, and nothing takes it out. A landmark the robot sees is a fixed point it can measure itself against, but only if it knows where the landmark is, which it can only work out from where it thinks it is itself. EKF-SLAM keeps both in one state, (x, y, yaw, m1x, m1y, m2x, m2y, ...), with one covariance over all of it. That covariance holds how the errors of the robot and of every landmark are tied together, and that is what makes it work: when the robot sees again a landmark it mapped long ago, the correction reaches its own pose and every landmark correlated with it. This is loop closure.

  • predict() moves the robot with the odometry, see SlamMotionModel;
  • update() takes sightings of landmarks, each a range and a bearing from the robot. A landmark seen for the first time is added to the state, where the sighting puts it, with a covariance that ties it to the robot's; a landmark seen before corrects the whole state, through the gate of ExtendedKalmanFilter.

With update() every landmark has an id, as a fiducial marker does, so the filter knows which landmark each sighting is of. Without ids it has to work that out itself, data association, and update_unknown() does: it scores every sighting against every landmark by the squared Mahalanobis distance of the innovation, \(d^2 = \nu^T S^{-1} \nu\), which measures the difference between what was seen and what was expected against how uncertain both are. Then

  • it pairs sightings and landmarks greedily, the smallest \(d^2\) first, if it is within Association::match_gate, each landmark with at most one sighting of a scan: two posts seen at once are two posts;
  • a sighting left over that is beyond Association::new_gate from every landmark is a new landmark, and gets the next free id;
  • one in between could be either, and is not used: a doubtful sighting is cheaper to drop than to believe.

A pairing is final: a wrong one moves the robot and the map to agree with it, and the filter cannot take it back. That is why the gate measures against the uncertainty and not in metres: the same 20 cm is a good match far from the start and a bad one next to it.

The map's frame is the robot's start: its first pose is known exactly, so the uncertainty grows from there.

The state, and so the work of each step, grows with the number of landmarks: predict and update cost \(O(n^3)\) here for n values in the state. That is fine for tens of landmarks; large maps need sparser methods.

Constructor & Destructor Documentation

◆ EKFSlam() [1/2]

bitsim::estimation::EKFSlam::EKFSlam ( const Pose & start,
const Parameters & parameters )
Parameters
startThe robot's first pose, the origin of the map's frame
parametersThe noises of the odometry and the sightings, and the gate

◆ EKFSlam() [2/2]

bitsim::estimation::EKFSlam::EKFSlam ( const EKFSlam & )
delete

Member Function Documentation

◆ covariance()

const DynMat< real_t > & bitsim::estimation::EKFSlam::covariance ( ) const
inline

The covariance of the whole state.

◆ has_landmark()

bool bitsim::estimation::EKFSlam::has_landmark ( uint_t id) const
inline

◆ landmark()

std::array< real_t, 2 > bitsim::estimation::EKFSlam::landmark ( uint_t id) const

Where landmark id is [m].

◆ landmark_covariance()

DynMat< real_t > bitsim::estimation::EKFSlam::landmark_covariance ( uint_t id) const

The 2 x 2 covariance of landmark id.

◆ landmark_ids()

const std::vector< uint_t > & bitsim::estimation::EKFSlam::landmark_ids ( ) const
inlinenoexcept

The ids of the landmarks, in the order they were mapped.

◆ mahalanobis()

real_t bitsim::estimation::EKFSlam::mahalanobis ( uint_t id,
const Sighting & sighting ) const

The squared Mahalanobis distance of a sighting from landmark id: how many variances of the innovation apart what was seen and what the estimate expects are.

◆ n_landmarks()

uint_t bitsim::estimation::EKFSlam::n_landmarks ( ) const
inlinenoexcept

◆ n_rejected()

uint_t bitsim::estimation::EKFSlam::n_rejected ( ) const
inlinenoexcept

How many re-sightings the gate turned away.

◆ operator=()

EKFSlam & bitsim::estimation::EKFSlam::operator= ( const EKFSlam & )
delete

◆ parameters()

const Parameters & bitsim::estimation::EKFSlam::parameters ( ) const
inlinenoexcept

◆ pose()

EKFSlam::Pose bitsim::estimation::EKFSlam::pose ( ) const

◆ pose_covariance()

DynMat< real_t > bitsim::estimation::EKFSlam::pose_covariance ( ) const

The 3 x 3 covariance of (x, y, yaw).

◆ predict()

void bitsim::estimation::EKFSlam::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].

◆ update()

uint_t bitsim::estimation::EKFSlam::update ( const std::vector< Observation > & sightings)

Correct with sightings taken at the same time. Returns how many were used: added as new landmarks, or accepted by the gate.

◆ update_unknown() [1/2]

EKFSlam::AssociationResult bitsim::estimation::EKFSlam::update_unknown ( const std::vector< Sighting > & sightings)

Correct with sightings taken at the same time whose landmarks are not known: work out which landmark each is of, or whether it is a new one, see the class. Do not mix with update() on the same filter unless the ids cannot collide: new landmarks take the next id after the largest in the map.

◆ update_unknown() [2/2]

EKFSlam::AssociationResult bitsim::estimation::EKFSlam::update_unknown ( const std::vector< Sighting > & sightings,
const Association & association )

The documentation for this class was generated from the following files: