Skip to content

Robot Kalman Filters - #3906

Open
sunghyuneun wants to merge 93 commits into
UBC-Thunderbots:masterfrom
sunghyuneun:sunghyuneun/robot_filter
Open

sunghyuneun wants to merge 93 commits into
UBC-Thunderbots:masterfrom
sunghyuneun:sunghyuneun/robot_filter

Conversation

@sunghyuneun

@sunghyuneun sunghyuneun commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

Description

Makes changes to robot filter. Notable changes:

  • Rewrote robot filter to utilize linear Kalman filter to get more accurate measurements of each robot's orientation and position
  • As Kalman filter is able to predict the robot position, if the robot temporarily goes missing in the data, it predicts the robot position at that time
  • Removes robot and filter if it is missing for 2 seconds, fixing RobotFilter does not remove expired robots if there are no new detections #1717.

Do not review any changes to src/software/er_force_simulator_main.cpp‎ as all changes are for logging purposes. I can probably also delete the .gitignore change it was just bothering me.

Testing with real robots is incomplete, and some constants should be changed.

Testing Done

  • Unit tests
  • MATLAB Analysis

MSE & Median Error Comparison

Category / Slice Metric Master Kalman Filter Comparison (Master / KF)
Rows 826,968 640,771
Position (m) Median Error 0.00178 0.00154 1.15× lower
MSE 0.00026 0.00126 0.21× (Master 4.85× lower)
Velocity (m/s) Median Error 0.133 0.0205 6.47× lower
MSE 0.601 0.0227 26.44× lower
Velocity: [0, 0.05) MSE 0.287 (n=669,147) 0.00104 (n=349,683) 275× lower
Velocity: [0.05, 0.5) MSE 5.18 (n=58,316) 0.0323 (n=115,397) 160× lower
Velocity: [0.5, 1.5) MSE 0.0309 (n=67,994) 0.058 (n=125,042) 0.53× (Master 1.88× lower)
Velocity: [1.5, 3) MSE 0.0307 (n=31,511) 0.0635 (n=50,649) 0.48× (Master 2.07× lower)
Velocity: ≥ 3 MSE n/a (n=0) n/a (n=0) No data
Angle (rad) Median Error 0.00738 0.0061 1.21× lower
MSE 0.000312 0.000995 0.31× (Master 3.19× lower)
Angular Velocity (rad/s) Median Error 0.585 0.12 4.88× lower
MSE 0.916 0.218 4.20× lower
Ang. Velocity: [0, 0.1) MSE 0.778 (n=711,521) 0.0299 (n=425,938) 26.05× lower
Ang. Velocity: [0.1, 1) MSE 1.54 (n=69,409) 0.317 (n=112,279) 4.85× lower
Ang. Velocity: [1, 3) MSE 2.75 (n=29,646) 0.885 (n=61,326) 3.11× lower
Ang. Velocity: [3, 6) MSE 0.982 (n=13,562) 0.941 (n=34,842) 1.04× lower
Ang. Velocity: ≥ 6 MSE 0.85 (n=2,830) 0.677 (n=6,386) 1.26× lower

Error Distribution Comparison

Position Error (m)
Error Range Master (%) Kalman Filter (%) Comparison
[0, 0.005] (Nominal) 84.72% 63.81% -20.91% frames in low-error band
(0.005, 0.01] 3.18% 7.13% 2.25× more errors
(0.01, 0.025] 6.73% 15.87% 2.36× more errors
(0.025, 0.05] 4.96% 12.05% 2.43× more errors
(0.05, 0.1] 0.41% 1.12% 2.69× more errors
> 0.1 (Catastrophic) 0.0015% 0.01% 9.79× more errors
Velocity Error (m/s)
Error Range Master (%) Kalman Filter (%) Comparison
[0, 0.05] (Nominal) 9.39% 60.34% +50.95% frames in low-error band
(0.05, 0.1] 23.11% 5.27% 4.38× fewer errors
(0.1, 0.25] 58.91% 20.95% 2.81× fewer errors
(0.25, 0.5] 8.57% 13.16% 1.54× more errors
(0.5, 1] 0.01% 0.24% 18.00× more errors
> 1 (Catastrophic) 0.003% 0.04% 14.56× more errors
Angle Error (rad)
Error Range Master (%) Kalman Filter (%) Comparison
[0, 0.01] (Nominal) 63.46% 68.46% +5.00% frames in low-error band
(0.01, 0.05] 34.48% 23.76% 1.45× fewer errors
(0.05, 0.1] 1.71% 6.13% 3.59× more errors
(0.1, 0.25] 0.36% 1.64% 4.58× more errors
(0.25, 0.5] 0.00024% 0.0041% 16.78× more errors
> 0.5 (Catastrophic) 0.00085% 0.01% 12.54× more errors
Angular Velocity Error (rad/s)
Error Range Master (%) Kalman Filter (%) Comparison
[0, 0.1] (Nominal) 9.12% 43.51% +34.39% frames in low-error band
(0.1, 0.5] 34.48% 39.89% 1.16× more errors
(0.5, 1] 31.49% 8.59% 3.67× fewer errors
(1, 2] 22.76% 7.96% 2.86× fewer errors
(2, 4] 2.15% 0.04% 48.71× fewer errors
> 4 (Catastrophic) 0.0016% 0.01% 6.85× more errors

