-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
140 additions
and
21 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
54 changes: 52 additions & 2 deletions
54
app/raspberry_pi/src/car/system/movement/wheels/FrontWheel.hpp
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 |
---|---|---|
@@ -1,6 +1,56 @@ | ||
#include <algorithm> | ||
|
||
#include <PCA9685.h> | ||
#include <spdlog/spdlog.h> | ||
|
||
namespace car::system::movement::wheels | ||
{ | ||
class FrontWheel { | ||
class FrontWheel { | ||
private: | ||
/** | ||
* Following method clamps the x to in_min and in_max. | ||
* Afterwards, it puts the result of that into the range of out_min and out_max | ||
*/ | ||
static int map(int x, int in_min, int in_max, int out_min, int out_max) | ||
{ | ||
return ((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min); | ||
} | ||
|
||
static constexpr int MIN_PULSE_WIDTH = 900; | ||
static constexpr int MAX_PULSE_WIDTH = 2100; | ||
static constexpr int FREQUENCY = 50; | ||
|
||
public: | ||
FrontWheel(std::shared_ptr<PCA9685> pwm, int channel) : pwm(pwm), channel(channel) { | ||
} | ||
|
||
// Some of the code was from: https://github.com/chaoticmachinery/pca9685 | ||
int getAnalogAngle() const | ||
{ | ||
float pulse_wide = map(this->angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH); | ||
int analog_angle = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096); | ||
return analog_angle; | ||
} | ||
|
||
int getAngle() const | ||
{ | ||
return this->angle; | ||
} | ||
|
||
// Some of the code was from: https://github.com/chaoticmachinery/pca9685 | ||
int setAngle(const int& angle) | ||
{ | ||
this->angle = std::clamp(angle, 0, 180); | ||
int analog_angle = getAnalogAngle(); | ||
this->pwm->setPWM(channel, 0, analog_angle); | ||
spdlog::info("Channel: {} \tAngle: {} \tAnalog Angle: {}", this->channel, angle, analog_angle); | ||
return (0); | ||
} | ||
|
||
private: | ||
const std::shared_ptr<PCA9685> pwm; | ||
const int channel; | ||
|
||
}; | ||
int angle; | ||
}; | ||
} // namespace car::system::movement::wheels |
87 changes: 85 additions & 2 deletions
87
app/raspberry_pi/src/car/system/movement/wheels/RearWheel.hpp
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 |
---|---|---|
@@ -1,6 +1,89 @@ | ||
#include <memory> | ||
|
||
#include <spdlog/spdlog.h> | ||
|
||
#include <PCA9865.h> | ||
#include <TB6612.hpp> | ||
|
||
// Made with the help of ChatGPT | ||
|
||
namespace car::system::movement::wheels | ||
{ | ||
class RearWheel { | ||
class RearWheel { | ||
private: | ||
static constexpr int Motor_A = 17; | ||
static constexpr int Motor_B = 27; | ||
static constexpr int PWM_A = 4; | ||
static constexpr int PWM_B = 5; | ||
public: | ||
RearWheel(std::shared_ptr<PCA9865> pwm, std::unique_ptr<TB6612> motor) : | ||
pwm(pwm), | ||
motor(motor) | ||
{ | ||
this->forward_A = true; | ||
|
||
this->speed = 0; | ||
} | ||
|
||
void forward() | ||
{ | ||
this->motor->forward(); | ||
} | ||
|
||
void backward() | ||
{ | ||
this->motor->backward(); | ||
} | ||
|
||
void stop() | ||
{ | ||
this->motor->stop(); | ||
} | ||
|
||
int getSpeed() const | ||
{ | ||
return this->speed; | ||
} | ||
|
||
void setSpeed(const int& speed) | ||
{ | ||
this->speed = std::clamp(speed, 0, 100); | ||
const int pulse_wide = (this->speed / 100.0f) * 4095; | ||
this->pwm->setPWM(PWM_A, 0, pulse_wide); | ||
} | ||
|
||
void ready() | ||
{ | ||
this->motor->setOffset(this->forward_A); | ||
this->stop(); | ||
} | ||
|
||
void calibration() | ||
{ | ||
this->setSpeed(50); | ||
this->forward(); | ||
this->cali_forward = this->forward_A; | ||
} | ||
|
||
void caliLeft() | ||
{ | ||
this->cali_forward = (1 + this->cali_forward) & 1; | ||
this->motor->setOffset(this->cali_forward); | ||
this->forward(); | ||
} | ||
|
||
void caliOK() | ||
{ | ||
this->forward_A = this->cali_forward; | ||
this->stop(); | ||
} | ||
|
||
private: | ||
std::shared_ptr<PCA9685> pwm; | ||
std::unique_ptr<TB6612> motor; | ||
|
||
}; | ||
int forward_A; | ||
int cali_forward; | ||
int speed; | ||
}; | ||
} // namespace car::system::movement::wheels |
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