RTC Toolkit 4.0.2
Loading...
Searching...
No Matches
readerHelpers.hpp
Go to the documentation of this file.
1
13#ifndef RTCTK_DATATASK_READERHELPERS_HPP
14#define RTCTK_DATATASK_READERHELPERS_HPP
15
17
18#include <chrono>
19#include <cmath>
20#include <system_error>
21
22namespace rtctk::dataTask {
23
24namespace detail {
25
35[[deprecated]] inline std::chrono::milliseconds
36CalcTimeout(size_t count, float loop_frequency, float error_margin) {
37 using namespace std::chrono;
38 return milliseconds{static_cast<int>(ceil(1000.0 * count / loop_frequency) * error_margin)};
39}
40
54template <typename ReaderType, typename Operation>
55[[deprecated]] std::error_code
56Read(ReaderType& reader, Operation&& op, size_t count, float loop_frequency, float error_margin) {
57 using namespace std::chrono;
58
59 auto& logger = rtctk::componentFramework::GetLogger("rtctk");
60
61 auto timeout_short = CalcTimeout(1, loop_frequency, error_margin);
62 // cppcheck-suppress unreadVariable
63 auto timeout_total = CalcTimeout(count, loop_frequency, error_margin);
64
65 // cppcheck-suppress unreadVariable
66 size_t read = 0;
67 std::error_code const ok;
68 std::pair<std::error_code, size_t> ret;
69 milliseconds time_elapsed{0};
70 // cppcheck-suppress unreadVariable
71 auto time_start = steady_clock::now();
72
73 while (1) {
74 ret = reader.Read(std::forward<Operation>(op), count - read, timeout_short);
75 if (ret.first != ok) {
76 LOG4CPLUS_ERROR(logger, "!Reading from shm timed out: check if queue is being filled");
77 LOG4CPLUS_ERROR(logger,
78 "Read: " << ret.second << " in " << timeout_short.count() << " ms");
79 LOG4CPLUS_ERROR(logger, "Expected: " << count);
80 return ret.first;
81 }
82 read += ret.second;
83 if (read == count) {
84 return {};
85 }
86
87 time_elapsed = duration_cast<milliseconds>(steady_clock::now() - time_start);
88 if (time_elapsed > timeout_total) {
89 LOG4CPLUS_ERROR(logger, "Reading from shm timed out: check if queue is being filled");
90 LOG4CPLUS_ERROR(logger, "Read: " << read << " in " << time_elapsed.count() << " ms");
91 LOG4CPLUS_ERROR(logger, "Expected: " << read);
92 return std::make_error_code(std::errc::timed_out);
93 }
94 }
95}
96
108template <typename ReaderType>
109[[deprecated]] std::error_code
110Skip(ReaderType& reader, size_t count, float loop_frequency, float error_margin) {
111 using namespace std::chrono;
112
113 auto& logger = rtctk::componentFramework::GetLogger("rtctk");
114
115 auto timeout_short = CalcTimeout(1, loop_frequency, error_margin);
116 // cppcheck-suppress unreadVariable
117 auto timeout_total = CalcTimeout(count, loop_frequency, error_margin);
118
119 // cppcheck-suppress unreadVariable
120 size_t skipped = 0;
121 std::error_code const ok;
122 std::pair<std::error_code, size_t> ret;
123 milliseconds time_elapsed{0};
124 // cppcheck-suppress unreadVariable
125 auto time_start = steady_clock::now();
126
127 while (1) {
128 ret = reader.Skip(count - skipped, timeout_short);
129 if (ret.first != ok) {
130 LOG4CPLUS_ERROR(logger, "!Skipping from shm timed out: check if queue is being filled");
131 LOG4CPLUS_ERROR(logger,
132 "Read: " << ret.second << " in " << timeout_short.count() << " ms");
133 LOG4CPLUS_ERROR(logger, "Expected: " << count);
134 return ret.first;
135 }
136 skipped += ret.second;
137 if (skipped == count) {
138 return {};
139 }
140
141 time_elapsed = duration_cast<milliseconds>(steady_clock::now() - time_start);
142 if (time_elapsed > timeout_total) {
143 LOG4CPLUS_ERROR(logger, "Skipping from shm timed out: check if queue is being filled");
144 LOG4CPLUS_ERROR(logger, "Read: " << skipped << " in " << time_elapsed.count() << " ms");
145 LOG4CPLUS_ERROR(logger, "Expected: " << skipped);
146 return std::make_error_code(std::errc::timed_out);
147 }
148 }
149}
150
159template <typename ReaderType>
160[[deprecated]] std::error_code Reset(ReaderType& reader) {
161 if (reader.Size() != 0) {
162 return reader.Reset();
163 }
164 return {};
165}
166
175template <typename ReaderType>
176[[deprecated]] size_t NumFree(ReaderType& reader) {
177 return reader.Size() - reader.NumAvailable();
178}
179
180} // namespace detail
181
182} // namespace rtctk::dataTask
183
184#endif // RTCTK_DATATASK_READERHELPERS_HPP
Logging Support Library based on log4cplus.
log4cplus::Logger & GetLogger(const std::string &name="app")
Get handle to a specific logger.
Definition: logger.cpp:180
std::chrono::milliseconds CalcTimeout(size_t count, float loop_frequency, float error_margin)
Helper function to calculate the estimated time to read the a number of samples at a given frequency.
Definition: readerHelpers.hpp:36
std::error_code Read(ReaderType &reader, Operation &&op, size_t count, float loop_frequency, float error_margin)
Helper function to wrap the ipcq.read with handling of timeouts and count values.
Definition: readerHelpers.hpp:56
std::error_code Skip(ReaderType &reader, size_t count, float loop_frequency, float error_margin)
Helper function to wrap the ipcq.skip with handling of timeouts and count values.
Definition: readerHelpers.hpp:110
std::error_code Reset(ReaderType &reader)
Helper function to reset the ipcq.reader to latest sample.
Definition: readerHelpers.hpp:160
size_t NumFree(ReaderType &reader)
Helper function to get the free space in the shm.
Definition: readerHelpers.hpp:176
Definition: computationBase.hpp:33