software:firmware
MakAir Firmware
battery.cpp File Reference

Battery related functions. More...

#include "../includes/battery.h"
#include "Arduino.h"
#include "../includes/alarm_controller.h"
#include "../includes/debug.h"
#include "../includes/parameters.h"

Go to the source code of this file.

Functions

void initBattery ()
 Initialize battery abstraction. More...
 
void updateBatterySample ()
 Handle battery voltage calculation. More...
 
void updateBatteryState (uint32_t p_cycleNumber)
 Updates battery states. More...
 
void batteryLoop (uint32_t p_cycleNumber)
 Handle battery events. More...
 
uint32_t getBatteryLevel ()
 Returns battery level. More...
 
uint32_t getBatteryLevelX10 ()
 Returns battery level x10 for better accuracy. More...
 
uint32_t getBatteryLevelX100 ()
 Returns battery level x100 for better accuracy. More...
 
bool isBatteryVeryLow ()
 Check if battery level is very low. More...
 
bool isBatteryDeepDischarged ()
 Check if battery is deeply discharged. More...
 
bool isMainsConnected ()
 Check if mains are connected. More...
 
bool isMainsAvailable ()
 Check if the cable between power supply and expander input is connected. More...
 

Variables

static uint32_t rawBatterySample [BATTERY_MAX_SAMPLES]
 
static uint32_t batteryCurrentSample = 0
 
static uint32_t batteryTotalSamples = 0
 
static uint32_t rawBatteryMeanVoltage = RAW_VOLTAGE_MAINS
 
static bool isRunningOnBattery = false
 
static bool mainsConnected = false
 
static bool mainsConnectedAvailable = false
 

Detailed Description

Battery related functions.

Author
Makers For Life

Definition in file battery.cpp.

Function Documentation

◆ batteryLoop()

void batteryLoop ( uint32_t  p_cycleNumber)

Handle battery events.

Parameters
p_cycleNumberNumber of cycles since boot
Warning
It must be called in the program loop

Definition at line 117 of file battery.cpp.

117  {
119  updateBatteryState(p_cycleNumber);
120 }
void updateBatteryState(uint32_t p_cycleNumber)
Updates battery states.
Definition: battery.cpp:71
void updateBatterySample()
Handle battery voltage calculation.
Definition: battery.cpp:49

◆ getBatteryLevel()

uint32_t getBatteryLevel ( )

Returns battery level.

Returns
Battery level in volts

Definition at line 123 of file battery.cpp.

static uint32_t rawBatteryMeanVoltage
Definition: battery.cpp:28
#define RAW_BATTERY_MULTIPLIER
The divider between real battery voltage and STM32 input is 8.2K-1k resistors So, the multiplier is 1...
Definition: battery.h:22

◆ getBatteryLevelX10()

uint32_t getBatteryLevelX10 ( )

Returns battery level x10 for better accuracy.

Returns
Battery level in volts x10

Definition at line 126 of file battery.cpp.

126 { return rawBatteryMeanVoltage * (10.0 * RAW_BATTERY_MULTIPLIER); }

◆ getBatteryLevelX100()

uint32_t getBatteryLevelX100 ( )

Returns battery level x100 for better accuracy.

Returns
Battery level in volts x100

Definition at line 129 of file battery.cpp.

129 { return rawBatteryMeanVoltage * (100.0 * RAW_BATTERY_MULTIPLIER); }

◆ initBattery()

void initBattery ( )

Initialize battery abstraction.

Warning
It must be called once to be able to check battery level

Definition at line 33 of file battery.cpp.

33  {
34  // hardware v3 expander is connected to AC ON relay. that remains an optionnal wiring.
35  pinMode(PIN_IN_MAINS_CONNECTED, INPUT_PULLUP);
36  pinMode(PIN_IN_CONNECTION_TO_SUPPLY_OK, INPUT_PULLUP);
37  analogReadResolution(ADC_RESOLUTION_MAKAIR);
38 
39  for (uint8_t i = 0; i < BATTERY_MAX_SAMPLES; i++) {
40  rawBatterySample[i] = 0;
41  }
42 
43  // Running this in setup avoids triggering alarms at startup
44  for (uint8_t i = 0; i < BATTERY_MAX_SAMPLES; i++) {
46  }
47 }
static uint32_t rawBatterySample[BATTERY_MAX_SAMPLES]
Definition: battery.cpp:25
#define BATTERY_MAX_SAMPLES
Number of samples of the moving average.
Definition: battery.h:68
uint16_t i
#define ADC_RESOLUTION_MAKAIR
Definition: parameters.h:317
#define PIN_IN_MAINS_CONNECTED
Definition: parameters.h:242
#define PIN_IN_CONNECTION_TO_SUPPLY_OK
Definition: parameters.h:243

◆ isBatteryDeepDischarged()

bool isBatteryDeepDischarged ( )

Check if battery is deeply discharged.

Definition at line 136 of file battery.cpp.

