9 Commits

Author SHA1 Message Date
ishak jmilou.ishak
bc5f52922b removed image for now 2024-12-19 22:04:58 +01:00
ishak jmilou.ishak
52eff9ec39 return JSON response for /data endpoint 2024-12-19 22:02:36 +01:00
ishak jmilou.ishak
cf1350a3c0 zeker weten dat data word gestuurd 2024-12-19 21:55:51 +01:00
ishak jmilou.ishak
fa1aa6965d updated cmakelist 2024-12-19 20:31:50 +01:00
ishak jmilou.ishak
c5981f763b fixed error 2024-12-19 20:30:55 +01:00
ishak jmilou.ishak
45247b5574 added code from main branch 2024-12-19 20:22:43 +01:00
ishak jmilou.ishak
d8ce5de8f7 removed try catch 2024-12-19 20:05:48 +01:00
ishak jmilou.ishak
a6b1c04ea3 returned global variables 2024-12-19 20:03:36 +01:00
ishak jmilou.ishak
9cd6d3aa79 removed variable 2024-12-19 20:01:09 +01:00
5 changed files with 92 additions and 41 deletions

View File

@@ -6,7 +6,10 @@ set(CMAKE_CXX_STANDARD 23)
find_library(PAHO_MQTTPP_LIBRARY paho-mqttpp3 PATHS /usr/local/lib)
find_library(PAHO_MQTT_LIBRARY paho-mqtt3a PATHS /usr/local/lib)
include_directories(/usr/local/include)
# Find OpenCV package
find_package(OpenCV REQUIRED)
find_package(OpenEXR REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
set(SOURCE_FILES
src/KobukiDriver/KobukiParser.cpp
@@ -20,4 +23,4 @@ set(SOURCE_FILES
add_executable(kobuki_control ${SOURCE_FILES})
# Link the static libraries
target_link_libraries(kobuki_control ${PAHO_MQTTPP_LIBRARY} ${PAHO_MQTT_LIBRARY} pthread)
target_link_libraries(kobuki_control ${PAHO_MQTTPP_LIBRARY} ${PAHO_MQTT_LIBRARY} ${OpenCV_LIBS} pthread OpenEXR::OpenEXR)

View File

@@ -3,11 +3,15 @@
#include <thread>
#include "MQTT/MqttClient.h"
#include "KobukiDriver/CKobuki.h"
#include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/core.hpp>
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
std::string message = "stop";
@@ -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();
@@ -26,26 +30,33 @@ void setup()
int main()
{
// Unset the http_proxy environment variable
setup();
std::thread image (CapnSend);
std::thread safety([&]() { robot.robotSafety(&message); });
std::thread sendMqtt([&]() { sendKobukiData(robot.parser.data); });
while(true){
parseMQTT(readMQTT());
std::string message = readMQTT();
if (!message.empty()){
parseMQTT(message);
}
}
sendMqtt.join();
safety.join();
image.join();
}
std::string readMQTT()
{
message = client.getLastMessage();
if (!message.empty())
static std::string lastMessage;
std::string message = client.getLastMessage();
if (!message.empty() && message != lastMessage)
{
std::cout << "MQTT Message: " << message << std::endl;
lastMessage = message;
}
// Add a small delay to avoid busy-waiting
@@ -57,7 +68,7 @@ void parseMQTT(std::string message)
{
if (message == "up")
{
robot.forward(1024);
robot.forward(350);
}
else if (message == "left")
{
@@ -69,7 +80,7 @@ void parseMQTT(std::string message)
}
else if (message == "down")
{
robot.forward(-800);
robot.forward(-350);
}
else if (message == "stop")
{
@@ -155,6 +166,7 @@ void logToFile()
void sendIndividualKobukiData(const TKobukiData &data) {
while (true) {
std::cout << "Kobuki Data wordt gepubliceerd naar kobuki/data/timestamp: " << data.timestamp << std::endl;
client.publishMessage("kobuki/data/timestamp", std::to_string(data.timestamp));
client.publishMessage("kobuki/data/BumperCenter", std::to_string(data.BumperCenter));
client.publishMessage("kobuki/data/BumperLeft", std::to_string(data.BumperLeft));
@@ -276,3 +288,31 @@ void sendKobukiData(TKobukiData &data) {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
void CapnSend() {
VideoCapture cap(0);
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(300)); // Send image every 1000ms
}
}

View File

@@ -4,8 +4,11 @@ import mysql.connector
app = Flask(__name__)
# Globale variabelen
kobuki_message = ""
latest_image = None
# Globale MQTT setup
kobuki_message = "empty"
def on_message(client, message):
global kobuki_message, latest_image
if message.topic == "kobuki/data":
@@ -73,7 +76,7 @@ def move():
@app.route('/data', methods=['GET'])
def data():
return kobuki_message
return jsonify(kobuki_message)
@app.route("/database")
def database():

View File

@@ -1,25 +1,27 @@
// Selecteer alle knoppen en voeg een event listener toe aan elke knop
document.querySelectorAll(".btn").forEach((button) => {
button.addEventListener("click", function (event) {
event.preventDefault(); // voorkomt pagina-verversing
document.addEventListener("DOMContentLoaded", function () {
document.querySelectorAll(".btn").forEach((button) => {
button.addEventListener("click", function (event) {
event.preventDefault(); // prevents page refresh
// Haal de waarde van de knop op
const direction = event.target.value;
// Get the value of the button
const direction = event.target.value;
fetch("/move", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ direction: direction }),
})
.then((response) => response.json())
.then((data) => {
console.log("Success:", data);
fetch("/move", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ direction: direction }),
})
.catch((error) => {
console.error("Error:", error);
});
.then((response) => response.json())
.then((data) => {
script;
console.log("Success:", data);
})
.catch((error) => {
console.error("Error:", error);
});
});
});
// Fetch data from the server
@@ -32,18 +34,12 @@ document.querySelectorAll(".btn").forEach((button) => {
console.error("Error:", error);
}
}
// Parse the data and show it on the website
async function parseData() {
const data = await fetchData();
if(!data){
console.error("No data received");
return;
}
const sensorDataContainer = document.getElementById("sensor-data");
sensorDataContainer.innerHTML = ""; // Clear previous data
//for each object in json array create a new paragraph element and append it to the sensorDataContainer
// For each object in JSON array, create a new paragraph element and append it to the sensorDataContainer
for (const [key, value] of Object.entries(data)) {
const dataElement = document.createElement("p");
dataElement.textContent = `${key}: ${value}`;
@@ -51,6 +47,15 @@ document.querySelectorAll(".btn").forEach((button) => {
}
}
// Update the image
function updateImage() {
var img = document.getElementById("robot-image");
img.src = "/image?" + new Date().getTime(); // Add timestamp to avoid caching
}
// Fetch and display sensor data every 5 seconds
setInterval(parseData, 5000);
setInterval(parseData, 1000);
// Update the image every 5 seconds
setInterval(updateImage, 200);
});

View File

@@ -12,7 +12,7 @@
<body>
<div class="container">
<div class="image-section">
<img src="kobuki.jpg" alt="Kobuki Robot" id="robot-image" />
<!-- <img src="kobuki.jpg" alt="Kobuki Robot" id="robot-image" /> -->
</div>
<div class="button-section">
<form id="form" action="/move" method="post">