bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
EKF-SLAM without landmark ids: data association

In Mapping with EKF-SLAM every post carried a marker, and the sensor said which post it saw. Most landmarks are not so obliging: a corner, a pillar, a tree look alike, and a sensor reports only that something is at this range and bearing. Before the filter can use a sighting it has to decide which landmark it is of, or whether it is a landmark it has not mapped yet. This is data association, and it is where SLAM most often goes wrong.

The maps the four ways of association end with

The same loop, the same data, four ways to tell the posts apart. A tight radius in metres maps posts again and again; a wide one confuses posts that stand close together, and each wrong match drags the map with it, two landmarks a metre past the wall. Measured against the uncertainty, the map is as good as with the ids.

What you need

Nothing beyond bitsim itself. See Installing bitsim. It follows Mapping with EKF-SLAM.

Why it is hard

A wrong answer is not a missed measurement: the filter believes it. Matched to the wrong landmark, a sighting moves the robot and the map until they agree with it, and the covariance shrinks as if the filter knew more. There is no taking it back, and the errors it causes look, to the filter, like knowledge.

The two ways of going wrong pull against each other:

  • take a re-sighting of a mapped landmark for a new one, and the map gets a duplicate: a second copy, that the robot will also see, and that the loop can no longer close on;
  • take a sighting of one landmark for another, and the filter corrects with a lie.

The experiment

The robot drives the loop of Mapping with EKF-SLAM, with odometry twice as noisy, so that in the far room, even with the ids, its position is up to 25 cm off: more than half the distance between the closest posts. Two pairs of posts stand there only 40 cm apart: posts 13 and 17 on the east wall, 14 and 18 on the north. It drives the loop four times over the same data, the same true path, odometry and sightings, and associates them four ways:

  1. With the ids, as in Mapping with EKF-SLAM. This is the answer to compare with.
  2. The nearest post in metres, within 20 cm. Where the sighting puts the post, from the robot's estimated pose, is compared with the mapped posts; the nearest one within 20 cm is it, and if there is none, it is a new post.
  3. The same, within 50 cm.
  4. The nearest by the Mahalanobis distance, EKFSlam::update_unknown.

Measuring against the uncertainty

A fixed radius in metres cannot be right everywhere. Near the start the robot knows where it is to a few centimetres, and 20 cm is a long way; in the far room it can be 25 cm off, and a re-sighting of a mapped post can land more than 20 cm from it, as the run within 20 cm shows. The Mahalanobis distance measures in the uncertainty instead. For a sighting \(z\) and a landmark, with the predicted sighting \(h(x)\) and the innovation's covariance \(S = H P H^T + R\), which holds the uncertainty of the robot, of the landmark and of the sensor,

\[d^2 = \nu^T S^{-1} \nu, \qquad \nu = z - h(x) \]

is how many standard deviations apart what was seen and what was expected are, squared. If the sighting is of that landmark, and the filter is consistent, \(d^2\) follows about the chi-squared distribution with 2 degrees of freedom, whatever the robot's uncertainty, so one threshold works everywhere. update_unknown uses two:

  • a sighting within \(d^2 \le 9.21\) of a landmark, the 99 % point, can be of it. The pairs are taken greedily, the smallest \(d^2\) first, and each landmark at most once in a scan: two sightings at once are of two landmarks;
  • a sighting left with \(d^2 > 25\) from every landmark, beyond which a true re-sighting falls about once in 270 000 times, is a new landmark;
  • one in between is not used. It could be of a mapped landmark seen badly, or of a new one close to it; a doubtful sighting costs less dropped than believed.

Running the example

./ekf_slam_data_association

Output

1. The same loop, four ways to tell the posts apart
19 posts, two pairs 40 cm apart; 812 sightings, each of a post within 2.5 m
wrong not used landmarks duplicates max error end error map error
[m] [m] [m]
ids 0 1 19 0 0.246 0.052 0.108
nearest, within 0.2 m 0 0 28 9 0.232 0.049 0.156
nearest, within 0.5 m 22 0 20 1 1.776 0.046 0.939
Mahalanobis 0 6 19 0 0.217 0.052 0.083
2. What went wrong
nearest, within 0.2 m
post 2 mapped twice
post 4 mapped twice
post 5 mapped twice
post 6 mapped twice
post 9 mapped twice
post 12 mapped twice
post 13 mapped 3 times
post 14 mapped twice
nearest, within 0.5 m
post 16 mapped twice
post 14 taken for post 18 once
post 17 taken for post 13 21 times
Mahalanobis
nothing

