30 Commits

Author SHA1 Message Date
25a179ded6 fix last build errors 2024-11-04 12:29:24 +01:00
a3ed0f0f30 used wrong operator 2024-11-04 12:23:34 +01:00
c246258c6d . 2024-11-04 12:22:31 +01:00
94f1b40e74 fix compile errors 2024-11-04 12:19:38 +01:00
d845d01e0a reset mqtt var after running command 2024-11-04 12:18:14 +01:00
ishak jmilou.ishak
005021e2d5 Merge branch 'main' of ssh://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79 2024-11-04 12:17:18 +01:00
ishak jmilou.ishak
38dfccbd28 Change fetch endpoint from /send-direction to /move 2024-11-04 12:17:17 +01:00
d9f0abd86c fix 2024-11-04 12:15:14 +01:00
ishak jmilou.ishak
85d54f8422 Merge branch 'main' of ssh://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79 2024-11-04 12:14:43 +01:00
ishak jmilou.ishak
99676cc83a Refactor button event handling to send direction via fetch to /send-direction 2024-11-04 12:14:42 +01:00
d09352861f reset message after input 2024-11-04 12:13:52 +01:00
ff599e127d remove debug prints 2024-11-04 11:58:06 +01:00
cf59adae8b attempt to fix 2024-11-04 11:56:57 +01:00
7449f713fa added breaks 2024-11-04 11:55:27 +01:00
ishak jmilou.ishak
705ffcd98d Merge branch 'main' of ssh://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79 2024-11-04 11:54:22 +01:00
ishak jmilou.ishak
ca9b81c03e Refactor move endpoint to accept JSON input and update form submission method 2024-11-04 11:54:21 +01:00
b64ccd51eb more debug prints 2024-11-04 11:53:58 +01:00
7e1e46bcc3 added debugging print 2024-11-04 11:50:51 +01:00
60f27af05d some changes 2024-11-04 11:44:51 +01:00
ishak jmilou.ishak
17e1399643 Merge branch 'main' of ssh://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79 2024-11-04 11:43:21 +01:00
ishak jmilou.ishak
d673b4e0ad loading js correctly 2024-11-04 11:43:20 +01:00
e708cbb6ea added comments and controls for mqtt 2024-11-04 11:43:04 +01:00
ishak jmilou.ishak
41034ba85c fixed fetch 2024-11-04 11:34:42 +01:00
ishak jmilou.ishak
21ed9c5080 changed location 2024-11-04 10:55:39 +01:00
ishak jmilou.ishak
19bc1a4184 made get method 2024-11-04 10:26:15 +01:00
b8b61df756 it compiles again 2024-11-04 10:09:39 +01:00
896897d1b0 added thread library to driver 2024-11-04 09:36:21 +01:00
fd4615f0af re-added functions that where removed when merged 2024-11-04 09:32:02 +01:00
9c5825fd61 fix driver 2024-11-04 09:27:18 +01:00
ishak jmilou.ishak
6f17c1ed6f named action /move 2024-11-04 09:23:07 +01:00
7 changed files with 131 additions and 39 deletions

View File

@@ -3,6 +3,7 @@
#include "termios.h"
#include <cstddef>
#include <iostream>
#include <thread>
// plot p;
static std::vector<float> vectorX;
@@ -727,7 +728,7 @@ void CKobuki::goToXy(long double xx, long double yy) {
return;
}
void CKobuki::forward(int speedvalue, long double distance) {
void CKobuki::forward(int speedvalue) {
// Use the goStraight logic to determine the speed and distance
// Calculate the actual speed and radius values based on the conversion table
@@ -755,3 +756,65 @@ void CKobuki::forward(int speedvalue, long double distance) {
uint32_t pocet;
pocet = write(HCom, &message, 11);
}
/// @brief Makes the kobuki rotate
/// @param degrees Rotation in degrees
void CKobuki::Rotate(int degrees) {
// convert raidans to degrees
float radians = degrees * PI / 180.0;
// Calculate the rotation speed in radians per second
double radpersec = 1;
// calculator rotation time and give absolute value
float rotation_time = std::abs(radians / radpersec);
// Use original function to set the rotation speed in mm/s
setRotationSpeed(radians);
// Sleep for the calculated rotation time
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(rotation_time * 1000)));
// Stop the robot after the rotation
setRotationSpeed(0);
}
void CKobuki::robotSafety() {
while (true) {
if (parser.data.BumperCenter || parser.data.BumperLeft || parser.data.BumperRight ||
parser.data.CliffLeft || parser.data.CliffCenter || parser.data.CliffRight) {
std::cout << "Safety condition triggered!" << std::endl; // Debug print
forward(-100); // reverse the robot
}
}
}
void CKobuki::sendNullMessage(){
unsigned char message[11] = {
0xaa, // Start byte 1
0x55, // Start byte 2
0x08, // Payload length (the first 2 bytes dont count)
0x01, // payload type (0x01 = control command)
0x04, // Control byte or additional identifier
0x00, // Lower byte of speed value
0x00, // Upper byte of speed value
0x00, // Placeholder for radius
0x00, // Placeholder for radius
0x00 // Placeholder for checksum
};
message[10] = message[2] ^ message[3] ^ message[4] ^ message[5] ^ message[6] ^
message[7] ^ message[8] ^ message[9];
// Send the message
uint32_t pocet;
pocet = write(HCom, &message, 11);
}

View File

