STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1/**
2 * stream_one_adc: Read ADC1 only, single fixed channel (no mux cycling).
3 * Streams readings over Ethernet UDP using DAQv2-Comms packet format.
4 */
5#include "main.h"
6
7#include <Arduino.h>
8#include <Ethernet.h>
9#include <EthernetUdp.h>
10#include <SPI.h>
11#include <daq-protocol.h>
12
13#include <cstring>
14
15#include "STAR_ADS126X.h"
16#include "esp_mac.h"
17
18// Board pin layout: define before any header that uses it (PT_Board, LC_Board,
19// RTD_Board, TC_Board)
20#define PINS_ACTIVE_LAYOUT PT_Board
21#include "connector_adc_map.h"
22#include "sense_board_pins.h"
23
24// Fixed input: connector 1, pin 1 (single-ended vs AINCOM). Change if needed.
25#define FIXED_CONNECTOR 1
26#define FIXED_PIN 1
27
28using namespace sense_board_pins;
29
30// Ethernet configuration
31byte mac[6];
32IPAddress staticIP(192, 168, 2, 101);
33IPAddress gateway(0, 0, 0, 0);
34IPAddress subnet(255, 255, 255, 0);
35IPAddress dns(192, 168, 2, 1);
36IPAddress receiverIP(192, 168, 2, 20);
37const int receiverPort = 5006;
38EthernetUDP udp;
39
41SPIClass ADC_SPI(HSPI);
42
43void read_data(int count);
45
46#define NUM_SENSORS 1
47#define MAX_CHUNKS 10
48std::vector<daq::SensorDataChunkCollection> dataChunks;
49static const uint8_t kSensorId = 1; // Single channel, fixed ID
50
51float convert_code_to_voltage(int32_t code) {
52 return ((float)code * 2.5f) / 2147483648.0f;
53}
54
55void setup() {
56 Serial.begin(115200);
57 Serial.println("stream_one_adc: ADC1 only, no muxing");
58
59#if USE_VDD_REFERENCE
60 Serial.println("ADC Reference: VDD");
61#else
62 Serial.println("ADC Reference: Internal 2.5V");
63#endif
64
65 ADC_SPI.begin(Pins.ADC_SCLK, Pins.ADC_MISO, Pins.ADC_MOSI, Pins.ADC_CS_1);
66 ADC_SPI.setDataMode(SPI_MODE1);
67 pinMode(Pins.ADC_DRDY_1, INPUT);
68
69 ads126x.begin(Pins.ADC_CS_1, &ADC_SPI);
70 ads126x.stopADC1();
71
72 // Single fixed channel: set mux once, never change
73 const int posChannel = getAdcChannel(FIXED_CONNECTOR, FIXED_PIN);
74 ads126x.setInputMux(static_cast<uint8_t>(posChannel), ADS126X_AINCOM);
75
76#if USE_VDD_REFERENCE
78#endif
79
80 ads126x.bypassPGA();
81 ads126x.setFilter(FILTER);
82 ads126x.setRate(DATA_RATE);
83 ads126x.startADC1();
84
85 ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_ETH));
86 Serial.print("MAC: ");
87 for (int i = 0; i < 6; i++) {
88 if (i > 0)
89 Serial.print(":");
90 if (mac[i] < 0x10)
91 Serial.print("0");
92 Serial.print(mac[i], HEX);
93 }
94 Serial.println();
95
96 Serial.println("Initializing Ethernet...");
97 SPI.begin(Pins.ETH_SCLK, Pins.ETH_MISO, Pins.ETH_MOSI, Pins.ETH_CS);
98 delay(1000);
99 Ethernet.init(Pins.ETH_CS);
100 delay(1000);
102 delay(1000);
103 udp.begin(5005);
104
105 Serial.print("Ethernet OK. IP: ");
106 Serial.println(Ethernet.localIP());
107 Serial.println("Setup complete.");
108}
109
110void loop() {
112
113 if (dataChunks.size() >= MAX_CHUNKS) {
115 dataChunks.clear();
116 }
117}
118
119void read_data(int count) {
121
122 for (int i = 0; i < count; i++) {
123 while (digitalRead(Pins.ADC_DRDY_1) != LOW) {
125 }
126 const auto reading = ads126x.readADC1();
127 if (!reading.checksumValid) {
128 Serial.println("Warning: Bad checksum");
129 continue;
130 }
131 chunk.add_datapoint(kSensorId, static_cast<uint32_t>(reading.value));
132 }
133
134 if (!chunk.empty()) {
135 dataChunks.push_back(chunk);
136 }
137}
138
140 if (dataChunks.empty())
141 return;
142
144 size_t packetSize = daq::create_sensor_data_packet(
146
147 if (packetSize == 0) {
148 Serial.println("Error: Failed to create sensor data packet");
149 return;
150 }
151
152 udp.beginPacket(receiverIP, receiverPort);
153 udp.write(packetBuffer, packetSize);
154 udp.endPacket();
155
156 Serial.print("Sent ");
157 Serial.print(packetSize);
158 Serial.print(" bytes, ");
159 Serial.print(dataChunks.size());
160 Serial.println(" chunks");
161}
#define FILTER
Definition main.h:4
#define DATA_RATE
Definition main.h:5
#define READINGS_PER_CHUNK
Definition main.h:3
static SerialStub Serial
Definition Arduino.h:128
#define INPUT
Definition Arduino.h:21
#define HSPI
Definition Arduino.h:132
int digitalRead(int)
Definition Arduino.h:50
void pinMode(int, int)
Definition Arduino.h:46
unsigned long millis()
Definition Arduino.h:29
#define HEX
Definition Arduino.h:24
#define SPI_MODE1
Definition Arduino.h:131
#define LOW
Definition Arduino.h:20
void delayMicroseconds(unsigned int)
Definition Arduino.h:58
static EthernetClass Ethernet
Definition Ethernet.h:116
#define MAX_PACKET_SIZE
Definition main.h:6
#define ADS126X_REF_POS_VDD
Definition STAR_ADS126X.h:9
#define ADS126X_REF_NEG_VSS
Definition STAR_ADS126X.h:8
#define ADS126X_AINCOM
void delay(int ms)
static uint8_t chunk[64]
Definition board_uart9.c:12
const int receiverPort
Definition main.cpp:26
IPAddress receiverIP(192, 168, 2, 20)
static ADS126X ads126x
Definition main.cpp:20
SPIClass ADC_SPI(HSPI)
static const uint8_t kSensorId
Definition main.cpp:49
#define FIXED_CONNECTOR
Definition main.cpp:25
void setup()
Definition main.cpp:55
IPAddress dns(192, 168, 2, 1)
SPIClass ADC_SPI(HSPI)
IPAddress subnet(255, 255, 255, 0)
IPAddress receiverIP(192, 168, 2, 20)
void sendSensorDataPacket()
Definition main.cpp:139
float convert_code_to_voltage(int32_t code)
Definition main.cpp:51
#define MAX_CHUNKS
Definition main.cpp:47
std::vector< daq::SensorDataChunkCollection > dataChunks
Definition main.cpp:48
void read_data(int count)
Definition main.cpp:119
IPAddress staticIP(192, 168, 2, 101)
#define FIXED_PIN
Definition main.cpp:26
#define NUM_SENSORS
Definition main.cpp:46
IPAddress gateway(0, 0, 0, 0)
void loop()
Definition main.cpp:110
IPAddress dns(192, 168, 2, 1)
byte mac[6]
Definition main.cpp:31
IPAddress subnet(255, 255, 255, 0)
IPAddress staticIP(192, 168, 2, BOARD_ID_DEFAULT)
EthernetUDP udp
Definition main.cpp:40
IPAddress gateway(0, 0, 0, 0)
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]
Definition main.cpp:15
size_type count(const key_type &key) const
Definition json.hpp:19264
size_t create_sensor_data_packet(const std::vector< SensorDataChunkCollection > &chunks, const uint8_t num_sensors, uint32_t timestamp_ms, uint8_t *buffer, size_t buffer_size)
Creates a complete Sensor Data packet in the provided buffer.
constexpr int getAdcChannel(uint8_t connector, uint8_t pin)
constexpr const Layout & Pins
Represents a single data chunk with timestamp and sensor datapoints.