REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
daq.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
4
5#include <proto/main.pb.h>
6#include <Arduino.h>
7#include <algorithm>
8#include <bitset>
9
10#include <carrier/carrier.h>
11#include <daq/daq.h>
12
13std::array<int16_t, daq::NUM_CHANNELS> daq::details::raw_zero_offsets = {0};
14
15size_t daq::raw_to_normalized(uint16_t raw) {
16 // exact conversion is 0.152658243301, but we only care about so much precision
17 return std::max(std::min((static_cast<unsigned int>(raw) * 1527u) / 10000u, 2500u), 0u);
18}
19
20float daq::raw_to_float(const uint16_t raw, const int16_t raw_zero_offset) {
21 return map(static_cast<float>(raw + raw_zero_offset), details::RAW_MINUS_ONE_POINT_TWO_FIVE,
22 details::RAW_PLUS_ONE_POINT_TWO_FIVE, -1.25f, 1.25f);
23}
24
25std::array<float, daq::NUM_CHANNELS> daq::raw_to_float(std::array<uint16_t, NUM_CHANNELS> raw) {
26 static_assert(NUM_CHANNELS == 8, "Adapt raw_to_float when changing NUM_CHANNELS.");
27 // Maybe easier to optimize for the compiler than std::transform
28 return {
29 raw_to_float(raw[0], details::raw_zero_offsets[0]), raw_to_float(raw[1], details::raw_zero_offsets[1]),
30 raw_to_float(raw[2], details::raw_zero_offsets[2]), raw_to_float(raw[3], details::raw_zero_offsets[3]),
31 raw_to_float(raw[4], details::raw_zero_offsets[4]), raw_to_float(raw[5], details::raw_zero_offsets[5]),
32 raw_to_float(raw[6], details::raw_zero_offsets[6]), raw_to_float(raw[7], details::raw_zero_offsets[7])};
33}
34
35std::array<uint16_t, daq::NUM_CHANNELS> daq::raw_to_offset_corrected(std::array<uint16_t, NUM_CHANNELS> raw) {
36 static_assert(NUM_CHANNELS == 8, "Adapt raw_to_corrected when changing NUM_CHANNELS.");
37 // Maybe easier to optimize for the compiler than std::transform
38 return {static_cast<uint16_t>(raw[0] + details::raw_zero_offsets[0]),
39 static_cast<uint16_t>(raw[1] + details::raw_zero_offsets[1]),
40 static_cast<uint16_t>(raw[2] + details::raw_zero_offsets[2]),
41 static_cast<uint16_t>(raw[3] + details::raw_zero_offsets[3]),
42 static_cast<uint16_t>(raw[4] + details::raw_zero_offsets[4]),
43 static_cast<uint16_t>(raw[5] + details::raw_zero_offsets[5]),
44 static_cast<uint16_t>(raw[6] + details::raw_zero_offsets[6]),
45 static_cast<uint16_t>(raw[7] + details::raw_zero_offsets[7])};
46}