STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
TypeK.cpp
Go to the documentation of this file.
2
3#include <cmath>
4#include <limits>
5
6namespace fsw {
7namespace calibration {
8
9double type_k_c_from_mv(double v_mv) {
10 // ITS-90 Type K inverse, rational form in 5 sub-ranges:
11 // (v_min_mV, v_max_mV, T0, V0, p1, p2, p3, p4, q1, q2, q3); T = T0 + x*num/den, x = V - V0.
12 // Moved verbatim from calibration_main.cpp convert_tc_adc_to_temp_c().
13 static const double ranges[5][11] = {
14 {-6.404, -3.554, -121.47164, -4.1790858, 36.069513, 30.722076, 7.791386, 0.52593997,
15 0.93939547, 0.2779128, 0.02516334},
16 {-3.554, 4.096, -8.7935962, -0.34489914, 25.678719, -0.49887904, -0.44705222, -0.044869202,
17 0.00023893439, -0.02039775, -0.0018424107},
18 {4.096, 16.397, 310.18976, 12.631386, 24.061949, 4.0158622, 0.26853917, -0.0097188544,
19 0.16995872, 0.011413069, -0.00039275155},
20 {16.397, 33.275, 605.72562, 25.148718, 23.539401, 0.046547228, 0.0134444, 0.0005923685,
21 0.00083445513, 0.0004612144, 0.00002548812},
22 {33.275, 69.553, 1018.4705, 41.99385, 25.783239, -1.8363403, 0.05617666, 0.000185324,
23 -0.074803355, 0.002384186, 0.0},
24 };
25 for (const auto& r : ranges) {
26 if (v_mv >= r[0] && v_mv <= r[1]) {
27 const double x = v_mv - r[3];
28 const double num = r[4] + x * (r[5] + x * (r[6] + x * r[7]));
29 const double den = 1.0 + x * (r[8] + x * (r[9] + x * r[10]));
30 if (std::abs(den) < 1e-20)
31 return std::numeric_limits<double>::quiet_NaN();
32 return r[2] + (x * num) / den;
33 }
34 }
35 return std::numeric_limits<double>::quiet_NaN();
36}
37
38double type_k_mv_from_c(double c) {
39 // The inverse is increasing across its whole span, so bisection on it converges to the EMF
40 // whose temperature is `c`. 60 halvings of a 76 mV interval is far below a nanovolt.
41 double lo = kTypeKMinMv, hi = kTypeKMaxMv;
42 const double t_lo = type_k_c_from_mv(lo), t_hi = type_k_c_from_mv(hi);
43 if (!std::isfinite(c) || c < t_lo || c > t_hi)
44 return std::numeric_limits<double>::quiet_NaN();
45 for (int i = 0; i < 60; ++i) {
46 const double mid = 0.5 * (lo + hi);
47 if (type_k_c_from_mv(mid) < c)
48 lo = mid;
49 else
50 hi = mid;
51 }
52 return 0.5 * (lo + hi);
53}
54
55} // namespace calibration
56} // namespace fsw
Output smoothing for calibrated PT pressure, per sensor uid (ESP32 and LabJack T7 alike).
double type_k_mv_from_c(double c)
EMF (mV, referenced to 0 C) a type K produces at c.
Definition TypeK.cpp:38
double type_k_c_from_mv(double mv)
Temperature (C) for a type K EMF in mV, referenced to 0 C.
Definition TypeK.cpp:9
constexpr double kTypeKMaxMv
Definition TypeK.hpp:15
constexpr double kTypeKMinMv
Lowest and highest EMF the inverse covers (mV): -250 C to 1372 C.
Definition TypeK.hpp:14
Motorized ball valve position: raw ADC counts -> % open.
static boot_report_t r