diff --git a/selfdrive/locationd/locationd.cc b/selfdrive/locationd/locationd.cc index 0e742cb6e9..44812c7788 100755 --- a/selfdrive/locationd/locationd.cc +++ b/selfdrive/locationd/locationd.cc @@ -181,7 +181,14 @@ void Localizer::handle_sensors(double current_time, const capnp::List 0.1) { + LOGE("Sensor reading ignored, sensor timestamp more than 100ms off from log time"); + return; + } + + // TODO: handle messages from two IMUs at the same time if (sensor_reading.getSource() == cereal::SensorEventData::SensorSource::LSM6DS3) { continue; } @@ -337,6 +344,15 @@ void Localizer::finite_check(double current_time) { } } +void Localizer::time_check(double current_time) { + double filter_time = this->kf->get_filter_time(); + bool big_time_gap = !isnan(filter_time) && (current_time - filter_time > 10); + if (big_time_gap){ + LOGE("Time gap of over 10s detected, kalman reset"); + this->reset_kalman(current_time); + } +} + void Localizer::reset_kalman(double current_time, VectorXd init_orient, VectorXd init_pos) { // too nonlinear to init on completely wrong VectorXd init_x = this->kf->get_initial_x(); @@ -358,6 +374,7 @@ void Localizer::handle_msg_bytes(const char *data, const size_t size) { void Localizer::handle_msg(const cereal::Event::Reader& log) { double t = log.getLogMonoTime() * 1e-9; + this->time_check(t); if (log.isSensorEvents()) { this->handle_sensors(t, log.getSensorEvents()); } else if (log.isGpsLocationExternal()) { diff --git a/selfdrive/locationd/locationd.h b/selfdrive/locationd/locationd.h index f50056dc6a..58030cd921 100755 --- a/selfdrive/locationd/locationd.h +++ b/selfdrive/locationd/locationd.h @@ -12,6 +12,7 @@ #include "selfdrive/common/swaglog.h" #include "selfdrive/common/timing.h" #include "selfdrive/common/util.h" +#include #include "selfdrive/sensord/sensors/constants.h" #define VISION_DECIMATION 2 @@ -29,6 +30,7 @@ public: void reset_kalman(double current_time = NAN); void reset_kalman(double current_time, Eigen::VectorXd init_orient, Eigen::VectorXd init_pos); void finite_check(double current_time = NAN); + void time_check(double current_time = NAN); kj::ArrayPtr get_message_bytes(MessageBuilder& msg_builder, uint64_t logMonoTime, bool inputsOK, bool sensorsOK, bool gpsOK); diff --git a/selfdrive/locationd/models/live_kf.cc b/selfdrive/locationd/models/live_kf.cc index 6661b06fad..00080ab839 100755 --- a/selfdrive/locationd/models/live_kf.cc +++ b/selfdrive/locationd/models/live_kf.cc @@ -66,6 +66,10 @@ MatrixXdr LiveKalman::get_P() { return this->filter->covs(); } +double LiveKalman::get_filter_time() { + return this->filter->get_filter_time(); +} + std::vector LiveKalman::get_R(int kind, int n) { std::vector R; for (int i = 0; i < n; i++) { diff --git a/selfdrive/locationd/models/live_kf.h b/selfdrive/locationd/models/live_kf.h index c4411bbcbd..4cd4756c9f 100755 --- a/selfdrive/locationd/models/live_kf.h +++ b/selfdrive/locationd/models/live_kf.h @@ -29,6 +29,7 @@ public: Eigen::VectorXd get_x(); MatrixXdr get_P(); + double get_filter_time(); std::vector get_R(int kind, int n); std::optional predict_and_observe(double t, int kind, std::vector meas, std::vector R = {}); diff --git a/selfdrive/locationd/test/test_locationd.py b/selfdrive/locationd/test/test_locationd.py index 27852c9bf6..5194ce4441 100755 --- a/selfdrive/locationd/test/test_locationd.py +++ b/selfdrive/locationd/test/test_locationd.py @@ -54,6 +54,7 @@ void localizer_handle_msg_bytes(Localizer_t localizer, const char *data, size_t def test_device_fell(self): msg = messaging.new_message('sensorEvents', 1) msg.sensorEvents[0].sensor = 1 + msg.sensorEvents[0].timestamp = msg.logMonoTime msg.sensorEvents[0].type = 1 msg.sensorEvents[0].init('acceleration') msg.sensorEvents[0].acceleration.v = [10.0, 0.0, 0.0] # zero with gravity @@ -64,6 +65,7 @@ void localizer_handle_msg_bytes(Localizer_t localizer, const char *data, size_t msg = messaging.new_message('sensorEvents', 1) msg.sensorEvents[0].sensor = 1 + msg.sensorEvents[0].timestamp = msg.logMonoTime msg.sensorEvents[0].type = 1 msg.sensorEvents[0].init('acceleration') msg.sensorEvents[0].acceleration.v = [50.1, 0.0, 0.0] # more than 40 m/s**2