@@ -536,6 +536,8 @@ void RosFilter<T>::integrateMeasurements(const rclcpp::Time & current_time)
536536 " \n " <<
537537 measurement_queue_.size () << " measurements in queue.\n " );
538538
539+ bool predict_to_current_time = predict_to_current_time_;
540+
539541 // If we have any measurements in the queue, process them
540542 if (!measurement_queue_.empty ()) {
541543 // Check if the first measurement we're going to process is older than the
@@ -558,8 +560,9 @@ void RosFilter<T>::integrateMeasurements(const rclcpp::Time & current_time)
558560 if (!revertTo (first_measurement->time_ - rclcpp::Duration (1 ))) {
559561 RF_DEBUG (" ERROR: history interval is too small to revert to time " <<
560562 filter_utilities::toSec (first_measurement->time_ ) << " \n " );
561- // ROS_WARN_STREAM_THROTTLE(10.0, "Received old measurement for topic "
562- // << first_measurement->topic_name_ <<
563+ // ROS_WARN_STREAM_DELAYED_THROTTLE(history_length_,
564+ // "Received old measurement for topic "
565+ // << first_measurement->topic_name_ <<
563566 // ", but history interval is insufficiently
564567 // sized to " "revert state and measurement
565568 // queue.");
@@ -624,23 +627,30 @@ void RosFilter<T>::integrateMeasurements(const rclcpp::Time & current_time)
624627
625628 // If we get a large delta, then continuously predict until
626629 if (last_update_delta >= filter_.getSensorTimeout ()) {
630+ predict_to_current_time = true ;
631+
627632 RF_DEBUG (" Sensor timeout! Last measurement time was " <<
628633 filter_utilities::toSec (filter_.getLastMeasurementTime ()) <<
629634 " , current time is " << filter_utilities::toSec (current_time) <<
630635 " , delta is " << filter_utilities::toSec (last_update_delta) <<
631636 " \n " );
632-
633- filter_.validateDelta (last_update_delta);
634- filter_.predict (current_time, last_update_delta);
635-
636- // Update the last measurement time and last update time
637- filter_.setLastMeasurementTime (filter_.getLastMeasurementTime () +
638- last_update_delta);
639637 }
640638 } else {
641639 RF_DEBUG (" Filter not yet initialized.\n " );
642640 }
643641
642+ if (filter_.getInitializedStatus () && predict_to_current_time) {
643+ rclcpp::Duration last_update_delta =
644+ current_time - filter_.getLastMeasurementTime ();
645+
646+ filter_.validateDelta (last_update_delta);
647+ filter_.predict (current_time, last_update_delta);
648+
649+ // Update the last measurement time and last update time
650+ filter_.setLastMeasurementTime (filter_.getLastMeasurementTime () +
651+ last_update_delta);
652+ }
653+
644654 RF_DEBUG (" \n ----- /RosFilter<T>::integrateMeasurements ------\n " );
645655}
646656
@@ -1404,7 +1414,7 @@ void RosFilter<T>::loadParams()
14041414 differential, relative, pose_mahalanobis_thresh);
14051415 const CallbackData twist_callback_data (
14061416 imu_topic_name + " _twist" , twist_update_vec, twist_update_sum,
1407- differential, relative, pose_mahalanobis_thresh );
1417+ differential, relative, twist_mahalanobis_thresh );
14081418 const CallbackData accel_callback_data (
14091419 imu_topic_name + " _acceleration" , accel_update_vec, accelUpdateSum,
14101420 differential, relative, accel_mahalanobis_thresh);
@@ -1919,9 +1929,12 @@ void RosFilter<T>::periodicUpdate()
19191929 clearExpiredHistory (filter_.getLastMeasurementTime () - history_length_);
19201930 }
19211931
1922- if ((this ->now () - cur_time).seconds () > 1 . / frequency_) {
1932+ // Warn the user if the update took too long
1933+ const double loop_elapsed = (this ->now () - cur_time).seconds ();
1934+ if (loop_elapsed > 1 . / frequency_) {
19231935 std::cerr <<
1924- " Failed to meet update rate! Try decreasing the rate, limiting "
1936+ " Failed to meet update rate! Took " << std::setprecision (20 ) <<
1937+ loop_elapsed << " seconds. Try decreasing the rate, limiting "
19251938 " sensor output frequency, or limiting the number of sensors.\n " ;
19261939 }
19271940}
@@ -2973,31 +2986,45 @@ bool RosFilter<T>::revertTo(const rclcpp::Time & time)
29732986
29742987 // Walk back through the queue until we reach a filter state whose time stamp
29752988 // is less than or equal to the requested time. Since every saved state after
2976- // that time will be overwritten/corrected, we can pop from the queue.
2989+ // that time will be overwritten/corrected, we can pop from the queue. If the
2990+ // history is insufficiently short, we just take the oldest state we have.
2991+ FilterStatePtr last_history_state;
29772992 while (!filter_state_history_.empty () &&
29782993 filter_state_history_.back ()->last_measurement_time_ > time)
29792994 {
2995+ last_history_state = filter_state_history_.back ();
29802996 filter_state_history_.pop_back ();
29812997 }
29822998
2983- // The state and measurement histories are stored at the same time, so if we
2984- // have insufficient state history, we will also have insufficient measurement
2985- // history.
2986- if (filter_state_history_.empty ()) {
2999+ // If the state history is not empty at this point, it means that our history
3000+ // was large enough, and we should revert to the state at the back of the
3001+ // history deque.
3002+ bool ret_val = false ;
3003+ if (!filter_state_history_.empty ()) {
3004+ ret_val = true ;
3005+ last_history_state = filter_state_history_.back ();
3006+ } else {
29873007 RF_DEBUG (" Insufficient history to revert to time " <<
29883008 filter_utilities::toSec (time) << " \n " );
29893009
2990- return false ;
3010+ if (last_history_state.get () != NULL ) {
3011+ RF_DEBUG (" Will revert to oldest state at " <<
3012+ filter_utilities::toSec (last_history_state->latest_control_time_ ) <<
3013+ " .\n " );
3014+ }
29913015 }
29923016
2993- // Reset filter to the latest state from the queue.
2994- const FilterStatePtr & state = filter_state_history_.back ();
2995- filter_.setState (state->state_ );
2996- filter_.setEstimateErrorCovariance (state->estimate_error_covariance_ );
2997- filter_.setLastMeasurementTime (state->last_measurement_time_ );
3017+ // If we have a valid reversion state, revert
3018+ if (last_history_state.get () != NULL ) {
3019+ // Reset filter to the latest state from the queue.
3020+ const FilterStatePtr & state = filter_state_history_.back ();
3021+ filter_.setState (state->state_ );
3022+ filter_.setEstimateErrorCovariance (state->estimate_error_covariance_ );
3023+ filter_.setLastMeasurementTime (state->last_measurement_time_ );
29983024
2999- RF_DEBUG (" Reverted to state with time " <<
3000- filter_utilities::toSec (state->last_measurement_time_ ) << " \n " );
3025+ RF_DEBUG (" Reverted to state with time " <<
3026+ filter_utilities::toSec (state->last_measurement_time_ ) << " \n " );
3027+ }
30013028
30023029 // Repeat for measurements, but push every measurement onto the measurement
30033030 // queue as we go
@@ -3014,7 +3041,7 @@ bool RosFilter<T>::revertTo(const rclcpp::Time & time)
30143041
30153042 RF_DEBUG (" \n ----- /RosFilter<T>::revertTo\n " );
30163043
3017- return true ;
3044+ return ret_val ;
30183045}
30193046
30203047template <typename T>
0 commit comments