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
16 Commits
8b66702605
...
7b51330675
Author | SHA1 | Date | |
---|---|---|---|
7b51330675 | |||
3bb44ad4ab | |||
|
fb12b20a0b | ||
|
1b3ccd1e72 | ||
|
a16abe068c | ||
|
9f7d7e7ac9 | ||
|
6f34a0f554 | ||
|
364f6e5259 | ||
7c30d838f7 | |||
|
50bf777f78 | ||
|
95e2d292c9 | ||
|
3367f1dbd2 | ||
e273e175cb | |||
06e08a2cfb | |||
|
4e78caa577 | ||
|
5b0e843654 |
47
README.md
47
README.md
@@ -29,11 +29,42 @@ This project is a kobuki that drives around in dangerous areas and detects objec
|
|||||||
- Type `git clone https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
|
- Type `git clone https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
|
||||||
|
|
||||||
3. **Install the required packages**
|
3. **Install the required packages**
|
||||||
- Open the terminal and navigate to the project - scr - Python - flask.
|
- Install the following packages on the server: "docker docker-buildx mosquitto nginx"
|
||||||
- Run the following command to install the required packages:
|
- Install the following packages on the Raspberry Pi: "g++ make cmake", https://github.com/eclipse-paho/paho.mqtt.c, https://github.com/eclipse-paho/paho.mqtt.cpp
|
||||||
- `pip install -r requirements.txt`
|
|
||||||
- This will install all the python packages required to run the project.
|
|
||||||
- for C++, you will need to install the following packages:
|
|
||||||
- OpenCV
|
4. **Run the project**
|
||||||
- mqtt-client
|
|
||||||
4.
|
#### Server side
|
||||||
|
- Run the following commands in the terminal to start the website:
|
||||||
|
- `cd src/Python/flask`
|
||||||
|
- `sudo docker buildx build -t flaskapp:latest .`
|
||||||
|
- `sudo docker run --network="host" --restart=always flaskapp:latest`
|
||||||
|
- Run the following commands in the terminal to start the MQTT broker:
|
||||||
|
- `cd src/config/server/`
|
||||||
|
- `mosquitto -c mosquitto.conf`
|
||||||
|
- Run the following commands in the terminal to start the Nginx server:
|
||||||
|
- `cd src/config/server/`
|
||||||
|
- `cp nginx.conf /etc/nginx/nginx.conf`
|
||||||
|
- `cp nginx-sites.conf /etc/nginx/sites-enable/nginx-sites.conf`
|
||||||
|
|
||||||
|
|
||||||
|
#### Raspberry Pi side
|
||||||
|
- Run the following commands to build and start the driver:
|
||||||
|
- `cd src/C++/Driver`
|
||||||
|
- `cmake ..`
|
||||||
|
- `make`
|
||||||
|
- `./kobuki_driver`
|
||||||
|
- Run the following commands to autostart the driver on startup of the Raspberry Pi:
|
||||||
|
- `cd src/config/rpi/`
|
||||||
|
- `cp kobukiDriver.service /etc/systemd/system/kobukiDriver.service`
|
||||||
|
- `systemctl enable kobukiDriver.service`
|
||||||
|
- `systemctl start kobukiDriver.service`
|
||||||
|
|
||||||
|
## Extra notes
|
||||||
|
Dont forget to change the IP address in the `src/C++/Driver/src/main.cpp` file to the IP address of the server.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -10,11 +10,10 @@ using namespace cv;
|
|||||||
CKobuki robot;
|
CKobuki robot;
|
||||||
std::atomic<bool> kobuki_connected(false);
|
std::atomic<bool> kobuki_connected(false);
|
||||||
|
|
||||||
|
|
||||||
std::string readMQTT();
|
std::string readMQTT();
|
||||||
void parseMQTT(std::string message);
|
void parseMQTT(std::string message);
|
||||||
void CapnSend();
|
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("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);
|
||||||
@@ -22,93 +21,114 @@ 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();
|
||||||
client.subscribe("home/commands");
|
client.subscribe("home/commands");
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkKobukiConnection() {
|
void checkKobukiConnection()
|
||||||
while (true) {
|
{
|
||||||
bool connected = robot.isConnected();
|
while (true)
|
||||||
if (!connected && kobuki_connected) {
|
{
|
||||||
cout << "Kobuki is disconnected" << endl;
|
bool connected = robot.isConnected();
|
||||||
kobuki_connected = false;
|
if (!connected && kobuki_connected)
|
||||||
} else if (connected && !kobuki_connected) {
|
{
|
||||||
cout << "Kobuki is connecting..." << endl;
|
cout << "Kobuki is disconnected" << endl;
|
||||||
// Start de Kobuki automatisch
|
kobuki_connected = false;
|
||||||
robot.startCommunication("/dev/ttyUSB0", true, nullptr);
|
|
||||||
}
|
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(5)); // Controleer elke 5 seconden
|
|
||||||
}
|
}
|
||||||
|
else if (connected && !kobuki_connected)
|
||||||
|
{
|
||||||
|
cout << "Kobuki is connecting..." << endl;
|
||||||
|
// Start de Kobuki automatisch
|
||||||
|
robot.startCommunication("/dev/ttyUSB0", true, nullptr);
|
||||||
|
}
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(5)); // Controleer elke 5 seconden
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
setup();
|
setup();
|
||||||
std::thread image (CapnSend);
|
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); });
|
||||||
std::thread connectionThread(checkKobukiConnection);
|
|
||||||
|
|
||||||
while(true){
|
|
||||||
std::string message = readMQTT();
|
|
||||||
if (!message.empty()){
|
|
||||||
parseMQTT(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
std::string message = readMQTT();
|
||||||
|
if (!message.empty())
|
||||||
|
{
|
||||||
|
parseMQTT(message);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sendMqtt.join();
|
sendMqtt.join();
|
||||||
safety.join();
|
safety.join();
|
||||||
image.join();
|
image.join();
|
||||||
connectionThread.join();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string readMQTT()
|
std::string readMQTT()
|
||||||
{
|
{
|
||||||
static std::string lastMessage;
|
static std::string lastMessage;
|
||||||
|
|
||||||
std::string message = client.getLastMessage();
|
std::string message = client.getLastMessage();
|
||||||
if (!message.empty() && message != lastMessage)
|
if (!message.empty() && message != lastMessage)
|
||||||
{
|
{
|
||||||
std::cout << "MQTT Message: " << message << std::endl;
|
std::cout << "MQTT Message: " << message << std::endl;
|
||||||
lastMessage = message;
|
lastMessage = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 lastMessage;
|
return lastMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
void parseMQTT(std::string message) {
|
void parseMQTT(std::string message)
|
||||||
if (message == "up") {
|
{
|
||||||
|
if (message == "up")
|
||||||
|
{
|
||||||
robot.forward(350);
|
robot.forward(350);
|
||||||
} else if (message == "left") {
|
}
|
||||||
|
else if (message == "left")
|
||||||
|
{
|
||||||
robot.setRotationSpeed(4);
|
robot.setRotationSpeed(4);
|
||||||
} else if (message == "right") {
|
}
|
||||||
|
else if (message == "right")
|
||||||
|
{
|
||||||
robot.setRotationSpeed(-4);
|
robot.setRotationSpeed(-4);
|
||||||
} else if (message == "down") {
|
}
|
||||||
|
else if (message == "down")
|
||||||
|
{
|
||||||
robot.forward(-350);
|
robot.forward(-350);
|
||||||
} else if (message == "stop") {
|
}
|
||||||
|
else if (message == "stop")
|
||||||
|
{
|
||||||
robot.sendNullMessage();
|
robot.sendNullMessage();
|
||||||
robot.sendNullMessage();
|
robot.sendNullMessage();
|
||||||
} else if (message == "estop") {
|
}
|
||||||
|
else if (message == "estop")
|
||||||
|
{
|
||||||
robot.forward(-400);
|
robot.forward(-400);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
std::cout << "Invalid command" << std::endl;
|
std::cout << "Invalid command" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void logToFile() {
|
void logToFile()
|
||||||
while (true) {
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
TKobukiData robotData = robot.parser.data;
|
TKobukiData robotData = robot.parser.data;
|
||||||
std::ofstream outputFile("log",
|
std::ofstream outputFile("log",
|
||||||
std::ios_base::app); // Open file in append mode to
|
std::ios_base::app); // Open file in append mode to
|
||||||
// not overwrite own content
|
// not overwrite own content
|
||||||
if (outputFile.is_open()) { // check if the file was opened successfully
|
if (outputFile.is_open())
|
||||||
|
{ // check if the file was opened successfully
|
||||||
// Get current time
|
// Get current time
|
||||||
std::time_t now = std::time(nullptr);
|
std::time_t now = std::time(nullptr);
|
||||||
outputFile << "Timestamp: " << std::ctime(&now);
|
outputFile << "Timestamp: " << std::ctime(&now);
|
||||||
@@ -166,7 +186,9 @@ void logToFile() {
|
|||||||
outputFile << "UDID1: " << robotData.extraInfo.UDID1 << "\n";
|
outputFile << "UDID1: " << robotData.extraInfo.UDID1 << "\n";
|
||||||
outputFile << "UDID2: " << robotData.extraInfo.UDID2 << "\n";
|
outputFile << "UDID2: " << robotData.extraInfo.UDID2 << "\n";
|
||||||
outputFile.close();
|
outputFile.close();
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
std::cerr << "Error opening file\n";
|
std::cerr << "Error opening file\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,8 +196,10 @@ void logToFile() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendIndividualKobukiData(const TKobukiData &data) {
|
void sendIndividualKobukiData(const TKobukiData &data)
|
||||||
while (true) {
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
std::cout << "Kobuki Data wordt gepubliceerd naar kobuki/data/timestamp: "
|
std::cout << "Kobuki Data wordt gepubliceerd naar kobuki/data/timestamp: "
|
||||||
<< data.timestamp << std::endl;
|
<< data.timestamp << std::endl;
|
||||||
client.publishMessage("kobuki/data/timestamp",
|
client.publishMessage("kobuki/data/timestamp",
|
||||||
@@ -263,7 +287,8 @@ void sendIndividualKobukiData(const TKobukiData &data) {
|
|||||||
client.publishMessage("kobuki/data/extraInfo/UDID2",
|
client.publishMessage("kobuki/data/extraInfo/UDID2",
|
||||||
std::to_string(data.extraInfo.UDID2));
|
std::to_string(data.extraInfo.UDID2));
|
||||||
|
|
||||||
if (!data.gyroData.empty()) {
|
if (!data.gyroData.empty())
|
||||||
|
{
|
||||||
const auto &latestGyro = data.gyroData.back();
|
const auto &latestGyro = data.gyroData.back();
|
||||||
client.publishMessage("kobuki/data/gyroData/x",
|
client.publishMessage("kobuki/data/gyroData/x",
|
||||||
std::to_string(latestGyro.x));
|
std::to_string(latestGyro.x));
|
||||||
@@ -277,7 +302,8 @@ void sendIndividualKobukiData(const TKobukiData &data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string serializeKobukiData(const TKobukiData &data) {
|
std::string serializeKobukiData(const TKobukiData &data)
|
||||||
|
{
|
||||||
std::string json =
|
std::string json =
|
||||||
"{\"timestamp\":" + std::to_string(data.timestamp) +
|
"{\"timestamp\":" + std::to_string(data.timestamp) +
|
||||||
",\"BumperCenter\":" + std::to_string(data.BumperCenter) +
|
",\"BumperCenter\":" + std::to_string(data.BumperCenter) +
|
||||||
@@ -330,7 +356,8 @@ std::string serializeKobukiData(const TKobukiData &data) {
|
|||||||
",\"UDID1\":" + std::to_string(data.extraInfo.UDID1) +
|
",\"UDID1\":" + std::to_string(data.extraInfo.UDID1) +
|
||||||
",\"UDID2\":" + std::to_string(data.extraInfo.UDID2) + "},\"gyroData\":[";
|
",\"UDID2\":" + std::to_string(data.extraInfo.UDID2) + "},\"gyroData\":[";
|
||||||
|
|
||||||
if (!data.gyroData.empty()) {
|
if (!data.gyroData.empty())
|
||||||
|
{
|
||||||
const auto &latestGyro = data.gyroData.back();
|
const auto &latestGyro = data.gyroData.back();
|
||||||
json += "{\"x\":" + std::to_string(latestGyro.x) +
|
json += "{\"x\":" + std::to_string(latestGyro.x) +
|
||||||
",\"y\":" + std::to_string(latestGyro.y) +
|
",\"y\":" + std::to_string(latestGyro.y) +
|
||||||
@@ -342,38 +369,54 @@ std::string serializeKobukiData(const TKobukiData &data) {
|
|||||||
}
|
}
|
||||||
// create extra function to send the message every 100ms
|
// create extra function to send the message every 100ms
|
||||||
// needed it so it can be threaded
|
// needed it so it can be threaded
|
||||||
void sendKobukiData(TKobukiData &data) {
|
void sendKobukiData(TKobukiData &data)
|
||||||
while (true) {
|
{
|
||||||
client.publishMessage("kobuki/data", serializeKobukiData(data));
|
while (true)
|
||||||
std::cout << "Sent data" << std::endl;
|
{
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
client.publishMessage("kobuki/data", serializeKobukiData(data));
|
||||||
}
|
std::cout << "Sent data" << std::endl;
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CapnSend() {
|
void CapnSend()
|
||||||
VideoCapture cap(0);
|
{
|
||||||
if (!cap.isOpened()) {
|
VideoCapture cap(0);
|
||||||
cerr << "Error: Could not open camera" << endl;
|
if (!cap.isOpened())
|
||||||
return;
|
{
|
||||||
|
cerr << "Error: Could not capture image" << endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Mat frame;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
cap >> frame; // Capture a new image frame
|
||||||
|
if (frame.empty())
|
||||||
|
{
|
||||||
|
cerr << "Error: Could not capture image" << endl;
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(1)); // Wait before retrying
|
||||||
|
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
Mat frame;
|
// Convert the image to a byte array
|
||||||
while (true) {
|
vector<uchar> buf;
|
||||||
cap >> frame; // Capture a new image frame
|
imencode(".jpg", frame, buf);
|
||||||
if (frame.empty()) {
|
auto *enc_msg = reinterpret_cast<unsigned char *>(buf.data());
|
||||||
cerr << "Error: Could not capture image" << endl;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert the image to a byte array
|
// Publish the image data
|
||||||
vector<uchar> buf;
|
client.publishMessage("kobuki/cam", string(enc_msg, enc_msg + buf.size()));
|
||||||
imencode(".jpg", frame, buf);
|
cout << "Sent image" << endl;
|
||||||
auto* enc_msg = reinterpret_cast<unsigned char*>(buf.data());
|
|
||||||
|
|
||||||
// Publish the image data
|
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // Send image every 200ms
|
||||||
client.publishMessage("kobuki/cam", string(enc_msg, enc_msg + buf.size()));
|
|
||||||
cout << "Sent image" << endl;
|
|
||||||
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // Send image every 200ms
|
if (!cap.isOpened())
|
||||||
|
{
|
||||||
|
cerr << "Camera disconnected, attempting to reconnect..." << endl;
|
||||||
|
|
||||||
|
cap.open(0);
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(1)); // Wait before retrying
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@@ -48,6 +48,7 @@ def on_message(client, userdata, message):
|
|||||||
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
||||||
cv2.rectangle(processed_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
cv2.rectangle(processed_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
||||||
cv2.putText(processed_image, f"{class_name} {box.conf.item():.2f}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
cv2.putText(processed_image, f"{class_name} {box.conf.item():.2f}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
||||||
|
# yolo_results_db()
|
||||||
|
|
||||||
# Create an MQTT client instance
|
# Create an MQTT client instance
|
||||||
mqtt_client = mqtt.Client()
|
mqtt_client = mqtt.Client()
|
||||||
@@ -162,17 +163,27 @@ def image():
|
|||||||
def yolo_results_endpoint():
|
def yolo_results_endpoint():
|
||||||
global yolo_results
|
global yolo_results
|
||||||
with lock:
|
with lock:
|
||||||
|
print(f"YOLO Results: {yolo_results}") # Debug statement
|
||||||
|
db = get_db()
|
||||||
|
with db.cursor() as cursor:
|
||||||
|
sql_yolo = "INSERT INTO image (class, confidence) VALUES (%s, %s)"
|
||||||
|
yolo_tuples = [(result["class"], result["confidence"]) for result in yolo_results]
|
||||||
|
print(f"YOLO Tuples: {yolo_tuples}") # Debug statement
|
||||||
|
cursor.executemany(sql_yolo, yolo_tuples)
|
||||||
|
db.commit()
|
||||||
|
cursor.close()
|
||||||
return jsonify(yolo_results)
|
return jsonify(yolo_results)
|
||||||
|
|
||||||
def yolo_results_db():
|
# def yolo_results_db():
|
||||||
global yolo_results
|
# global yolo_results
|
||||||
db = get_db()
|
# with lock:
|
||||||
with db.cursor() as cursor:
|
# db = get_db()
|
||||||
sql_yolo = "INSERT INTO yolo_results (object, confidence) VALUES (%s, %s)"
|
# with db.cursor() as cursor:
|
||||||
yolo_tuples = [(result["class"], result["confidence"]) for result in yolo_results]
|
# sql_yolo = "INSERT INTO image (object, confidence) VALUES (%s, %s)"
|
||||||
cursor.executemany(sql_yolo, yolo_tuples)
|
# yolo_tuples = [(result["class"], result["confidence"]) for result in yolo_results]
|
||||||
db.commit()
|
# cursor.executemany(sql_yolo, yolo_tuples)
|
||||||
cursor.close()
|
# db.commit()
|
||||||
|
# cursor.close()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@@ -43,19 +43,21 @@ document.addEventListener("DOMContentLoaded", function() {
|
|||||||
for (const [key, value] of Object.entries(data)) {
|
for (const [key, value] of Object.entries(data)) {
|
||||||
const dataElement = document.createElement("p");
|
const dataElement = document.createElement("p");
|
||||||
dataElement.textContent = `${key}: ${value}`;
|
dataElement.textContent = `${key}: ${value}`;
|
||||||
sensorDataContainer.appendChild(dataElement); // Voeg het element toe aan de container
|
sensorDataContainer.appendChild(dataElement); // Add the element to the container
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the image
|
// Update the image
|
||||||
function updateImage() {
|
async function updateImage() {
|
||||||
var img = document.getElementById("robot-image");
|
let img = document.getElementById("robot-image");
|
||||||
img.src = "/image?" + new Date().getTime(); // Add timestamp to avoid caching
|
img.src = "/image?" + new Date().getTime(); // Add timestamp to avoid caching
|
||||||
|
// Wait for 200 milliseconds before fetching the next image
|
||||||
|
setTimeout(updateImage, 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch and display sensor data every 1 second
|
// Fetch and display sensor data every 1 second
|
||||||
setInterval(parseData, 1000);
|
setInterval(parseData, 1000);
|
||||||
|
|
||||||
// Update the image every 200 milliseconds
|
// Start updating the image
|
||||||
setInterval(updateImage, 100);
|
updateImage();
|
||||||
});
|
});
|
Reference in New Issue
Block a user