Skip to content

Commit

Permalink
🔀 Merge branch 'yann/refactor/rc/use-two-timeout' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Jan 13, 2023
2 parents 747cee2 + 583294e commit 9c6e1e0
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 122 deletions.
6 changes: 4 additions & 2 deletions app/os/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,15 +408,17 @@ namespace robot {

namespace internal {

auto sleep_timeout = CoreTimeout {};
auto timeout_state_internal = CoreTimeout {};
auto timeout_state_transition = CoreTimeout {};

auto mcu = CoreMCU {};
auto serialnumberkit = SerialNumberKit {mcu, SerialNumberKit::DEFAULT_CONFIG};

} // namespace internal

auto controller = RobotController {
internal::sleep_timeout,
internal::timeout_state_internal,
internal::timeout_state_transition,
battery::cells,
internal::serialnumberkit,
firmware::kit,
Expand Down
37 changes: 19 additions & 18 deletions libs/RobotKit/include/RobotController.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ class RobotController : public interface::RobotController
public:
sm_t state_machine {static_cast<interface::RobotController &>(*this), logger};

explicit RobotController(interface::Timeout &timeout, interface::Battery &battery, SerialNumberKit &serialnumberkit,
explicit RobotController(interface::Timeout &timeout_state_internal, interface::Timeout &timeout_state_transition,
interface::Battery &battery, SerialNumberKit &serialnumberkit,
interface::FirmwareUpdate &firmware_update, interface::Motor &motor_left,
interface::Motor &motor_right, interface::LED &ears, interface::LED &belt,
interface::LedKit &ledkit, interface::LCD &lcd, interface::VideoKit &videokit,
BehaviorKit &behaviorkit, CommandKit &cmdkit, RFIDKit &rfidkit, ActivityKit &activitykit)
: _timeout(timeout),
: _timeout_state_internal(timeout_state_internal),
_timeout_state_transition(timeout_state_transition),
_battery(battery),
_serialnumberkit(serialnumberkit),
_firmware_update(firmware_update),
Expand Down Expand Up @@ -81,23 +83,23 @@ class RobotController : public interface::RobotController
{
using namespace system::robot::sm;
auto on_sleep_timeout = [this] { raise(event::sleep_timeout_did_end {}); };
_timeout.onTimeout(on_sleep_timeout);
_timeout_state_transition.onTimeout(on_sleep_timeout);

_timeout.start(_sleep_timeout_duration);
_timeout_state_transition.start(_sleep_timeout_duration);
}

void stopSleepTimeout() final { _timeout.stop(); }
void stopSleepTimeout() final { _timeout_state_transition.stop(); }

void startIdleTimeout() final
{
using namespace system::robot::sm;
auto on_idle_timeout = [this] { raise(event::idle_timeout_did_end {}); };
_timeout.onTimeout(on_idle_timeout);
_timeout_state_transition.onTimeout(on_idle_timeout);

_timeout.start(_idle_timeout_duration);
_timeout_state_transition.start(_idle_timeout_duration);
}

void stopIdleTimeout() final { _timeout.stop(); }
void stopIdleTimeout() final { _timeout_state_transition.stop(); }

void startWaitingBehavior() final
{
Expand Down Expand Up @@ -125,14 +127,14 @@ class RobotController : public interface::RobotController
_event_queue.call(&_lcd, &interface::LCD::turnOff);
_event_queue.call(&_ledkit, &interface::LedKit::stop);
};
_timeout.onTimeout(on_sleeping_start_timeout);
_timeout_state_internal.onTimeout(on_sleeping_start_timeout);

_timeout.start(20s);
_timeout_state_internal.start(20s);
}

void stopSleepingBehavior() final
{
_timeout.stop();
_timeout_state_internal.stop();
_behaviorkit.stop();
}

Expand Down Expand Up @@ -171,14 +173,14 @@ class RobotController : public interface::RobotController
_lcd.turnOn();

auto on_charging_start_timeout = [this] { _event_queue.call(&_lcd, &interface::LCD::turnOff); };
_timeout.onTimeout(on_charging_start_timeout);
_timeout_state_internal.onTimeout(on_charging_start_timeout);

_timeout.start(1min);
_timeout_state_internal.start(1min);
}

void stopChargingBehavior() final
{
_timeout.stop();
_timeout_state_internal.stop();
_behaviorkit.stop();
}

Expand Down Expand Up @@ -446,9 +448,6 @@ class RobotController : public interface::RobotController

// Setup callbacks for each State Machine events

auto on_idle_timeout = [this]() { raise(event::idle_timeout_did_end {}); };
_timeout.onTimeout(on_idle_timeout);

auto on_charge_did_start = [this]() { raise(event::charge_did_start {}); };
_battery.onChargeDidStart(on_charge_did_start);

Expand Down Expand Up @@ -498,9 +497,11 @@ class RobotController : public interface::RobotController
private:
system::robot::sm::logger logger {};

interface::Timeout &_timeout_state_internal;

std::chrono::seconds _sleep_timeout_duration {60};
std::chrono::seconds _idle_timeout_duration {600};
interface::Timeout &_timeout;
interface::Timeout &_timeout_state_transition;

const rtos::Kernel::Clock::time_point kSystemStartupTimestamp = rtos::Kernel::Clock::now();

Expand Down
33 changes: 22 additions & 11 deletions libs/RobotKit/tests/RobotController_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class RobotControllerTest : public testing::Test

mock::EventQueue event_queue {};

mock::Timeout timeout {};
mock::Timeout timeout_state_internal {};
mock::Timeout timeout_state_transition {};
mock::Battery battery {};

mock::MCU mock_mcu {};
Expand Down Expand Up @@ -109,10 +110,22 @@ class RobotControllerTest : public testing::Test
stub::EventLoopKit event_loop {};
CommandKit cmdkit {event_loop};

RobotController<bsml::sm<system::robot::StateMachine, bsml::testing>> rc {
timeout, battery, serialnumberkit, firmware_update, mock_motor_left,
mock_motor_right, mock_ears, mock_belt, mock_ledkit, mock_lcd,
mock_videokit, bhvkit, cmdkit, rfidkit, activitykit};
RobotController<bsml::sm<system::robot::StateMachine, bsml::testing>> rc {timeout_state_internal,
timeout_state_transition,
battery,
serialnumberkit,
firmware_update,
mock_motor_left,
mock_motor_right,
mock_ears,
mock_belt,
mock_ledkit,
mock_lcd,
mock_videokit,
bhvkit,
cmdkit,
rfidkit,
activitykit};

ble::GapMock &mbed_mock_gap = ble::gap_mock();
ble::GattServerMock &mbed_mock_gatt = ble::gatt_server_mock();
Expand Down Expand Up @@ -201,8 +214,6 @@ class RobotControllerTest : public testing::Test
EXPECT_CALL(mbed_mock_gap, setAdvertisingPayload).InSequence(on_data_updated_sequence);
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(2).InSequence(on_data_updated_sequence);

EXPECT_CALL(timeout, onTimeout).WillOnce(GetCallback<interface::Timeout::callback_t>(&on_idle_timeout));

EXPECT_CALL(battery, onChargeDidStart).WillOnce(GetCallback<mbed::Callback<void()>>(&on_charge_did_start));

EXPECT_CALL(battery, onChargeDidStop).WillOnce(GetCallback<mbed::Callback<void()>>(&on_charge_did_stop));
Expand Down Expand Up @@ -231,10 +242,10 @@ class RobotControllerTest : public testing::Test
expectedCallsRunLaunchingBehavior();

Sequence on_idle_entry_sequence;
EXPECT_CALL(timeout, onTimeout)
EXPECT_CALL(timeout_state_transition, onTimeout)
.InSequence(on_idle_entry_sequence)
.WillOnce(GetCallback<interface::Timeout::callback_t>(&on_sleep_timeout));
EXPECT_CALL(timeout, start).InSequence(on_idle_entry_sequence);
EXPECT_CALL(timeout_state_transition, start).InSequence(on_idle_entry_sequence);

EXPECT_CALL(mock_videokit, playVideoOnRepeat).InSequence(on_idle_entry_sequence);
EXPECT_CALL(mock_lcd, turnOn).Times(AtLeast(1)).InSequence(on_idle_entry_sequence);
Expand All @@ -257,10 +268,10 @@ class RobotControllerTest : public testing::Test
EXPECT_CALL(mock_videokit, displayImage).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(mock_ledkit, start).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(mock_lcd, turnOn).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(timeout, onTimeout)
EXPECT_CALL(timeout_state_internal, onTimeout)
.InSequence(start_charging_behavior_sequence)
.WillOnce(GetCallback<interface::Timeout::callback_t>(&on_charging_start_timeout));
EXPECT_CALL(timeout, start).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(timeout_state_internal, start).InSequence(start_charging_behavior_sequence);
}

