Skip to content

Commit

Permalink
♻️ (Autocharge): Update spike using BehaviorKit
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Jan 23, 2024
1 parent 3ced8bd commit 461374b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
1 change: 1 addition & 0 deletions spikes/lk_auto_charge/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ target_link_libraries(spike_lk_auto_charge
CoreI2C
IMUKit
BLEKit
BehaviorKit
)

target_link_custom_leka_targets(spike_lk_auto_charge)
39 changes: 14 additions & 25 deletions spikes/lk_auto_charge/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "BLEServiceCommands.h"

#include "BatteryKit.h"
#include "BehaviorKit.h"
#include "CoreBattery.h"
#include "CoreI2C.h"
#include "CoreLSM6DSOX.hpp"
Expand All @@ -24,6 +25,7 @@
#include "LogKit.h"
#include "MathUtils.h"
#include "SealStrategy.h"
#include "behaviors/AutochargeSeal.h"

using namespace std::chrono;
using namespace leka;
Expand Down Expand Up @@ -100,30 +102,11 @@ auto service_commands = BLEServiceCommands {};
auto services = std::to_array<interface::BLEService *>({&service_commands});
auto blekit = BLEKit {};

auto seal_strategy =
SealStrategy {event_loop, timeout, battery::cells, motors::left::motor, motors::right::motor, imukit};
auto happy_toupie = HappyToupie {event_loop, timeout, battery::cells, motors::left::motor, motors::right::motor};
auto happy_fishy = HappyFishy {event_loop, timeout, battery::cells, motors::left::motor, motors::right::motor};
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 last_strategy = uint8_t {0x00};

void runStrategy(uint8_t id)
{
if (id == 0x01) {
seal_strategy.start();
last_strategy = id;
} else if (id == 0x02) {
happy_toupie.start();
last_strategy = id;
} else if (id == 0x03) {
happy_fishy.start();
last_strategy = id;
} else {
seal_strategy.stop();
happy_toupie.stop();
happy_fishy.stop();
}
}
auto last_strategy = BehaviorID {0x00};

auto main() -> int
{
Expand All @@ -140,10 +123,16 @@ auto main() -> int
imukit.init();
imukit.start();

battery::cells.onChargeDidStop([] { runStrategy(last_strategy); });
behavior_kit.registerBehaviors(behaviors);

battery::cells.onChargeDidStart([] { behavior_kit.stop(); });
battery::cells.onChargeDidStop([] { behavior_kit.start(last_strategy); });

blekit.setServices(services);
service_commands.onCommandsReceived([](std::span<uint8_t> _buffer) { runStrategy(_buffer[0]); });
service_commands.onCommandsReceived([](std::span<uint8_t> _buffer) {
behavior_kit.start(_buffer[0]);
last_strategy = _buffer[0];
});
blekit.init();

while (true) {
Expand Down

0 comments on commit 461374b

Please sign in to comment.