refactor: improve Kobuki connection handling and add USB device check

This commit is contained in:
ishak jmilou.ishak
2025-01-20 12:11:38 +01:00
parent ef3407b742
commit 2c630bf89b

View File

@@ -59,41 +59,46 @@ std::mutex connectionMutex;
void checkKobukiConnection() void checkKobukiConnection()
{ {
while (true) while (true)
{
{ {
std::lock_guard<std::mutex> lock(connectionMutex); std::lock_guard<std::mutex> lock(connectionMutex);
if (!robot.isConnected())
{ // Controleer of het apparaat beschikbaar is
if (kobuki_connected) if (!std::ifstream("/dev/ttyUSB0")){
{ if (kobuki_connected){
cout << "Kobuki is disconnected" << endl; cout << "Kobuki disconnected: USB device not found." << endl;
kobuki_connected = false; kobuki_connected = false;
} }
// Probeer opnieuw te verbinden std::this_thread::sleep_for(std::chrono::seconds(5));
continue; // Probeer later opnieuw
}
// Controleer of de Kobuki verbonden is
if (!robot.isConnected()){
if (kobuki_connected){
cout << "Kobuki disconnected." << endl;
kobuki_connected = false;
}
cout << "Attempting to reconnect Kobuki..." << endl; cout << "Attempting to reconnect Kobuki..." << endl;
robot.startCommunication("/dev/ttyUSB0", true, nullptr); robot.startCommunication("/dev/ttyUSB0", true, nullptr);
kobuki_connected = robot.isConnected();
if (kobuki_connected) if (robot.isConnected()){
{
cout << "Kobuki reconnected successfully!" << endl; cout << "Kobuki reconnected successfully!" << endl;
}
else
{
cout << "Failed to reconnect Kobuki, retrying in 5 seconds..." << endl;
}
}else{
if (!kobuki_connected)
{
cout << "Kobuki is connected" << endl;
kobuki_connected = true; kobuki_connected = true;
} }
else{
cout << "Failed to reconnect Kobuki, retrying in 5 seconds..." << endl;
} }
std::this_thread::sleep_for(std::chrono::seconds(5)); // Controleer elke 5 seconden }else if (!kobuki_connected){
} // Update status als de verbinding hersteld is
} cout << "Kobuki is connected." << endl;
kobuki_connected = true;
} }
// Wacht voordat je opnieuw controleert
std::this_thread::sleep_for(std::chrono::seconds(5));
}
}
std::string readMQTT() std::string readMQTT()
{ {