From e2d6a18dbd4892b5534bbf7a14d32b177bcaad35 Mon Sep 17 00:00:00 2001 From: Yann Locatelli Date: Sat, 19 Mar 2022 12:56:50 +0100 Subject: [PATCH 1/2] :sparkles: (rc): Add system reset on low battery --- libs/RobotKit/include/RobotController.h | 3 +++ libs/RobotKit/tests/RobotController_test.cpp | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/libs/RobotKit/include/RobotController.h b/libs/RobotKit/include/RobotController.h index 10fe9d4c05..83ffb09b5f 100644 --- a/libs/RobotKit/include/RobotController.h +++ b/libs/RobotKit/include/RobotController.h @@ -158,6 +158,9 @@ class RobotController : public interface::RobotController _battery_kit.onDataUpdated([](uint8_t level) { _service_battery.setBatteryLevel(level); }); + auto on_low_battery = [this] { system_reset(); }; + _battery_kit.onLowBattery(on_low_battery); + _battery_kit.startEventHandler(); // Setup callbacks for each State Machine events diff --git a/libs/RobotKit/tests/RobotController_test.cpp b/libs/RobotKit/tests/RobotController_test.cpp index 2b882dbd27..d3b225e64e 100644 --- a/libs/RobotKit/tests/RobotController_test.cpp +++ b/libs/RobotKit/tests/RobotController_test.cpp @@ -58,7 +58,7 @@ class RobotControllerTest : public testing::Test rc.initializeComponents(); - EXPECT_CALL(battery, level).Times(1); + EXPECT_CALL(battery, level).Times(AnyNumber()); EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(AnyNumber()); EXPECT_CALL(sleep_timeout, onTimeout).WillOnce(GetCallback(&on_sleep_timeout)); EXPECT_CALL(battery, onChargeDidStart).WillOnce(GetCallback>(&on_charge_did_start)); @@ -178,7 +178,7 @@ TEST_F(RobotControllerTest, stateSetupEventSetupCompleteGuardIsChargingFalse) rc.state_machine.set_current_states(lksm::state::setup); auto expected_level = 0x2A; - EXPECT_CALL(battery, level).WillOnce(Return(expected_level)); + EXPECT_CALL(battery, level).WillRepeatedly(Return(expected_level)); EXPECT_CALL(mbed_mock_gatt, write(_, sameValue(expected_level), _, _)).Times(1); EXPECT_CALL(sleep_timeout, onTimeout).Times(1); @@ -200,7 +200,7 @@ TEST_F(RobotControllerTest, stateSetupEventSetupCompleteGuardIsChargingTrue) rc.state_machine.set_current_states(lksm::state::setup); auto expected_level = 0x2A; - EXPECT_CALL(battery, level).WillOnce(Return(expected_level)); + EXPECT_CALL(battery, level).WillRepeatedly(Return(expected_level)); EXPECT_CALL(mbed_mock_gatt, write(_, sameValue(expected_level), _, _)).Times(1); EXPECT_CALL(sleep_timeout, onTimeout).Times(1); From 506dad8c34e30fb3df4cc3d5ec3e7cdd3cb2ce05 Mon Sep 17 00:00:00 2001 From: Yann Locatelli Date: Sat, 19 Mar 2022 12:58:24 +0100 Subject: [PATCH 2/2] :children_crossing: (rc): Add behavior when battery is low Increase battery level triggering the callback --- libs/BatteryKit/source/BatteryKit.cpp | 2 +- libs/BehaviorKit/include/BehaviorKit.h | 2 ++ libs/BehaviorKit/source/BehaviorKit.cpp | 5 +++++ libs/BehaviorKit/tests/BehaviorKit_test.cpp | 5 +++++ libs/RobotKit/include/RobotController.h | 9 ++++++++- 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/libs/BatteryKit/source/BatteryKit.cpp b/libs/BatteryKit/source/BatteryKit.cpp index 548e932cd4..d08b7b104b 100644 --- a/libs/BatteryKit/source/BatteryKit.cpp +++ b/libs/BatteryKit/source/BatteryKit.cpp @@ -10,7 +10,7 @@ using namespace std::chrono_literals; void BatteryKit::startEventHandler() { auto on_tick = [this] { - if (_on_low_battery && level() == 0) { + if (_on_low_battery && level() <= 5) { _on_low_battery(); } diff --git a/libs/BehaviorKit/include/BehaviorKit.h b/libs/BehaviorKit/include/BehaviorKit.h index fa986ef8fb..2514eb5ded 100644 --- a/libs/BehaviorKit/include/BehaviorKit.h +++ b/libs/BehaviorKit/include/BehaviorKit.h @@ -23,6 +23,8 @@ class BehaviorKit void sleeping(); void waiting(); + void lowBattery(); + void chargingZero(); void chargingRed(); void chargingOrange(); diff --git a/libs/BehaviorKit/source/BehaviorKit.cpp b/libs/BehaviorKit/source/BehaviorKit.cpp index d22cca8a14..5614f77f76 100644 --- a/libs/BehaviorKit/source/BehaviorKit.cpp +++ b/libs/BehaviorKit/source/BehaviorKit.cpp @@ -36,6 +36,11 @@ void BehaviorKit::waiting() "/fs/videos/2022_02_14-animation-face-state-waiting-looking-top-right-to-left-without-eyebrows.avi"); } +void BehaviorKit::lowBattery() +{ + _videokit.displayImage("/fs/images/loading.jpg"); +} + void BehaviorKit::chargingZero() { _ledkit.start(&LedKit::animation::charging_red); diff --git a/libs/BehaviorKit/tests/BehaviorKit_test.cpp b/libs/BehaviorKit/tests/BehaviorKit_test.cpp index bc59356e8e..90be899ae7 100644 --- a/libs/BehaviorKit/tests/BehaviorKit_test.cpp +++ b/libs/BehaviorKit/tests/BehaviorKit_test.cpp @@ -86,6 +86,11 @@ TEST_F(BehaviorKitTest, spinRight) behaviorkit.spinBlink(); } +TEST_F(BehaviorKitTest, lowBattery) +{ + behaviorkit.lowBattery(); +} + TEST_F(BehaviorKitTest, stop) { static constexpr auto speed = 0.5; diff --git a/libs/RobotKit/include/RobotController.h b/libs/RobotKit/include/RobotController.h index 83ffb09b5f..44c765e887 100644 --- a/libs/RobotKit/include/RobotController.h +++ b/libs/RobotKit/include/RobotController.h @@ -158,7 +158,14 @@ class RobotController : public interface::RobotController _battery_kit.onDataUpdated([](uint8_t level) { _service_battery.setBatteryLevel(level); }); - auto on_low_battery = [this] { system_reset(); }; + auto on_low_battery = [this] { + _event_queue.call(&_behaviorkit, &BehaviorKit::lowBattery); + // TODO: Add turn on screen + + if (_battery.level() == 0) { + system_reset(); + } + }; _battery_kit.onLowBattery(on_low_battery); _battery_kit.startEventHandler();