Resolved Issues

Resolves #3701, fixes #1717.

Length Justification and Key Files to Review

Review Checklist

It is the reviewers responsibility to also make sure every item here has been covered

  • Function & Class comments: All function definitions (usually in the .h file) should have a javadoc style comment at the start of them. For examples, see the functions defined in thunderbots/software/geom. Similarly, all classes should have an associated Javadoc comment explaining the purpose of the class.
  • Remove all commented out code
  • Remove extra print statements: for example, those just used for testing
  • Resolve all TODO's: All TODO (or similar) statements should either be completed or associated with a github issue

@sunghyuneun
sunghyuneun marked this pull request as ready for review November 11, 2026 22:08
@Andrewyx

Copy link
Copy Markdown
Contributor

Is this data suggesting that master is more accurate? Why?

Velocity: [0.5, 1.5) MSE 0.0309 (n=67,994) 0.058 (n=125,042) 0.53× (Master 1.88× lower)
Velocity: [1.5, 3) MSE 0.0307 (n=31,511) 0.0635 (n=50,649) 0.48× (Master 2.07× lower)
Velocity: ≥ 3 MSE n/a (n=0) n/a (n=0) No data

Comment on lines +38 to +47
# Disable for kalman filter
#cc_test(
# name = "ball_filter_test",
# srcs = ["ball_filter_test.cpp"],
# deps = [
# ":ball_filter",
# "//shared/test_util:tbots_gtest_main",
# "//software/world:field",
# ],
#)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-bump, should this be removed


