STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
sense_binary_cali.cpp
Go to the documentation of this file.
1// #include <ADS126X.h>
2// #include <SPI.h>
3// #include <Arduino.h>
4
5// // #define DRDY_PIN 45
6// // #define DOUT 41
7// // #define DIN 47
8// // #define SCLK
9// // #define CS 40
10
11// //Module 8
12// #define DRDY_PIN 14
13// #define DOUT 41 // MISO
14// #define DIN 5 // MOSI
15// #define SCLK 13
16// #define CS 37
17// #define START 43
18
19// // 1 = ASCII voltages for the Python calibration script
20// // 0 = packed binary records (original behavior)
21// #define TEXT_OUTPUT 0
22// #define PT_NUM_START 0
23// #define NUM_PTS 10
24
25// // --- Buffer settings ---
26// static const size_t PKT_MAX = 1000; // packet size in bytes
27// static const uint32_t FLUSH_MS = 10; // flush interval in ms
28
29// static uint8_t pktA[PKT_MAX];
30// static uint8_t pktB[PKT_MAX];
31// static uint8_t* active = pktA;
32// static uint8_t* standby = pktB;
33// static size_t used = 0;
34// static uint32_t lastFlushMs = 0;
35// inline void flushPacketIfNeeded(bool force);
36
37// ADS126X adc;
38
39// int pos_pin = 0; // AIN0
40// int neg_pin = 0xA; // AINCOM
41
42// #pragma pack(push, 1)
43// struct SampleRecord {
44// uint32_t t_us;
45// uint8_t channel;
46// int32_t volt_reader; // Raw ADC code (signed)
47// float voltage; // Volts
48// uint32_t read_time_us;
49// float samples_per_second;
50// uint32_t sent_us;
51// };
52// #pragma pack(pop)
53
54// void setup() {
55// Serial.begin(115200);
56// SPI.begin(SCLK, DOUT, DIN, CS);
57
58// adc.begin(CS);
59// adc.startADC1();
60// adc.setRate(ADS126X_RATE_38400);
61// adc.enableInternalReference();
62// // adc.bypassPGA(); // enable if you want to bypass the PGA
63// pinMode(DRDY_PIN, INPUT);
64// pinMode(START, OUTPUT);
65// digitalWrite(START, LOW);
66// }
67
68// void loop() {
69// float voltages[NUM_PTS];
70
71// for (uint8_t ch = 0; ch < NUM_PTS; ch++) {
72
73// long raw_wase = adc.readADC1(ch, neg_pin);
74
75// // Wait until DRDY is low
76// for (int j = 0; j < 4; j++) {
77// while (digitalRead(DRDY_PIN) == HIGH) {
78// // wait
79// }
80// }
81
82// uint32_t t_start = micros();
83
84// long raw = adc.readADC1(ch, neg_pin);
85// uint32_t read_time = micros() - t_start;
86
87// // Convert to volts. Signed 32-bit full-scale assumed, Vref = 2.5 V
88// float volts = (float)raw * 2.5f / 2147483648.0f;
89// voltages[ch] = volts;
90
91// if (!TEXT_OUTPUT) {
92// float sps = (read_time > 0) ? (1000000.0f / (float)read_time) : 0.0f;
93
94// SampleRecord rec;
95// rec.t_us = t_start;
96// rec.channel = ch;
97// rec.volt_reader = (int32_t)raw;
98// rec.voltage = volts;
99// rec.read_time_us = read_time;
100// rec.samples_per_second = sps;
101// rec.sent_us = 0;
102
103// // Serial.write((uint8_t *)&rec, sizeof(rec));
104
105// // Buffer packet
106// // if this record won’t fit, flush first
107// if (used + sizeof(rec) > PKT_MAX) {
108// flushPacketIfNeeded(true);
109// }
110
111// // copy record into buffer
112// memcpy(active + used, &rec, sizeof(rec));
113// used += sizeof(rec);
114
115// // flush periodically (time-based)
116// flushPacketIfNeeded(false);
117
118// }
119// }
120
121// if (TEXT_OUTPUT) {
122// for (int i = 0; i < NUM_PTS; i++) {
123// Serial.print(voltages[i], 6);
124// if (i < NUM_PTS - 1) Serial.print(" ");
125// }
126// Serial.print("\r\n"); // CRLF as expected by the Python script
127// }
128// }
129
130// inline void flushPacketIfNeeded(bool force = false) {
131// uint32_t now = millis();
132// if (force || (used > 0 && (now - lastFlushMs >= FLUSH_MS))) {
133// uint32_t send_ts = micros();
134// if (used > 0) {
135// size_t count = used / sizeof(SampleRecord);
136// SampleRecord* recs = reinterpret_cast<SampleRecord*>(active);
137// for (size_t i = 0; i < count; ++i) {
138// recs[i].sent_us = send_ts;
139// }
140// }
141// Serial.write(active, used);
142// Serial.println(); // optional delimiter
143// uint8_t* tmp = active;
144// active = standby;
145// standby = tmp;
146// used = 0;
147// lastFlushMs = now;
148// }
149// }