mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-05 12:54:57 +00:00
Compare commits
9 Commits
810309c37d
...
bc5f52922b
Author | SHA1 | Date | |
---|---|---|---|
|
bc5f52922b | ||
|
52eff9ec39 | ||
|
cf1350a3c0 | ||
|
fa1aa6965d | ||
|
c5981f763b | ||
|
45247b5574 | ||
|
d8ce5de8f7 | ||
|
a6b1c04ea3 | ||
|
9cd6d3aa79 |
@@ -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)
|
||||
|
@@ -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
|
||||
}
|
||||
}
|
@@ -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():
|
||||
|
@@ -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);
|
||||
});
|
||||
|
@@ -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">
|
||||
|
Reference in New Issue
Block a user