Hotwheels-Cluster 1.2
Creation of Cluster APP for SEA:ME project.
 
Loading...
Searching...
No Matches
ControlsManager.cpp
Go to the documentation of this file.
1
17
18#include "ControlsManager.hpp"
19#include <fcntl.h>
20#include <sys/mman.h>
21#include <unistd.h>
22#include <QDebug>
23
35ControlsManager::ControlsManager(int argc, char **argv, QObject *parent)
36 : QObject(parent), m_engineController(0x40, 0x60, this),
38 m_clientObject(nullptr), m_carDataObject(nullptr),
40 m_carDataThread(nullptr), m_clientThread(nullptr),
42{
43
44 // Initialize the joystick controller with callbacks
46 [this](int steering)
47 {
49 {
50 m_engineController.set_steering(steering);
51 }
52 },
53 [this](int speed)
54 {
56 {
57 m_engineController.set_speed(speed);
58 }
59 });
60
61 if (!m_manualController->init())
62 {
63 qDebug() << "Failed to initialize joystick controller.";
64 return;
65 }
66
67 // Start the joystick controller in its own thread
68 m_manualControllerThread = new QThread(this);
70
71 connect(m_manualControllerThread, &QThread::started, m_manualController,
74 m_manualControllerThread, &QThread::quit);
75
77
78 // **Server Middleware Thread**
79
80 // Run thread to start the server
82
83 // Initialize m_carDataObject before creating the thread
84 m_carDataThread = QThread::create([this, argc, argv]()
85 {
86 while (m_threadRunning) {
87 m_carDataObject->runServer(argc, argv);
88 } });
89 m_carDataThread->start();
90
91 // **Client Middleware Interface Thread**
93 m_clientThread = QThread::create([this, argc, argv]()
94 { m_clientObject->runClient(argc, argv); });
95 m_clientThread->start();
96
97 // **Process Monitoring Thread**
98 m_processMonitorThread = QThread::create([this]()
99 {
100 QString targetProcessName = "HotWheels-app"; // Change this to actual process name
101
102 while (m_threadRunning) {
103 if (!isProcessRunning(targetProcessName)) {
106 //qDebug() << "Cluster is not running.";
107 }
108 QThread::sleep(1); // Check every 1 second
109 } });
110 m_processMonitorThread->start();
111
112 // **Joystick Control Thread**
113 m_joystickControlThread = QThread::create([this]()
114 {
115 while (m_threadRunning) {
117 QThread::msleep(1000); // Adjust delay as needed
118 } });
120}
121
122
131
133{
134 // Stop the client thread safely
135 if (m_clientThread)
136 {
137 m_clientObject->setRunning(false);
138 m_clientThread->quit();
139 m_clientThread->wait();
140 delete m_clientThread;
141 }
142
143 // Stop the shared memory thread safely
144 if (m_carDataThread)
145 {
146 m_threadRunning = false;
147 m_carDataThread->quit();
148 m_carDataThread->wait();
149 delete m_carDataThread;
150 }
151
152 // Stop the process monitoring thread safely
154 {
155 m_threadRunning = false;
159 }
160
161 // Stop the controller thread safely
163 {
164 m_manualController->requestStop();
168 }
169
170 // Stop the joystick control thread safely
172 {
176 }
177
178 delete m_carDataObject;
179 delete m_clientObject;
180 delete m_manualController;
181}
182
189bool ControlsManager::isProcessRunning(const QString &processName)
190{
191 QProcess process;
192 process.start("pgrep", QStringList() << processName);
193 process.waitForFinished();
194
195 return !process.readAllStandardOutput().isEmpty();
196}
197
204{
205 bool joystickData = m_clientObject->getJoystickValue();
206 if (joystickData)
207 {
209 }
210 else
211 {
213 }
214}
215
222{
223 if (m_currentMode == mode)
224 return;
225
226 m_currentMode = mode;
227}
DrivingMode
Enum class for the driving mode.
Definition enums.hpp:24
Class for the client thread.
void readJoystickEnable()
Reads joystick enable status.
DrivingMode m_currentMode
QThread * m_processMonitorThread
Data::CarDataI * m_carDataObject
QThread * m_joystickControlThread
bool isProcessRunning(const QString &processName)
Check if a process is running.
JoysticksController * m_manualController
void setMode(DrivingMode mode)
Sets the driving mode.
~ControlsManager()
Destructor for the ControlsManager class.
QThread * m_manualControllerThread
ControlsManager(int argc, char **argv, QObject *parent=nullptr)
Constructs a ControlsManager object.
ClientThread * m_clientObject
std::atomic< bool > m_threadRunning
EngineController m_engineController
QThread * m_carDataThread
Class for the car data.
Definition CarDataI.hpp:38
The JoysticksController class.
void processInput()
Runs the joystick controller loop.