battery.cpp
Go to the documentation of this file.
1 
10 /*****************************************************************************
11 ** Includes
12 *****************************************************************************/
13 
14 #include "../../include/kobuki_driver/modules/battery.hpp"
15 
16 /*****************************************************************************
17 ** Namespaces
18 *****************************************************************************/
19 
20 namespace kobuki {
21 
22 /*****************************************************************************
23 ** Statics
24 *****************************************************************************/
25 
26 double Battery::capacity = 16.5;
27 double Battery::low = 14.0;
28 double Battery::dangerous = 13.2;
29 
30 /*****************************************************************************
31 ** Implementation
32 *****************************************************************************/
39 Battery::Battery (const uint8_t &new_voltage, const uint8_t &charger_flag) :
40  voltage(static_cast<double>(new_voltage)/10.0)
41 {
42  uint8_t state = (charger_flag & CoreSensors::Flags::BatteryStateMask);
44  charging_state = Charging;
45  } else if ( state == CoreSensors::Flags::Charged ) {
46  charging_state = Charged;
47  capacity = voltage;
48  } else {
49  charging_state = Discharging;
50  }
51 
52  if (charging_state == Discharging) {
53  charging_source = None;
54  } else if ( charger_flag & CoreSensors::Flags::AdapterType ) {
55  charging_source = Adapter;
56  } else {
57  charging_source = Dock;
58  }
59 };
60 
62  if ( charging_state == Charged ) { return Maximum; }
63  if ( voltage > low ) { return Healthy; }
64  if ( voltage > dangerous ) { return Low; }
65  return Dangerous;
66 }
67 
68 float Battery::percent() const {
69  // convert battery voltage to percent: 100% -> capacity / 5% -> dangerous
70  float percent = ((95*(voltage - dangerous)) / (capacity - dangerous)) + 5;
71  return std::max(std::min(percent, 100.0f), 0.0f);
72 }
73 
74 } // namespace kobuki
kobuki::Battery::Level
Level
Definition: battery.hpp:61
kobuki::Battery::Maximum
@ Maximum
Definition: battery.hpp:65
kobuki::CoreSensors::Flags::Charging
static const uint8_t Charging
Definition: core_sensors.hpp:96
kobuki::Battery::Battery
Battery(const uint8_t &new_voltage, const uint8_t &charger_flag)
Definition: battery.cpp:43
kobuki::Battery::low
static double low
Definition: battery.hpp:79
kobuki
Definition: command.hpp:31
kobuki::Battery::percent
float percent() const
Definition: battery.cpp:72
kobuki::Battery::Charged
@ Charged
Definition: battery.hpp:69
kobuki::Battery::Dangerous
@ Dangerous
Definition: battery.hpp:62
kobuki::Battery::capacity
static double capacity
Definition: battery.hpp:78
f
void f(int i)
kobuki::Battery::voltage
double voltage
Definition: battery.hpp:81
kobuki::Battery::dangerous
static double dangerous
Definition: battery.hpp:80
kobuki::CoreSensors::Flags::Charged
static const uint8_t Charged
Definition: core_sensors.hpp:95
kobuki::Battery::level
Level level() const
Definition: battery.cpp:65
kobuki::CoreSensors::Flags::BatteryStateMask
static const uint8_t BatteryStateMask
Definition: core_sensors.hpp:93
kobuki::CoreSensors::Flags::AdapterType
static const uint8_t AdapterType
Definition: core_sensors.hpp:91
kobuki::Battery::Low
@ Low
Definition: battery.hpp:63
kobuki::Battery::Healthy
@ Healthy
Definition: battery.hpp:64
kobuki::Battery::charging_state
State charging_state
Definition: battery.hpp:82


kobuki_driver
Author(s): Daniel Stonier , Younghun Ju , Jorge Santos Simon
autogenerated on Wed Mar 2 2022 00:26:14