Avionics
Core avionics package for CURE flight computers
Loading...
Searching...
No Matches
PowerManagement.h
Go to the documentation of this file.
1
#ifndef PowerManagement_H
2
#define PowerManagement_H
3
4
#include "
ArduinoHAL.h
"
5
20
class
BatteryVoltage
{
21
public
:
28
BatteryVoltage
(uint8_t adcPin,
float
factor,
int
numAdcBits,
float
voltageThreshold)
29
: pin(adcPin), factor(factor), numAdcBits(numAdcBits), voltageThreshold(voltageThreshold) {analogReadResolution(numAdcBits);}
30
35
float
readVoltage
() {
36
// Set the pin for reading
37
pinMode(pin, INPUT);
38
uint32_t rawValue = analogRead(pin);
// Should give a value between 0 and 2^numAdcBits - 1
39
float
vRef = 3.3f;
// reference voltage for the ADC on MARTHA 1.4
40
41
// Convert raw ADC value to voltage at the pin, then apply the factor to get battery voltage
42
float
vPin = (
static_cast<
float
>
(rawValue) / (
static_cast<
float
>
(1 << numAdcBits) - 1)) * vRef;
43
float
vBat = vPin * factor;
44
45
return
vBat;
46
}
47
53
bool
isLow
(){
54
return
readVoltage
() < voltageThreshold;
55
}
56
57
private
:
58
uint8_t pin;
59
float
factor;
60
int
numAdcBits;
61
float
voltageThreshold;
62
};
63
64
#endif
// PowerManagement_H
ArduinoHAL.h
BatteryVoltage::readVoltage
float readVoltage()
Sample the ADC and convert to battery voltage.
Definition
PowerManagement.h:35
BatteryVoltage::isLow
bool isLow()
Check whether voltage exceeds a minimal threshold.
Definition
PowerManagement.h:53
BatteryVoltage::BatteryVoltage
BatteryVoltage(uint8_t adcPin, float factor, int numAdcBits, float voltageThreshold)
Definition
PowerManagement.h:28
include
PowerManagement.h
Generated by
1.16.1