STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_pt_smoothing.cpp
Go to the documentation of this file.
1/**
2 * PtSmoother: calibrated PT output smoothing. The first reading must come straight through —
3 * the old map default-inserted 0.0 and every PT ramped up from 0 psi after a restart.
4 */
5#include <cmath>
6#include <iostream>
7#include <limits>
8#include <string>
9
11
13
14static int g_failures = 0;
15static void check(bool ok, const std::string& what) {
16 std::cout << (ok ? " ok " : " FAIL ") << what << std::endl;
17 if (!ok)
18 ++g_failures;
19}
20static bool near(double a, double b) {
21 return std::fabs(a - b) < 1e-9;
22}
23
24int main() {
25 PtSmoother s; // alpha 0.25
26
27 check(near(s.step(1, 300.0), 300.0),
28 "first reading passes through unsmoothed (not ramped from 0)");
29 check(near(s.step(1, 300.0), 300.0), "a steady reading stays put");
30 check(near(s.step(1, 340.0), 310.0), "then it smooths: 0.25*340 + 0.75*300 = 310");
31
32 check(near(s.step(2, 50.0), 50.0), "each uid seeds independently");
33 check(near(s.step(1, 310.0), 310.0), "another uid does not disturb this one");
34
35 const double nan = std::numeric_limits<double>::quiet_NaN();
36 check(std::isnan(s.step(3, nan)), "a non-finite reading is passed through");
37 check(near(s.step(3, 120.0), 120.0),
38 "and the next finite one re-seeds instead of averaging NaN");
39
40 s.reset(1);
41 check(near(s.step(1, 0.0), 0.0), "reset re-seeds from the next reading");
42
43 // Several readings from cold: the archive's minimum is the first reading, not a ramp from 0.
44 PtSmoother cold;
45 double min_seen = 1e9;
46 for (int i = 0; i < 10; ++i)
47 min_seen = std::fmin(min_seen, cold.step(42, 300.0));
48 check(near(min_seen, 300.0), "a 300 psi tank never reads below 300 psi from a cold start");
49
50 std::cout << (g_failures ? "FAILED" : "PASSED") << std::endl;
51 return g_failures ? 1 : 0;
52}
double step(uint16_t uid, double value)
Feed one reading; returns the smoothed value.
static bool ok
Definition flight_e2e.c:435
*brief create JSON pointer *sa reference_tokens const string_t const string_t & b
Definition json.hpp:13878
*brief create JSON pointer *sa reference_tokens const string_t & a
Definition json.hpp:13877
static int g_failures
static void check(bool ok, const std::string &what)
static bool near(double a, double b)
int main()