Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (battery): Get battery level #435

Merged
merged 6 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/bootloader/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ static auto battery = leka::CoreBattery {PinName::BATTERY_VOLTAGE, charge_sta

auto main() -> int
{
while (battery.getVoltage() < CoreBattery::Capacity::empty) {
while (battery.voltage() < CoreBattery::Capacity::empty) {
rtos::ThisThread::sleep_for(10s);
}

auto address = default_address;

if (battery.isCharging() && battery.getVoltage() > CoreBattery::Capacity::quarter) {
if (battery.isCharging() && battery.voltage() > CoreBattery::Capacity::quarter) {
// Initialize mbedtls crypto for use by MCUboot
mbedtls_platform_context unused_ctx;
if (auto ret = mbedtls_platform_setup(&unused_ctx); ret != 0) {
Expand Down
11 changes: 7 additions & 4 deletions drivers/CoreBattery/include/CoreBattery.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Leka - LekaOS
// Copyright 2021 APF France handicap
// Copyright 2021-2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#ifndef _LEKA_OS_DRIVER_CORE_BATTERY_H_
Expand All @@ -8,17 +8,20 @@
#include "drivers/AnalogIn.h"
#include "drivers/interfaces/InterfaceDigitalIn.h"

#include "interface/drivers/Battery.h"

namespace leka {

class CoreBattery
class CoreBattery : public interface::Battery
{
public:
explicit CoreBattery(PinName voltage_pin, mbed::interface::DigitalIn &charge_status_input)
: _voltage_pin {mbed::AnalogIn(voltage_pin, analog_voltage_reference)},
_charge_status_input(charge_status_input) {};

auto getVoltage() -> float;
auto isCharging() -> bool;
auto voltage() -> float final;
auto level() -> uint8_t final;
auto isCharging() -> bool final;

struct Capacity {
static constexpr auto full = float {12.00};
Expand Down
27 changes: 26 additions & 1 deletion drivers/CoreBattery/source/CoreBattery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

#include "CoreBattery.h"

#include "MathUtils.h"

namespace leka {

auto CoreBattery::getVoltage() -> float
auto CoreBattery::voltage() -> float
{
auto raw_average = getAverageVoltage();
auto voltage = convertToRealVoltage(raw_average);
Expand Down Expand Up @@ -36,6 +38,29 @@ auto CoreBattery::convertToRealVoltage(float value) const -> float
PolynomialCoefficient::degree_2 * value * value;
}

auto CoreBattery::level() -> uint8_t
{
using namespace utils::math;

auto level = uint8_t {0};

if (auto value = voltage(); value < Capacity::empty) {
level = 0;
} else if (value < Capacity::quarter) {
level = map(value, Capacity::empty, Capacity::quarter, uint8_t {0}, uint8_t {25});
} else if (value < Capacity::half) {
level = 25 + map(value, Capacity::quarter, Capacity::half, uint8_t {0}, uint8_t {25});
} else if (value < Capacity::three_quarter) {
level = 50 + map(value, Capacity::half, Capacity::three_quarter, uint8_t {0}, uint8_t {25});
} else if (value < Capacity::full) {
level = 75 + map(value, Capacity::three_quarter, Capacity::full, uint8_t {0}, uint8_t {25});
} else {
level = 100;
}

return level;
}

auto CoreBattery::isCharging() -> bool
{
return _charge_status_input.read() == 1;
Expand Down
58 changes: 35 additions & 23 deletions drivers/CoreBattery/tests/CoreBattery_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Leka - LekaOS
// Copyright 2021 APF rance handicap
// Copyright 2021-2022 APF rance handicap
// SPDX-License-Identifier: Apache-2.0

#include "CoreBattery.h"
Expand Down Expand Up @@ -30,68 +30,80 @@ TEST_F(CoreBatteryTest, initialization)
ASSERT_NE(&battery, nullptr);
}

TEST_F(CoreBatteryTest, getVoltageAboveFull)
TEST_F(CoreBatteryTest, voltageAboveFull)
{
auto AnalogIn_voltage_value = float {2.208};
auto expected_battery_voltage = float {12.6};
auto expected_battery_level = uint8_t {100};
spy_AnalogIn_setVoltageValue(AnalogIn_voltage_value);

ASSERT_NEAR(battery.getVoltage(), expected_battery_voltage, 0.1);
ASSERT_GT(battery.getVoltage(), CoreBattery::Capacity::full);
ASSERT_NEAR(battery.voltage(), expected_battery_voltage, 0.1);
ASSERT_GT(battery.voltage(), CoreBattery::Capacity::full);
ASSERT_EQ(battery.level(), expected_battery_level);
}

TEST_F(CoreBatteryTest, getVoltageBetweenThreeQuarterAndFull)
TEST_F(CoreBatteryTest, voltageBetweenThreeQuarterAndFull)
{
auto AnalogIn_voltage_value = float {2.166};
auto expected_battery_voltage = float {11.8};
auto expected_battery_level = uint8_t {80};
spy_AnalogIn_setVoltageValue(AnalogIn_voltage_value);

ASSERT_NEAR(battery.getVoltage(), expected_battery_voltage, 0.1);
ASSERT_LT(battery.getVoltage(), CoreBattery::Capacity::full);
ASSERT_GT(battery.getVoltage(), CoreBattery::Capacity::three_quarter);
ASSERT_NEAR(battery.voltage(), expected_battery_voltage, 0.1);
ASSERT_LT(battery.voltage(), CoreBattery::Capacity::full);
ASSERT_GT(battery.voltage(), CoreBattery::Capacity::three_quarter);
ASSERT_NEAR(battery.level(), expected_battery_level, 5);
}

TEST_F(CoreBatteryTest, getVoltageBetweenHalfAndThreeQuarter)
TEST_F(CoreBatteryTest, voltageBetweenHalfAndThreeQuarter)
{
auto AnalogIn_voltage_value = float {2.144};
auto expected_battery_voltage = float {11.4};
auto expected_battery_level = uint8_t {65};
spy_AnalogIn_setVoltageValue(AnalogIn_voltage_value);

ASSERT_NEAR(battery.getVoltage(), expected_battery_voltage, 0.1);
ASSERT_LT(battery.getVoltage(), CoreBattery::Capacity::three_quarter);
ASSERT_GT(battery.getVoltage(), CoreBattery::Capacity::half);
ASSERT_NEAR(battery.voltage(), expected_battery_voltage, 0.1);
ASSERT_LT(battery.voltage(), CoreBattery::Capacity::three_quarter);
ASSERT_GT(battery.voltage(), CoreBattery::Capacity::half);
ASSERT_NEAR(battery.level(), expected_battery_level, 5);
}

TEST_F(CoreBatteryTest, getVoltageBetweenQuarterAndHalf)
TEST_F(CoreBatteryTest, voltageBetweenQuarterAndHalf)
{
auto AnalogIn_voltage_value = float {2.108};
auto expected_battery_voltage = float {10.8};
auto expected_battery_level = uint8_t {42};
spy_AnalogIn_setVoltageValue(AnalogIn_voltage_value);

ASSERT_NEAR(battery.getVoltage(), expected_battery_voltage, 0.1);
ASSERT_LT(battery.getVoltage(), CoreBattery::Capacity::half);
ASSERT_GT(battery.getVoltage(), CoreBattery::Capacity::quarter);
ASSERT_NEAR(battery.voltage(), expected_battery_voltage, 0.1);
ASSERT_LT(battery.voltage(), CoreBattery::Capacity::half);
ASSERT_GT(battery.voltage(), CoreBattery::Capacity::quarter);
ASSERT_NEAR(battery.level(), expected_battery_level, 5);
}

TEST_F(CoreBatteryTest, getVoltageBetweenEmptyAndQuarter)
TEST_F(CoreBatteryTest, voltageBetweenEmptyAndQuarter)
{
auto AnalogIn_voltage_value = float {2.033};
auto expected_battery_voltage = float {9.7};
auto expected_battery_level = uint8_t {15};
spy_AnalogIn_setVoltageValue(AnalogIn_voltage_value);

ASSERT_NEAR(battery.getVoltage(), expected_battery_voltage, 0.1);
ASSERT_LT(battery.getVoltage(), CoreBattery::Capacity::quarter);
ASSERT_GT(battery.getVoltage(), CoreBattery::Capacity::empty);
ASSERT_NEAR(battery.voltage(), expected_battery_voltage, 0.1);
ASSERT_LT(battery.voltage(), CoreBattery::Capacity::quarter);
ASSERT_GT(battery.voltage(), CoreBattery::Capacity::empty);
ASSERT_NEAR(battery.level(), expected_battery_level, 5);
}

TEST_F(CoreBatteryTest, getVoltageBelowEmpty)
TEST_F(CoreBatteryTest, voltageBelowEmpty)
{
auto AnalogIn_voltage_value = float {1.82};
auto expected_battery_voltage = float {7.5};
auto expected_battery_level = uint8_t {0};
spy_AnalogIn_setVoltageValue(AnalogIn_voltage_value);

ASSERT_NEAR(battery.getVoltage(), expected_battery_voltage, 0.1);
ASSERT_LT(battery.getVoltage(), CoreBattery::Capacity::empty);
ASSERT_NEAR(battery.voltage(), expected_battery_voltage, 0.1);
ASSERT_LT(battery.voltage(), CoreBattery::Capacity::empty);
ASSERT_EQ(battery.level(), expected_battery_level);
}

TEST_F(CoreBatteryTest, isCharging)
Expand Down
24 changes: 24 additions & 0 deletions include/interface/drivers/Battery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#ifndef _LEKA_OS_DRIVER_INTERFACE_BATTERY_H_
#define _LEKA_OS_DRIVER_INTERFACE_BATTERY_H_

#include <cstdint>

namespace leka::interface {

class Battery
{
public:
virtual ~Battery() = default;

virtual auto voltage() -> float = 0;
virtual auto level() -> uint8_t = 0;
virtual auto isCharging() -> bool = 0;
};

} // namespace leka::interface

#endif //_LEKA_OS_DRIVER_INTERFACE_BATTERY_H_
6 changes: 3 additions & 3 deletions spikes/lk_sensors_battery/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Leka - LekaOS
// Copyright 2021 APF France handicap
// Copyright 2021-2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include "drivers/BufferedSerial.h"
Expand Down Expand Up @@ -27,9 +27,9 @@ auto main() -> int

while (true) {
if (corebattery.isCharging()) {
log_info("Battery at %.2fV and in charge.", corebattery.getVoltage());
log_info("Battery at %.2fV (%d%%) and in charge.", corebattery.voltage(), corebattery.level());
} else {
log_info("Battery at %.2fV.", corebattery.getVoltage());
log_info("Battery at %.2fV (%d%%).", corebattery.voltage(), corebattery.level());
}
rtos::ThisThread::sleep_for(1s);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/mocks/mocks/leka/Battery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#ifndef _LEKA_OS_BATTERY_MOCK_H_
#define _LEKA_OS_BATTERY_MOCK_H_

#include "gmock/gmock.h"
#include "interface/drivers/Battery.h"

namespace leka::mock {

class Battery : public interface::Battery
{
public:
MOCK_METHOD(float, getVoltage, (), (override));
MOCK_METHOD(uint8_t, getLevel, (), (override));
MOCK_METHOD(bool, isCharging, (), (override));
};

} // namespace leka::mock

#endif // _LEKA_OS_BATTERY_MOCK_H_