Skip to content

Commit

Permalink
✨ (MotionKit): Add startStabilization method
Browse files Browse the repository at this point in the history
  • Loading branch information
HPezz committed Mar 6, 2023
1 parent 3524708 commit 4aa661a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
4 changes: 4 additions & 0 deletions libs/MotionKit/include/MotionKit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <interface/drivers/Motor.h>

#include "RotationControl.hpp"
#include "StabilizationControl.hpp"
#include "interface/drivers/Timeout.h"
#include "interface/libs/IMUKit.hpp"

Expand All @@ -23,11 +24,13 @@ class MotionKit

void startYawRotation(float degrees, Rotation direction,
const std::function<void()> &on_rotation_ended_callback = {});
void startStabilization();

void stop();

private:
void processAngleForRotation(const EulerAngles &angles, Rotation direction);
void processAngleForStabilization(const EulerAngles &angles);

void setMotorsSpeedAndDirection(float speed, Rotation direction);

Expand All @@ -37,6 +40,7 @@ class MotionKit
interface::Timeout &_timeout;

RotationControl _rotation_control;
StabilizationControl _stabilization_control;
std::function<void()> _on_rotation_ended_callback {};

bool _target_not_reached = false;
Expand Down
21 changes: 21 additions & 0 deletions libs/MotionKit/source/MotionKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ void MotionKit::startYawRotation(float degrees, Rotation direction,
_on_rotation_ended_callback = on_rotation_ended_callback;
}

void MotionKit::startStabilization()
{
stop();

auto starting_angle = _imukit.getEulerAngles();
_stabilization_control.setTarget(starting_angle);

auto on_euler_angles_rdy_callback = [this](const EulerAngles &euler_angles) {
processAngleForStabilization(euler_angles);
};

_imukit.onEulerAnglesReady(on_euler_angles_rdy_callback);
}

// LCOV_EXCL_START - Dynamic behavior, involving motors and time.
void MotionKit::processAngleForRotation(const EulerAngles &angles, Rotation direction)
{
Expand All @@ -68,6 +82,13 @@ void MotionKit::processAngleForRotation(const EulerAngles &angles, Rotation dire
}
}

void MotionKit::processAngleForStabilization(const EulerAngles &angles)
{
auto [speed, rotation] = _stabilization_control.processStabilizationAngle(angles);

setMotorsSpeedAndDirection(speed, rotation);
}

void MotionKit::setMotorsSpeedAndDirection(float speed, Rotation direction)
{
if (speed == 0.F) {
Expand Down
40 changes: 40 additions & 0 deletions libs/MotionKit/tests/MotionKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,43 @@ TEST_F(MotionKitTest, rotateAndTimeOutOver)

on_timeout_callback();
}

TEST_F(MotionKitTest, startStabilization)
{
const EulerAngles angles_quarter_left {0.0F, 0.0F, 90.F};

EXPECT_CALL(mock_timeout, stop).Times(1);
EXPECT_CALL(mock_motor_left, stop).Times(1);
EXPECT_CALL(mock_motor_right, stop).Times(1);

EXPECT_CALL(mock_imukit, getEulerAngles).Times(1);

EXPECT_CALL(mock_motor_left, spin).Times(AtLeast(1));
EXPECT_CALL(mock_motor_right, spin).Times(AtLeast(1));

motion.startStabilization();
mock_imukit.call_angles_ready_callback(angles_quarter_left);
}

TEST_F(MotionKitTest, startStabilizationAndStop)
{
const EulerAngles angles_quarter_left {0.0F, 0.0F, 90.F};

EXPECT_CALL(mock_timeout, stop).Times(1);
EXPECT_CALL(mock_motor_left, stop).Times(1);
EXPECT_CALL(mock_motor_right, stop).Times(1);

EXPECT_CALL(mock_imukit, getEulerAngles).Times(1);

EXPECT_CALL(mock_motor_left, spin).Times(AtLeast(1));
EXPECT_CALL(mock_motor_right, spin).Times(AtLeast(1));

motion.startStabilization();
mock_imukit.call_angles_ready_callback(angles_quarter_left);

EXPECT_CALL(mock_timeout, stop).Times(1);
EXPECT_CALL(mock_motor_left, stop).Times(1);
EXPECT_CALL(mock_motor_right, stop).Times(1);

motion.stop();
}
8 changes: 8 additions & 0 deletions spikes/lk_motion_kit/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ void onMagicCardAvailable(const MagicCard &card)
case (MagicCard::number_7.getId()):
motionkit.startYawRotation(7, Rotation::counterClockwise);
break;
case (MagicCard::number_8.getId()):
motionkit.startStabilization();
rtos::ThisThread::sleep_for(10s);
motionkit.stop();
break;
case (MagicCard::number_9.getId()):
motionkit.startStabilization();
break;
case (MagicCard::number_10.getId()):
motionkit.stop();
break;
Expand Down

0 comments on commit 4aa661a

Please sign in to comment.