Wrong counts sightings matched to a landmark that another post made; not used, sightings the association dropped, or with the ids the filter's gate; duplicates, landmarks made for a post that already had one. The errors are of the robot's position, its worst and at the end, and of the landmarks from the posts that made them.

Within 20 cm nothing is confused, but whenever the robot has drifted a re-sighting lands outside 20 cm, and it becomes a new post: 28 landmarks for 19 posts, eight of them mapped twice or three times. Within 50 cm the posts 40 cm apart become one: post 17 is taken for post 13 21 times, and the robot is dragged 1.8 m off. It finds its way back by the end, but the map does not: its landmarks are 0.94 m from their posts on average, some of them outside the warehouse.

By the Mahalanobis distance nothing is confused and nothing is mapped twice: 19 landmarks for 19 posts, and the robot and the map as close as with the ids. The cost is 6 sightings dropped as doubtful, out of 812.

Things to try

  1. Lower the new-landmark threshold to the match gate, 9.21, so nothing is dropped. Do duplicates appear?
  2. Move posts 17 and 18 to 20 cm from their neighbours. When does the Mahalanobis distance start to confuse them too?
  3. Raise the odometry's noise to 4 times ekf_slam's. Which method breaks first?
  4. Associate each sighting on its own, without the rule that a landmark takes one sighting a scan. What happens at the pairs of posts? This, done jointly for all the sightings of a scan, is what joint compatibility (JCBB) does more carefully.

The code

