ros2_control - rolling
Loading...
Searching...
No Matches
ros2_control::TimeWindowStatistics Class Reference

#include <statistics_types.hpp>

Public Types

using StatisticData = ros2_control::StatisticData

Public Member Functions

 TimeWindowStatistics ()=default
 Construct a disabled collector.
 TimeWindowStatistics (double window_duration)
void set_window_duration (double window_duration)
double get_window_duration () const
bool is_enabled () const
void reset ()
 Clear the window, keeping its duration.
void add_measurement (const double item, const double time_seconds)
StatisticData get_statistics () const

Static Public Attributes

static constexpr std::size_t NUM_BUCKETS = 10
 Number of time buckets the window is split into.

Detailed Description

Statistics (average, minimum, maximum, standard deviation (population) and sample count) over the samples observed within the most recent window_duration seconds. Unlike MovingAverageStatistics, an old outlier leaves the statistics once the window has moved past it, so they describe the recent behaviour rather than the whole history.

The window is split into NUM_BUCKETS time buckets, each holding the running statistics of its samples, so the memory is constant and independent of the sample rate, and add_measurement() is O(1) and never allocates. The price is the resolution of the window's trailing edge: it advances one bucket at a time, so the statistics cover between (NUM_BUCKETS - 1) / NUM_BUCKETS of the duration and the full duration.

The time of a sample is given by the caller, so the window follows whichever clock drives the measurements (e.g. simulation time). If the time goes back past the current bucket (e.g. a reset simulation), the window is restarted.

The window ends at the latest sample, not at the current time: if the samples stop arriving (e.g. a stalled asynchronous component), get_statistics() keeps returning the last window until a new sample comes in, like the since-startup statistics do.

A non-positive (or non-finite) duration disables the collector: add_measurement() is a no-op. When statistics are not available, e.g. no observations have been made, NaNs are returned.

Constructor & Destructor Documentation

◆ TimeWindowStatistics()

ros2_control::TimeWindowStatistics::TimeWindowStatistics ( double window_duration)
inlineexplicit
Parameters
window_durationDuration of the window in seconds. Non-positive disables the collector.

Member Function Documentation

◆ add_measurement()

void ros2_control::TimeWindowStatistics::add_measurement ( const double item,
const double time_seconds )
inline

Observe a sample. Samples older than the window are dropped. Note: samples whose value or time is NaN or infinity are discarded, as well as samples whose time divided by the bucket width (window_duration / NUM_BUCKETS) exceeds 1e18 in magnitude.

Parameters
itemThe item that was observed
time_secondsThe time at which the item was observed, in seconds

◆ get_statistics()

StatisticData ros2_control::TimeWindowStatistics::get_statistics ( ) const
inline

Return a StatisticData object, containing average, minimum, maximum, standard deviation (population) and sample count over the current window. For an empty window, the average, min, max and standard deviation are NaN.

◆ get_window_duration()

double ros2_control::TimeWindowStatistics::get_window_duration ( ) const
inline
Returns
The duration of the window in seconds, as set by set_window_duration().

◆ is_enabled()

bool ros2_control::TimeWindowStatistics::is_enabled ( ) const
inline
Returns
true if the window duration is positive, i.e. measurements are collected.

◆ set_window_duration()

void ros2_control::TimeWindowStatistics::set_window_duration ( double window_duration)
inline

Set the duration of the window and clear it.

Parameters
window_durationDuration of the window in seconds. Non-positive disables the collector.

The documentation for this class was generated from the following file: