STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
T7Calibration.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cmath>
5#include <limits>
6#include <set>
7
8#include "t7/T7Stream.hpp"
9
10namespace fsw {
11namespace t7 {
12
13uint8_t t7_logical_channel(uint8_t slot, uint8_t ain) {
14 return static_cast<uint8_t>(kT7LogicalBase + (slot - 1) * 16 + ain);
15}
16
17uint16_t t7_uid(int board_id, int ain) {
18 return static_cast<uint16_t>(board_id * 100 + ain);
19}
20
21int32_t t7_counts(double volts) {
22 const double uv = std::round(volts * 1e6);
23 if (!std::isfinite(uv))
24 return 0;
25 return static_cast<int32_t>(
26 std::clamp(uv, static_cast<double>(INT32_MIN), static_cast<double>(INT32_MAX)));
27}
28
29double t7_volts(double counts) {
30 return counts / 1e6;
31}
32
33std::array<uint8_t, 2> cjc_table_id(uint8_t slot) {
34 return {kTypeHi, static_cast<uint8_t>((slot - 1) * 0x20 + kCjcTableOffset)};
35}
36
38 if (!cfg.t7_errors.empty())
39 return;
40 for (const auto& b : cfg.t7_boards) {
41 if (!b.enabled)
42 continue;
43 const std::string key =
44 b.section.rfind("boards.", 0) == 0 ? b.section.substr(7) : b.section;
45 for (const auto& c : b.channels) {
46 if (c.kind != "PT" && c.kind != "LC" && c.kind != "TC" && c.kind != "RTD")
47 continue;
49 t.board_key = key;
50 t.board_id = b.board_id;
51 t.slot = b.slot();
52 t.channel = c;
53 t.uid = t7_uid(b.board_id, c.ain);
54 t.logical_ch = t7_logical_channel(t.slot, static_cast<uint8_t>(c.ain));
55 by_uid_[t.uid] = channels_.size();
56 by_table_[{t.slot, static_cast<uint8_t>(c.ain)}] = channels_.size();
57 channels_.push_back(std::move(t));
58 }
59 }
60}
61
62const T7CalChannel* T7CalibrationMap::by_uid(uint16_t uid) const {
63 auto it = by_uid_.find(uid);
64 return it == by_uid_.end() ? nullptr : &channels_[it->second];
65}
66
67const T7CalChannel* T7CalibrationMap::by_table(uint8_t slot, uint8_t ain) const {
68 auto it = by_table_.find({slot, ain});
69 return it == by_table_.end() ? nullptr : &channels_[it->second];
70}
71
72std::vector<uint8_t> T7CalibrationMap::tc_slots() const {
73 std::set<uint8_t> s;
74 for (const auto& c : channels_)
75 if (c.channel.kind == "TC")
76 s.insert(c.slot);
77 return {s.begin(), s.end()};
78}
79
80std::vector<std::pair<uint8_t, uint8_t>> T7CalibrationMap::raw_tables() const {
81 std::vector<std::pair<uint8_t, uint8_t>> out;
82 for (const auto& c : channels_) {
83 const auto id = table_id(c.slot, static_cast<uint8_t>(c.channel.ain));
84 out.emplace_back(id[0], id[1]);
85 }
86 for (uint8_t slot : tc_slots()) {
87 const auto id = cjc_table_id(slot);
88 out.emplace_back(id[0], id[1]);
89 }
90 return out;
91}
92
93} // namespace t7
94} // namespace fsw
std::map< uint16_t, size_t > by_uid_
const T7CalChannel * by_uid(uint16_t uid) const
std::vector< uint8_t > tc_slots() const
Slots with at least one TC (they publish a cold-junction table).
std::vector< T7CalChannel > channels_
std::map< std::pair< uint8_t, uint8_t >, size_t > by_table_
std::vector< std::pair< uint8_t, uint8_t > > raw_tables() const
Raw tables calibration_service subscribes to: every input above, plus each TC slot's CJC.
const T7CalChannel * by_table(uint8_t slot, uint8_t ain) const
The input behind raw table {0x26, (slot-1)*0x20 + ain}, or nullptr.
static uint8_t board_id
Definition main.cpp:30
*brief create JSON pointer *sa reference_tokens const string_t const string_t & b
Definition json.hpp:13878
*brief remove last reference token *sa JSON pointer has no nullptr
Definition json.hpp:13968
constexpr uint8_t kCjcTableOffset
Raw-table offset (within a slot's 32-entry block) of the cold-junction table: AIN15 does not exist on...
std::array< uint8_t, 2 > cjc_table_id(uint8_t slot)
Cold-junction table id for a slot.
constexpr uint8_t kTypeHi
Elodin table-id high byte for T7 analog inputs (0x20-0x25 are the ESP32 sensor types).
Definition T7Stream.hpp:27
int32_t t7_counts(double volts)
The calibration "ADC code" for a T7 reading: microvolts, rounded and clamped to int32.
std::array< uint8_t, 2 > table_id(uint8_t slot, uint8_t ain)
Table id for one AIN: {0x26, (slot-1)*0x20 + ain}, the same layout as the ESP32 boards.
Definition T7Stream.cpp:22
double t7_volts(double counts)
Volts from a calibration code.
constexpr uint8_t kT7LogicalBase
First logical calibration channel for T7 inputs.
uint16_t t7_uid(int board_id, int ain)
uid of a T7 input: board_id * 100 + ain, the ESP32 scheme with the AIN as connector.
uint8_t t7_logical_channel(uint8_t slot, uint8_t ain)
Logical calibration channel for a T7 input.
Motorized ball valve position: raw ADC counts -> % open.
The whole parsed config.toml, restricted to what C++ consumes.
Definition Config.hpp:264
One T7 input as calibration sees it.
static double volts(uint32_t code)
Shunt volts for a code, as calibration_main's convert_hp_pt_to_pressure computes it.