STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <Arduino.h>
2#include <Ethernet.h>
3#include <EthernetUdp.h>
4#include <SPI.h>
5#include <daq-protocol.h>
6#include <esp_mac.h>
7
8#include <cstring>
9#include <vector>
10
11#include "STAR_ADS126X.h"
12
13// Change the following line to automatically use the correct pins for the board
14// being tested (PT_Board, LC_Board, RTD_Board, or TC_Board)
15#define PINS_ACTIVE_LAYOUT sense_board_pins::LC_Board
16
17// These lines MUST be after the #define PINS_ACTIVE_LAYOUT or they will
18// overwrite it with the default value!
19#include "adc_mappings.h"
20#include "connector_adc_map.h"
21#include "sense_board_pins.h"
22
23using namespace sense_board_pins;
24
26SPIClass ADC_SPI(HSPI); // ADC on HSPI, Ethernet on default SPI (VSPI)
27
28// LC Board ADC1 connectors: 1, 2, 3, 6, 7
29const uint8_t ADC1_CONNECTORS[] = {1, 2, 3, 6, 7};
30const uint8_t NUM_LC_SENSORS =
31 sizeof(ADC1_CONNECTORS) / sizeof(ADC1_CONNECTORS[0]);
32#define FILTER ADS126X_SINC4
33#define DATA_RATE ADS126X_RATE_1200
34
35// ---------------------------------------------------------------------------
36// Ethernet
37// ---------------------------------------------------------------------------
38byte mac[6];
39IPAddress staticIP(192, 168, 2, 100);
40IPAddress gateway(192, 168, 2, 1);
41IPAddress subnet(255, 255, 255, 0);
42IPAddress dns(192, 168, 2, 1);
43IPAddress receiverIP(192, 168, 2, 20);
44const int receiverPort = 5006;
45EthernetUDP udp;
47
48float convert_code_to_voltage(int32_t code) {
49 // Assumes the 2.5V internal reference is being used!
50 return ((float)code * 2.5f) / 2147483648.0f;
51}
52
53void flush_cycles(int n) {
54 for (int i = 0; i < n; i++) {
55 while (digitalRead(Pins.ADC_DRDY_1) != LOW)
57 ads126x.readADC1();
58 }
59}
60
61void setup() {
62 Serial.begin(115200);
63 while (!Serial)
64 delay(10);
65 delay(500);
66
67 Serial.println("LC Calibration - Based on LC_Simple_Test with Ethernet");
68
69 // Setup SPI for ADC (HSPI) - exactly like LC_Simple_Test
70 ADC_SPI.begin(Pins.ADC_SCLK, Pins.ADC_MISO, Pins.ADC_MOSI);
71
72 // Due to the ADC output having valid data on FALLING CLK edges
73 ADC_SPI.setDataMode(SPI_MODE1);
74 pinMode(Pins.ADC_DRDY_1, INPUT);
75
76 // Setup ADC - exactly like LC_Simple_Test
77 ads126x.begin(Pins.ADC_CS_1, &ADC_SPI);
78
79 ads126x.enablePGA();
80 ads126x.setGain(ADS126X_GAIN_8);
81
82 // Stop it while we config it, as suggested by datasheet
83 ads126x.stopADC1();
84
85 // Set the filter. You can change this to try different filters
86 ads126x.setFilter(FILTER);
87
88 // Set the datarate. You can change this, but the options depends on the
89 // filter
90 ads126x.setRate(DATA_RATE);
91
92 // Start ADC now that configuration is done
93 ads126x.startADC1();
94
95 // Ethernet (default SPI / VSPI)
96 ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_ETH));
97 SPI.begin(Pins.ETH_SCLK, Pins.ETH_MISO, Pins.ETH_MOSI);
98 delay(300);
99 Ethernet.init(Pins.ETH_CS);
100 delay(300);
102 delay(300);
103 udp.begin(5005);
104
105 Serial.print("IP: ");
106 Serial.println(Ethernet.localIP());
107 Serial.print("Link: ");
108 Serial.println(Ethernet.linkStatus() == LinkON ? "ON" : "OFF");
109 Serial.print("Send to: ");
110 Serial.print(receiverIP);
111 Serial.print(":");
112 Serial.println(receiverPort);
113 Serial.print("Reading LC connectors: ");
114 for (uint8_t i = 0; i < NUM_LC_SENSORS; i++) {
115 Serial.print(ADC1_CONNECTORS[i]);
116 if (i < NUM_LC_SENSORS - 1)
117 Serial.print(", ");
118 }
119 Serial.println();
120 Serial.println();
121}
122
123void loop() {
124 // Create sensor data chunk with timestamp
126
127 // Read all ADC1 LC connectors (connectors 1, 2, 3, 6, 7)
128 for (uint8_t i = 0; i < NUM_LC_SENSORS; i++) {
129 uint8_t conn = ADC1_CONNECTORS[i];
130
131 // Get ADC channels for pin 1 (positive) and pin 2 (negative)
132 int pos_channel = getAdcChannel(conn, 1);
133 int neg_channel = getAdcChannel(conn, 2);
134
135 float voltage = 0.0f;
136
137 if (pos_channel >= 0 && neg_channel >= 0) {
138 // Set input mux for differential reading (pin 1 vs pin 2)
139 ads126x.setInputMux(pos_channel, neg_channel);
140
141 // Flush cycles to let mux settle after change
143
144 // Wait for DRDY pin - exactly like LC_Simple_Test
145 while (digitalRead(Pins.ADC_DRDY_1) != LOW) {
147 }
148
150
151 // Get the most recent value - exactly like LC_Simple_Test
152 const auto reading = ads126x.readADC1();
153
154 if (reading.checksumValid) {
155 voltage = convert_code_to_voltage(reading.value);
156
157 // Print to serial
158 Serial.print("LC ");
159 Serial.print(conn);
160 Serial.print(": ");
161 Serial.print(voltage, 6);
162 Serial.println(" V");
163 } else {
164 Serial.print("LC ");
165 Serial.print(conn);
166 Serial.println(": Bad checksum!");
167 }
168 }
169
170 // Convert float to uint32_t bits for packet
171 uint32_t vbits;
172 memcpy(&vbits, &voltage, sizeof(float));
173 // Sensor ID is 0-based index (0-4 for connectors 1,2,3,6,7)
174 chunk.add_datapoint(i, vbits);
175 }
176
177 Serial.println(); // Blank line between readings
178
179 // Send packet via Ethernet UDP
180 std::vector<daq::SensorDataChunkCollection> chunks;
181 chunks.push_back(chunk);
182
183 size_t packetSize = daq::create_sensor_data_packet(
185 if (packetSize > 0) {
186 udp.beginPacket(receiverIP, receiverPort);
187 udp.write(packetBuffer, packetSize);
188 udp.endPacket();
189
190 Serial.print("Sent ");
191 Serial.print(packetSize);
192 Serial.print(" B, ");
193 Serial.print(NUM_LC_SENSORS);
194 Serial.println(" LCs");
195 }
196
197 delay(100);
198}
#define FILTER
Definition main.h:4
#define DATA_RATE
Definition main.h:5
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 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_GAIN_8
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
SPIClass ADC_SPI(HSPI)
std::vector< daq::SensorDataChunkCollection > chunks
Definition main.cpp:55
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
const uint8_t ADC1_CONNECTORS[]
Definition main.cpp:29
void setup()
Definition main.cpp:61
IPAddress dns(192, 168, 2, 1)
SPIClass ADC_SPI(HSPI)
void flush_cycles(int n)
Definition main.cpp:53
IPAddress subnet(255, 255, 255, 0)
IPAddress receiverIP(192, 168, 2, 20)
float convert_code_to_voltage(int32_t code)
Definition main.cpp:48
const uint8_t NUM_LC_SENSORS
Definition main.cpp:30
IPAddress gateway(192, 168, 2, 1)
IPAddress staticIP(192, 168, 2, 100)
void loop()
Definition main.cpp:123
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.
*brief user defined string literal for JSON values *sa std::size_t n
Definition json.hpp:24474
constexpr int getAdcChannel(uint8_t connector, uint8_t pin)
constexpr const Layout & Pins
Represents a single data chunk with timestamp and sensor datapoints.