STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1/**
2 * Multi-Board Comms Testing - PRIMARY
3 *
4 * Sends SENSOR_DATA packets to the secondary board (192.168.2.101:5006),
5 * receives reply SENSOR_DATA packets on port 5005, and prints both
6 * sent and received packets to Serial.
7 *
8 * Uses: Arduino Ethernet, EthernetUDP, DAQv2-Comms (sensor packet format).
9 */
10
11#include <Arduino.h>
12#include <Ethernet.h>
13#include <EthernetUdp.h>
14#include <SPI.h>
15#include <daq-protocol.h>
16#include <esp_mac.h>
17
18#include <cstring>
19#include <vector>
20
21#include "actuator_board_pins.h"
22
23namespace pins = actuator_board_pins;
24
25// ---------------------------------------------------------------------------
26// Network: primary = 192.168.2.100, listen 5005; send to secondary
27// 192.168.2.101:5006
28// ---------------------------------------------------------------------------
29byte mac[6];
30IPAddress staticIP(192, 168, 2, 100);
31IPAddress gateway(192, 168, 2, 1);
32IPAddress subnet(255, 255, 255, 0);
33IPAddress dns(192, 168, 2, 1);
34
35IPAddress secondaryIP(192, 168, 2, 101);
36const int secondaryPort = 5006;
37const int localPort = 5005;
38
39EthernetUDP udp;
41
42// ---------------------------------------------------------------------------
43// Sensor packet: 1 chunk, 2 synthetic sensors for testing
44// ---------------------------------------------------------------------------
45const uint8_t NUM_SENSORS = 2;
46const unsigned long SEND_INTERVAL_MS = 1000;
47unsigned long lastSendMs = 0;
48
49// ---------------------------------------------------------------------------
50// Helpers: packet type name, print header
51// ---------------------------------------------------------------------------
52static const char* packetTypeName(uint8_t t) {
53 switch (t) {
55 return "BOARD_HEARTBEAT";
57 return "SERVER_HEARTBEAT";
58 case (uint8_t)daq::PacketType::SENSOR_DATA:
59 return "SENSOR_DATA";
61 return "ACTUATOR_COMMAND";
63 return "SENSOR_CONFIG";
65 return "ACTUATOR_CONFIG";
66 case (uint8_t)daq::PacketType::ABORT:
67 return "ABORT";
68 case (uint8_t)daq::PacketType::ABORT_DONE:
69 return "ABORT_DONE";
70 case (uint8_t)daq::PacketType::CLEAR_ABORT:
71 return "CLEAR_ABORT";
72 default:
73 return "UNKNOWN";
74 }
75}
76
77void printReceivedPacket(const uint8_t* buf, size_t len, IPAddress fromIP,
78 uint16_t fromPort) {
79 if (!buf || len < sizeof(daq::PacketHeader)) {
80 Serial.print("RECV: (invalid, too short) ");
81 Serial.print(len);
82 Serial.print(" bytes from ");
83 Serial.print(fromIP);
84 Serial.print(":");
85 Serial.println(fromPort);
86 return;
87 }
88 daq::PacketHeader h;
89 memcpy(&h, buf, sizeof(daq::PacketHeader));
90 Serial.print("RECV: [");
91 Serial.print(packetTypeName((uint8_t)h.packet_type));
92 Serial.print("] ");
93 Serial.print(len);
94 Serial.print(" bytes from ");
95 Serial.print(fromIP);
96 Serial.print(":");
97 Serial.print(fromPort);
98 Serial.print(" | type=");
99 Serial.print((int)h.packet_type);
100 Serial.print(" version=");
101 Serial.print((int)h.version);
102 Serial.print(" ts=");
103 Serial.println(h.timestamp);
104}
105
106// ---------------------------------------------------------------------------
107// Send one SENSOR_DATA packet to secondary
108// ---------------------------------------------------------------------------
110 unsigned long ts = millis();
112 float v0 = (float)(ts % 10000) / 1000.0f;
113 float v1 = (float)((ts / 17) % 1000) / 100.0f;
114 uint32_t b0, b1;
115 memcpy(&b0, &v0, sizeof(float));
116 memcpy(&b1, &v1, sizeof(float));
117 chunk.add_datapoint(0, b0);
118 chunk.add_datapoint(1, b1);
119
120 std::vector<daq::SensorDataChunkCollection> chunks;
121 chunks.push_back(chunk);
122
125 if (n == 0) {
126 Serial.println("SENT: (create_sensor_data_packet failed)");
127 return;
128 }
129
130 udp.beginPacket(secondaryIP, secondaryPort);
131 udp.write(packetBuffer, n);
132 udp.endPacket();
133
134 Serial.print("SENT: [SENSOR_DATA] ");
135 Serial.print(n);
136 Serial.print(" bytes, 1 chunk, ");
137 Serial.print(NUM_SENSORS);
138 Serial.print(" sensors | ts=");
139 Serial.println(ts);
140}
141
142// ---------------------------------------------------------------------------
143// Setup & loop
144// ---------------------------------------------------------------------------
145void setup() {
146 Serial.begin(115200);
147 while (!Serial)
148 delay(10);
149
150 Serial.println("Multi-Board Comms Testing – PRIMARY");
151 Serial.println(" Sends SENSOR_DATA -> 192.168.2.101:5006");
152 Serial.println(" Listens on :5005 for reply");
153 Serial.println("----------------------------------------");
154
155 ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_ETH));
156 Serial.print("MAC: ");
157 for (int i = 0; i < 6; i++) {
158 if (i)
159 Serial.print(":");
160 if (mac[i] < 0x10)
161 Serial.print("0");
162 Serial.print(mac[i], HEX);
163 }
164 Serial.println();
165
168 delay(500);
170 delay(500);
172 delay(500);
173
174 udp.begin(localPort);
175
176 Serial.print("IP: ");
177 Serial.println(Ethernet.localIP());
178 Serial.print("Link: ");
179 Serial.println(Ethernet.linkStatus() == LinkON ? "ON" : "OFF");
180 Serial.println();
181}
182
183void loop() {
184 // 1) Check for incoming reply on 5005
185 int n = udp.parsePacket();
186 if (n > 0) {
187 int len = udp.read(packetBuffer, sizeof(packetBuffer));
188 if (len > 0)
189 printReceivedPacket(packetBuffer, (size_t)len, udp.remoteIP(),
190 udp.remotePort());
191 }
192
193 // 2) Periodically send SENSOR_DATA to secondary
194 unsigned long now = millis();
195 if (now - lastSendMs >= SEND_INTERVAL_MS) {
196 lastSendMs = now;
198 }
199
200 delay(10);
201}
static SerialStub Serial
Definition Arduino.h:128
unsigned long millis()
Definition Arduino.h:29
#define HEX
Definition Arduino.h:24
static EthernetClass Ethernet
Definition Ethernet.h:116
#define MAX_PACKET_SIZE
Definition main.h:6
void delay(int ms)
static uint8_t chunk[64]
Definition board_uart9.c:12
void setup()
Definition main.cpp:142
void loop()
Definition main.cpp:201
std::vector< daq::SensorDataChunkCollection > chunks
Definition main.cpp:55
#define NUM_SENSORS
Definition main.cpp:46
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)
static IPAddress subnet(255, 255, 255, 0)
static IPAddress gateway(192, 168, 2, 1)
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]
Definition main.cpp:15
IPAddress dns(192, 168, 2, 1)
unsigned int localPort
Definition main.cpp:13
IPAddress staticIP(192, 168, 2, 101)
static constexpr int ETH_CS
Ball Valve Hotfire — motorized ball valve prototype board.
Definition main.cpp:53
static constexpr int ETH_SCLK
Definition main.cpp:55
static constexpr int ETH_MISO
Definition main.cpp:56
static constexpr int ETH_MOSI
Definition main.cpp:54
const int secondaryPort
Definition main.cpp:36
const unsigned long SEND_INTERVAL_MS
Definition main.cpp:46
void printReceivedPacket(const uint8_t *buf, size_t len, IPAddress fromIP, uint16_t fromPort)
Definition main.cpp:77
IPAddress secondaryIP(192, 168, 2, 101)
void sendSensorPacket()
Definition main.cpp:109
static const char * packetTypeName(uint8_t t)
Definition main.cpp:52
unsigned long lastSendMs
Definition main.cpp:47
constexpr Layout Actuator_Board
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
Represents a single data chunk with timestamp and sensor datapoints.