Files
smart-serow/arduino/main/voltage.cpp
Mikkeli Matlock f1ed809c71 arduino: TSV telemetry protocol with mock RPM/gear
- null-terminated TSV frame: V_bat, IMU (9 fields), RPM, gear
- mock RPM ramps 800-8000, gear derived from RPM bands
- voltage calibration offset
- PROTOCOL.md documents wire format

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 17:00:14 +09:00

29 lines
824 B
C++

#include "voltage.h"
// Pin definitions
static const int PIN_VBAT = A0;
// Voltage divider constants
// Divider: 100k upper (to Vin), 47k lower (to GND)
// Vout = Vin * (47k / (100k + 47k)) = Vin * 0.3197
// At 12V: ADC sees 3.84V | At 14.4V: ADC sees 4.60V
static const float DIVIDER_RATIO = 47.0 / (100.0 + 47.0); // ~0.3197
static const float ADC_REF = 5.0;
static const int ADC_MAX = 1023;
static const float OFFSET = 0.2; // calib
void voltage_init() {
// analogRead doesn't need explicit pinMode, but here for future config
// e.g., could switch to internal 1.1V reference for different range
}
int voltage_read_raw() {
return analogRead(PIN_VBAT);
}
float voltage_read() {
int raw = voltage_read_raw();
float vDivider = (raw / (float)ADC_MAX) * ADC_REF;
return vDivider / DIVIDER_RATIO + OFFSET;
}