A driver for analog battery monitors. More...
#include <battery_monitor.h>
Public Member Functions | |
bool | has_current_sense () const |
Checks if current sense is available. As there isn't hardware to detect this, this simply checks if the current multiplier is non-zero. More... | |
bool | has_voltage_sense () const |
Checks if voltage sense is available. As there isn't hardware to detect this, this simply checks if the voltage multiplier is non-zero. More... | |
void | init (const BatteryMonitorHardwareStruct &def, AnalogDigitalConverter *adc, float voltage_multiplier=0, float current_multiplier=0) |
Initialize the battery monitor. More... | |
float | read_current () const |
Read the current battery current. More... | |
float | read_voltage () const |
Read the current battery voltage. More... | |
void | set_current_multiplier (double multiplier) |
Sets the current multiplier for the battery monitor. More... | |
void | set_voltage_multiplier (double multiplier) |
Sets the voltage multiplier for the battery monitor. More... | |
Private Attributes | |
float | current_multiplier_ {0} |
AnalogPin | current_pin_ |
float | voltage_multiplier_ {0} |
AnalogPin | voltage_pin_ |
A driver for analog battery monitors.
These battery monitors provide an analog voltage proportional to the battery voltage and/or current. This class handles setting up pins and unit conversions. This class does not initialize the ADC.
Definition at line 44 of file battery_monitor.h.
bool BatteryMonitor::has_current_sense | ( | ) | const |
Checks if current sense is available. As there isn't hardware to detect this, this simply checks if the current multiplier is non-zero.
Definition at line 68 of file battery_monitor.cpp.
bool BatteryMonitor::has_voltage_sense | ( | ) | const |
Checks if voltage sense is available. As there isn't hardware to detect this, this simply checks if the voltage multiplier is non-zero.
Definition at line 63 of file battery_monitor.cpp.
void BatteryMonitor::init | ( | const BatteryMonitorHardwareStruct & | def, |
AnalogDigitalConverter * | adc, | ||
float | voltage_multiplier = 0 , |
||
float | current_multiplier = 0 |
||
) |
Initialize the battery monitor.
Does not initialize the ADC. The ADC must be initialized before calling.
def | The hardware definition struct for the battery monitor, mostly pin info |
adc | The ADC used for the monitor. Must be initialized already |
voltage_multiplier | The voltage multiplier. Set to 0 for no voltage sense. See set_voltage_multiplier. |
current_multiplier | The current multiplier. Set to 0 for no current sense. See set_current_multiplier |
Definition at line 34 of file battery_monitor.cpp.
float BatteryMonitor::read_current | ( | ) | const |
Read the current battery current.
Definition at line 48 of file battery_monitor.cpp.
float BatteryMonitor::read_voltage | ( | ) | const |
Read the current battery voltage.
Definition at line 44 of file battery_monitor.cpp.
void BatteryMonitor::set_current_multiplier | ( | double | multiplier | ) |
Sets the current multiplier for the battery monitor.
Analog battery monitors which support current measurement generate a voltage proportional to the current through them. This voltage is read by the ADC, and then multiplied by the current multiplier to measure the current. I.e BatteryCurrent = ADCVoltage * CurrentMultiplier. This number can be obtained from the spec sheet for the battery monitor. This value is in amps per volt.
multiplier |
Definition at line 58 of file battery_monitor.cpp.
void BatteryMonitor::set_voltage_multiplier | ( | double | multiplier | ) |
Sets the voltage multiplier for the battery monitor.
Because the battery voltage is usually much too high for the ADC to read, analog battery monitors use a voltage divider or similar to reduce the voltage to a reasonable level. The voltage read by the ADC is multiplied by the voltage multiplier to get the actual reading. I.e BatteryVoltage = ADCVoltage * VoltageMultiplier. This number can be obtained from the spec sheet for the battery monitor, or through simple math for DIY monitors. This value is in volts per volt (i.e. unitless).
multiplier | The new voltage multiplier |
Definition at line 53 of file battery_monitor.cpp.
|
private |
Definition at line 111 of file battery_monitor.h.
|
private |
Definition at line 109 of file battery_monitor.h.
|
private |
Definition at line 110 of file battery_monitor.h.
|
private |
Definition at line 108 of file battery_monitor.h.