software:firmware
MakAir Firmware
battery.cpp
Go to the documentation of this file.
1 
8 #pragma once
9 
10 // INCLUDES ===================================================================
11 
12 // Associated header
13 #include "../includes/battery.h"
14 
15 // Externals
16 #include "Arduino.h"
17 
18 // Internal
19 #include "../includes/alarm_controller.h"
20 #include "../includes/debug.h"
21 #include "../includes/parameters.h"
22 
23 // PROGRAM =====================================================================
24 
25 static uint32_t rawBatterySample[BATTERY_MAX_SAMPLES]; // Array to store battery voltage samples
26 static uint32_t batteryCurrentSample = 0; // Current battery sample index
27 static uint32_t batteryTotalSamples = 0; // Battery total samples
28 static uint32_t rawBatteryMeanVoltage = RAW_VOLTAGE_MAINS; // Mean battery voltage in volts
29 static bool isRunningOnBattery = false;
30 static bool mainsConnected = false;
31 static bool mainsConnectedAvailable = false;
32 
33 void initBattery() {
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 }
48 
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 }
70 
71 void updateBatteryState(uint32_t p_cycleNumber) {
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 }
116 
117 void batteryLoop(uint32_t p_cycleNumber) {
119  updateBatteryState(p_cycleNumber);
120 }
121 
122 // cppcheck-suppress unusedFunction
124 
125 // cppcheck-suppress unusedFunction
127 
128 // cppcheck-suppress unusedFunction
130 
131 // cppcheck-suppress unusedFunction
134 }
135 
138 }
139 
140 // cppcheck-suppress unusedFunction
142 
143 // cppcheck-suppress unusedFunction
AlarmController alarmController
Instance of the alarm controller.
#define RCM_SW_11
#define RCM_SW_12
#define RCM_SW_16
uint32_t getBatteryLevelX100()
Returns battery level x100 for better accuracy.
Definition: battery.cpp:129
uint32_t getBatteryLevelX10()
Returns battery level x10 for better accuracy.
Definition: battery.cpp:126
void updateBatteryState(uint32_t p_cycleNumber)
Updates battery states.
Definition: battery.cpp:71
static bool isRunningOnBattery
Definition: battery.cpp:29
bool isBatteryDeepDischarged()
Check if battery is deeply discharged.
Definition: battery.cpp:136
static uint32_t batteryCurrentSample
Definition: battery.cpp:26
uint32_t getBatteryLevel()
Returns battery level.
Definition: battery.cpp:123
static uint32_t rawBatteryMeanVoltage
Definition: battery.cpp:28
static uint32_t batteryTotalSamples
Definition: battery.cpp:27
void updateBatterySample()
Handle battery voltage calculation.
Definition: battery.cpp:49
bool isMainsAvailable()
Check if the cable between power supply and expander input is connected.
Definition: battery.cpp:144
bool isBatteryVeryLow()
Check if battery level is very low.
Definition: battery.cpp:132
bool isMainsConnected()
Check if mains are connected.
Definition: battery.cpp:141
static bool mainsConnected
Definition: battery.cpp:30
static uint32_t rawBatterySample[BATTERY_MAX_SAMPLES]
Definition: battery.cpp:25
static bool mainsConnectedAvailable
Definition: battery.cpp:31
void initBattery()
Initialize battery abstraction.
Definition: battery.cpp:33
void batteryLoop(uint32_t p_cycleNumber)
Handle battery events.
Definition: battery.cpp:117
#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 BATTERY_MAX_SAMPLES
Number of samples of the moving average.
Definition: battery.h:68
#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
#define RAW_VOLTAGE_ON_BATTERY_NOT_STARTING_THRESHOLD
Below this value, the machine wont start = 22 => 22 / RAW_BATTERY_MULTIPLIER.
Definition: battery.h:59
#define RAW_VOLTAGE_ON_BATTERY_LOW
RCM_SW_12 = 22,6 => 22,6 / RAW_BATTERY_MULTIPLIER.
Definition: battery.h:53
#define RAW_VOLTAGE_MAINS
Expected voltage in volts when power cord is plugged 27,4 V => 27,4 / RAW_BATTERY_MULTIPLIER.
Definition: battery.h:28
#define RAW_VOLTAGE_ON_BATTERY_STOP_THRESHOLD
Below this value, the machine will stop immediately = 20 => 20 / RAW_BATTERY_MULTIPLIER.
Definition: battery.h:65
#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.
uint16_t i
#define ADC_RESOLUTION_MAKAIR
Definition: parameters.h:317
#define PIN_IN_MAINS_CONNECTED
Definition: parameters.h:242
#define PIN_BATTERY
Definition: parameters.h:278
#define PIN_IN_CONNECTION_TO_SUPPLY_OK
Definition: parameters.h:243