mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-03 11:55:00 +00:00
refactor: improve Kobuki connection handling and add USB device check
This commit is contained in:
@@ -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()
|
||||
{
|
||||
|
Reference in New Issue
Block a user