STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
test_hp_pt_code.cpp
Go to the documentation of this file.
1/**
2 * Unit tests for fsw::calibration::hp_pt_shunt_code (4-20 mA PT wire code coercion).
3 * House style: plain main(), return 0 on pass / 1 on failure. Registered in lib/CMakeLists.txt.
4 *
5 * The headline case is the code captured live on 2026-11-07 from HPD-PT (PT board 2 ch3):
6 * 0xFFFF0FB5, a small negative ADC reading that the old coercion clamped to full scale.
7 */
8
9#include <cstdint>
10#include <cstdio>
11#include <initializer_list>
12
14
16
17static int g_failures = 0;
18
19#define CHECK(cond, ...) \
20 do { \
21 if (!(cond)) { \
22 g_failures++; \
23 std::printf(" ❌ [%s:%d] ", __func__, __LINE__); \
24 std::printf(__VA_ARGS__); \
25 std::printf("\n"); \
26 } \
27 } while (0)
28
29/** Shunt volts for a code, as calibration_main's convert_hp_pt_to_pressure computes it. */
30static double volts(uint32_t code) {
31 return static_cast<double>(code) / 2147483648.0 * 2.5;
32}
33
35 // Captured from the live stream: HPD-PT raw_adc_counts 4294905781 = 0xFFFF0FB5 = -61515 signed.
36 const uint32_t captured = 4294905781u;
37 const uint32_t c = hp_pt_shunt_code(captured);
38 CHECK(c == 0u, "captured negative code 0x%08x -> 0x%08x, want 0 (was clamped to full scale)",
39 captured, c);
40 // Full scale would have been 2.5 V: 20.8 mA across 120 Ohm, i.e. 5000 psi.
41 CHECK(volts(c) < 0.01, "negative code reads %.3f V on the shunt, want ~0", volts(c));
42}
43
45 for (uint32_t w : {0x80000000u, 0x80000001u, 0xC0000000u, 0xFFFFFF00u, 0xFFFFFFFFu})
46 CHECK(hp_pt_shunt_code(w) == 0u, "0x%08x -> 0x%08x, want 0", w, hp_pt_shunt_code(w));
47}
48
50 // 4 mA and 20 mA across 120 Ohm against 2.5 V, plus the ends of the positive range.
51 const uint32_t four_ma = static_cast<uint32_t>(0.48 / 2.5 * 2147483648.0);
52 const uint32_t twenty_ma = static_cast<uint32_t>(2.40 / 2.5 * 2147483648.0);
53 for (uint32_t w : {0u, 1u, 282305u, four_ma, twenty_ma, 0x7FFFFFFFu})
54 CHECK(hp_pt_shunt_code(w) == w, "valid code 0x%08x changed to 0x%08x", w,
56}
57
58int main() {
59 std::printf("test_hp_pt_code\n");
63 if (g_failures) {
64 std::printf("FAILED: %d check(s)\n", g_failures);
65 return 1;
66 }
67 std::printf("all passed\n");
68 return 0;
69}
uint32_t hp_pt_shunt_code(uint32_t wire)
A 4-20 mA PT's wire code -> the shunt code the conversions and calibrations work in.
Definition HpPtCode.hpp:23
static int g_failures
#define CHECK(cond,...)
static void live_loop_codes_pass_through()
uint32_t hp_pt_shunt_code(uint32_t wire)
Unit tests for fsw::calibration::hp_pt_shunt_code (4-20 mA PT wire code coercion).
Definition HpPtCode.hpp:23
static double volts(uint32_t code)
Shunt volts for a code, as calibration_main's convert_hp_pt_to_pressure computes it.
static void every_negative_code_is_zero()
static void live_negative_code_is_no_current()
int main()