Skip to content

Commit

Permalink
✨ (MotionKit): Add on_rotation_ended_callback to rotate function
Browse files Browse the repository at this point in the history
  • Loading branch information
HPezz committed Dec 8, 2022
1 parent c8b1a9b commit 4abb309
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion libs/MotionKit/include/MotionKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class MotionKit

void init();

void rotate(uint8_t number_of_rotations, Rotation direction);
void rotate(uint8_t number_of_rotations, Rotation direction,
const std::function<void()> &on_rotation_ended_callback = {});
void startStabilisation();

void stop();
Expand All @@ -39,6 +40,7 @@ class MotionKit
PID _pid;

uint8_t _rotations_to_execute = 0;
std::function<void()> _on_rotation_ended_callback {};

bool _target_not_reached = false;
bool _stabilisation_requested = false;
Expand Down
10 changes: 9 additions & 1 deletion libs/MotionKit/source/MotionKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ void MotionKit::stop()
_rotate_x_turns_requested = false;
}

void MotionKit::rotate(uint8_t number_of_rotations, Rotation direction)
void MotionKit::rotate(uint8_t number_of_rotations, Rotation direction,
const std::function<void()> &on_rotation_ended_callback)
{
stop();

Expand All @@ -43,6 +44,8 @@ void MotionKit::rotate(uint8_t number_of_rotations, Rotation direction)
_motor_right.spin(direction, kPwmMaxValue);

_event_loop.start();

_on_rotation_ended_callback = on_rotation_ended_callback;
}

void MotionKit::startStabilisation()
Expand Down Expand Up @@ -93,6 +96,11 @@ void MotionKit::run()

rtos::ThisThread::sleep_for(70ms);
}

if (_on_rotation_ended_callback != nullptr) {
_on_rotation_ended_callback();
}

_imukit.stop();
}

Expand Down
2 changes: 1 addition & 1 deletion spikes/lk_motion_kit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void onMagicCardAvailable(const MagicCard &card)
{
switch (card.getId()) {
case (MagicCard::number_1.getId()):
motionkit.rotate(1, Rotation::counterClockwise);
motionkit.rotate(1, Rotation::counterClockwise, [&] { log_debug("Callback end of rotation"); });
break;
case (MagicCard::number_2.getId()):
motionkit.rotate(2, Rotation::clockwise);
Expand Down

0 comments on commit 4abb309

Please sign in to comment.