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