@@ -3,18 +3,18 @@
MqttClient::MqttClient(const std::string& address, const std::string& clientId, const std::string& username, const std::string& password)
: client_(address, clientId), username_(username), password_(password), callback_(*this) {
client_.set_callback(callback_);
connOpts_.set_clean_session(true);
connOpts_.set_mqtt_version(MQTTVERSION_3_1_1); // For MQTT 3.1.1
options.set_clean_session(true);
options.set_mqtt_version(MQTTVERSION_3_1_1); // For MQTT 3.1.1
if (!username_.empty() && !password_.empty()) {
connOpts_.set_user_name(username_);
connOpts_.set_password(password_);
options.set_user_name(username_);
options.set_password(password_);
}
}
void MqttClient::connect() {
try {
std::cout << "Connecting to broker..." << std::endl;
client_.connect(connOpts_)->wait();
client_.connect(options)->wait();
std::cout << "Connected!" << std::endl;
} catch (const mqtt::exception& exc) {
std::cerr << "Error: " << exc.what() << std::endl;
@@ -41,6 +41,7 @@ void MqttClient::run() {
}
void MqttClient::Callback::message_arrived(mqtt::const_message_ptr msg) {
//lock the variable, it automaticly unlocks when going out of scope
std::lock_guard<std::mutex> lock(client_.messageMutex_);
client_.lastMessage_ = msg->to_string();
}
@@ -55,7 +56,9 @@ void MqttClient::Callback::delivery_complete(mqtt::delivery_token_ptr token) {
/// @brief Get the last message received from the MQTT broker
/// @return The last message received in a string
//std::string is the datatype of the return value
std::string MqttClient::getLastMessage() {
//lock the variable, it automaticly unlocks when going out of scope
std::lock_guard<std::mutex> lock(messageMutex_);
return lastMessage_;
}

View File

@@ -27,7 +27,7 @@ private:
};
mqtt::async_client client_;
mqtt::connect_options connOpts_;
mqtt::connect_options options;
Callback callback_;
std::string username_;
std::string password_;
@@ -35,4 +35,4 @@ private:
std::mutex messageMutex_;
};
#endif // MQTTCLIENT_H
#endif //MQTTCLIENT_H

View File

@@ -10,7 +10,8 @@ using namespace std;
CKobuki robot;
int movement();
std::string readMQTT();
MqttClient client("mqtt://localhost:1883", "KobukiRPI", "ishak", "kobuki");
void parseMQTT(std::string message);
MqttClient client("mqtt://145.92.224.21:1883", "KobukiRPI", "ishak", "kobuki"); //create a client object
void setup(){
@@ -18,15 +19,13 @@ void setup(){
robot.startCommunication("/dev/ttyUSB0", true, null_ptr);
client.connect();
client.subscribe("home/commands");
}
int main(){
setup();
while(true){
readMQTT();
parseMQTT(readMQTT());
}
client.run();
return 0;
}
@@ -42,6 +41,26 @@ std::string readMQTT()
return message;
}
void parseMQTT(std::string message){
if(message == "up"){
robot.forward(600);
}
else if(message == "left"){
robot.Rotate(90);
}
else if(message == "down"){
robot.Rotate(-90);
}
else if(message == "stop"){
robot.sendNullMessage();
}
else{
std::cout << "Invalid command" << std::endl;
}
}
int movement()
{
int text;
@@ -143,4 +162,4 @@ void logToFile()
std::this_thread::sleep_for(std::chrono::seconds(2)); // Sleep for 2 seconds
}
}
}

View File

@@ -9,24 +9,20 @@ mqtt_client.username_pw_set("ishak", "kobuki")
mqtt_client.connect("localhost", 1883, 60)
mqtt_client.loop_start()
@app.route('/', methods=["POST"])
@app.route('/', methods=["GET","POST"])
def index():
return render_template('index.html')
@app.route('/move', methods=['POST'])
def move():
# Get the direction from the form data
direction = request.form['direction']
# Publish the direction to the MQTT topic "home/commands"
result = mqtt_client.publish("home/commands", direction)
# Check if the publish was successful
if result.rc == mqtt.MQTT_ERR_SUCCESS:
return jsonify({"message": "Bericht succesvol gepubliceerd"}), 200
else:
return jsonify({"message": "Fout bij het publiceren van bericht"}), 500
data = request.get_json()
direction = data.get("direction")
# Verstuur de richting via MQTT
if direction:
mqtt_client.publish("home/commands", direction) # Het topic kan aangepast worden
return jsonify({"status": "success", "direction": direction})
# Run the Flask application in debug mode
if __name__ == '__main__':
app.run(debug=True)

View File

@@ -1,14 +1,25 @@
window.onload = function () {
const form = document.getElementById("form");
form.onclick = function (event) {
event.preventDefault();
// 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
// Haal de waarde van de knop op
const direction = event.target.value;
// Verstuur de richting naar de server met fetch
fetch("/move", {
method: "GET",
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ direction: direction })
})
.then((response) => {})
.then(() => {
fetch
});
};
};
9
.then(response => response.json())
.then(data => {
console.log("Success:", data);
})
.catch(error => {
console.error("Error:", error);
});
});
});

View File

@@ -12,7 +12,7 @@
<img src="kobuki.jpg" alt="Kobuki Robot" id="robot-image" />
</div>
<div class="button-section">
<form id = "form" action="/" method="post">
<form id = "form" action="/move" method="post">
<button class="btn" name="direction" value="left"></button>
<button class="btn" name="direction" value="up"></button>
<button class="btn" name="direction" value="right"></button>
@@ -23,7 +23,7 @@
<div class="container">
<h1>Sensor Data</h1>
</div>
<script href="../static/script.js"></script>
<script src="../static/script.js"></script>
</body>
</html>