ros2_control - jazzy
Loading...
Searching...
No Matches
statistics_types.hpp
1// Copyright 2025 PAL Robotics S.L.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
17#ifndef HARDWARE_INTERFACE__TYPES__STATISTICS_TYPES_HPP_
18#define HARDWARE_INTERFACE__TYPES__STATISTICS_TYPES_HPP_
19
20#include <limits>
21#include <memory>
22
23#include "libstatistics_collector/moving_average_statistics/moving_average.hpp"
24#include "libstatistics_collector/moving_average_statistics/types.hpp"
25#if !defined(_WIN32) && !defined(__APPLE__)
26#include "realtime_tools/mutex.hpp"
27#define DEFAULT_MUTEX realtime_tools::prio_inherit_mutex
28#else
29#define DEFAULT_MUTEX std::mutex
30#endif
31
32namespace ros2_control
33{
39{
40 using MovingAverageStatisticsCollector =
41 libstatistics_collector::moving_average_statistics::MovingAverageStatistics;
42 using StatisticData = libstatistics_collector::moving_average_statistics::StatisticData;
43
44public:
46 {
47 reset();
48 reset_statistics_sample_count_ = std::numeric_limits<unsigned int>::max();
49 }
50
55 void update_statistics(const std::shared_ptr<MovingAverageStatisticsCollector> & statistics)
56 {
57 std::unique_lock<DEFAULT_MUTEX> lock(mutex_);
58 if (statistics->GetCount() > 0)
59 {
60 statistics_data.average = statistics->Average();
61 statistics_data.min = statistics->Min();
62 statistics_data.max = statistics->Max();
63 statistics_data.standard_deviation = statistics->StandardDeviation();
64 statistics_data.sample_count = statistics->GetCount();
65 statistics_data = statistics->GetStatistics();
66 }
67 if (statistics->GetCount() >= reset_statistics_sample_count_)
68 {
69 statistics->Reset();
70 }
71 }
72
77 void set_reset_statistics_sample_count(unsigned int reset_sample_count)
78 {
79 std::unique_lock<DEFAULT_MUTEX> lock(mutex_);
80 reset_statistics_sample_count_ = reset_sample_count;
81 }
82
83 void reset()
84 {
85 statistics_data.average = std::numeric_limits<double>::quiet_NaN();
86 statistics_data.min = std::numeric_limits<double>::quiet_NaN();
87 statistics_data.max = std::numeric_limits<double>::quiet_NaN();
88 statistics_data.standard_deviation = std::numeric_limits<double>::quiet_NaN();
89 statistics_data.sample_count = 0;
90 }
91
96 const StatisticData & get_statistics() const
97 {
98 std::unique_lock<DEFAULT_MUTEX> lock(mutex_);
99 return statistics_data;
100 }
101
102private:
104 StatisticData statistics_data;
106 unsigned int reset_statistics_sample_count_ = std::numeric_limits<unsigned int>::max();
108 mutable DEFAULT_MUTEX mutex_;
109};
110} // namespace ros2_control
111
112namespace hardware_interface
113{
118struct HardwareComponentStatisticsCollector
119{
120 HardwareComponentStatisticsCollector()
121 {
122 execution_time = std::make_shared<MovingAverageStatisticsCollector>();
123 periodicity = std::make_shared<MovingAverageStatisticsCollector>();
124 }
125
126 using MovingAverageStatisticsCollector =
127 libstatistics_collector::moving_average_statistics::MovingAverageStatistics;
128
133 void reset_statistics()
134 {
135 execution_time->Reset();
136 periodicity->Reset();
137 }
138
140 std::shared_ptr<MovingAverageStatisticsCollector> execution_time = nullptr;
142 std::shared_ptr<MovingAverageStatisticsCollector> periodicity = nullptr;
143};
144} // namespace hardware_interface
145
146#endif // HARDWARE_INTERFACE__TYPES__STATISTICS_TYPES_HPP_
Data structure to store the statistics of a moving average. The data is protected by a mutex and the ...
Definition statistics_types.hpp:39
const StatisticData & get_statistics() const
Get the statistics data.
Definition statistics_types.hpp:96
void set_reset_statistics_sample_count(unsigned int reset_sample_count)
Set the number of samples to reset the statistics.
Definition statistics_types.hpp:77
void update_statistics(const std::shared_ptr< MovingAverageStatisticsCollector > &statistics)
Update the statistics data with the new statistics data.
Definition statistics_types.hpp:55
Definition actuator.hpp:22