// SLAM when the landmarks have no names. In ekf_slam every post carried a marker, and the sensor
// said which post it saw. Here it does not: a sighting is a range and a bearing, and the filter has
// to work out which post it is of, or whether it is one it has not mapped yet. This is data
// association, and a wrong answer is not forgotten: it pulls the robot and the map to agree with
// it.
//
// The robot drives the loop of ekf_slam, with odometry twice as noisy, four times over the same
// data, the same true path, the same odometry and the same sightings, and associates them in turn:
// - with the ids, as ekf_slam did: the answer to compare with;
// - by the nearest post in metres: a sighting is of the mapped post closest to where it puts the
// post, if one is within a fixed radius, 20 cm or 50 cm, and otherwise of a new post;
// - by the Mahalanobis distance, EKFSlam::update_unknown(): the nearest post measured against
// the uncertainty of the robot, the post and the sighting, one sighting to a post, and a
// doubtful sighting dropped.
// Two pairs of posts stand only 40 cm apart, close enough to be confused once the robot is unsure
// where it is.
//
// Usage: ekf_slam_data_association
#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <random>
#include <string>
#include <vector>
{
const real_t DT = 0.1; // [s]
const real_t SPEED = 0.3; // [m/s]
const real_t MAX_TURN = 1.5; // [rad/s]
const real_t SENSOR_RANGE = 2.5; // [m]
const uint_t SIGHT_EVERY = 5; // steps between sightings
const real_t ODOMETRY_NOISE = 2.0; // times the noise of ekf_slam's odometry, so the robot is
// unsure enough for the posts to be confused
using Point = std::array<real_t, 2>;
// The posts of ekf_slam, and two more, each 40 cm from one of them, in the far room
const std::vector<Point> POSTS{{1.0, 0.25}, {2.75, 0.25}, {0.25, 2.5}, {1.25, 2.5}, {2.25, 1.0},
{2.25, 4.0}, {1.0, 4.75}, {3.25, 4.5}, {3.25, 1.5}, {3.95, 1.5},
{4.75, 0.25}, {6.75, 1.75}, {5.25, 3.5}, {7.75, 2.5}, {6.25, 4.75},
{7.25, 0.4}, {4.25, 4.5}, {7.75, 2.9}, {6.65, 4.75}};
const std::vector<Point> ROUTE{{2.8, 0.6}, {2.8, 3.4}, {4.5, 3.4}, {4.5, 0.8}, {7.3, 0.8},
{7.3, 4.7}, {4.5, 4.7}, {4.5, 3.4}, {2.8, 3.4}, {2.4, 4.5},
{0.8, 4.5}, {0.8, 0.8}, {0.5, 0.5}};
enum class Method
{
ids,
};
// How a run went
struct Score
{
uint_t n_wrong{0}; // matched to a landmark of another post
uint_t n_discarded{0}; // not used
uint_t n_landmarks{0}; // in the map at the end
uint_t n_duplicates{0}; // landmarks made for a post that already had one
real_t max_error{0.0}; // of the robot's position [m]
real_t map_error{0.0}; // mean, of each landmark from the post that made it [m]
std::vector<Point> landmarks; // the map at the end
std::vector<uint_t> made_by; // the post whose sighting made each landmark
std::map<std::pair<uint_t, uint_t>, uint_t> confused; // (post seen, post matched) -> times
};
// A sighting from the true pose, and the post it really is of
struct Seen
{
};
std::vector<Seen> sight(const BoxWorld &world, const Pose &pose, const EKFSlam::Parameters &p,
std::mt19937 &rng)
{
std::normal_distribution<real_t> range_noise(0.0, p.range_noise);
std::normal_distribution<real_t> bearing_noise(0.0, p.bearing_noise);
std::vector<Seen> seen;
for (uint_t id = 0; id < POSTS.size(); ++id)
{
const real_t dx = POSTS[id][0] - pose.x;
const real_t dy = POSTS[id][1] - pose.y;
const real_t range = std::hypot(dx, dy);
if (range > SENSOR_RANGE)
{
continue;
}
const real_t angle = std::atan2(dy, dx);
if (world.ray_cast({pose.x, pose.y}, angle, range).hit)
{
continue;
}
seen.push_back(
{id, range + range_noise(rng), wrap_angle(angle - pose.yaw + bearing_noise(rng))});
}
return seen;
}
Score run(Method method, real_t radius = 0.0)
{
const BoxWorld world = BoxWorld::warehouse();
p.turn_noise *= ODOMETRY_NOISE;
p.turn_drift *= ODOMETRY_NOISE;
if (method == Method::nearest_metres)
{
// the nearest post in metres is taken at its word: no gate of the filter's either
p.gate = std::numeric_limits<real_t>::infinity();
}
// every method gets the same random draws, so the same data
std::mt19937 rng(7);
std::normal_distribution<real_t> unit(0.0, 1.0);
Pose truth{world.start()[0], world.start()[1], 0.0};
EKFSlam slam(truth, p);
std::map<uint_t, uint_t> made_by; // landmark id -> the post whose sighting made it
Score score;
const auto make = [&](uint_t landmark, uint_t post)
{
for (const auto &[l, q] : made_by)
{
if (q == post)
{
++score.n_duplicates;
break;
}
}
made_by[landmark] = post;
};
std::size_t step = 0;
for (const auto &target : ROUTE)
{
while (std::hypot(target[0] - truth.x, target[1] - truth.y) > 0.05)
{
const real_t error =
wrap_angle(std::atan2(target[1] - truth.y, target[0] - truth.x) - truth.yaw);
const real_t turn = std::clamp(error, -MAX_TURN * DT, MAX_TURN * DT);
const real_t distance = SPEED * DT * std::max(0.0, std::cos(error));
const real_t mid = truth.yaw + 0.5 * turn;
truth.x += distance * std::cos(mid);
truth.y += distance * std::sin(mid);
truth.yaw = wrap_angle(truth.yaw + turn);
const real_t sd = p.distance_noise * std::sqrt(distance);
const real_t st = std::sqrt(p.turn_noise * p.turn_noise * std::abs(turn) +
p.turn_drift * p.turn_drift * distance);
const real_t measured_distance = distance + sd * unit(rng);
const real_t measured_turn = turn + st * unit(rng);
slam.predict(measured_distance, measured_turn);
if (++step % SIGHT_EVERY == 0)
{
const auto seen = sight(world, truth, p, rng);
score.n_sightings += seen.size();
if (method == Method::ids)
{
std::vector<EKFSlam::Observation> observations;
for (const auto &s : seen)
{
if (!slam.has_landmark(s.post))
{
make(s.post, s.post);
}
observations.push_back({s.post, s.range, s.bearing});
}
slam.update(observations);
}
else if (method == Method::nearest_metres)
{
// where each sighting puts its post, and the mapped post nearest to that
for (const auto &s : seen)
{
const Pose at = slam.pose();
const real_t a = at.yaw + s.bearing;
const Point where{at.x + s.range * std::cos(a),
at.y + s.range * std::sin(a)};
uint_t best = 0;
real_t best_d = std::numeric_limits<real_t>::infinity();
for (const auto id : slam.landmark_ids())
{
const auto m = slam.landmark(id);
const real_t d = std::hypot(m[0] - where[0], m[1] - where[1]);
if (d < best_d)
{
best_d = d;
best = id;
}
}
if (best_d <= radius)
{
if (made_by[best] != s.post)
{
++score.n_wrong;
++score.confused[{s.post, made_by[best]}];
}
}
else
{
best = slam.n_landmarks();
make(best, s.post);
}
slam.update({{best, s.range, s.bearing}});
}
}
else
{
std::vector<EKFSlam::Sighting> sightings;
for (const auto &s : seen)
{
sightings.push_back({s.range, s.bearing});
}
const auto result = slam.update_unknown(sightings);
for (std::size_t i = 0; i < seen.size(); ++i)
{
if (result.kinds[i] == Kind::added)
{
make(result.ids[i], seen[i].post);
}
else if (result.kinds[i] == Kind::matched &&
made_by[result.ids[i]] != seen[i].post)
{
++score.n_wrong;
++score.confused[{seen[i].post, made_by[result.ids[i]]}];
}
}
score.n_discarded += result.n_discarded;
}
}
const Pose at = slam.pose();
score.max_error = std::max(score.max_error, std::hypot(at.x - truth.x, at.y - truth.y));
}
}
const Pose at = slam.pose();
score.final_error = std::hypot(at.x - truth.x, at.y - truth.y);
score.n_landmarks = slam.n_landmarks();
for (const auto id : slam.landmark_ids())
{
const auto m = slam.landmark(id);
const auto &post = POSTS[made_by.at(id)];
score.map_error += std::hypot(m[0] - post[0], m[1] - post[1]);
score.landmarks.push_back(m);
score.made_by.push_back(made_by.at(id));
}
score.map_error /= std::max<uint_t>(1, score.n_landmarks);
if (method == Method::ids)
{
score.n_discarded += slam.n_rejected();
}
return score;
}
void report(const std::string &name, const Score &s)
{
std::cout << " " << std::left << std::setw(22) << name << std::right << std::setw(7)
<< s.n_wrong << std::setw(11) << s.n_discarded << std::setw(11) << s.n_landmarks
<< std::setw(12) << s.n_duplicates << std::fixed << std::setprecision(3)
<< std::setw(12) << s.max_error << std::setw(11) << s.final_error << std::setw(11)
<< s.map_error << "\n";
}
// "once", "twice", "3 times"
std::string times(uint_t n)
{
return n == 1 ? "once" : (n == 2 ? "twice" : std::to_string(n) + " times");
}
// Which posts a method got wrong: mapped more than once, or taken for another post
void explain(const std::string &name, const Score &s)
{
std::cout << " " << name << "\n";
bool any = false;
std::map<uint_t, uint_t> copies;
for (const auto post : s.made_by)
{
++copies[post];
}
for (const auto &[post, n] : copies)
{
if (n > 1)
{
std::cout << " post " << post << " mapped " << times(n) << "\n";
any = true;
}
}
for (const auto &[pair, n] : s.confused)
{
std::cout << " post " << pair.first << " taken for post " << pair.second << " "
<< times(n) << "\n";
any = true;
}
if (!any)
{
std::cout << " nothing\n";
}
}
void run()
{
bitsim::utils::io::print_title("1. The same loop, four ways to tell the posts apart");
std::cout << " " << POSTS.size() << " posts, two pairs 40 cm apart; " << ids.n_sightings
<< " sightings, each of a post within " << SENSOR_RANGE << " m\n\n";
std::cout << " wrong not used landmarks duplicates max error"
" end error map error\n";
std::cout << " [m]"
" [m] [m]\n";
const Score far = run(Method::nearest_metres, 0.5);
report("ids", ids);
report("nearest, within 0.2 m", near);
report("nearest, within 0.5 m", far);
report("Mahalanobis", mahalanobis);
bitsim::utils::io::print_title("2. What went wrong");
explain("nearest, within 0.2 m", near);
explain("nearest, within 0.5 m", far);
explain("Mahalanobis", mahalanobis);
}
} // namespace ekf_slam_data_association
int main()
{
return 0;
}
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
Simultaneous localisation and mapping with an extended Kalman filter: a robot works out where it is a...
Definition ekf_slam.h:145
A rectangular slice of the plane with axis-aligned box obstacles, a start and a goal.
Definition box_world.h:29
static BoxWorld warehouse()
The warehouse of the grid_planning and rrt examples: 8 x 5 m, a 0.25 m robot, five shelves and a wall...
Definition box_world.cpp:152
int main()
Definition discounted_returns.cpp:19
real_t wrap_angle(real_t angle)
The angle [rad] wrapped to , so that angles that differ by whole turns compare equal.
Definition math_utils.h:66
void print_title(const std::string &title, std::ostream &out)
An empty line and then the title.
Definition print_utils.cpp:17
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
real_t near(real_t angle, real_t reference)
Definition diff_drive_mpc.cpp:120
Definition ekf_slam_data_association.cpp:38
const real_t ODOMETRY_NOISE
Definition ekf_slam_data_association.cpp:51
const real_t DT
Definition ekf_slam_data_association.cpp:46
EKFSlam::Pose Pose
Definition ekf_slam_data_association.cpp:73
std::string times(uint_t n)
Definition ekf_slam_data_association.cpp:292
void report(const std::string &name, const Score &s)
Definition ekf_slam_data_association.cpp:282
void run()
Definition ekf_slam_data_association.cpp:327
const real_t SENSOR_RANGE
Definition ekf_slam_data_association.cpp:49
std::array< real_t, 2 > Point
Definition ekf_slam_data_association.cpp:54
std::vector< Seen > sight(const BoxWorld &world, const Pose &pose, const EKFSlam::Parameters &p, std::mt19937 &rng)
Definition ekf_slam_data_association.cpp:99
const std::vector< Point > ROUTE
Definition ekf_slam_data_association.cpp:62
const real_t SPEED
Definition ekf_slam_data_association.cpp:47
const std::vector< Point > POSTS
Definition ekf_slam_data_association.cpp:57
real_t wrap_angle(real_t angle)
The angle [rad] wrapped to , so that angles that differ by whole turns compare equal.
Definition math_utils.h:66
void explain(const std::string &name, const Score &s)
Definition ekf_slam_data_association.cpp:298
Method
Definition ekf_slam_data_association.cpp:67
@ nearest_metres
Definition ekf_slam_data_association.cpp:69
@ ids
Definition ekf_slam_data_association.cpp:68
@ mahalanobis
Definition ekf_slam_data_association.cpp:70
const real_t MAX_TURN
Definition ekf_slam_data_association.cpp:48
const uint_t SIGHT_EVERY
Definition ekf_slam_data_association.cpp:50
real_t error(const Pose &a, const Pose &b)
Definition ekf_slam.cpp:176
where(plane)
Definition grid_world_gif.py:37
Vector step(const QuadrotorDynamicsConfig &config, const Vector &x, const Vector &u, real_t max_rotor_speed)
Definition quadrotor_lqr.cpp:114
Print labelled values, one per line, as the examples do: two spaces, the label padded to a width,...
real_t distance_noise
of the odometry's distance [m per square root of m]
Definition ekf_slam.h:149
Definition ekf_slam_data_association.cpp:77
std::vector< uint_t > made_by
Definition ekf_slam_data_association.cpp:87
real_t max_error
Definition ekf_slam_data_association.cpp:83
std::map< std::pair< uint_t, uint_t >, uint_t > confused
Definition ekf_slam_data_association.cpp:88
uint_t n_duplicates
Definition ekf_slam_data_association.cpp:82
std::vector< Point > landmarks
Definition ekf_slam_data_association.cpp:86
uint_t n_landmarks
Definition ekf_slam_data_association.cpp:81
uint_t n_sightings
Definition ekf_slam_data_association.cpp:78
real_t map_error
Definition ekf_slam_data_association.cpp:85
uint_t n_wrong
Definition ekf_slam_data_association.cpp:79
real_t final_error
Definition ekf_slam_data_association.cpp:84
uint_t n_discarded
Definition ekf_slam_data_association.cpp:80
Definition ekf_slam_data_association.cpp:93
real_t bearing
Definition ekf_slam_data_association.cpp:96
uint_t post
Definition ekf_slam_data_association.cpp:94
real_t range
Definition ekf_slam_data_association.cpp:95