re enable robot communication

This commit is contained in:
2024-12-09 10:31:01 +01:00
parent 9e07a243ea
commit 2f4e5ae096

View File

@@ -3,13 +3,17 @@
#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/core.hpp>
using namespace std; using namespace std;
using namespace cv;
CKobuki robot; CKobuki robot;
std::string readMQTT(); std::string readMQTT();
void parseMQTT(std::string message); void parseMQTT(std::string message);
void CapnSend();
//ip, clientID, username, password //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 message = "stop";
std::string serializeKobukiData(const TKobukiData &data); std::string serializeKobukiData(const TKobukiData &data);
void sendKobukiData(TKobukiData &data); void sendKobukiData(TKobukiData &data);
@@ -17,7 +21,7 @@ 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); robot.startCommunication("/dev/ttyUSB0", true, null_ptr);
//connect mqtt server and sub to commands //connect mqtt server and sub to commands
client.connect(); client.connect();
@@ -30,6 +34,7 @@ int main()
setup(); setup();
std::thread image (CapnSend);
std::thread safety([&]() { robot.robotSafety(&message); }); std::thread safety([&]() { robot.robotSafety(&message); });
std::thread sendMqtt([&]() { sendKobukiData(robot.parser.data); }); std::thread sendMqtt([&]() { sendKobukiData(robot.parser.data); });
@@ -38,6 +43,7 @@ int main()
} }
sendMqtt.join(); sendMqtt.join();
safety.join(); safety.join();
image.join();
} }
std::string readMQTT() std::string readMQTT()
@@ -276,3 +282,32 @@ void sendKobukiData(TKobukiData &data) {
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 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<uchar> buf;
imencode(".jpg", frame, buf);
auto* enc_msg = reinterpret_cast<unsigned char*>(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
}
}