22#include <linux/i2c-dev.h>
34template <
typename T> T
clamp(T value, T min_val, T max_val) {
35 return (value < min_val) ? min_val : ((value > max_val) ? max_val : value);
114 speed =
clamp(speed, -100, 100);
115 int pwm_value =
static_cast<int>(std::abs(speed) / 100.0 * 4096);
119 pcontrol->set_motor_pwm(0, pwm_value);
121 pcontrol->set_motor_pwm(2, pwm_value);
122 pcontrol->set_motor_pwm(5, pwm_value);
124 pcontrol->set_motor_pwm(7, pwm_value);
126 }
else if (speed < 0) {
127 pcontrol->set_motor_pwm(0, pwm_value);
128 pcontrol->set_motor_pwm(1, pwm_value);
131 pcontrol->set_motor_pwm(6, pwm_value);
132 pcontrol->set_motor_pwm(7, pwm_value);
135 for (
int channel = 0; channel < 9; ++channel)
136 pcontrol->set_motor_pwm(channel, 0);
157 static_cast<int>((angle /
static_cast<float>(
MAX_ANGLE)) *
159 }
else if (angle > 0) {
161 static_cast<int>((angle /
static_cast<float>(
MAX_ANGLE)) *
T clamp(T value, T min_val, T max_val)
Clamps a value to a given range.
File containing the EngineController class.
File containing the PeripheralController class.
CarDirection
Enum class for the car direction.
const int SERVO_RIGHT_PWM
EngineController()
Default constructor for the EngineController class.
void stop()
Stops the engine.
void setDirection(CarDirection newDirection)
Sets the direction of the car and emits the directionUpdated signal if the direction has changed.
IPeripheralController * pcontrol
std::atomic< int > m_current_speed
std::atomic< bool > m_running
std::atomic< int > m_current_angle
void steeringUpdated(int newAngle)
CarDirection m_currentDirection
const int STEERING_CHANNEL
const int SERVO_CENTER_PWM
void set_speed(int speed)
Sets the speed of the car.
void start()
Starts the engine.
~EngineController()
Destructor for the EngineController class.
void set_steering(int angle)
Sets the steering angle of the car.
void directionUpdated(CarDirection newDirection)
The PeripheralController class.