From 2f4e5ae096cd9a8924f8496873184de8ae2a1c40 Mon Sep 17 00:00:00 2001 From: Sam Hos Date: Mon, 9 Dec 2024 10:31:01 +0100 Subject: [PATCH] re enable robot communication --- src/C++/Driver/src/main.cpp | 39 +++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/C++/Driver/src/main.cpp b/src/C++/Driver/src/main.cpp index af68be6..b70dcd4 100644 --- a/src/C++/Driver/src/main.cpp +++ b/src/C++/Driver/src/main.cpp @@ -3,13 +3,17 @@ #include #include "MQTT/MqttClient.h" #include "KobukiDriver/CKobuki.h" +#include +#include using namespace std; +using namespace cv; CKobuki robot; std::string readMQTT(); void parseMQTT(std::string message); +void CapnSend(); //ip, clientID, username, password -MqttClient client("ws://145.92.224.21/ws/", "KobukiRPI", "rpi", "rpiwachtwoordofzo"); // create a client object +MqttClient client("mqtt://localhost:1883", "KobukiRPI", "rpi", "rpiwachtwoordofzo"); // create a client object std::string message = "stop"; std::string serializeKobukiData(const TKobukiData &data); void sendKobukiData(TKobukiData &data); @@ -17,7 +21,7 @@ void sendKobukiData(TKobukiData &data); void setup() { unsigned char *null_ptr(0); - // robot.startCommunication("/dev/ttyUSB0", true, null_ptr); + robot.startCommunication("/dev/ttyUSB0", true, null_ptr); //connect mqtt server and sub to commands client.connect(); @@ -30,6 +34,7 @@ int main() setup(); + std::thread image (CapnSend); std::thread safety([&]() { robot.robotSafety(&message); }); std::thread sendMqtt([&]() { sendKobukiData(robot.parser.data); }); @@ -38,6 +43,7 @@ int main() } sendMqtt.join(); safety.join(); + image.join(); } std::string readMQTT() @@ -276,3 +282,32 @@ void sendKobukiData(TKobukiData &data) { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } } + + +void CapnSend() { + VideoCapture cap(0); // Open the default camera + if (!cap.isOpened()) { + cerr << "Error: Could not open camera" << endl; + return; + } + + Mat frame; + while (true) { + cap >> frame; // Capture a new image frame + if (frame.empty()) { + cerr << "Error: Could not capture image" << endl; + continue; + } + + // Convert the image to a byte array + vector buf; + imencode(".jpg", frame, buf); + auto* enc_msg = reinterpret_cast(buf.data()); + + // Publish the image data + client.publishMessage("kobuki/cam", string(enc_msg, enc_msg + buf.size())); + cout << "Sent image" << endl; + + std::this_thread::sleep_for(std::chrono::milliseconds(400)); // Send image every 1000ms + } +}