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
30 Commits
a46900dfd2
...
25a179ded6
Author | SHA1 | Date | |
---|---|---|---|
25a179ded6 | |||
a3ed0f0f30 | |||
c246258c6d | |||
94f1b40e74 | |||
d845d01e0a | |||
|
005021e2d5 | ||
|
38dfccbd28 | ||
d9f0abd86c | |||
|
85d54f8422 | ||
|
99676cc83a | ||
d09352861f | |||
ff599e127d | |||
cf59adae8b | |||
7449f713fa | |||
|
705ffcd98d | ||
|
ca9b81c03e | ||
b64ccd51eb | |||
7e1e46bcc3 | |||
60f27af05d | |||
|
17e1399643 | ||
|
d673b4e0ad | ||
e708cbb6ea | |||
|
41034ba85c | ||
|
21ed9c5080 | ||
|
19bc1a4184 | ||
b8b61df756 | |||
896897d1b0 | |||
fd4615f0af | |||
9c5825fd61 | |||
|
6f17c1ed6f |
@@ -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);
|
||||
}
|
||||
|
||||
|
@@ -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_;
|
||||
}
|
@@ -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
|
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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)
|
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -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>
|
||||
|
||||
|
Reference in New Issue
Block a user