mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-03 20:04:58 +00:00
Refactor safety checks and improve message handling in CKobuki class
This commit is contained in:
@@ -282,7 +282,6 @@ int CKobuki::measure() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
long double CKobuki::gyroToRad(signed short GyroAngle) {
|
long double CKobuki::gyroToRad(signed short GyroAngle) {
|
||||||
|
|
||||||
long double rad;
|
long double rad;
|
||||||
@@ -582,39 +581,38 @@ void CKobuki::Rotate(int degrees) {
|
|||||||
setRotationSpeed(radians);
|
setRotationSpeed(radians);
|
||||||
|
|
||||||
// Sleep for the calculated rotation time
|
// Sleep for the calculated rotation time
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(rotation_time * 1000)));
|
std::this_thread::sleep_for(
|
||||||
|
std::chrono::milliseconds(static_cast<int>(rotation_time * 1000)));
|
||||||
|
|
||||||
// Stop the robot after the rotation
|
// Stop the robot after the rotation
|
||||||
setRotationSpeed(0);
|
setRotationSpeed(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void CKobuki::robotSafety(std::string *pointerToMessage) {
|
void CKobuki::robotSafety(std::string *pointerToMessage) {
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|
||||||
if (parser.data.BumperCenter || parser.data.BumperLeft || parser.data.BumperRight ||
|
if (parser.data.BumperCenter || parser.data.BumperLeft ||
|
||||||
parser.data.CliffLeft || parser.data.CliffCenter || parser.data.CliffRight) {
|
parser.data.BumperRight || parser.data.CliffLeft ||
|
||||||
|
parser.data.CliffCenter || parser.data.CliffRight) {
|
||||||
std::cout << "Safety condition triggered!" << std::endl; // Debug print
|
std::cout << "Safety condition triggered!" << std::endl; // Debug print
|
||||||
*pointerToMessage = "estop";
|
*pointerToMessage = "estop";
|
||||||
|
safetyActive = true;
|
||||||
forward(-100); // reverse the robot
|
forward(-100); // reverse the robot
|
||||||
|
safetyActive = false;
|
||||||
}
|
}
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(100)));
|
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(100)));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CKobuki::robotSafety() {
|
void CKobuki::robotSafety() {
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|
||||||
if (parser.data.BumperCenter || parser.data.BumperLeft || parser.data.BumperRight ||
|
if (parser.data.BumperCenter || parser.data.BumperLeft ||
|
||||||
parser.data.CliffLeft || parser.data.CliffCenter || parser.data.CliffRight) {
|
parser.data.BumperRight || parser.data.CliffLeft ||
|
||||||
|
parser.data.CliffCenter || parser.data.CliffRight) {
|
||||||
std::cout << "Safety condition triggered!" << std::endl; // Debug print
|
std::cout << "Safety condition triggered!" << std::endl; // Debug print
|
||||||
forward(-100); // reverse the robot
|
forward(-100); // reverse the robot
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -633,7 +631,6 @@ void CKobuki::sendNullMessage(){
|
|||||||
0x00 // Placeholder for checksum
|
0x00 // Placeholder for checksum
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
message[10] = message[2] ^ message[3] ^ message[4] ^ message[5] ^ message[6] ^
|
message[10] = message[2] ^ message[3] ^ message[4] ^ message[5] ^ message[6] ^
|
||||||
message[7] ^ message[8] ^ message[9];
|
message[7] ^ message[8] ^ message[9];
|
||||||
|
|
||||||
@@ -641,4 +638,3 @@ message[10] = message[2] ^ message[3] ^ message[4] ^ message[5] ^ message[6] ^
|
|||||||
uint32_t pocet;
|
uint32_t pocet;
|
||||||
pocet = write(HCom, &message, 11);
|
pocet = write(HCom, &message, 11);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -79,6 +79,7 @@ public:
|
|||||||
void robotSafety(std::string *pointerToMessage);
|
void robotSafety(std::string *pointerToMessage);
|
||||||
void robotSafety(); //overload
|
void robotSafety(); //overload
|
||||||
void sendNullMessage();
|
void sendNullMessage();
|
||||||
|
bool safetyActive = false;
|
||||||
KobukiParser parser;
|
KobukiParser parser;
|
||||||
|
|
||||||
|
|
||||||
|
@@ -49,6 +49,10 @@ std::string readMQTT()
|
|||||||
|
|
||||||
void parseMQTT(std::string message)
|
void parseMQTT(std::string message)
|
||||||
{
|
{
|
||||||
|
if(robot.safetyActive){
|
||||||
|
std::cout << "Safety mode active. Ignoring command: " << message << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (message == "up")
|
if (message == "up")
|
||||||
{
|
{
|
||||||
robot.forward(1024);
|
robot.forward(1024);
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
body {
|
body {
|
||||||
font-family: 'Poppins', sans-serif;
|
font-family: "Poppins", sans-serif;
|
||||||
text-align: -webkit-center;
|
text-align: -webkit-center;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
@@ -60,7 +60,7 @@ body {
|
|||||||
|
|
||||||
.btn {
|
.btn {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
background-color: #007BFF;
|
background-color: #007bff;
|
||||||
color: white;
|
color: white;
|
||||||
border: none;
|
border: none;
|
||||||
border-radius: 50%;
|
border-radius: 50%;
|
||||||
@@ -74,31 +74,36 @@ body {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Direction buttons */
|
/* Direction buttons */
|
||||||
.btn:nth-child(1) { /* Left */
|
.btn:nth-child(1) {
|
||||||
|
/* Left */
|
||||||
top: 50%;
|
top: 50%;
|
||||||
left: 50%;
|
left: 50%;
|
||||||
transform: translate(-160%, -50%);
|
transform: translate(-160%, -50%);
|
||||||
}
|
}
|
||||||
|
|
||||||
.btn:nth-child(2) { /* Up */
|
.btn:nth-child(2) {
|
||||||
|
/* Up */
|
||||||
top: 0;
|
top: 0;
|
||||||
left: 50%;
|
left: 50%;
|
||||||
transform: translate(-50%, -35%);
|
transform: translate(-50%, -35%);
|
||||||
}
|
}
|
||||||
|
|
||||||
.btn:nth-child(3) { /* Right */
|
.btn:nth-child(3) {
|
||||||
|
/* Right */
|
||||||
top: 50%;
|
top: 50%;
|
||||||
right: 0;
|
right: 0;
|
||||||
transform: translate(35%, -50%);
|
transform: translate(35%, -50%);
|
||||||
}
|
}
|
||||||
|
|
||||||
.btn:nth-child(4) { /* Down */
|
.btn:nth-child(4) {
|
||||||
|
/* Down */
|
||||||
bottom: 0;
|
bottom: 0;
|
||||||
left: 50%;
|
left: 50%;
|
||||||
transform: translate(-50%, 35%);
|
transform: translate(-50%, 35%);
|
||||||
}
|
}
|
||||||
|
|
||||||
.btn:nth-child(5) { /* Stop Button */
|
.btn:nth-child(5) {
|
||||||
|
/* Stop Button */
|
||||||
top: 50%;
|
top: 50%;
|
||||||
left: 50%;
|
left: 50%;
|
||||||
transform: translate(-50%, -50%);
|
transform: translate(-50%, -50%);
|
||||||
@@ -121,3 +126,17 @@ body {
|
|||||||
background-color: darkred; /* Different hover color for the stop button */
|
background-color: darkred; /* Different hover color for the stop button */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
}
|
||||||
|
|
||||||
|
th,td {
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
padding: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
th {
|
||||||
|
background-color: #f2f2f2;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
@@ -1,8 +1,6 @@
|
|||||||
{% extends 'base.html' %}
|
{% extends 'base.html' %} {% block head %}
|
||||||
{% block head %}
|
|
||||||
<link rel="stylesheet" href="../static/style.css" />
|
<link rel="stylesheet" href="../static/style.css" />
|
||||||
{% endblock %}
|
{% endblock %} {% block content %}
|
||||||
{% block content %}
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
@@ -22,12 +20,27 @@
|
|||||||
<button class="btn" name="direction" value="up">↑</button>
|
<button class="btn" name="direction" value="up">↑</button>
|
||||||
<button class="btn" name="direction" value="right">→</button>
|
<button class="btn" name="direction" value="right">→</button>
|
||||||
<button class="btn" name="direction" value="down">↓</button>
|
<button class="btn" name="direction" value="down">↓</button>
|
||||||
<button class="btn stop-button" name="direction" value="stop">Stop</button>
|
<button class="btn stop-button" name="direction" value="stop">
|
||||||
|
Stop
|
||||||
|
</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1>Sensor Data</h1>
|
<h1>Sensor Data</h1>
|
||||||
|
<div class="data">
|
||||||
|
<table id="sensor-data-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Sensor</th>
|
||||||
|
<th>Value</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<!-- Sensor data rows will be inserted here -->
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<script src="../static/script.js"></script>
|
<script src="../static/script.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
Reference in New Issue
Block a user