STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include "main.h"
2
3#include <Arduino.h>
4#include <Ethernet.h>
5#include <EthernetUdp.h>
6#include <SPI.h>
7#include <daq-protocol.h>
8
9#include <cstring>
10
11#include "STAR_ADS126X.h"
12#include "adc_mappings.h"
13#include "esp_mac.h"
14
15// Change the following line to automatically use the correct pins for the board
16// being tested (PT_Board, LC_Board, RTD_Board, or TC_Board)
17#define PINS_ACTIVE_LAYOUT sense_board_pins::PT_Board
18
19// These lines MUST be after the #define PINS_ACTIVE_LAYOUT or they will
20// overwrite it with the default value!
21#include "connector_adc_map.h"
22#include "sense_board_pins.h"
23
24// Connectors 1-10 on the sense board. Each chunk = one full scan (all
25// connectors). Packet format expects num_sensors datapoints per chunk; max 9
26// chunks fits in 512 bytes.
27#define TEST_PIN 1
28#define NUM_CONNECTORS 10
29#define MAX_CHUNKS_BEFORE_SEND 9
30
31using namespace sense_board_pins;
32
33// Ethernet configuration
34byte mac[6]; // Will be populated with unique MAC from ESP32 eFuse
35IPAddress staticIP(192, 168, 2, 101);
36IPAddress gateway(0, 0, 0, 0);
37IPAddress subnet(255, 255, 255, 0);
38IPAddress dns(192, 168, 2, 1);
39IPAddress receiverIP(192, 168, 2, 20);
40const int receiverPort = 5006;
41EthernetUDP udp;
42
44SPIClass ADC_SPI(HSPI);
45
46// Forward declarations
47void flush_adc_cycles(int cycles);
48void read_single_connector(uint8_t connector_id, int num_readings,
50void collect_chunk();
52
53// One chunk = full scan (10 datapoints). Accumulate up to
54// MAX_CHUNKS_BEFORE_SEND.
55std::vector<daq::SensorDataChunkCollection> chunks;
56
57float convert_code_to_voltage(int32_t code) {
58 // Assumes the 2.5V internal reference is being used!
59 return ((float)code * 2.5f) / 2147483648.0f;
60}
61
62void setup() {
63 Serial.begin(115200);
64 // while (!Serial) {
65 // delay(10); // Wait for native USB serial to connect
66 // }
67
68 Serial.println("Starting ADC with Ethernet...");
69
70// Print reference configuration
71#if USE_VDD_REFERENCE
72 Serial.println("ADC Reference: VDD");
73#else
74 Serial.println("ADC Reference: Internal 2.5V");
75#endif
76
77 // Setup ADC SPI
78 ADC_SPI.begin(Pins.ADC_SCLK, Pins.ADC_MISO, Pins.ADC_MOSI, Pins.ADC_CS_1);
79
80 // You can set the freq if you want, or just use the default
81 // SPI.setFrequency(1'000'000);
82
83 // Due to the ADC output having valid data on FALLING CLK edges
84 ADC_SPI.setDataMode(SPI_MODE1);
85 pinMode(Pins.ADC_DRDY_1, INPUT);
86
87 // Setup ADC
88 ads126x.begin(Pins.ADC_CS_1, &ADC_SPI);
89
90 // Stop it while we config it, as suggested by datasheet
91 ads126x.stopADC1();
92
93 // Set initial input mux (will be updated per connector in each chunk)
95
96// Set the reference voltage based on configuration in main.h
97#if USE_VDD_REFERENCE
98 // Using VDD as reference (typically 3.3V or 5V depending on board)
100#else
101 // Using internal 2.5V reference (default if setReference is not called)
102 // No call to setReference() - ADC defaults to internal reference
103#endif
104
105 // Bypas the PGA, so it does not affect measurements
106 ads126x.bypassPGA();
107
108 // Set the filter. You can change this to try different filters
109 ads126x.setFilter(FILTER);
110
111 // Set the datarate. You can change this, but the options depends on the
112 // filter I do not know what happens if you program an invalid data rate for
113 // a given filter
114 ads126x.setRate(DATA_RATE);
115
116 // Start ADC now that configuration is done
117 ads126x.startADC1();
118
119 // Generate unique MAC address from ESP32 eFuse (derived for Ethernet)
120 ESP_ERROR_CHECK(
121 esp_read_mac(mac, ESP_MAC_ETH)); // Derived from base eFuse MAC
122
123 Serial.print("Generated unique MAC address: ");
124 for (int i = 0; i < 6; i++) {
125 if (i > 0)
126 Serial.print(":");
127 if (mac[i] < 0x10)
128 Serial.print("0");
129 Serial.print(mac[i], HEX);
130 }
131 Serial.println();
132
133 // Setup Ethernet SPI
134 Serial.println("Initializing Ethernet...");
135 SPI.begin(Pins.ETH_SCLK, Pins.ETH_MISO, Pins.ETH_MOSI, Pins.ETH_CS);
136 delay(1000);
137
138 // Initialize Ethernet with CS pin
139 Ethernet.init(Pins.ETH_CS);
140 delay(1000);
141
142 // Start Ethernet with static IP
144 delay(1000);
145
146 // Start UDP
147 udp.begin(5005);
148
149 // Print Ethernet status
150 Serial.print("Ethernet initialized. IP: ");
151 Serial.println(Ethernet.localIP());
152 Serial.print("Link Status: ");
153 if (Ethernet.linkStatus() == LinkON) {
154 Serial.println("Connected");
155 } else if (Ethernet.linkStatus() == LinkOFF) {
156 Serial.println("Disconnected");
157 } else {
158 Serial.println("Unknown");
159 }
160
161 Serial.println("Setup complete!");
162}
163
164void loop() {
166
167 if (chunks.size() >= MAX_CHUNKS_BEFORE_SEND) {
169 chunks.clear();
170 }
171}
172
173void flush_adc_cycles(int cycles) {
174 for (int i = 0; i < cycles; i++) {
175 while (digitalRead(Pins.ADC_DRDY_1) != LOW)
177 ads126x.readADC1();
178 }
179}
180
181void read_single_connector(uint8_t connector_id, int num_readings,
183 uint32_t value = 0;
184 for (int i = 0; i < num_readings; i++) {
185 while (digitalRead(Pins.ADC_DRDY_1) != LOW)
187 const auto reading = ads126x.readADC1();
188 if (!reading.checksumValid)
189 continue;
190 value = static_cast<uint32_t>(reading.value);
191 }
192 chunk.add_datapoint(connector_id, value);
193}
194
195// One chunk = full scan of all 10 connectors (packet format expects num_sensors
196// per chunk)
199 for (uint8_t connector_id = 1; connector_id <= NUM_CONNECTORS;
200 connector_id++) {
201 ads126x.setInputMux(getAdcChannel(connector_id, TEST_PIN),
205 }
206 if (chunk.size() == NUM_CONNECTORS)
207 chunks.push_back(chunk);
208}
209
211 if (chunks.empty()) {
212 return;
213 }
214
216 size_t packetSize = daq::create_sensor_data_packet(
218
219 if (packetSize == 0) {
220 Serial.println("Error: Failed to create sensor data packet");
221 return;
222 }
223
224 udp.beginPacket(receiverIP, receiverPort);
225 udp.write(packetBuffer, packetSize);
226 udp.endPacket();
227
228 Serial.print("Sent sensor data packet: ");
229 Serial.print(packetSize);
230 Serial.print(" bytes, ");
231 Serial.print(chunks.size());
232 Serial.println(" chunks");
233}
#define FILTER
Definition main.h:4
#define DATA_RATE
Definition main.h:5
#define READINGS_PER_MUX
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)
constexpr uint8_t settlePulses(uint8_t filter, uint8_t data_rate, bool chop_enabled=false, bool idac_rotation_enabled=false)
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
#define TEST_PIN
Definition main.cpp:23
void read_single_connector(uint8_t connector_id, int num_readings, daq::SensorDataChunkCollection &chunk)
Definition main.cpp:181
#define MAX_CHUNKS_BEFORE_SEND
Definition main.cpp:29
void setup()
Definition main.cpp:62
IPAddress dns(192, 168, 2, 1)
SPIClass ADC_SPI(HSPI)
IPAddress subnet(255, 255, 255, 0)
void collect_chunk()
Definition main.cpp:197
IPAddress receiverIP(192, 168, 2, 20)
void sendSensorDataPacket()
Definition main.cpp:210
std::vector< daq::SensorDataChunkCollection > chunks
Definition main.cpp:55
float convert_code_to_voltage(int32_t code)
Definition main.cpp:57
void flush_adc_cycles(int cycles)
Definition main.cpp:173
IPAddress staticIP(192, 168, 2, 101)
IPAddress gateway(0, 0, 0, 0)
#define NUM_CONNECTORS
Definition main.cpp:28
void loop()
Definition main.cpp:164
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_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.