11#include " robot_localizer.h"
22
3- #include < chrono>
4-
53RobotLocalizer::RobotLocalizer (const RobotLocalizerConfig& config)
64 : process_linear_acceleration_noise_variance_(config.process_noise_variance),
75 process_angular_acceleration_noise_variance_(config.process_noise_variance)
@@ -16,24 +14,21 @@ RobotLocalizer::RobotLocalizer(const RobotLocalizerConfig& config)
1614 config.motor_sensor_noise_variance , config.motor_sensor_noise_variance ,
1715 ImuService::IMU_VARIANCE )
1816 .asDiagonal ();
19-
20- last_step_time_ = std::chrono::steady_clock::now ();
2117}
2218
23- void RobotLocalizer::step (const Vector& linear_acceleration)
19+ void RobotLocalizer::step (const Vector& linear_acceleration,
20+ const double delta_time_seconds)
2421{
22+ current_time_seconds_ += delta_time_seconds;
23+
2524 FilterStep step{
2625 .prediction = std::make_optional<FilterStep::Predict>(),
2726 .update = std::nullopt ,
2827 .state_estimate = filter_.state_estimate ,
2928 .state_covariance = filter_.state_covariance ,
30- .time = std::chrono::steady_clock::now () ,
29+ .time_seconds = current_time_seconds_ ,
3130 };
3231
33- const std::chrono::duration<double > delta_time = step.time - last_step_time_;
34- const double delta_time_seconds = delta_time.count ();
35- last_step_time_ = step.time ;
36-
3732 // clang-format off
3833 step.prediction ->process_model <<
3934 1 , 0 , 0 , delta_time_seconds, 0 , 0 ,
@@ -136,12 +131,10 @@ void RobotLocalizer::update(const VisionData& data)
136131 return ;
137132 }
138133
139- const auto current_time = std::chrono::steady_clock::now ();
140- const auto sample_age = std::chrono::duration<double >(data.age_seconds );
141-
142134 auto rollback_point = std::find_if (
143135 history.begin (), history.end (),
144- [&](const FilterStep& step) { return (current_time - step.time ) >= sample_age; });
136+ [&](const FilterStep& step)
137+ { return (current_time_seconds_ - step.time_seconds ) >= data.age_seconds ; });
145138
146139 if (rollback_point == history.begin ())
147140 {
@@ -231,29 +224,7 @@ void RobotLocalizer::updateFilterWithVision(const Point& position,
231224 static_cast <Eigen::Index>(MeasurementIndex::VISION_ORIENTATION ),
232225 static_cast <Eigen::Index>(StateIndex::ORIENTATION )) = 1 ;
233226
234- // Vision observes pose (position + orientation), not velocity. The velocity states
235- // are observed directly by the wheel encoders (and IMU for angular velocity), which
236- // are accurate. Letting the vision position/orientation correction also adjust the
237- // velocity states through the position<->velocity covariance coupling is unstable
238- // here: the tight vision noise leaves the position correction lagging, and that
239- // persistent lag is fed into the velocity estimate every frame, inflating it (a
240- // robot moving 1 m/s was estimated at ~1.8 m/s, and worse without delay
241- // compensation). So we hold the velocity states fixed across the vision update.
242- const double x_velocity =
243- filter_.state_estimate (static_cast <Eigen::Index>(StateIndex::X_VELOCITY ));
244- const double y_velocity =
245- filter_.state_estimate (static_cast <Eigen::Index>(StateIndex::Y_VELOCITY ));
246- const double angular_velocity =
247- filter_.state_estimate (static_cast <Eigen::Index>(StateIndex::ANGULAR_VELOCITY ));
248-
249227 filter_.update (measurement);
250-
251- filter_.state_estimate (static_cast <Eigen::Index>(StateIndex::X_VELOCITY )) =
252- x_velocity;
253- filter_.state_estimate (static_cast <Eigen::Index>(StateIndex::Y_VELOCITY )) =
254- y_velocity;
255- filter_.state_estimate (static_cast <Eigen::Index>(StateIndex::ANGULAR_VELOCITY )) =
256- angular_velocity;
257228}
258229
259230void RobotLocalizer::update (const MotorData& data)
@@ -286,7 +257,7 @@ void RobotLocalizer::update(const MotorData& data)
286257 .update = update,
287258 .state_estimate = filter_.state_estimate ,
288259 .state_covariance = filter_.state_covariance ,
289- .time = std::chrono::steady_clock::now () ,
260+ .time_seconds = current_time_seconds_ ,
290261 };
291262
292263 history.push_front (step);
@@ -313,7 +284,7 @@ void RobotLocalizer::update(const ImuData& data)
313284 .update = update,
314285 .state_estimate = filter_.state_estimate ,
315286 .state_covariance = filter_.state_covariance ,
316- .time = std::chrono::steady_clock::now () ,
287+ .time_seconds = current_time_seconds_ ,
317288 };
318289
319290 history.push_front (step);
0 commit comments