void expectedCallsRunLaunchingBehavior()
Expand Down
12 changes: 4 additions & 8 deletions libs/RobotKit/tests/RobotController_test_registerEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ TEST_F(RobotControllerTest, registerEventsBatteryIsNotCharging)
// TODO: Specify which BLE service and what is expected if necessary
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(2).InSequence(on_data_updated_sequence);

EXPECT_CALL(timeout, onTimeout);

EXPECT_CALL(battery, onChargeDidStart);

EXPECT_CALL(battery, onChargeDidStop);
Expand All @@ -49,8 +47,8 @@ TEST_F(RobotControllerTest, registerEventsBatteryIsNotCharging)
.InSequence(run_launching_behavior_sequence);
EXPECT_CALL(mock_lcd, turnOn).InSequence(run_launching_behavior_sequence);

EXPECT_CALL(timeout, onTimeout);
EXPECT_CALL(timeout, start);
EXPECT_CALL(timeout_state_transition, onTimeout);
EXPECT_CALL(timeout_state_transition, start);

EXPECT_CALL(mock_videokit, playVideoOnRepeat);
EXPECT_CALL(mock_lcd, turnOn);
Expand Down Expand Up @@ -79,8 +77,6 @@ TEST_F(RobotControllerTest, registerEventsBatteryIsCharging)
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(2).InSequence(on_data_updated_sequence);
EXPECT_CALL(mock_videokit, displayImage).InSequence(on_data_updated_sequence);

