Skip to content

Commit ad66d82

Browse files
committed
Pass time elapsed to robot localizer
1 parent df6d06c commit ad66d82

6 files changed

Lines changed: 29 additions & 57 deletions

File tree

src/software/embedded/primitive_executor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ void PrimitiveExecutor::updatePrimitive(const TbotsProto::Primitive& primitive_m
5151

5252
trajectory_path_ = new_trajectory_path;
5353
position_controller_.reset();
54-
time_since_linear_trajectory_creation_s_ = 0.5;
54+
time_since_linear_trajectory_creation_s_ = VISION_TO_ROBOT_DELAY_S;
5555

5656
angular_trajectory_ = new_angular_trajectory;
5757
orientation_controller_.reset();
58-
time_since_angular_trajectory_creation_s_ = 0.5;
58+
time_since_angular_trajectory_creation_s_ = VISION_TO_ROBOT_DELAY_S;
5959
}
6060

6161
const auto update_end = std::chrono::steady_clock::now();
@@ -136,7 +136,7 @@ TbotsProto::DirectControlPrimitive PrimitiveExecutor::stepPrimitive(
136136
createAngularVelocity(robot_status.imu_status().angular_velocity())});
137137
}
138138

139-
robot_localizer_.step(Vector());
139+
robot_localizer_.step(Vector(), delta_time_s);
140140

141141
TbotsProto::PrimitiveExecutorStatus& prim_exec_status =
142142
*(robot_status.mutable_primitive_executor_status());

src/software/embedded/robot_localizer.cpp

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "robot_localizer.h"
22

3-
#include <chrono>
4-
53
RobotLocalizer::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

259230
void 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);

src/software/embedded/robot_localizer.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ class RobotLocalizer
6868
explicit RobotLocalizer(const RobotLocalizerConfig& config);
6969

7070
/**
71-
* Runs one prediction step using elapsed time since the previous call.
71+
* Runs one prediction step over the given elapsed time.
7272
*
7373
* @param linear_acceleration The current linear acceleration of the robot
74+
* @param delta_time_seconds The elapsed time since the previous step, in seconds
7475
*/
75-
void step(const Vector& linear_acceleration);
76+
void step(const Vector& linear_acceleration, double delta_time_seconds);
7677

7778
/**
7879
* Update the robot's position and orientation from data reported by vision.
@@ -162,7 +163,7 @@ class RobotLocalizer
162163
Eigen::Vector<double, STATE_SIZE> state_estimate;
163164
Eigen::Matrix<double, STATE_SIZE, STATE_SIZE> state_covariance;
164165

165-
std::chrono::time_point<std::chrono::steady_clock> time;
166+
double time_seconds;
166167
};
167168

168169
KalmanFilter<STATE_SIZE, MEASUREMENT_SIZE, CONTROL_SIZE> filter_;
@@ -171,8 +172,8 @@ class RobotLocalizer
171172
double process_linear_acceleration_noise_variance_;
172173
double process_angular_acceleration_noise_variance_;
173174

174-
std::chrono::time_point<std::chrono::steady_clock> last_step_time_;
175-
176175
// History is ordered newest-first (front is the most recent step)
177176
std::deque<FilterStep> history;
177+
178+
double current_time_seconds_ = 0.0;
178179
};

src/software/embedded/robot_localizer_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ RobotLocalizer runConstantVelocity(bool feed_vision, double vision_age = RTT_S /
4747
localToGlobalVelocity(local_velocity, localizer.getOrientation()),
4848
AngularVelocity::zero()});
4949

50-
localizer.step(Vector(0.0, 0.0));
50+
localizer.step(Vector(0.0, 0.0), DT);
5151

5252
// Periodic vision fix (~60 Hz). Feed the position from RTT_S/2 ago, consistent
5353
// with the reported age.

src/software/simulation/er_force_simulator.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -478,13 +478,13 @@ void ErForceSimulator::stepSimulation(const Duration& time_step)
478478
{
479479
current_time = current_time + time_step;
480480

481-
SSLSimulationProto::RobotControl yellow_robot_control = updateSimulatorRobots(
482-
yellow_primitive_executor_map, *yellow_team_world_msg, time_step,
483-
gameController::Team::YELLOW);
481+
SSLSimulationProto::RobotControl yellow_robot_control =
482+
updateSimulatorRobots(yellow_primitive_executor_map, *yellow_team_world_msg,
483+
time_step, gameController::Team::YELLOW);
484484

485-
SSLSimulationProto::RobotControl blue_robot_control = updateSimulatorRobots(
486-
blue_primitive_executor_map, *blue_team_world_msg, time_step,
487-
gameController::Team::BLUE);
485+
SSLSimulationProto::RobotControl blue_robot_control =
486+
updateSimulatorRobots(blue_primitive_executor_map, *blue_team_world_msg,
487+
time_step, gameController::Team::BLUE);
488488

489489
auto yellow_radio_responses =
490490
er_force_sim->acceptYellowRobotControlCommand(yellow_robot_control);

src/software/simulation/er_force_simulator.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class ErForceSimulator
2828
* @param robot_constants The robot constants
2929
* @param realism_config realism configuration
3030
*/
31-
explicit ErForceSimulator(
32-
const TbotsProto::FieldType& field_type,
33-
const robot_constants::RobotConstants& robot_constants,
34-
std::unique_ptr<RealismConfigErForce>& realism_config, const bool ramping = true);
31+
explicit ErForceSimulator(const TbotsProto::FieldType& field_type,
32+
const robot_constants::RobotConstants& robot_constants,
33+
std::unique_ptr<RealismConfigErForce>& realism_config,
34+
const bool ramping = true);
3535
ErForceSimulator() = delete;
3636
~ErForceSimulator() = default;
3737

0 commit comments

Comments
 (0)