136  {
138 }
#define RAW_VOLTAGE_ON_BATTERY_STOP_THRESHOLD
Below this value, the machine will stop immediately = 20 => 20 / RAW_BATTERY_MULTIPLIER.
Definition: battery.h:65

◆ isBatteryVeryLow()

bool isBatteryVeryLow ( )

Check if battery level is very low.

Definition at line 132 of file battery.cpp.

132  {
134 }
#define RAW_VOLTAGE_ON_BATTERY_NOT_STARTING_THRESHOLD
Below this value, the machine wont start = 22 => 22 / RAW_BATTERY_MULTIPLIER.
Definition: battery.h:59

◆ isMainsAvailable()

bool isMainsAvailable ( )

Check if the cable between power supply and expander input is connected.

Definition at line 144 of file battery.cpp.

144 { return mainsConnectedAvailable; }
static bool mainsConnectedAvailable
Definition: battery.cpp:31

◆ isMainsConnected()

bool isMainsConnected ( )

Check if mains are connected.

Definition at line 141 of file battery.cpp.

141 { return (!isRunningOnBattery); }
static bool isRunningOnBattery
Definition: battery.cpp:29

◆ updateBatterySample()

void updateBatterySample ( )

Handle battery voltage calculation.

Definition at line 49 of file battery.cpp.

49  {
50  uint16_t rawVout = analogRead(PIN_BATTERY);
51 
52  // Assign sample from Vout
54 
55  // Increment sample
57 
58  // If we reached max samples
61  }
62 
64  for (uint8_t i = 0; i < BATTERY_MAX_SAMPLES; i++) {
66  }
67  // Updates mean voltage
69 }
static uint32_t batteryCurrentSample
Definition: battery.cpp:26
static uint32_t batteryTotalSamples
Definition: battery.cpp:27
#define PIN_BATTERY
Definition: parameters.h:278

◆ updateBatteryState()

void updateBatteryState ( uint32_t  p_cycleNumber)

Updates battery states.

Parameters
p_cycleNumberNumber of cycle since start

Definition at line 71 of file battery.cpp.

71  {
72  // The connector includes a small wire between gnd and PIN_IN_CONNECTION_TO_SUPPLY_OK.
73  // If this is not detected, there may be a end of line connection problem,
74  // and there is a fallback to Vbat value
76  mainsConnected = (LOW == digitalRead(PIN_IN_MAINS_CONNECTED));
77 
84  isRunningOnBattery = true;
89  isRunningOnBattery = false;
90  } else {
91  // This is an hysteresis, so do nothing here
92  }
93 
94  if (!mainsConnected
101  } else {
102  // This is an hysteresis, so do nothing here
103  }
104 
105  if (!mainsConnected
107  alarmController.detectedAlarm(RCM_SW_12, p_cycleNumber,
112  } else {
113  // This is an hysteresis, so do nothing here
114  }
115 }
AlarmController alarmController
Instance of the alarm controller.
#define RCM_SW_11
#define RCM_SW_12
#define RCM_SW_16
static bool mainsConnected
Definition: battery.cpp:30
#define RAW_VOLTAGE_ON_BATTERY_HIGH
RCM_SW_16 Expected voltage in volts when power cord is unplugged = 27 => 27 / RAW_BATTERY_MULTIPLIER.
Definition: battery.h:35
#define RAW_VOLTAGE_ON_BATTERY_LOW
RCM_SW_12 = 22,6 => 22,6 / RAW_BATTERY_MULTIPLIER.
Definition: battery.h:53
#define RAW_VOLTAGE_HYSTERESIS
Hysteresis is used to prevent fast switching when voltage is at the limit of 2 states analogRead(PIN)...
Definition: battery.h:41
#define RAW_VOLTAGE_ON_BATTERY
RCM_SW_11 = 23,2 => 23,2 / RAW_BATTERY_MULTIPLIER.
Definition: battery.h:47
void detectedAlarm(uint8_t p_alarmCode, uint32_t p_cycleNumber, uint32_t p_expected, uint32_t p_measured)
Mark a specific alarm as detected.
void notDetectedAlarm(uint8_t p_alarmCode)
Reset detection of a specific alarm.

Variable Documentation

◆ batteryCurrentSample

uint32_t batteryCurrentSample = 0
static

Definition at line 26 of file battery.cpp.

◆ batteryTotalSamples

uint32_t batteryTotalSamples = 0
static

Definition at line 27 of file battery.cpp.

◆ isRunningOnBattery

bool isRunningOnBattery = false
static

Definition at line 29 of file battery.cpp.

◆ mainsConnected

bool mainsConnected = false
static

Definition at line 30 of file battery.cpp.

◆ mainsConnectedAvailable

bool mainsConnectedAvailable = false
static

Definition at line 31 of file battery.cpp.

◆ rawBatteryMeanVoltage

uint32_t rawBatteryMeanVoltage = RAW_VOLTAGE_MAINS
static

Definition at line 28 of file battery.cpp.

◆ rawBatterySample

uint32_t rawBatterySample[BATTERY_MAX_SAMPLES]
static

Definition at line 25 of file battery.cpp.