Skip to content

Commit 6450a73

Browse files
committed
Add FilteredInterpArray
1 parent 1085b5f commit 6450a73

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

util/filtered_interp_array.hh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#pragma once
2+
#include "util/interp_param.hh"
3+
#include <array>
4+
#include <atomic>
5+
#include <cstddef>
6+
7+
template<size_t N, typename Filter, typename T = float>
8+
struct FilteredInterpArray {
9+
10+
void mark_new_data_ready() {
11+
_new_data_ready = true;
12+
}
13+
14+
// If new data is ready, filter each raw reading
15+
// and set it as the new interpolation target.
16+
// read_fn: (unsigned i) -> T
17+
void add_new_readings(auto read_fn) {
18+
if (_new_data_ready) {
19+
_new_data_ready = false;
20+
21+
for (unsigned i = 0; i < N; i++) {
22+
_interps[i].set_new_value(_filters[i].add_val(read_fn(i)));
23+
if (_update_count > 0)
24+
_interps[i].set_num_updates(_update_count);
25+
}
26+
_update_count = 0;
27+
}
28+
}
29+
30+
// Advance interpolation and write output values.
31+
// output_fn: (unsigned i, T val) -> void
32+
void get_interp_values(auto output_fn) {
33+
_update_count++;
34+
35+
bool did_overshoot = (_update_count >= _interps[0].get_num_updates());
36+
37+
if (did_overshoot) {
38+
for (unsigned i = 0; i < N; i++) {
39+
_interps[i].cur_val = _interps[i].target_val;
40+
output_fn(i, _interps[i].target_val);
41+
}
42+
} else {
43+
for (unsigned i = 0; i < N; i++)
44+
output_fn(i, _interps[i].next());
45+
}
46+
}
47+
48+
void set_num_updates(unsigned n) {
49+
for (auto &interp : _interps)
50+
interp.set_num_updates(n);
51+
}
52+
53+
static constexpr size_t size() {
54+
return N;
55+
}
56+
57+
private:
58+
std::array<InterpParamVariable<T>, N> _interps{};
59+
std::array<Filter, N> _filters{};
60+
std::atomic<bool> _new_data_ready{false};
61+
unsigned _update_count{0};
62+
};

0 commit comments

Comments
 (0)