-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚗️ (spike): Autocharge - HappyToupie
An original suggestion from @HPezz Co-Authored-By: Hugo Pezziardi <[email protected]>
- Loading branch information
1 parent
a2dc68d
commit 5ddba79
Showing
4 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Leka - LekaOS | ||
// Copyright 2023 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "HappyToupie.h" | ||
|
||
#include "rtos/ThisThread.h" | ||
|
||
#include "MathUtils.h" | ||
|
||
using namespace leka; | ||
using namespace std::chrono; | ||
|
||
auto HappyToupie::convertToPwmFrom(float angle) const -> float | ||
{ | ||
auto res = utils::math::map(angle, kMinAngleInput, kMaxAngleInput, kMinPwmOutput, kMaxPwmOutput); | ||
return res; | ||
} | ||
|
||
void HappyToupie::start() | ||
{ | ||
auto on_timeout = [this] { stop(); }; | ||
_timeout.onTimeout(on_timeout); | ||
_timeout.start(20s); | ||
|
||
should_stop = false; | ||
|
||
_event_loop.registerCallback([this] { run(); }); | ||
_event_loop.start(); | ||
} | ||
|
||
void HappyToupie::stop() | ||
{ | ||
should_stop = true; | ||
_event_loop.stop(); | ||
rtos::ThisThread::sleep_for(100ms); | ||
stopMotors(); | ||
_timeout.stop(); | ||
} | ||
|
||
void HappyToupie::run() | ||
{ | ||
if (should_stop || _battery.isCharging()) { | ||
stop(); | ||
return; | ||
} | ||
|
||
spinLeft(1.F, 1.F); | ||
|
||
rtos::ThisThread::sleep_for(10ms); | ||
_event_loop.start(); | ||
} | ||
|
||
void HappyToupie::stopMotors() | ||
{ | ||
_motor_right.stop(); | ||
_motor_left.stop(); | ||
} | ||
|
||
void HappyToupie::spinLeft(float left_speed, float right_speed) | ||
{ | ||
_motor_left.spin(Rotation::clockwise, left_speed); | ||
_motor_right.spin(Rotation::clockwise, right_speed); | ||
} | ||
|
||
void HappyToupie::spinRight(float left_speed, float right_speed) | ||
{ | ||
_motor_left.spin(Rotation::counterClockwise, left_speed); | ||
_motor_right.spin(Rotation::counterClockwise, right_speed); | ||
} | ||
|
||
void HappyToupie::moveForward(float speed) | ||
{ | ||
_motor_left.spin(Rotation::counterClockwise, speed); | ||
_motor_right.spin(Rotation::clockwise, speed); | ||
} | ||
|
||
void HappyToupie::moveBackward(float speed) | ||
{ | ||
_motor_left.spin(Rotation::clockwise, speed); | ||
_motor_right.spin(Rotation::counterClockwise, speed); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Leka - LekaOS | ||
// Copyright 2023 APF France handicap | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include "interface/drivers/Battery.h" | ||
#include "interface/drivers/Motor.h" | ||
#include "interface/drivers/Timeout.h" | ||
#include "interface/libs/EventLoop.h" | ||
|
||
namespace leka { | ||
|
||
class HappyToupie | ||
{ | ||
public: | ||
HappyToupie(interface::EventLoop &event_loop, interface::Timeout &timeout, interface::Battery &battery, | ||
interface::Motor &motor_left, interface::Motor &motor_right) | ||
: _event_loop(event_loop), | ||
_timeout(timeout), | ||
_battery(battery), | ||
_motor_left(motor_left), | ||
_motor_right(motor_right) | ||
{ | ||
} | ||
|
||
~HappyToupie() = default; | ||
|
||
void start(); | ||
void stop(); | ||
|
||
private: | ||
[[nodiscard]] auto convertToPwmFrom(float angle) const -> float; | ||
|
||
void run(); | ||
|
||
void stopMotors(); | ||
void spinRight(float left_speed, float right_speed); | ||
void spinLeft(float left_speed, float right_speed); | ||
void moveForward(float speed); | ||
void moveBackward(float speed); | ||
|
||
interface::EventLoop &_event_loop; | ||
interface::Timeout &_timeout; | ||
|
||
interface::Battery &_battery; | ||
interface::Motor &_motor_left; | ||
interface::Motor &_motor_right; | ||
|
||
const float kMinAngleInput = 0.F; | ||
const float kMaxAngleInput = 30.F; | ||
const float kMinPwmOutput = 0.15F; // Min to move robot | ||
const float kMaxPwmOutput = 0.70F; | ||
|
||
const float kRollTolerance = 3.F; // in degrees | ||
const float kPitchTolerance = 3.F; // in degrees | ||
|
||
bool should_stop = true; | ||
}; | ||
|
||
} // namespace leka |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters