fix: improve Kobuki communication startup and reconnection logging

This commit is contained in:
ishak jmilou.ishak
2025-01-09 15:43:45 +01:00
parent 3bca04053a
commit 7d3a5fa9a3

View File

@@ -1,13 +1,10 @@
#include <iostream> #include <iostream>
#include <cmath>
#include <thread> #include <thread>
#include "MQTT/MqttClient.h" #include "MQTT/MqttClient.h"
#include "KobukiDriver/CKobuki.h" #include "KobukiDriver/CKobuki.h"
#include <opencv4/opencv2/opencv.hpp> #include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/core.hpp> #include <opencv4/opencv2/core.hpp>
#include <thread>
using namespace std; using namespace std;
using namespace cv; using namespace cv;
CKobuki robot; CKobuki robot;
@@ -15,7 +12,6 @@ CKobuki robot;
std::string readMQTT(); std::string readMQTT();
void parseMQTT(std::string message); void parseMQTT(std::string message);
void CapnSend(); void CapnSend();
// ip, clientID, username, password
MqttClient client("ws://145.92.224.21/ws/", "KobukiRPI", "rpi","rpiwachtwoordofzo"); // create a client object MqttClient client("ws://145.92.224.21/ws/", "KobukiRPI", "rpi","rpiwachtwoordofzo"); // create a client object
std::string message = "stop"; std::string message = "stop";
std::string serializeKobukiData(const TKobukiData &data); std::string serializeKobukiData(const TKobukiData &data);
@@ -23,12 +19,16 @@ void sendKobukiData(TKobukiData &data);
void setup() { void setup() {
unsigned char *null_ptr(0); unsigned char *null_ptr(0);
robot.startCommunication("/dev/ttyUSB0", true, null_ptr); std::cout << "Attempting to start communication with Kobuki..." << std::endl;
if (!robot.startCommunication("/dev/ttyUSB0", true, null_ptr)) {
std::cerr << "Failed to start communication with Kobuki." << std::endl;
} else {
std::cout << "Successfully started communication with Kobuki." << std::endl;
}
// connect mqtt server and sub to commands // connect mqtt server and sub to commands
client.connect(); client.connect();
client.subscribe("home/commands"); client.subscribe("home/commands");
} }
int main() { int main() {
@@ -41,7 +41,9 @@ int main() {
while (true) { while (true) {
if (!robot.isConnected()) { if (!robot.isConnected()) {
std::cout << "Kobuki is not connected anymore. Reconnecting..." << std::endl; std::cout << "Kobuki is not connected anymore. Reconnecting..." << std::endl;
robot.startCommunication("/dev/ttyUSB0", true, nullptr); if (!robot.startCommunication("/dev/ttyUSB0", true, nullptr)) {
std::cerr << "Failed to reconnect to Kobuki." << std::endl;
}
while (!robot.isConnected()) { while (!robot.isConnected()) {
std::cout << "Attempting to reconnect..." << std::endl; std::cout << "Attempting to reconnect..." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
@@ -71,7 +73,7 @@ std::string readMQTT() {
// Add a small delay to avoid busy-waiting // Add a small delay to avoid busy-waiting
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
return message; return lastMessage;
} }
void parseMQTT(std::string message) { void parseMQTT(std::string message) {