The average over last errors, this class uses a std::deque to maintain a sliding window of previous errors:
|
last_errors.push_back(error); |
While this approach is semantically correct, it is highly inefficient, copying 50 values around in each call.
There are much more efficient ways to just a compute a sliding average:
- Use a circular buffer, maintain the average sum, and just subtract the popped and add the pushed value (all others values remain the same).
- Use estimated sliding average (which should be sufficient here):
avg_error = (1.0 - lambda) * avg_error + lambda * error
where lambda adjusts the smoothing and should be chosen lambda=1.0/nb_errors_for_avg here.
I strongly suggest the latter method and storing lambda once in the class (nb_errors_for_avg shouldn't change over time anyway!) As this requires API changes, I leave it to you to decide on this and implement it.
The average over last errors, this class uses a
std::dequeto maintain a sliding window of previous errors:sr_core/sr_utilities/include/sr_utilities/sr_deadband.hpp
Line 90 in 9d90e75
sr_core/sr_utilities/include/sr_utilities/sr_deadband.hpp
Line 136 in 9d90e75
While this approach is semantically correct, it is highly inefficient, copying 50 values around in each call.
There are much more efficient ways to just a compute a sliding average:
avg_error = (1.0 - lambda) * avg_error + lambda * errorwhere lambda adjusts the smoothing and should be chosen
lambda=1.0/nb_errors_for_avghere.I strongly suggest the latter method and storing lambda once in the class (
nb_errors_for_avgshouldn't change over time anyway!) As this requires API changes, I leave it to you to decide on this and implement it.