Hotwheels-Cluster 1.2
Creation of Cluster APP for SEA:ME project.
 
Loading...
Searching...
No Matches
EngineController.cpp
Go to the documentation of this file.
1
15
16#include "EngineController.hpp"
18#include <QDebug>
19#include <atomic>
20#include <cmath>
21#include <fcntl.h>
22#include <linux/i2c-dev.h>
23#include <sys/ioctl.h>
24#include <unistd.h>
25
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);
36}
37
42
50EngineController::EngineController(int servo_addr, int motor_addr,
51 QObject *parent)
52 : QObject(parent), m_running(false), m_current_speed(0),
54 pcontrol = new PeripheralController(servo_addr, motor_addr);
55
56 pcontrol->init_servo();
57 pcontrol->init_motors();
58}
59
69
76
83 m_running = false;
84 set_speed(0);
85 set_steering(0);
86}
87
95 if (newDirection != this->m_currentDirection) {
96 emit this->directionUpdated(newDirection);
97 this->m_currentDirection = newDirection;
98 }
99}
100
111
113
114 speed = clamp(speed, -100, 100);
115 int pwm_value = static_cast<int>(std::abs(speed) / 100.0 * 4096);
116
117 if (speed >
118 0) { // Forward (but actually backward because joysticks are reversed)
119 pcontrol->set_motor_pwm(0, pwm_value);
120 pcontrol->set_motor_pwm(1, 0);
121 pcontrol->set_motor_pwm(2, pwm_value);
122 pcontrol->set_motor_pwm(5, pwm_value);
123 pcontrol->set_motor_pwm(6, 0);
124 pcontrol->set_motor_pwm(7, pwm_value);
126 } else if (speed < 0) { // Backwards
127 pcontrol->set_motor_pwm(0, pwm_value);
128 pcontrol->set_motor_pwm(1, pwm_value);
129 pcontrol->set_motor_pwm(2, 0);
130 pcontrol->set_motor_pwm(5, 0);
131 pcontrol->set_motor_pwm(6, pwm_value);
132 pcontrol->set_motor_pwm(7, pwm_value);
134 } else { // Stop
135 for (int channel = 0; channel < 9; ++channel)
136 pcontrol->set_motor_pwm(channel, 0);
138 }
139 m_current_speed = speed;
140}
141
153 angle = clamp(angle, -MAX_ANGLE, MAX_ANGLE);
154 int pwm = 0;
155 if (angle < 0) {
156 pwm = SERVO_CENTER_PWM +
157 static_cast<int>((angle / static_cast<float>(MAX_ANGLE)) *
159 } else if (angle > 0) {
160 pwm = SERVO_CENTER_PWM +
161 static_cast<int>((angle / static_cast<float>(MAX_ANGLE)) *
163 } else {
164 pwm = SERVO_CENTER_PWM;
165 }
166
167 pcontrol->set_servo_pwm(STEERING_CHANNEL, 0, pwm);
168 m_current_angle = angle;
169 emit this->steeringUpdated(angle);
170}
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.
Definition enums.hpp:30
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
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.