Skip to content

Commit

Permalink
Timeout in BehaviorKit
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Dec 18, 2023
1 parent 83880ac commit 5558904
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
2 changes: 2 additions & 0 deletions include/interface/libs/BehaviorKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <chrono>
#include <span>

#include "interface/Behavior.h"
Expand All @@ -17,6 +18,7 @@ class BehaviorKit
virtual ~BehaviorKit() = default;

virtual void registerBehaviors(std::span<interface::Behavior *> behaviors) = 0;
virtual void setTimeoutDuration(std::chrono::seconds duration) = 0;
virtual void start(interface::Behavior *behavior) = 0;
virtual void start(BehaviorID id) = 0;
virtual void stop() = 0;
Expand Down
6 changes: 5 additions & 1 deletion libs/BehaviorKit/include/BehaviorKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <span>

#include "interface/Behavior.h"
#include "interface/drivers/Timeout.h"
#include "interface/libs/BehaviorKit.h"
#include "interface/libs/EventLoop.h"
#include "internal/BehaviorID.h"
Expand All @@ -16,9 +17,10 @@ namespace leka {
class BehaviorKit : public interface::BehaviorKit
{
public:
explicit BehaviorKit(interface::EventLoop &event_loop);
explicit BehaviorKit(interface::EventLoop &event_loop, interface::Timeout &timeout);

void registerBehaviors(std::span<interface::Behavior *> behaviors) final;
void setTimeoutDuration(std::chrono::seconds duration) final;

void start(interface::Behavior *behavior) final;
void start(BehaviorID id) final;
Expand All @@ -28,6 +30,8 @@ class BehaviorKit : public interface::BehaviorKit
void run();

interface::EventLoop &_event_loop;
interface::Timeout &_timeout;
std::chrono::seconds _timeout_duration {20};

std::span<interface::Behavior *> _behaviors {};
interface::Behavior *_behavior = nullptr;
Expand Down
13 changes: 12 additions & 1 deletion libs/BehaviorKit/source/BehaviorKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@

using namespace leka;

BehaviorKit::BehaviorKit(interface::EventLoop &event_loop) : _event_loop(event_loop)
BehaviorKit::BehaviorKit(interface::EventLoop &event_loop, interface::Timeout &timeout)
: _event_loop(event_loop), _timeout(timeout)
{
_event_loop.registerCallback([this] { run(); });
_timeout.onTimeout([this] { stop(); });
}

void BehaviorKit::registerBehaviors(std::span<interface::Behavior *> behaviors)
{
_behaviors = behaviors;
}

void BehaviorKit::setTimeoutDuration(std::chrono::seconds duration)
{
_timeout_duration = duration;
}

void BehaviorKit::start(interface::Behavior *behavior)
{
stop();
Expand All @@ -28,6 +35,9 @@ void BehaviorKit::start(interface::Behavior *behavior)
}

_event_loop.start();
if (_timeout_duration != std::chrono::seconds {0}) {
_timeout.start(_timeout_duration);
}
}

void BehaviorKit::start(BehaviorID id)
Expand All @@ -54,4 +64,5 @@ void BehaviorKit::stop()
if (_behavior != nullptr) {
_behavior->stop();
}
_timeout.stop();
}
17 changes: 15 additions & 2 deletions libs/BehaviorKit/tests/BehaviorKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "mocks/leka/Behavior.h"
#include "mocks/leka/Timeout.h"
#include "stubs/leka/EventLoopKit.h"

using namespace leka;
Expand All @@ -22,7 +23,8 @@ class BehaviorKitTest : public ::testing::Test
// void TearDown() override {}

stub::EventLoopKit stub_event_loop {};
BehaviorKit behaviorkit {stub_event_loop};
mock::Timeout mock_timeout {};
BehaviorKit behaviorkit {stub_event_loop, mock_timeout};

mock::Behavior mock_behavior_a {};
mock::Behavior mock_behavior_b {};
Expand All @@ -38,14 +40,17 @@ TEST_F(BehaviorKitTest, startFirstBehavior)
auto behaviors = std::to_array<interface::Behavior *>({&mock_behavior_a});
behaviorkit.registerBehaviors(behaviors);

EXPECT_CALL(mock_timeout, stop);
EXPECT_CALL(mock_timeout, start);
EXPECT_CALL(mock_behavior_a, run);

behaviorkit.start(&mock_behavior_a);
}

TEST_F(BehaviorKitTest, startBehaviorNullPtr)
{
// nothing expected
EXPECT_CALL(mock_timeout, stop);
EXPECT_CALL(mock_timeout, start).Times(0);

behaviorkit.start(nullptr);
}
Expand All @@ -61,6 +66,8 @@ TEST_F(BehaviorKitTest, startFirstBehaviorID)
auto behavior_a_id = BehaviorID {1};
auto behavior_b_id = BehaviorID {2};

EXPECT_CALL(mock_timeout, stop);
EXPECT_CALL(mock_timeout, start);
EXPECT_CALL(mock_behavior_a, id).WillRepeatedly(Return(behavior_a_id));
EXPECT_CALL(mock_behavior_b, id).WillRepeatedly(Return(behavior_b_id));
EXPECT_CALL(mock_behavior_b, run);
Expand All @@ -79,6 +86,8 @@ TEST_F(BehaviorKitTest, startBehaviorIDNotRegistered)
auto behavior_a_id = BehaviorID {1};
auto behavior_b_id = BehaviorID {2};

EXPECT_CALL(mock_timeout, stop);
EXPECT_CALL(mock_timeout, start).Times(0);
EXPECT_CALL(mock_behavior_a, id).WillRepeatedly(Return(behavior_a_id));
EXPECT_CALL(mock_behavior_b, id).WillRepeatedly(Return(behavior_b_id));

Expand All @@ -93,10 +102,14 @@ TEST_F(BehaviorKitTest, startAnyBehavior)
});
behaviorkit.registerBehaviors(behaviors);

EXPECT_CALL(mock_timeout, stop);
EXPECT_CALL(mock_timeout, start);
EXPECT_CALL(mock_behavior_a, run);

behaviorkit.start(&mock_behavior_a);

EXPECT_CALL(mock_timeout, stop);
EXPECT_CALL(mock_timeout, start);
EXPECT_CALL(mock_behavior_a, stop);
EXPECT_CALL(mock_behavior_b, run);

Expand Down
2 changes: 1 addition & 1 deletion spikes/lk_auto_charge/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ auto blekit = BLEKit {};

auto behavior_autocharge_seal = behavior::AutochargeSeal {motors::left::motor, motors::right::motor, imukit};
auto behaviors = std::to_array<interface::Behavior *>({&behavior_autocharge_seal});
auto behavior_kit = BehaviorKit {event_loop};
auto behavior_kit = BehaviorKit {event_loop, timeout};

auto last_strategy = BehaviorID {0x00};

Expand Down

0 comments on commit 5558904

Please sign in to comment.