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
12 Commits
b48243f831
...
8b66702605
Author | SHA1 | Date | |
---|---|---|---|
|
8b66702605 | ||
|
d8b3ec2938 | ||
|
97076dfe05 | ||
|
967bc8247c | ||
|
5d61579973 | ||
|
ebd88e43ab | ||
|
2fbe18be76 | ||
|
74d9687af5 | ||
|
48023773c6 | ||
56ac9cf687 | |||
|
3232ff121f | ||
5844387b19 |
@@ -8,6 +8,8 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace cv;
|
using namespace cv;
|
||||||
CKobuki robot;
|
CKobuki robot;
|
||||||
|
std::atomic<bool> kobuki_connected(false);
|
||||||
|
|
||||||
|
|
||||||
std::string readMQTT();
|
std::string readMQTT();
|
||||||
void parseMQTT(std::string message);
|
void parseMQTT(std::string message);
|
||||||
@@ -28,12 +30,28 @@ void setup()
|
|||||||
client.subscribe("home/commands");
|
client.subscribe("home/commands");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void checkKobukiConnection() {
|
||||||
|
while (true) {
|
||||||
|
bool connected = robot.isConnected();
|
||||||
|
if (!connected && kobuki_connected) {
|
||||||
|
cout << "Kobuki is disconnected" << endl;
|
||||||
|
kobuki_connected = false;
|
||||||
|
} 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){
|
while(true){
|
||||||
std::string message = readMQTT();
|
std::string message = readMQTT();
|
||||||
@@ -46,6 +64,7 @@ int main()
|
|||||||
sendMqtt.join();
|
sendMqtt.join();
|
||||||
safety.join();
|
safety.join();
|
||||||
image.join();
|
image.join();
|
||||||
|
connectionThread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string readMQTT()
|
std::string readMQTT()
|
||||||
|
@@ -14,5 +14,5 @@ EXPOSE 5000
|
|||||||
CMD ["python", "web/app.py"]
|
CMD ["python", "web/app.py"]
|
||||||
|
|
||||||
#build instruction: sudo docker buildx build -t flaskapp:latest .
|
#build instruction: sudo docker buildx build -t flaskapp:latest .
|
||||||
#run instruction: sudo docker run --network="host" flaskapp:latest
|
#run instruction: sudo docker run --network="host" --restart=always flaskapp:latest
|
||||||
# need to use network host to connect to the host's mqtt server
|
# need to use network host to connect to the host's mqtt server
|
@@ -59,9 +59,6 @@ mqtt_client.subscribe("kobuki/cam")
|
|||||||
|
|
||||||
mqtt_client.on_message = on_message # this line needs to be under the function definition otherwise it can't find which function it needs to use
|
mqtt_client.on_message = on_message # this line needs to be under the function definition otherwise it can't find which function it needs to use
|
||||||
|
|
||||||
mqtt_client.on_message = on_message # this line needs to be under the function definition otherwise it can't find which function it needs to use
|
|
||||||
|
|
||||||
|
|
||||||
# Database connectie-functie
|
# Database connectie-functie
|
||||||
def get_db():
|
def get_db():
|
||||||
if 'db' not in g: # 'g' is specifiek voor een request en leeft zolang een request duurt
|
if 'db' not in g: # 'g' is specifiek voor een request en leeft zolang een request duurt
|
||||||
@@ -110,15 +107,6 @@ def move():
|
|||||||
cursor.close()
|
cursor.close()
|
||||||
db_connection.close()
|
db_connection.close()
|
||||||
return jsonify({"status": "success", "direction": direction})
|
return jsonify({"status": "success", "direction": direction})
|
||||||
|
|
||||||
@app.route("/database")
|
|
||||||
def database():
|
|
||||||
db = get_db()
|
|
||||||
cursor = db.cursor()
|
|
||||||
cursor.execute("SELECT * FROM kobuki_data")
|
|
||||||
rows = cursor.fetchall()
|
|
||||||
cursor.close()
|
|
||||||
return str(rows)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/data', methods=['GET'])
|
@app.route('/data', methods=['GET'])
|
||||||
@@ -142,15 +130,15 @@ def data():
|
|||||||
|
|
||||||
# Database-insert
|
# Database-insert
|
||||||
db = get_db()
|
db = get_db()
|
||||||
cursor = db.cursor()
|
with db.cursor() as cursor:
|
||||||
|
|
||||||
# Zorg dat je tabel `kobuki_data` kolommen heeft: `name` en `value`
|
# Zorg dat je tabel `kobuki_data` kolommen heeft: `name` en `value`
|
||||||
sql_sensor = "INSERT INTO kobuki_data (name, value) VALUES (%s, %s)"
|
sql_sensor = "INSERT INTO kobuki_data (name, value) VALUES (%s, %s)"
|
||||||
cursor.executemany(sql_sensor, sensor_data_tuples)
|
cursor.executemany(sql_sensor, sensor_data_tuples)
|
||||||
|
|
||||||
# Commit en sluit de cursor
|
# Commit en sluit de cursor
|
||||||
db.commit()
|
db.commit()
|
||||||
cursor.close()
|
cursor.close()
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
print(f"JSON decode error: {e}")
|
print(f"JSON decode error: {e}")
|
||||||
except mysql.connector.Error as err:
|
except mysql.connector.Error as err:
|
||||||
@@ -176,6 +164,16 @@ def yolo_results_endpoint():
|
|||||||
with lock:
|
with lock:
|
||||||
return jsonify(yolo_results)
|
return jsonify(yolo_results)
|
||||||
|
|
||||||
|
def yolo_results_db():
|
||||||
|
global yolo_results
|
||||||
|
db = get_db()
|
||||||
|
with db.cursor() as cursor:
|
||||||
|
sql_yolo = "INSERT INTO yolo_results (object, confidence) VALUES (%s, %s)"
|
||||||
|
yolo_tuples = [(result["class"], result["confidence"]) for result in yolo_results]
|
||||||
|
cursor.executemany(sql_yolo, yolo_tuples)
|
||||||
|
db.commit()
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(debug=True, port=5000)
|
app.run(debug=True, port=5000)
|
Reference in New Issue
Block a user