RBControl
Library for the RB3201-RBControl board with the ESP32 by RoboticsBrno.
RBControl_battery.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <atomic>
4 
5 #include <esp_adc_cal.h>
6 
7 #include "RBControl_leds.hpp"
8 #include "RBControl_piezo.hpp"
9 
10 namespace rb {
11 
12 class Manager;
13 
17 class Battery {
18  friend class Manager;
19 
20 public:
21  static constexpr uint32_t VOLTAGE_MIN = 3300 * 2;
22  static constexpr uint32_t VOLTAGE_MAX = 4200 * 2;
23  static constexpr uint32_t VOLTAGE_WARNING = 3500 * 2;
24  static constexpr float BATT_DIVIDER = 10.0f / (82.0f + 10.0f);
25 
26  void setFineTuneCoef(float coef);
27  float fineTuneCoef() const;
28 
29  uint32_t raw() const;
30  uint32_t pct() const;
31  uint32_t voltageMv() const;
32 
33  void shutdown();
34 private:
35  Battery(Piezo& piezo, Leds& leds, Adafruit_MCP23017& expander);
36  Battery(const Battery&) = delete;
37  ~Battery();
38 
39  void install(bool disableEmergencyShutdown = false);
40 
41  void updateVoltage();
42  void setWarning(bool on);
43  uint32_t rawToMv(uint32_t rawVal);
44 
45  esp_adc_cal_characteristics_t m_adcChars;
46 
47  std::atomic<uint32_t> m_raw;
48  std::atomic<uint32_t> m_voltageMv;
49  std::atomic<float> m_coef;
50 
51  bool m_warningOn;
52  bool m_emergencyShutdown;
53  uint8_t m_undervoltedCounter;
54  Piezo& m_piezo;
55  Leds& m_leds;
56  Adafruit_MCP23017& m_expander;
57 };
58 
59 };
Controls the expander pins.
Definition: Adafruit_MCP23017.h:24
Contains the battery state and can control the robot's power.
Definition: RBControl_battery.hpp:17
static constexpr uint32_t VOLTAGE_MAX
Maximal battery voltage, in mV.
Definition: RBControl_battery.hpp:22
static constexpr uint32_t VOLTAGE_MIN
Minimal battery voltage, in mV, at which the robot shuts down.
Definition: RBControl_battery.hpp:21
static constexpr uint32_t VOLTAGE_WARNING
The voltage at which alert triggers.
Definition: RBControl_battery.hpp:23
uint32_t voltageMv() const
returns current battery voltage
Definition: RBControl_battery.cpp:84
void setFineTuneCoef(float coef)
Tunes battery measurement to compensate e.g. for voltage divider error. Default 1,...
Definition: RBControl_battery.cpp:56
uint32_t pct() const
returns current battery percentage
Definition: RBControl_battery.cpp:88
uint32_t raw() const
returns the raw value
Definition: RBControl_battery.cpp:80
void shutdown()
shuts the robot down
Definition: RBControl_battery.cpp:69
static constexpr float BATT_DIVIDER
Voltage divider ratio.
Definition: RBControl_battery.hpp:24
float fineTuneCoef() const
Definition: RBControl_battery.cpp:65
Helper class for controlling the LEDs connected to the expander.
Definition: RBControl_leds.hpp:10
The main library class for working with the RBControl board. Call the install() method at the start o...
Definition: RBControl_manager.hpp:49
Helper class for controlling the piezo.
Definition: RBControl_piezo.hpp:10
The base namespace. Contains some logging functions, too.
Definition: half_duplex_uart.cpp:53