use std::collections::VecDeque;
use std::ops::Range;
use nalgebra::{Matrix2, UnitQuaternion, Vector2, Vector3};
use visloc_core::geometry::SE3;
const MAX_FIX_WHEEL_GAP_NS: i64 = 200_000_000;
const FIX_WHEEL_HISTORY_NS: i64 = 10_000_000_000;
const MAX_WAITING_FIXES: usize = 100;
const VIO_STDDEV_M: f64 = 0.005;
const FIX_STDDEV_M: f64 = 0.015;
#[derive(Debug, Clone, PartialEq)]
pub struct WheelCalibrationConfig {
pub max_gap_ns: i64,
pub min_turn_deg: f64,
pub min_travel_m: f64,
pub min_pairs: usize,
pub min_fixes: usize,
pub max_fix_residual_m: f64,
pub window_s: f64,
pub segment_s: f64,
pub fix_segment_s: f64,
pub offset_stddev_m: f64,
pub level: bool,
}
impl Default for WheelCalibrationConfig {
fn default() -> Self {
Self {
max_gap_ns: 20_000_000,
min_turn_deg: 180.0,
min_travel_m: 1.0,
min_pairs: 200,
min_fixes: 20,
max_fix_residual_m: 0.10,
window_s: 0.0,
segment_s: 5.0,
fix_segment_s: 30.0,
offset_stddev_m: 0.03,
level: true,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CalibrationSource {
Vio,
Map,
Prior,
Default,
}
impl CalibrationSource {
pub const fn name(self) -> &'static str {
match self {
Self::Vio => "vio",
Self::Map => "map",
Self::Prior => "prior",
Self::Default => "default",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct WheelCalibrationEstimate {
pub offset: Vector2<f64>,
pub scale: f64,
pub vio_scale: Option<f64>,
pub body_from_level: UnitQuaternion<f64>,
pub tilt_deg: f64,
pub rms_m: f64,
pub rms_without_offset_m: f64,
pub map_rms_m: Option<f64>,
pub turned_deg: f64,
pub pairs: usize,
pub fixes: usize,
pub offset_from: CalibrationSource,
pub scale_from: CalibrationSource,
pub level_from: CalibrationSource,
}
impl WheelCalibrationEstimate {
pub fn identity() -> Self {
Self {
offset: Vector2::zeros(),
scale: 1.0,
vio_scale: None,
body_from_level: UnitQuaternion::identity(),
tilt_deg: 0.0,
rms_m: f64::NAN,
rms_without_offset_m: f64::NAN,
map_rms_m: None,
turned_deg: 0.0,
pairs: 0,
fixes: 0,
offset_from: CalibrationSource::Default,
scale_from: CalibrationSource::Default,
level_from: CalibrationSource::Default,
}
}
pub fn imu_from_wheel(&self, imu_from_body: &SE3) -> SE3 {
let rotation = imu_from_body.rotation * self.body_from_level;
let offset = Vector3::new(self.offset.x, self.offset.y, 0.0);
SE3::new(rotation, imu_from_body.translation - rotation * offset)
}
pub fn odom_from_body(&self, odom_from_wheel: &SE3) -> SE3 {
let offset = Vector3::new(self.offset.x, self.offset.y, 0.0);
SE3::new(
odom_from_wheel.rotation * self.body_from_level.inverse(),
self.scale * (odom_from_wheel.translation + odom_from_wheel.rotation * offset),
)
}
}
#[derive(Debug, Clone, Copy)]
struct Pair {
timestamp_ns: i64,
body: Vector2<f64>,
wheel: Vector2<f64>,
wheel_yaw: f64,
up_in_body: Vector3<f64>,
}
#[derive(Debug, Clone)]
pub struct WheelCalibration {
config: WheelCalibrationConfig,
imu_from_body: Option<SE3>,
prior: Option<WheelCalibrationEstimate>,
vio: VecDeque<(i64, SE3)>,
waiting: VecDeque<(i64, SE3)>,
wheel_history: VecDeque<(i64, SE3)>,
waiting_fixes: VecDeque<(i64, SE3)>,
pairs: VecDeque<Pair>,
fix_pairs: VecDeque<Pair>,
}
impl WheelCalibration {
pub fn new(config: WheelCalibrationConfig, imu_from_body: SE3) -> Self {
let mut calibration = Self::without_vio(config);
calibration.imu_from_body = Some(imu_from_body);
calibration
}
pub fn without_vio(config: WheelCalibrationConfig) -> Self {
Self {
config,
imu_from_body: None,
prior: None,
vio: VecDeque::new(),
waiting: VecDeque::new(),
wheel_history: VecDeque::new(),
waiting_fixes: VecDeque::new(),
pairs: VecDeque::new(),
fix_pairs: VecDeque::new(),
}
}
#[must_use]
pub fn with_prior(mut self, prior: WheelCalibrationEstimate) -> Self {
self.prior = Some(prior);
self
}
pub fn set_imu_from_body(&mut self, imu_from_body: SE3) {
self.imu_from_body = Some(imu_from_body);
}
pub const fn has_imu_from_body(&self) -> bool {
self.imu_from_body.is_some()
}
pub fn pairs(&self) -> usize {
self.pairs.len()
}
pub fn fixes(&self) -> usize {
self.fix_pairs.len()
}
pub fn turned_deg(&self) -> f64 {
turned_rad(&self.pairs).to_degrees()
}
pub fn travel_m(&self) -> f64 {
travel(&self.pairs)
}
pub fn fix_turned_deg(&self) -> f64 {
turned_rad(&self.fix_pairs).to_degrees()
}
pub fn fix_travel_m(&self) -> f64 {
travel(&self.fix_pairs)
}
pub fn is_ready(&self) -> bool {
self.pairs.len() >= self.config.min_pairs.max(3)
&& self.turned_deg() >= self.config.min_turn_deg
&& self.travel_m() >= self.config.min_travel_m
}
pub fn fixes_ready(&self) -> bool {
self.fix_pairs.len() >= self.config.min_fixes.max(3)
}
pub fn fixes_travelled(&self) -> bool {
self.fixes_ready() && self.fix_travel_m() >= self.config.min_travel_m
}
pub fn add_vio(&mut self, timestamp_ns: i64, world_from_imu: &SE3) {
if self.imu_from_body.is_none()
|| self
.vio
.back()
.is_some_and(|(last, _)| timestamp_ns <= *last)
{
return;
}
self.vio.push_back((timestamp_ns, world_from_imu.clone()));
while let Some((t, _)) = self.waiting.front() {
if *t > timestamp_ns {
break;
}
let (t, wheel) = self.waiting.pop_front().expect("front exists");
self.pair(t, &wheel);
}
let keep_from = self
.waiting
.front()
.map_or(timestamp_ns, |(t, _)| (*t).min(timestamp_ns))
- self.config.max_gap_ns;
while self.vio.len() > 2 && self.vio[1].0 < keep_from {
self.vio.pop_front();
}
}
pub fn add_wheel(&mut self, timestamp_ns: i64, odom_from_wheel: &SE3) {
if self
.wheel_history
.back()
.is_some_and(|(last, _)| timestamp_ns <= *last)
{
return;
}
self.wheel_history
.push_back((timestamp_ns, odom_from_wheel.clone()));
while self
.wheel_history
.front()
.is_some_and(|(t, _)| timestamp_ns - t > FIX_WHEEL_HISTORY_NS)
{
self.wheel_history.pop_front();
}
while self
.waiting_fixes
.front()
.is_some_and(|(t, _)| *t <= timestamp_ns)
{
let (t, fix) = self.waiting_fixes.pop_front().expect("front exists");
self.pair_fix(t, &fix);
}
if self.imu_from_body.is_none() {
return;
}
if self
.vio
.back()
.is_some_and(|(last, _)| timestamp_ns <= *last)
{
self.pair(timestamp_ns, odom_from_wheel);
} else {
self.waiting
.push_back((timestamp_ns, odom_from_wheel.clone()));
}
}
pub fn add_fix(&mut self, timestamp_ns: i64, map_from_body: &SE3) {
if self
.wheel_history
.back()
.is_some_and(|(last, _)| timestamp_ns <= *last)
{
self.pair_fix(timestamp_ns, map_from_body);
} else if self.waiting_fixes.len() < MAX_WAITING_FIXES {
self.waiting_fixes
.push_back((timestamp_ns, map_from_body.clone()));
}
}
fn pair(&mut self, timestamp_ns: i64, odom_from_wheel: &SE3) {
let Some(imu_from_body) = &self.imu_from_body else {
return;
};
let Some((t, world_from_imu)) = self
.vio
.iter()
.min_by_key(|(t, _)| (t - timestamp_ns).abs())
else {
return;
};
if (t - timestamp_ns).abs() > self.config.max_gap_ns {
return;
}
let body = world_from_imu.translation + world_from_imu.rotation * imu_from_body.translation;
let world_from_body = world_from_imu.rotation * imu_from_body.rotation;
let pair = Pair {
timestamp_ns,
body: body.xy(),
wheel: odom_from_wheel.translation.xy(),
wheel_yaw: yaw_of(&odom_from_wheel.rotation),
up_in_body: world_from_body.inverse() * Vector3::z(),
};
self.pairs.push_back(pair);
drop_older(&mut self.pairs, timestamp_ns, self.config.window_s);
}
fn pair_fix(&mut self, timestamp_ns: i64, map_from_body: &SE3) {
let index = self
.wheel_history
.partition_point(|(t, _)| *t < timestamp_ns);
let Some((after_ns, after)) = self.wheel_history.get(index) else {
return;
};
let wheel = if *after_ns == timestamp_ns {
after.clone()
} else {
let Some((before_ns, before)) = index.checked_sub(1).map(|i| &self.wheel_history[i])
else {
return;
};
if after_ns - before_ns > MAX_FIX_WHEEL_GAP_NS {
return;
}
let alpha = (timestamp_ns - before_ns) as f64 / (after_ns - before_ns) as f64;
SE3::new(
before.rotation.slerp(&after.rotation, alpha),
before.translation.lerp(&after.translation, alpha),
)
};
let pair = Pair {
timestamp_ns,
body: map_from_body.translation.xy(),
wheel: wheel.translation.xy(),
wheel_yaw: yaw_of(&wheel.rotation),
up_in_body: map_from_body.rotation.inverse() * Vector3::z(),
};
if self
.fix_pairs
.back()
.is_some_and(|last| timestamp_ns <= last.timestamp_ns)
{
return;
}
self.fix_pairs.push_back(pair);
drop_older(&mut self.fix_pairs, timestamp_ns, self.config.window_s);
}
pub fn estimate(&self) -> Option<WheelCalibrationEstimate> {
let vio_ready = self.is_ready();
let fixes_ready = self.fixes_ready();
let fixes_travelled = self.fixes_travelled();
if !vio_ready && !fixes_ready {
return None;
}
let prior = self.prior.as_ref();
let prior_offset = prior.map_or_else(Vector2::zeros, |prior| prior.offset);
let weight = |stddev: f64| {
if self.config.offset_stddev_m > 0.0 {
(stddev / self.config.offset_stddev_m).powi(2)
} else {
0.0
}
};
let segment_ns = (self.config.segment_s.max(0.0) * 1e9) as i64;
let fix_segment_ns = (self.config.fix_segment_s.max(0.0) * 1e9) as i64;
let pairs: Vec<Pair> = self.pairs.iter().copied().collect();
let vio_fit = vio_ready.then(|| {
fit(
&pairs,
segment_ns,
OffsetFit::Fitted(prior_offset, weight(VIO_STDDEV_M)),
30,
)
});
let mut fixes: Vec<Pair> = self.fix_pairs.iter().copied().collect();
let (offset, offset_from, rms_m, rms_without_offset_m) = if let Some(vio_fit) = &vio_fit {
let without = fit(&pairs, segment_ns, OffsetFit::Held(Vector2::zeros()), 0);
(
vio_fit.offset,
CalibrationSource::Vio,
vio_fit.rms,
without.rms,
)
} else if fixes_travelled {
let fitted = robust_fit(
&mut fixes,
fix_segment_ns,
OffsetFit::Fitted(prior_offset, weight(FIX_STDDEV_M)),
self.config.max_fix_residual_m,
self.config.min_fixes,
);
let without = fit(&fixes, fix_segment_ns, OffsetFit::Held(Vector2::zeros()), 0);
(
fitted.offset,
CalibrationSource::Map,
fitted.rms,
without.rms,
)
} else if let Some(prior) = prior {
(prior.offset, CalibrationSource::Prior, f64::NAN, f64::NAN)
} else {
(
Vector2::zeros(),
CalibrationSource::Default,
f64::NAN,
f64::NAN,
)
};
let map_fit = fixes_travelled.then(|| {
robust_fit(
&mut fixes,
fix_segment_ns,
OffsetFit::Held(offset),
self.config.max_fix_residual_m,
self.config.min_fixes,
)
});
let (scale, scale_from) = match (&map_fit, prior) {
(Some(map_fit), _) => (map_fit.scale, CalibrationSource::Map),
(None, Some(prior)) => (prior.scale, CalibrationSource::Prior),
(None, None) => (1.0, CalibrationSource::Default),
};
let mean_up = |pairs: &[Pair]| {
pairs
.iter()
.map(|pair| pair.up_in_body)
.sum::<Vector3<f64>>()
.normalize()
};
let up = if fixes_ready {
Some((mean_up(&fixes), CalibrationSource::Map))
} else if vio_ready {
Some((mean_up(&pairs), CalibrationSource::Vio))
} else {
None
};
let (body_from_level, tilt_deg, level_from) = match (up, prior) {
(Some((up, source)), _) => {
let tilt_deg = up.z.clamp(-1.0, 1.0).acos().to_degrees();
let body_from_level = if self.config.level {
UnitQuaternion::rotation_between(&Vector3::z(), &up)
.unwrap_or_else(UnitQuaternion::identity)
} else {
UnitQuaternion::identity()
};
(body_from_level, tilt_deg, source)
}
(None, Some(prior)) => (
prior.body_from_level,
prior.tilt_deg,
CalibrationSource::Prior,
),
(None, None) => (UnitQuaternion::identity(), 0.0, CalibrationSource::Default),
};
Some(WheelCalibrationEstimate {
offset,
scale,
vio_scale: vio_fit.as_ref().map(|fit| fit.scale),
body_from_level,
tilt_deg,
rms_m,
rms_without_offset_m,
map_rms_m: map_fit.map(|fit| fit.rms),
turned_deg: self.turned_deg(),
pairs: self.pairs.len(),
fixes: fixes.len(),
offset_from,
scale_from,
level_from,
})
}
}
fn drop_older(pairs: &mut VecDeque<Pair>, newest_ns: i64, window_s: f64) {
if window_s <= 0.0 {
return;
}
let window_ns = (window_s * 1e9) as i64;
while pairs
.front()
.is_some_and(|pair| newest_ns - pair.timestamp_ns > window_ns)
{
pairs.pop_front();
}
}
fn turned_rad(pairs: &VecDeque<Pair>) -> f64 {
pairs
.iter()
.zip(pairs.iter().skip(1))
.map(|(a, b)| wrap_angle(b.wheel_yaw - a.wheel_yaw).abs())
.sum()
}
fn travel(pairs: &VecDeque<Pair>) -> f64 {
pairs
.iter()
.zip(pairs.iter().skip(1))
.map(|(a, b)| (b.wheel - a.wheel).norm())
.sum()
}
fn yaw_of(rotation: &UnitQuaternion<f64>) -> f64 {
let matrix = rotation.to_rotation_matrix();
matrix[(1, 0)].atan2(matrix[(0, 0)])
}
fn wrap_angle(angle: f64) -> f64 {
(angle + std::f64::consts::PI).rem_euclid(std::f64::consts::TAU) - std::f64::consts::PI
}
fn rotation2(angle: f64) -> Matrix2<f64> {
let (s, c) = angle.sin_cos();
Matrix2::new(c, -s, s, c)
}
struct Fit {
offset: Vector2<f64>,
scale: f64,
rms: f64,
residuals: Vec<f64>,
}
#[derive(Debug, Clone, Copy)]
enum OffsetFit {
Held(Vector2<f64>),
Fitted(Vector2<f64>, f64),
}
fn segments(pairs: &[Pair], segment_ns: i64) -> Vec<Range<usize>> {
let mut segments = Vec::new();
let mut start = 0;
for index in 1..pairs.len() {
if segment_ns > 0 && pairs[index].timestamp_ns - pairs[start].timestamp_ns > segment_ns {
segments.push(start..index);
start = index;
}
}
if start < pairs.len() {
segments.push(start..pairs.len());
}
segments
}
fn mean(points: impl Iterator<Item = Vector2<f64>>) -> Vector2<f64> {
let (sum, count) = points.fold((Vector2::zeros(), 0usize), |(sum, count), point| {
(sum + point, count + 1)
});
sum / count.max(1) as f64
}
fn fit(pairs: &[Pair], segment_ns: i64, offset_fit: OffsetFit, iterations: usize) -> Fit {
let segments = segments(pairs, segment_ns);
let (mut offset, prior) = match offset_fit {
OffsetFit::Held(offset) => (offset, None),
OffsetFit::Fitted(prior, weight) => (prior, Some((prior, weight))),
};
let similarity = |offset: &Vector2<f64>| {
let q: Vec<Vector2<f64>> = pairs
.iter()
.map(|pair| pair.wheel + rotation2(pair.wheel_yaw) * offset)
.collect();
let (mut stretches, mut dot, mut spread) = (Vec::with_capacity(segments.len()), 0.0, 0.0);
for range in &segments {
let (q, pairs) = (&q[range.clone()], &pairs[range.clone()]);
let mean_q = mean(q.iter().copied());
let mean_g = mean(pairs.iter().map(|pair| pair.body));
let h = q
.iter()
.zip(pairs)
.map(|(q, pair)| (q - mean_q) * (pair.body - mean_g).transpose())
.sum::<Matrix2<f64>>();
let rotation = rotation2((h[(0, 1)] - h[(1, 0)]).atan2(h[(0, 0)] + h[(1, 1)]));
for (q, pair) in q.iter().zip(pairs) {
dot += (pair.body - mean_g).dot(&(rotation * (q - mean_q)));
spread += (q - mean_q).norm_squared();
}
stretches.push((rotation, mean_q, mean_g));
}
let scale = if spread > 0.0 { dot / spread } else { 1.0 };
(stretches, scale, q)
};
if let Some((prior, weight)) = prior {
for _ in 0..iterations {
let (stretches, scale, _) = similarity(&offset);
let (mut normal, mut rhs) = (Matrix2::identity() * weight, prior * weight);
for (range, (rotation, _, _)) in segments.iter().zip(&stretches) {
let pairs = &pairs[range.clone()];
let count = pairs.len() as f64;
let mean_turn = pairs
.iter()
.map(|pair| rotation2(pair.wheel_yaw))
.sum::<Matrix2<f64>>()
/ count;
let mean_wheel = mean(pairs.iter().map(|pair| pair.wheel));
let mean_body = mean(pairs.iter().map(|pair| pair.body));
for pair in pairs {
let m = scale * rotation * (rotation2(pair.wheel_yaw) - mean_turn);
let b = pair.body - mean_body - scale * rotation * (pair.wheel - mean_wheel);
normal += m.transpose() * m;
rhs += m.transpose() * b;
}
}
match normal.try_inverse() {
Some(inverse) => offset = inverse * rhs,
None => break,
}
}
}
let (stretches, scale, q) = similarity(&offset);
let mut residuals = Vec::with_capacity(pairs.len());
for (range, (rotation, mean_q, mean_g)) in segments.iter().zip(&stretches) {
for (q, pair) in q[range.clone()].iter().zip(&pairs[range.clone()]) {
residuals.push((pair.body - mean_g - scale * rotation * (q - mean_q)).norm());
}
}
let rms = (residuals.iter().map(|r| r * r).sum::<f64>() / pairs.len().max(1) as f64).sqrt();
Fit {
offset,
scale,
rms,
residuals,
}
}
fn robust_fit(
pairs: &mut Vec<Pair>,
segment_ns: i64,
offset_fit: OffsetFit,
max_residual: f64,
min_kept: usize,
) -> Fit {
let mut result = fit(pairs, segment_ns, offset_fit, 30);
for _ in 0..2 {
if max_residual <= 0.0 || result.residuals.iter().all(|r| *r <= max_residual) {
break;
}
let kept: Vec<Pair> = pairs
.iter()
.zip(&result.residuals)
.filter(|(_, r)| **r <= max_residual)
.map(|(pair, _)| *pair)
.collect();
if kept.len() < min_kept.max(3) {
break;
}
*pairs = kept;
result = fit(pairs, segment_ns, offset_fit, 30);
}
result
}
#[cfg(test)]
mod tests {
use super::*;
const MS: i64 = 1_000_000;
fn rz(angle: f64) -> UnitQuaternion<f64> {
UnitQuaternion::from_axis_angle(&Vector3::z_axis(), angle)
}
fn drive_laps(laps: usize, turn_gain: f64) -> Vec<(f64, f64, f64)> {
let (mut x, mut y, mut yaw, mut poses) = (0.0, 0.0, 0.0, Vec::new());
for _ in 0..laps {
for (steps, speed, turn) in [
(100, 0.02, 0.0),
(90, 0.0, std::f64::consts::PI / 90.0),
(100, 0.02, 0.0),
(45, 0.0, std::f64::consts::PI / 90.0),
] {
for _ in 0..steps {
x += speed * f64::cos(yaw);
y += speed * f64::sin(yaw);
yaw += turn * turn_gain;
poses.push((x, y, yaw));
}
}
}
poses
}
fn drive() -> Vec<(f64, f64, f64)> {
drive_laps(1, 1.0)
}
fn imu_from_body() -> SE3 {
SE3::new(
UnitQuaternion::from_euler_angles(-1.55, 0.02, -1.6),
Vector3::new(0.03, -0.89, -0.25),
)
}
fn body_in(world: &SE3, wheel: &SE3, offset: Vector2<f64>, scale: f64) -> SE3 {
let body_position =
scale * (wheel.translation + wheel.rotation * Vector3::new(offset.x, offset.y, 0.0));
world.compose(&SE3::new(wheel.rotation, body_position))
}
fn wheel_pose((x, y, yaw): (f64, f64, f64)) -> SE3 {
SE3::new(rz(yaw), Vector3::new(x, y, 0.0))
}
fn run_with(
offset: Vector2<f64>,
scale: f64,
given: &SE3,
fixes: &[(i64, SE3)],
) -> WheelCalibration {
let truth = imu_from_body();
let world = SE3::new(rz(0.4), Vector3::new(0.3, -1.2, 0.5));
let mut calibration =
WheelCalibration::new(WheelCalibrationConfig::default(), given.clone());
let mut fixes = fixes.iter().peekable();
for (index, pose) in drive().into_iter().enumerate() {
let wheel = wheel_pose(pose);
let world_from_imu = body_in(&world, &wheel, offset, scale).compose(&truth.inverse());
let t = wheel_stamp(index);
if index % 2 == 0 {
calibration.add_vio(t + 2 * MS, &world_from_imu);
calibration.add_wheel(t, &wheel);
} else {
calibration.add_wheel(t, &wheel);
calibration.add_vio(t + 2 * MS, &world_from_imu);
}
while let Some((fix_t, fix)) = fixes.next_if(|(fix_t, _)| *fix_t <= t) {
calibration.add_fix(*fix_t, fix);
}
}
calibration
}
fn run(offset: Vector2<f64>, scale: f64, given: &SE3) -> WheelCalibration {
run_with(offset, scale, given, &[])
}
fn wheel_stamp(index: usize) -> i64 {
1_000 * MS + index as i64 * 50 * MS
}
fn fixes(
offset: Vector2<f64>,
scale: f64,
tilt: &UnitQuaternion<f64>,
every: usize,
) -> Vec<(i64, SE3)> {
let map = SE3::new(rz(-1.1), Vector3::new(4.0, 2.0, 0.0));
let poses = drive();
(1..poses.len())
.step_by(every)
.map(|index| {
let (a, b) = (wheel_pose(poses[index - 1]), wheel_pose(poses[index]));
let wheel = SE3::new(
a.rotation.slerp(&b.rotation, 0.5),
a.translation.lerp(&b.translation, 0.5),
);
let body = body_in(&map, &wheel, offset, scale);
(
wheel_stamp(index) - 25 * MS,
SE3::new(body.rotation * tilt.inverse(), body.translation),
)
})
.collect()
}
#[test]
fn recovers_the_offset_scale_and_level() {
let offset = Vector2::new(0.041, 0.064);
let calibration = run(offset, 1.03, &imu_from_body());
assert!(calibration.is_ready());
assert!(!calibration.fixes_ready());
let estimate = calibration.estimate().expect("estimate");
assert!((estimate.offset - offset).norm() < 1e-4, "{estimate:?}");
assert!((estimate.vio_scale.expect("vio") - 1.03).abs() < 1e-5);
assert_eq!(estimate.scale, 1.0);
assert!(estimate.rms_m < 1e-4);
assert!(estimate.rms_without_offset_m > 0.02);
assert!(estimate.tilt_deg < 1e-6);
assert!(
(estimate.turned_deg - 270.0).abs() < 2.5,
"{}",
estimate.turned_deg
);
assert_eq!(estimate.pairs, drive().len());
assert_eq!(
(
estimate.offset_from,
estimate.scale_from,
estimate.level_from
),
(
CalibrationSource::Vio,
CalibrationSource::Default,
CalibrationSource::Vio
)
);
assert_eq!(estimate.map_rms_m, None);
}
#[test]
fn a_tilted_extrinsic_is_levelled_and_the_body_follows_the_wheels() {
let truth = imu_from_body();
let tilt = UnitQuaternion::from_euler_angles(0.0, 2f64.to_radians(), 0.0);
let given = SE3::new(truth.rotation * tilt, truth.translation);
let offset = Vector2::new(0.013, 0.062);
let estimate = run(offset, 1.0, &given).estimate().expect("estimate");
assert!((estimate.tilt_deg - 2.0).abs() < 1e-6);
let imu_from_wheel = estimate.imu_from_wheel(&given);
assert!(imu_from_wheel.rotation.angle_to(&truth.rotation) < 1e-9);
let expected = truth.translation - truth.rotation * Vector3::new(offset.x, offset.y, 0.0);
assert!((imu_from_wheel.translation - expected).norm() < 1e-4);
let wheel = SE3::new(rz(0.7), Vector3::new(1.0, 2.0, 0.0));
let odom_from_body = estimate.odom_from_body(&wheel);
let imu_from_wheel_pose = wheel.compose(&imu_from_wheel.inverse());
let imu_from_body_pose = odom_from_body.compose(&given.inverse());
assert!((imu_from_wheel_pose.translation - imu_from_body_pose.translation).norm() < 1e-5);
assert!(
imu_from_wheel_pose
.rotation
.angle_to(&imu_from_body_pose.rotation)
< 1e-9
);
}
#[test]
fn not_ready_before_enough_turning() {
let mut calibration =
WheelCalibration::new(WheelCalibrationConfig::default(), imu_from_body());
for index in 0..300 {
let t = index as i64 * 50 * MS;
let pose = SE3::new(rz(0.0), Vector3::new(index as f64 * 0.02, 0.0, 0.0));
calibration.add_vio(t, &pose.compose(&imu_from_body().inverse()));
calibration.add_wheel(t, &pose);
}
assert_eq!(calibration.pairs(), 300);
assert!(calibration.travel_m() > 5.9);
assert!(!calibration.is_ready());
assert!(calibration.estimate().is_none());
calibration.add_wheel(400 * 50 * MS, &SE3::identity());
calibration.add_vio(401 * 50 * MS, &SE3::identity());
assert_eq!(calibration.pairs(), 300);
}
#[test]
fn map_localizations_give_the_scale_and_the_level_and_the_vio_the_offset() {
let offset = Vector2::new(0.041, 0.064);
let tilt = UnitQuaternion::from_euler_angles(1.5f64.to_radians(), 0.0, 0.0);
let fixes = fixes(offset, 1.012, &tilt, 7);
let calibration = run_with(offset, 1.03, &imu_from_body(), &fixes);
assert!(calibration.fixes_ready());
let estimate = calibration.estimate().expect("estimate");
assert_eq!(estimate.offset_from, CalibrationSource::Vio);
assert_eq!(estimate.scale_from, CalibrationSource::Map);
assert_eq!(estimate.level_from, CalibrationSource::Map);
assert!((estimate.offset - offset).norm() < 1e-4);
assert!((estimate.scale - 1.012).abs() < 1e-4, "{}", estimate.scale);
assert!(
(estimate.tilt_deg - 1.5).abs() < 1e-6,
"{}",
estimate.tilt_deg
);
assert!(estimate.map_rms_m.expect("map fit") < 1e-4);
assert_eq!(estimate.fixes, fixes.len());
}
#[test]
fn map_localizations_alone_hold_the_prior_offset_until_they_turn_and_drop_outliers() {
let offset = Vector2::new(0.03, -0.05);
let prior = WheelCalibrationEstimate {
offset: Vector2::new(0.02, -0.04),
scale: 1.05,
..WheelCalibrationEstimate::identity()
};
let mut fixes = fixes(offset, 0.99, &UnitQuaternion::identity(), 5);
fixes[30].1.translation.x += 1.0;
let mut calibration =
WheelCalibration::without_vio(WheelCalibrationConfig::default()).with_prior(prior);
let mut pending = fixes.iter().peekable();
let mut checked_before_turn = false;
for (index, pose) in drive().into_iter().enumerate() {
let t = wheel_stamp(index);
while let Some((fix_t, fix)) = pending.next_if(|(fix_t, _)| *fix_t <= t) {
calibration.add_fix(*fix_t, fix);
}
calibration.add_wheel(t, &wheel_pose(pose));
if !checked_before_turn && calibration.fix_turned_deg() > 0.5 {
checked_before_turn = true;
let estimate = calibration.estimate().expect("2 m of fixes");
assert_eq!(estimate.offset_from, CalibrationSource::Map);
assert!((estimate.offset - Vector2::new(0.02, -0.04)).norm() < 1e-3);
assert_eq!(estimate.scale_from, CalibrationSource::Map);
assert!((estimate.scale - 0.99).abs() < 1e-3, "{}", estimate.scale);
}
}
assert!(checked_before_turn);
assert!(calibration.fix_turned_deg() > 180.0);
let estimate = calibration.estimate().expect("estimate");
assert_eq!(estimate.offset_from, CalibrationSource::Map);
assert!((estimate.offset - offset).norm() < 3e-4, "{estimate:?}");
assert!((estimate.scale - 0.99).abs() < 1e-4);
assert!(estimate.tilt_deg < 1e-6);
assert_eq!(estimate.fixes, fixes.len() - 1);
}
#[test]
fn stretches_keep_the_wheels_heading_drift_out_of_the_offset() {
let offset = Vector2::new(0.041, 0.064);
let (truth, world) = (
imu_from_body(),
SE3::new(rz(0.4), Vector3::new(0.3, -1.2, 0.5)),
);
let offset_error = |segment_s: f64| {
let config = WheelCalibrationConfig {
segment_s,
..WheelCalibrationConfig::default()
};
let mut calibration = WheelCalibration::new(config, truth.clone());
for (index, (body, wheel)) in drive_laps(4, 1.0)
.into_iter()
.zip(drive_laps(4, 1.05))
.enumerate()
{
let t = wheel_stamp(index);
let body = body_in(&world, &wheel_pose(body), offset, 1.0);
calibration.add_vio(t, &body.compose(&truth.inverse()));
calibration.add_wheel(t, &wheel_pose(wheel));
}
let estimate = calibration.estimate().expect("estimate");
(estimate.offset - offset).norm()
};
let (stretches, one) = (offset_error(5.0), offset_error(0.0));
assert!(stretches < 0.005, "{stretches}");
assert!(one > 0.05, "{one}");
}
#[test]
fn the_window_keeps_only_recent_pairs() {
let config = WheelCalibrationConfig {
window_s: 5.0,
..WheelCalibrationConfig::default()
};
let mut calibration = WheelCalibration::new(config, imu_from_body());
for index in 0..300 {
let t = index as i64 * 50 * MS;
let pose = SE3::new(rz(0.0), Vector3::new(index as f64 * 0.02, 0.0, 0.0));
calibration.add_vio(t, &pose.compose(&imu_from_body().inverse()));
calibration.add_wheel(t, &pose);
}
assert_eq!(calibration.pairs(), 101);
}
}