Skip to content

Yann/feature/commandkit/trigger behaviors #1424

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions app/os/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include "SuperSimon.h"
#include "VideoKit.h"
#include "bootutil/bootutil_public.h"
#include "commands/BehaviorCommand.h"
#include "commands/LedFullCommand.h"
#include "commands/LedRangeCommand.h"
#include "commands/LedSingleCommand.h"
Expand Down Expand Up @@ -294,6 +295,7 @@ namespace command {
auto motors = MotorsCommand {motors::left::motor, motors::right::motor};
auto reinforcer = ReinforcerCommand {reinforcerkit};
auto video = VideoCommand {videokit};
auto behavior = BehaviorCommand {behaviorkit};

} // namespace internal

Expand All @@ -304,6 +306,7 @@ namespace command {
&internal::motors,
&internal::reinforcer,
&internal::video,
&internal::behavior,
});

} // namespace command
Expand Down
3 changes: 0 additions & 3 deletions libs/BehaviorKit/include/BehaviorKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ class BehaviorKit
// nothing do to
}

void spinLeft(float speed);
void spinRight(float speed);

void launching();
void sleeping();
void waiting();
Expand Down
12 changes: 0 additions & 12 deletions libs/BehaviorKit/source/BehaviorKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ namespace leka {

using namespace std::chrono;

void BehaviorKit::spinLeft(float speed)
{
_motor_left.spin(Rotation::clockwise, speed);
_motor_right.spin(Rotation::clockwise, speed);
}

void BehaviorKit::spinRight(float speed)
{
_motor_left.spin(Rotation::counterClockwise, speed);
_motor_right.spin(Rotation::counterClockwise, speed);
}

void BehaviorKit::launching()
{
_videokit.displayImage(fs::home::img::system::robot_misc_splash_screen_large_400);
Expand Down
20 changes: 0 additions & 20 deletions libs/BehaviorKit/tests/BehaviorKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,6 @@ TEST_F(BehaviorKitTest, initialization)
ASSERT_NE(&behaviorkit, nullptr);
}

TEST_F(BehaviorKitTest, spinLeftAnySpeed)
{
auto expected_speed = 0.7;

EXPECT_CALL(mock_motor_left, spin(Rotation::clockwise, expected_speed));
EXPECT_CALL(mock_motor_right, spin(Rotation::clockwise, expected_speed));

behaviorkit.spinLeft(expected_speed);
}

TEST_F(BehaviorKitTest, spinRightAnySpeed)
{
auto expected_speed = 0.3;

EXPECT_CALL(mock_motor_left, spin(Rotation::counterClockwise, expected_speed));
EXPECT_CALL(mock_motor_right, spin(Rotation::counterClockwise, expected_speed));

behaviorkit.spinRight(expected_speed);
}

TEST_F(BehaviorKitTest, launching)
{
EXPECT_CALL(mock_videokit, displayImage);
Expand Down
1 change: 1 addition & 0 deletions libs/CommandKit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ target_link_libraries(CommandKit
CoreLED
CoreMotor
ReinforcerKit
BehaviorKit
EventLoopKit
mbed-os
)
Expand Down
120 changes: 120 additions & 0 deletions libs/CommandKit/include/commands/BehaviorCommand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Leka - LekaOS
// Copyright 2022 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <span>

#include "BehaviorKit.h"
#include "Utils.h"
#include "interface/Command.h"

namespace leka {

struct BehaviorCommand : interface::Command {
explicit BehaviorCommand(BehaviorKit &kit) : _behaviorkit(kit) {}

auto id() -> uint8_t override { return cmd::id; }

auto data() -> uint8_t * override
{
args = {};
return args.data();
}

[[nodiscard]] auto size() const -> std::size_t override { return std::size(args); }

auto execute() -> bool override
{
auto [id, chcksm] = std::tuple_cat(args);

auto expected = [&] { return utils::math::checksum8(std::span {args.data(), args.size() - 1}); };

if (chcksm != expected()) {
return false;
}

switch (id) {
case cmd::behavior::stop:
_behaviorkit.stop();
break;
case cmd::behavior::launching:
_behaviorkit.launching();
break;
case cmd::behavior::sleeping:
_behaviorkit.sleeping();
break;
case cmd::behavior::waiting:
_behaviorkit.waiting();
break;
case cmd::behavior::blink_on_charge:
_behaviorkit.blinkOnCharge();
break;
case cmd::behavior::low_battery:
_behaviorkit.lowBattery();
break;
case cmd::behavior::charging_empty:
_behaviorkit.chargingEmpty();
break;
case cmd::behavior::charging_low:
_behaviorkit.chargingLow();
break;
case cmd::behavior::charging_medium:
_behaviorkit.chargingMedium();
break;
case cmd::behavior::charging_high:
_behaviorkit.chargingHigh();
break;
case cmd::behavior::charging_full:
_behaviorkit.chargingFull();
break;
case cmd::behavior::ble_connection_without_video:
_behaviorkit.bleConnectionWithoutVideo();
break;
case cmd::behavior::ble_connection_with_video:
_behaviorkit.bleConnectionWithVideo();
break;
case cmd::behavior::working:
_behaviorkit.working();
break;
case cmd::behavior::file_exchange:
_behaviorkit.fileExchange();
break;
default:
_behaviorkit.stop();
break;
}

return true;
}

private:
struct cmd {
static constexpr auto id = uint8_t {0x60};
static constexpr auto size = uint8_t {1 + 1}; // id + page + Checksum

struct behavior {
static constexpr auto stop = uint8_t {0x00};
static constexpr auto launching = uint8_t {0x01};
static constexpr auto sleeping = uint8_t {0x02};
static constexpr auto waiting = uint8_t {0x03};
static constexpr auto blink_on_charge = uint8_t {0x04};
static constexpr auto low_battery = uint8_t {0x05};
static constexpr auto charging_empty = uint8_t {0x06};
static constexpr auto charging_low = uint8_t {0x07};
static constexpr auto charging_medium = uint8_t {0x08};
static constexpr auto charging_high = uint8_t {0x09};
static constexpr auto charging_full = uint8_t {0x0A};
static constexpr auto ble_connection_without_video = uint8_t {0x0B};
static constexpr auto ble_connection_with_video = uint8_t {0x0C};
static constexpr auto working = uint8_t {0x0D};
static constexpr auto file_exchange = uint8_t {0x0E};
};
};

std::array<uint8_t, cmd::size> args {};
BehaviorKit &_behaviorkit;
};

} // namespace leka
4 changes: 4 additions & 0 deletions spikes/lk_command_kit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "ReinforcerKit.h"
#include "SDBlockDevice.h"
#include "VideoKit.h"
#include "commands/BehaviorCommand.h"
#include "commands/LedFullCommand.h"
#include "commands/LedRangeCommand.h"
#include "commands/LedSingleCommand.h"
Expand Down Expand Up @@ -148,6 +149,7 @@ auto videokit = VideoKit {internal::event_loop, internal::corevideo};

} // namespace display

auto behaviorkit = BehaviorKit {display::videokit, ledkit, motor::left, motor::right};
auto reinforcerkit = ReinforcerKit {display::videokit, ledkit, motionkit};

namespace command {
Expand All @@ -163,6 +165,7 @@ namespace internal {
auto motors = MotorsCommand {motor::left, motor::right};
auto reinforcer = ReinforcerCommand {reinforcerkit};
auto video = VideoCommand {display::videokit};
auto behavior = BehaviorCommand {behaviorkit};

} // namespace internal

Expand Down Expand Up @@ -214,6 +217,7 @@ auto list = std::to_array<interface::Command *>({
&internal::led_range,
&internal::reinforcer,
&internal::video,
&internal::behavior,
});

} // namespace command
Expand Down