-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
136 lines (115 loc) · 4.36 KB
/
Copy pathmain.cpp
File metadata and controls
136 lines (115 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// src/main.cpp
#include <Arduino.h>
#include <Wire.h>
#include <hwconfig.h>
#include <daqloop.h>
#include <ringbuffer.h>
#include <sensordispatcher.h>
#include <muxdriver.h>
#include <sdwrite.h>
#include <dispatcherthread.h>
#include <loramodule.h>
#include <lorasend.h>
#include <lora_config.h>
#include <solenoidreceive.h>
// Define the buffers
RingBuffer daq_buffer;
RingBuffer sd_buffer;
RingBuffer lora_buffer;
// SD writer instance (drains `sd_buffer` and performs block writes)
SDWrite sdwriter;
// LoRa module + sender
// NOTE: LoRa uses a hardware UART. On Teensy this project uses Serial4 (RX4/TX4).
// Constructor pins are ignored on Teensy — only the module address is required.
LoraModule lora_module(LORA_SENDER_ADDRESS);
LoraSend lora_sender;
SolenoidReceive solenoid_receive;
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("DAQ System Starting...");
// Initialize I2C (Teensy 4.1 uses default pins SDA=18, SCL=19)
Wire.begin();
// Initialize mux hardware
while (!mux_init()) {
Serial.println("ERROR: Mux init failed!");
delay(1000);
}
// Select mux channel for sensor initialization
// (All sensors are on channel 7 per sensorconfig.h)
while (!mux_select(0, ADS_MUX_CHANNEL)) {
Serial.println("ERROR: Mux channel select failed!");
delay(1000);
}
delay(5); // Allow mux to stabilize
// Initialize sensors
while (!sensor_dispatcher_init()) {
Serial.println("ERROR: Sensor init failed!");
delay(1000);
}
daq_init();
// Initialize SD writer (attach the sd_buffer the dispatcher pushes into)
if (!sdwriter.init(&sd_buffer, "data.bin")) {
Serial.println("ERROR: SD writer init failed — continuing without SD logging");
}
// Initialize LoRa module and sender
if (!lora_module.begin()) {
Serial.println("WARNING: LoRa module did not respond during init");
} else {
lora_module.configure(LORA_SENDER_ADDRESS, LORA_BAND, LORA_NETWORK_ID);
lora_sender.init(&lora_module, &lora_buffer, LORA_RECEIVER_ADDRESS);
}
// Initialize SolenoidReceive instance (DAQ owns mux control; init stores I2C addr only)
if (!solenoid_receive.init(SOLENOID_I2C_ADDR)) {
Serial.println("ERROR: SolenoidReceive init failed!");
}
Serial.println("DAQ Ready!");
#if ENABLE_PRESSURE_SERIAL
// Serial header for pressure columns (P0..)
for (int i = 0; i < SENSOR_COUNT; ++i) {
Serial.print("P"); Serial.print(i);
if (i < SENSOR_COUNT - 1) Serial.print('\t'); else Serial.println();
}
#endif
}
void loop() {
static uint32_t next_daq = 0;
// Run DAQ at 20Hz (50ms)
if (millis() >= next_daq) {
daq_step();
#if ENABLE_PRESSURE_SERIAL
// --- Serial pressure sensor read (one line per DAQ frame) ---
for (int i = 0; i < SENSOR_COUNT; ++i) {
const SensorDesc &desc = sensor_table[i];
// Ensure MUX is selected for this sensor (DAQ normally does this)
if (desc.mux_channel != NO_MUX) {
if (!mux_select(desc.bus_id, desc.mux_channel)) {
Serial.print("P"); Serial.print(desc.id); Serial.print(": MUX_ERR");
if (i < SENSOR_COUNT - 1) Serial.print('\t'); else Serial.println();
continue;
}
delay(1); // small settle time
}
int32_t processed = 0;
int16_t raw_adc = 0;
if (sensor_read_dispatch(desc, processed, raw_adc)) {
float psi = processed / 100.0f; // stored as psi*100
Serial.print(psi, 2);
} else {
Serial.print("ERR");
}
if (i < SENSOR_COUNT - 1) Serial.print('\t'); else Serial.println();
}
// --- end serial read ---
#endif
// schedule next DAQ step (value in milliseconds)
next_daq += 50;
}
if (!dispatcher_thread_step()) {
Serial.println("ERROR: Dispatcher thread step failed! Overflow detetcted!");
}
// Drive SD writer (drains `sd_buffer` and writes in blocks).
sdwriter.data();
// Attempt to send one pending LoRa frame each loop (if available)
lora_sender.send_next();
}