std::optional<Robot> RobotFilter::getFilteredData(
const std::vector<RobotDetection>& new_robot_data, const Timestamp& capture_timestamp,
std::optional<Robot> RobotFilter::estimateRobotState(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-bump, was this ever profiled?

// 1. Whether it is physically possible to arrive the new destination at max
// velocity
// 2. Statistical gating using Mahalanobis distance
// TODO: mahalanobisDistance and update both calculate pseudoinverse. is this a

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stray todo. link # or discard

Comment on lines +195 to +196
// because robots move using motors that stay on, unlike balls that just roll, i will
// be assuming that they keep moving with constant velocity

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i?

Comment on lines +246 to +247
// Without a previous detection there is no interval to reason over, so we have no
// grounds to call this one impossible

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can we assume it is possible then?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The explanation could be that we need to initialize the robot SOMEWHERE at the start. So we have to assume it is possible in order to start somewhere

@sunghyuneun

Copy link
Copy Markdown
Contributor Author

Is this data suggesting that master is more accurate? Why?

Velocity: [0.5, 1.5) MSE 0.0309 (n=67,994) 0.058 (n=125,042) 0.53× (Master 1.88× lower)
Velocity: [1.5, 3) MSE 0.0307 (n=31,511) 0.0635 (n=50,649) 0.48× (Master 2.07× lower)
Velocity: ≥ 3 MSE n/a (n=0) n/a (n=0) No data

Because of model tuning issues. The model is unsure whether to trust internal model more or the new update more, and so at high speeds which are unlikelier, Kalman believes its internal model more while master just says what it says. So perhaps process noise covariance needs to be increased.

@StarrryNight

Copy link
Copy Markdown
Contributor

Is this data suggesting that master is more accurate? Why?

Velocity: [0.5, 1.5) MSE 0.0309 (n=67,994) 0.058 (n=125,042) 0.53× (Master 1.88× lower)
Velocity: [1.5, 3) MSE 0.0307 (n=31,511) 0.0635 (n=50,649) 0.48× (Master 2.07× lower)
Velocity: ≥ 3 MSE n/a (n=0) n/a (n=0) No data

Because of model tuning issues. The model is unsure whether to trust internal model more or the new update more, and so at high speeds which are unlikelier, Kalman believes its internal model more while master just says what it says. So perhaps process noise covariance needs to be increased.

Yeah... And there are pros and cons for having high process covariance or low process covariance.
At high process covariance, the model trust prediction less and rely on measurements more. This means the velocity smoothing effect is weakened (because the filter basicaly uses the prediction to smooth the measurements)
At low process covariance, we trust preidction more but measurements less. This means the filter is more prone to latency and lag when robot actuates.
So ultimately depends on what the final goal is for this filter. IMO we should tune it to a sweet spot where we get a balance from both sides.

The more engineering way to do this is to set a requirement of how much lag vs how much error we want, and then find the tuned value that satisfies it.

@StarrryNight StarrryNight left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments, will add more later

0, 1, 0, delta_t,
0, 0, 1, 0,
0, 0, 0, 1;
ang_kalman_filter.process_model << 1, delta_t,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can u explain why we are splitting angle state and position state? Is it because of the angle wrapping code?

Comment on lines +246 to +247
// Without a previous detection there is no interval to reason over, so we have no
// grounds to call this one impossible

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The explanation could be that we need to initialize the robot SOMEWHERE at the start. So we have to assume it is possible in order to start somewhere

void reset(const PosMeasurement& pos_measurement,
const AngMeasurement& ang_measurement, const Timestamp& current_time);

PosKalmanFilter pos_kalman_filter;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo the code will be a lot cleaner if its one kf. It is more consistent with the other kfs in the codebase asw

#include "software/networking/unix/threaded_proto_unix_sender.hpp"
#include "software/simulation/er_force_simulator.h"

// CSV file that the filtered robot state is logged to, alongside the ground truth

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldnt pay much attention into diffs in this file except for the core logic. The eval code will be removed when we merge this PR anyway

Comment on lines +145 to +148
const Eigen::Vector<double, POS_STATE_SIZE> pos_state =
pos_kalman_filter.state_estimate;
const Eigen::Vector<double, ANG_STATE_SIZE> ang_state =
ang_kalman_filter.state_estimate;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No point creating a copy. Just alias by
` const auto& pos_state = pos_kalman_filter.state_estimate;

const Angle robot_orientation = Angle::fromRadians(ang_state(POS_W)).clamp();
const AngularVelocity robot_angular_velocity =
AngularVelocity::fromRadians(ang_state(VEL_W));
bool breakbeam_tripped = breakbeam_tripped_id == getRobotId();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this one out of every variable is not const?

@StarrryNight

Copy link
Copy Markdown
Contributor

Btw @sunghyuneun did all gameplay tests pass on ur machine. Cuz from my experience my ball filter caused some tests to failed and I had to tune+add heuristics

@StarrryNight StarrryNight left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left some more comments!

Comment on lines +214 to +217
acceleration_variance * delta_t_squared * delta_t_squared / 4.0;
const double correlation_noise =
acceleration_variance * delta_t_squared * delta_t / 2.0;
const double velocity_noise = acceleration_variance * delta_t_squared;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might as well make a delta_t_cubed and so

// entirely, letting it converge onto the robot within a few frames.
constexpr double RF_INITIAL_POSITION_UNCERTAINTY_M = 1.0;
constexpr double RF_INITIAL_VELOCITY_UNCERTAINTY_M_PER_S = 6.5;
constexpr double RF_INITIAL_ORIENTATION_UNCERTAINTY_RAD = M_PI * M_PI / 3;

@StarrryNight StarrryNight Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this $/pi^2/3$? And later you are squaring it again so we have a pi^4 in the initial covariance?

Comment on lines +146 to +147
std::optional<PosMeasurement> prev_pos_measurement;
std::optional<AngMeasurement> prev_ang_measurement;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never used

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now it is. but i deleted the ball one.

prev_pos_measurement = pos_measurement;
prev_ang_measurement = ang_measurement;
prev_detection_timestamp = current_time;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we only applying the gatings to position but not orientation?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the topic of that, I believe the code will be a lot cleaner if the two kfs are merged into one. In addition, you can just pass one state into a helper function that checks both ang and pos mahalanobis and within max speed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that the angle (as it is gated between -pi and pi) could like have moment where it jumps?

Comment on lines +107 to +109
else if (isWithinMaxRobotSpeed(best_robot_detection->position, current_time) &&
pos_kalman_filter.mahalanobisDistance(pos_measurement) <
RF_MAHALANOBIS_GATE_THRESHOLD)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition, this check happens AFTER we have run predict over dt. This means we are comparing whether measurement at $t$ is possible given the prediction of $t$, which doesn't make sense. We should be comparing measurement at $t$ verses state at $t-1$ instead

consecutive_outliers(0),
expiry_buffer_duration(expiry_buffer_duration)
{
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have two very similar constructors. Do we need ever actually need to initialize with a robot detection at all? Can we keep only one of them?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We initialize with robot detection in the team filter. If we were to remove one, it'd be initializing with the Robot.


// If the current_time called is ahead of current time, then runs predict step
// Updates the last_predict_timestamp to the current time called.
// Therefore, we must keey estimating with further points ahead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo, and im not quite understand what this comment mean...

const double velocity_noise = acceleration_variance * delta_t_squared;

// For angle kalman, we compute the angle process covariance with Continuous White
// Noise Acceleration model. i don't know anymore i just pray it works.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bro we are not merging this comment. The reason why we use a continous white noise acceleration model instead of a continuus white noise velocity model is because our process model assumes constant velocity (no acceleration). So the noise model should depict the unmodelled noise in acceleration.

constexpr double RF_ANG_ACCELERATION_NOISE_RAD_PER_S_SQUARED = 10.0;

// Maximum Mahalanobi's Distance before rejecting as outlier
constexpr double RF_MAHALANOBIS_GATE_THRESHOLD = 5;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this will be a value need to be tuned as well

@sunghyuneun

sunghyuneun commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Btw @sunghyuneun did all gameplay tests pass on ur machine. Cuz from my experience my ball filter caused some tests to failed and I had to tune+add heuristics

I was hoping for a little help? I was a little unsure about gameplay, I was thinking about talking to a pod or software lead tmr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate Robot Filters RobotFilter does not remove expired robots if there are no new detections

5 participants