EXPECT_CALL(timeout, onTimeout);

EXPECT_CALL(battery, onChargeDidStart);

EXPECT_CALL(battery, onChargeDidStop);
Expand Down Expand Up @@ -110,8 +106,8 @@ TEST_F(RobotControllerTest, registerEventsBatteryIsCharging)
EXPECT_CALL(mock_videokit, displayImage).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(mock_ledkit, start).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(mock_lcd, turnOn).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(timeout, onTimeout).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(timeout, start).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(timeout_state_internal, onTimeout).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(timeout_state_internal, start).InSequence(start_charging_behavior_sequence);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ TEST_F(RobotControllerTest, stateAutonomousActivityConnectedEventCommandReceived
EXPECT_CALL(mock_motor_right, stop).InSequence(on_autonomous_activity_exit_sequence);

Sequence on_working_entry_sequence;
EXPECT_CALL(timeout, onTimeout).InSequence(on_working_entry_sequence);
EXPECT_CALL(timeout, start).InSequence(on_working_entry_sequence);
EXPECT_CALL(timeout_state_transition, onTimeout).InSequence(on_working_entry_sequence);
EXPECT_CALL(timeout_state_transition, start).InSequence(on_working_entry_sequence);
EXPECT_CALL(mock_videokit, displayImage).InSequence(on_working_entry_sequence);
EXPECT_CALL(mock_lcd, turnOn).InSequence(on_working_entry_sequence);

Expand Down Expand Up @@ -47,8 +47,8 @@ TEST_F(RobotControllerTest, stateAutonomousActivityEventBleConnection)
EXPECT_CALL(mock_videokit, playVideoOnce).Times(1).InSequence(on_ble_connection_sequence);

Sequence on_working_entry_sequence;
EXPECT_CALL(timeout, onTimeout).InSequence(on_working_entry_sequence);
EXPECT_CALL(timeout, start).InSequence(on_working_entry_sequence);
EXPECT_CALL(timeout_state_transition, onTimeout).InSequence(on_working_entry_sequence);
EXPECT_CALL(timeout_state_transition, start).InSequence(on_working_entry_sequence);
EXPECT_CALL(mock_videokit, displayImage).InSequence(on_working_entry_sequence);
EXPECT_CALL(mock_lcd, turnOn).Times(2).InSequence(on_working_entry_sequence);

Expand All @@ -73,8 +73,8 @@ TEST_F(RobotControllerTest, stateAutonomousActivityEventChargeDidStartGuardIsCha
EXPECT_CALL(mock_videokit, displayImage).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(mock_ledkit, start).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(mock_lcd, turnOn).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(timeout, onTimeout).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(timeout, start).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(timeout_state_internal, onTimeout).InSequence(start_charging_behavior_sequence);
EXPECT_CALL(timeout_state_internal, start).InSequence(start_charging_behavior_sequence);

// TODO: Specify which BLE service and what is expected if necessary
EXPECT_CALL(mbed_mock_gatt, write(_, _, _, _)).Times(AtLeast(1));
Expand Down Expand Up @@ -210,8 +210,8 @@ TEST_F(RobotControllerTest, stateAutonomousActivityDiceRollDetectedDelayOverEven
EXPECT_CALL(mock_motor_right, stop).Times(AtLeast(1));

Sequence on_idle_entry_sequence;
EXPECT_CALL(timeout, onTimeout).InSequence(on_idle_entry_sequence);
EXPECT_CALL(timeout, start).InSequence(on_idle_entry_sequence);
EXPECT_CALL(timeout_state_transition, onTimeout).InSequence(on_idle_entry_sequence);
EXPECT_CALL(timeout_state_transition, start).InSequence(on_idle_entry_sequence);
EXPECT_CALL(mock_videokit, playVideoOnRepeat).InSequence(on_idle_entry_sequence);
EXPECT_CALL(mock_lcd, turnOn).InSequence(on_idle_entry_sequence);

Expand All @@ -233,8 +233,8 @@ TEST_F(RobotControllerTest, stateAutonomousActivityDiceRollDetectedDelayOverEven
EXPECT_CALL(mock_motor_right, stop).Times(AtLeast(1));

Sequence on_working_entry_sequence;
EXPECT_CALL(timeout, onTimeout).InSequence(on_working_entry_sequence);
EXPECT_CALL(timeout, start).InSequence(on_working_entry_sequence);
EXPECT_CALL(timeout_state_transition, onTimeout).InSequence(on_working_entry_sequence);
EXPECT_CALL(timeout_state_transition, start).InSequence(on_working_entry_sequence);
EXPECT_CALL(mock_videokit, displayImage).InSequence(on_working_entry_sequence);

spy_kernel_addElapsedTimeToTickCount(minimal_delay_over);
Expand Down
Loading

0 comments on commit 9c6e1e0

Please sign in to comment.