Skip to content

Commit fac9583

Browse files
committed
♻️ (Autocharge): Update spike using BehaviorKit
1 parent 01f0a45 commit fac9583

2 files changed

Lines changed: 15 additions & 25 deletions

File tree

spikes/lk_auto_charge/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ target_link_libraries(spike_lk_auto_charge
2828
CoreI2C
2929
IMUKit
3030
BLEKit
31+
BehaviorKit
3132
)
3233

3334
target_link_custom_leka_targets(spike_lk_auto_charge)

spikes/lk_auto_charge/main.cpp

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "BLEServiceCommands.h"
1111

1212
#include "BatteryKit.h"
13+
#include "BehaviorKit.h"
1314
#include "CoreBattery.h"
1415
#include "CoreI2C.h"
1516
#include "CoreLSM6DSOX.hpp"
@@ -24,6 +25,7 @@
2425
#include "LogKit.h"
2526
#include "MathUtils.h"
2627
#include "SealStrategy.h"
28+
#include "behaviors/AutochargeSeal.h"
2729

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

103-
auto seal_strategy =
104-
SealStrategy {event_loop, timeout, battery::cells, motors::left::motor, motors::right::motor, imukit};
105-
auto happy_toupie = HappyToupie {event_loop, timeout, battery::cells, motors::left::motor, motors::right::motor};
106-
auto happy_fishy = HappyFishy {event_loop, timeout, battery::cells, motors::left::motor, motors::right::motor};
105+
auto behavior_autocharge_seal = behavior::AutochargeSeal {motors::left::motor, motors::right::motor, imukit};
106+
auto behaviors = std::to_array<interface::Behavior *>({&behavior_autocharge_seal});
107+
auto behavior_kit = BehaviorKit {event_loop};
107108

108-
auto last_strategy = uint8_t {0x00};
109-
110-
void runStrategy(uint8_t id)
111-
{
112-
if (id == 0x01) {
113-
seal_strategy.start();
114-
last_strategy = id;
115-
} else if (id == 0x02) {
116-
happy_toupie.start();
117-
last_strategy = id;
118-
} else if (id == 0x03) {
119-
happy_fishy.start();
120-
last_strategy = id;
121-
} else {
122-
seal_strategy.stop();
123-
happy_toupie.stop();
124-
happy_fishy.stop();
125-
}
126-
}
109+
auto last_strategy = BehaviorID {0x00};
127110

128111
auto main() -> int
129112
{
@@ -140,10 +123,16 @@ auto main() -> int
140123
imukit.init();
141124
imukit.start();
142125

143-
battery::cells.onChargeDidStop([] { runStrategy(last_strategy); });
126+
behavior_kit.registerBehaviors(behaviors);
127+
128+
battery::cells.onChargeDidStart([] { behavior_kit.stop(); });
129+
battery::cells.onChargeDidStop([] { behavior_kit.start(last_strategy); });
144130

145131
blekit.setServices(services);
146-
service_commands.onCommandsReceived([](std::span<uint8_t> _buffer) { runStrategy(_buffer[0]); });
132+
service_commands.onCommandsReceived([](std::span<uint8_t> _buffer) {
133+
behavior_kit.start(_buffer[0]);
134+
last_strategy = _buffer[0];
135+
});
147136
blekit.init();
148137

149138
while (true) {

0 commit comments

Comments
 (0)