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.
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.
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,
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:
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.
#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <random>
#include <string>
#include <vector>
{
using Point = std::array<real_t, 2>;
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}};
{
};
{
};
{
};
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;
{
const real_t range = std::hypot(dx, dy);
{
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;
}
{
{
p.gate = std::numeric_limits<real_t>::infinity();
}
std::mt19937 rng(7);
std::normal_distribution<real_t> unit(0.0, 1.0);
Pose truth{world.start()[0], world.start()[1], 0.0};
std::map<uint_t, uint_t> made_by;
{
for (const auto &[l, q] : made_by)
{
if (q == post)
{
++score.n_duplicates;
break;
}
}
made_by[landmark] = post;
};
for (
const auto &target :
ROUTE)
{
while (std::hypot(target[0] - truth.x, target[1] - truth.y) > 0.05)
{
wrap_angle(std::atan2(target[1] - truth.y, target[0] - truth.x) - truth.yaw);
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);
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);
{
const auto seen =
sight(world, truth, p, rng);
score.n_sightings += seen.size();
{
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);
}
{
for (const auto &s : seen)
{
const Pose at = slam.pose();
const real_t a = at.yaw + s.bearing;
at.y + s.range * std::sin(a)};
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);
{
score.n_discarded += slam.n_rejected();
}
return score;
}
{
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";
}
{
return n == 1 ? "once" : (n == 2 ? "twice" : std::to_string(n) + " times");
}
{
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 << " "
any = true;
}
if (!any)
{
std::cout << " nothing\n";
}
}
{
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";
report(
"nearest, within 0.2 m", near);
report(
"nearest, within 0.5 m", far);
explain(
"nearest, within 0.2 m", near);
explain(
"nearest, within 0.5 m", far);
}
}
{
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,...
Kind
Definition ekf_slam.h:200
Definition ekf_slam.h:148
real_t distance_noise
of the odometry's distance [m per square root of m]
Definition ekf_slam.h:149
Definition ekf_slam.h:158
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