Skip to content

Commit d2c88af

Browse files
committed
Merge pull request cra-ros-pkg#279 from cra-ros-pkg/state-queue-fix
Fixing filter history and measurement queue behavior
2 parents 3aac24c + fb95df3 commit d2c88af

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

src/ros_filter.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ namespace RobotLocalization
438438
// Invariant still holds: measurementHistoryDeque_.back().time_ < measurementQueue_.top().time_
439439
measurementHistory_.push_back(measurement);
440440

441-
// We should only save the filter state one per unique timstamp
441+
// We should only save the filter state once per unique timstamp
442442
if (measurementQueue_.empty() ||
443443
::fabs(measurementQueue_.top()->time_ - filter_.getLastMeasurementTime()) > 1e-9)
444444
{
@@ -2580,7 +2580,8 @@ namespace RobotLocalization
25802580
state->estimateErrorCovariance_ = Eigen::MatrixXd(filter.getEstimateErrorCovariance());
25812581
state->lastMeasurementTime_ = filter.getLastMeasurementTime();
25822582
filterStateHistory_.push_back(state);
2583-
RF_DEBUG("Saved state with timestamp " << state->lastMeasurementTime_ << " to history. " << filterStateHistory_.size() << " measurements are in the queue.\n");
2583+
RF_DEBUG("Saved state with timestamp " << std::setprecision(20) << state->lastMeasurementTime_ <<
2584+
" to history. " << filterStateHistory_.size() << " measurements are in the queue.\n");
25842585
}
25852586

25862587
template<typename T>
@@ -2589,39 +2590,36 @@ namespace RobotLocalization
25892590
RF_DEBUG("\n----- RosFilter::revertTo -----\n");
25902591
RF_DEBUG("\nRequested time was " << std::setprecision(20) << time << "\n")
25912592

2592-
// Walk back through the queue until we reach a filter state whose time stamp
2593-
// is less than or equal to the requested time
2594-
FilterStateHistoryDeque::reverse_iterator state_it = filterStateHistory_.rbegin();
2595-
2596-
while(state_it != filterStateHistory_.rend() && (*state_it)->lastMeasurementTime_ > time)
2593+
// Walk back through the queue until we reach a filter state whose time stamp is less than or equal to the requested time.
2594+
// Since every saved state after that time will be overwritten/corrected, we can pop from the queue.
2595+
while (!filterStateHistory_.empty() && filterStateHistory_.back()->lastMeasurementTime_ > time)
25972596
{
2598-
state_it++;
2597+
filterStateHistory_.pop_back();
25992598
}
26002599

26012600
// The state and measurement histories are stored at the same time, so if we have insufficient state history, we
26022601
// will also have insufficient measurement history.
2603-
if (state_it == filterStateHistory_.rend())
2602+
if (filterStateHistory_.empty())
26042603
{
26052604
RF_DEBUG("Insufficient history to revert to time " << time << "\n");
26062605

26072606
return false;
26082607
}
26092608

26102609
// Reset filter to the latest state from the queue.
2611-
filter_.setState((*state_it)->state_);
2612-
filter_.setEstimateErrorCovariance((*state_it)->estimateErrorCovariance_);
2613-
filter_.setLastMeasurementTime((*state_it)->lastMeasurementTime_);
2610+
const FilterStatePtr &state = filterStateHistory_.back();
2611+
filter_.setState(state->state_);
2612+
filter_.setEstimateErrorCovariance(state->estimateErrorCovariance_);
2613+
filter_.setLastMeasurementTime(state->lastMeasurementTime_);
26142614

2615-
RF_DEBUG("Reverted to state with time " << (*state_it)->lastMeasurementTime_ << "\n");
2615+
RF_DEBUG("Reverted to state with time " << state->lastMeasurementTime_ << "\n");
26162616

26172617
// Repeat for measurements, but push every measurement onto the measurement queue as we go
2618-
MeasurementHistoryDeque::reverse_iterator meas_it = measurementHistory_.rbegin();
26192618
int restored_measurements = 0;
2620-
2621-
while (meas_it != measurementHistory_.rend() && (*meas_it)->time_ > time)
2619+
while (!measurementHistory_.empty() && measurementHistory_.back()->time_ > time)
26222620
{
2623-
measurementQueue_.push(*meas_it);
2624-
meas_it++;
2621+
measurementQueue_.push(measurementHistory_.back());
2622+
measurementHistory_.pop_back();
26252623
restored_measurements++;
26262624
}
26272625

0 commit comments

Comments
 (0)