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

@@ -60,40 +60,45 @@ void checkKobukiConnection()
{
while (true)
{
{
std::lock_guard<std::mutex> lock(connectionMutex);
if (!robot.isConnected())
{
if (kobuki_connected)
{
cout << "Kobuki is disconnected" << endl;
kobuki_connected = false;
}
// Probeer opnieuw te verbinden
cout << "Attempting to reconnect Kobuki..." << endl;
robot.startCommunication("/dev/ttyUSB0", true, nullptr);
kobuki_connected = robot.isConnected();
if (kobuki_connected)
{
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;
}
std::lock_guard<std::mutex> lock(connectionMutex);
// Controleer of het apparaat beschikbaar is
if (!std::ifstream("/dev/ttyUSB0")){
if (kobuki_connected){
cout << "Kobuki disconnected: USB device not found." << endl;
kobuki_connected = false;
}
std::this_thread::sleep_for(std::chrono::seconds(5));
continue; // Probeer later opnieuw
}
std::this_thread::sleep_for(std::chrono::seconds(5)); // Controleer elke 5 seconden
// 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;
robot.startCommunication("/dev/ttyUSB0", true, nullptr);
if (robot.isConnected()){
cout << "Kobuki reconnected successfully!" << endl;
kobuki_connected = true;
}
else{
cout << "Failed to reconnect Kobuki, retrying in 5 seconds..." << endl;
}
}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()
{