Refactor safety checks and improve message handling in CKobuki class

This commit is contained in:
ishak jmilou.ishak
2024-11-06 14:06:15 +01:00
parent 7424c2d033
commit d534940370
5 changed files with 154 additions and 121 deletions

View File

@@ -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;
@@ -569,76 +568,73 @@ void CKobuki::forward(int speedvalue) {
/// @param degrees Rotation in degrees /// @param degrees Rotation in degrees
void CKobuki::Rotate(int degrees) { void CKobuki::Rotate(int degrees) {
// convert raidans to degrees // convert raidans to degrees
float radians = degrees * PI / 180.0; float radians = degrees * PI / 180.0;
// Calculate the rotation speed in radians per second // Calculate the rotation speed in radians per second
double radpersec = 1; double radpersec = 1;
// calculator rotation time and give absolute value // calculator rotation time and give absolute value
float rotation_time = std::abs(radians / radpersec); 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 // Use original function to set the rotation speed in mm/s
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(rotation_time * 1000))); setRotationSpeed(radians);
// Stop the robot after the rotation // Sleep for the calculated rotation time
setRotationSpeed(0); 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(std::string *pointerToMessage) { void CKobuki::robotSafety(std::string *pointerToMessage) {
while (true) { 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
*pointerToMessage = "estop";
forward(-100); // reverse the robot
}
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(100)));
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
*pointerToMessage = "estop";
safetyActive = true;
forward(-100); // reverse the robot
safetyActive = false;
} }
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 ||
parser.data.CliffLeft || parser.data.CliffCenter || parser.data.CliffRight) {
std::cout << "Safety condition triggered!" << std::endl; // Debug print
forward(-100); // reverse the robot
}
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(){ void CKobuki::sendNullMessage() {
unsigned char message[11] = { unsigned char message[11] = {
0xaa, // Start byte 1 0xaa, // Start byte 1
0x55, // Start byte 2 0x55, // Start byte 2
0x08, // Payload length (the first 2 bytes dont count) 0x08, // Payload length (the first 2 bytes dont count)
0x01, // payload type (0x01 = control command) 0x01, // payload type (0x01 = control command)
0x04, // Control byte or additional identifier 0x04, // Control byte or additional identifier
0x00, // Lower byte of speed value 0x00, // Lower byte of speed value
0x00, // Upper byte of speed value 0x00, // Upper byte of speed value
0x00, // Placeholder for radius 0x00, // Placeholder for radius
0x00, // Placeholder for radius 0x00, // Placeholder for radius
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];
// Send the message // Send the message
uint32_t pocet; uint32_t pocet;
pocet = write(HCom, &message, 11); pocet = write(HCom, &message, 11);
} }

View File

@@ -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;

View File

@@ -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);

View File

@@ -1,8 +1,8 @@
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;
} }
/* This is my code for my navbar */ /* This is my code for my navbar */
@@ -29,95 +29,114 @@ body {
} }
.connectButton { .connectButton {
border-radius: 10px; border-radius: 10px;
height: 100%; height: 100%;
width: 100px; width: 100px;
box-shadow: none; box-shadow: none;
border: none; border: none;
font-size: 1rem; font-size: 1rem;
height: 40px; height: 40px;
background-color: #b3ffb3; background-color: #b3ffb3;
} }
/* end navbar */ /* end navbar */
.container { .container {
display: flex; display: flex;
justify-content: space-around; justify-content: space-around;
align-items: center; align-items: center;
margin-top: 50px; margin-top: 50px;
width: 80%; width: 80%;
background-color: white; background-color: white;
border-radius: 20px; border-radius: 20px;
box-shadow: 0px 8px 16px rgba(0, 0, 0, 0.2); box-shadow: 0px 8px 16px rgba(0, 0, 0, 0.2);
padding: 40px; padding: 40px;
} }
.button-section { .button-section {
position: relative; position: relative;
width: 150px; width: 150px;
height: 150px; height: 150px;
} }
.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%;
width: 60px; width: 60px;
height: 60px; height: 60px;
font-size: 1.2em; font-size: 1.2em;
text-align: center; text-align: center;
line-height: 60px; line-height: 60px;
cursor: pointer; cursor: pointer;
transition: transform 0.2s ease, background-color 0.2s ease; transition: transform 0.2s ease, background-color 0.2s ease;
} }
/* Direction buttons */ /* Direction buttons */
.btn:nth-child(1) { /* Left */ .btn:nth-child(1) {
top: 50%; /* Left */
left: 50%; top: 50%;
transform: translate(-160%, -50%); left: 50%;
transform: translate(-160%, -50%);
} }
.btn:nth-child(2) { /* Up */ .btn:nth-child(2) {
top: 0; /* Up */
left: 50%; top: 0;
transform: translate(-50%, -35%); left: 50%;
transform: translate(-50%, -35%);
} }
.btn:nth-child(3) { /* Right */ .btn:nth-child(3) {
top: 50%; /* Right */
right: 0; top: 50%;
transform: translate(35%,-50%); right: 0;
transform: translate(35%, -50%);
} }
.btn:nth-child(4) { /* Down */ .btn:nth-child(4) {
bottom: 0; /* Down */
left: 50%; bottom: 0;
transform: translate(-50%, 35%); left: 50%;
transform: translate(-50%, 35%);
} }
.btn:nth-child(5) { /* Stop Button */ .btn:nth-child(5) {
top: 50%; /* Stop Button */
left: 50%; top: 50%;
transform: translate(-50%, -50%); left: 50%;
background-color: red; /* Distinct color for the stop button */ transform: translate(-50%, -50%);
width: 60px; /* Slightly larger for emphasis */ background-color: red; /* Distinct color for the stop button */
height: 60px; /* Slightly larger for emphasis */ width: 60px; /* Slightly larger for emphasis */
line-height: 60px; /* Center text vertically */ height: 60px; /* Slightly larger for emphasis */
line-height: 60px; /* Center text vertically */
} }
/* Hover effects */ /* Hover effects */
.btn:hover { .btn:hover {
background-color: #0056b3; background-color: #0056b3;
} }
.btn:active { .btn:active {
background-color: #004494; background-color: #004494;
} }
.stop-button:hover { .stop-button:hover {
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;
} }

View File

@@ -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>