4 Commits

Author SHA1 Message Date
6fe28f997a edited kobuki speedvalue for safety 2025-01-07 12:51:24 +01:00
1bf9ebddab added mutex in python 2025-01-07 11:30:04 +01:00
585a0e9a52 fix thread crash 2025-01-07 11:19:47 +01:00
cb988a5260 store new image in processed_image 2025-01-07 11:09:33 +01:00
2 changed files with 44 additions and 31 deletions

View File

@@ -576,7 +576,7 @@ void CKobuki::robotSafety(std::string *pointerToMessage) {
parser.data.CliffCenter || parser.data.CliffRight) { 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";
forward(-100); // reverse the robot forward(-300); // reverse the robot
} }
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(100))); std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(100)));
} }
@@ -590,7 +590,7 @@ void CKobuki::robotSafety() {
parser.data.BumperRight || parser.data.CliffLeft || parser.data.BumperRight || parser.data.CliffLeft ||
parser.data.CliffCenter || parser.data.CliffRight) { 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(-300); // reverse the robot
} }
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(100))); std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(100)));

View File

@@ -3,6 +3,7 @@ import paho.mqtt.client as mqtt
from ultralytics import YOLO from ultralytics import YOLO
import cv2 import cv2
import numpy as np import numpy as np
import threading
app = Flask(__name__) app = Flask(__name__)
@@ -11,34 +12,40 @@ model = YOLO("yolo11n.pt") # pretrained YOLO11n model
kobuki_message = "" kobuki_message = ""
latest_image = None latest_image = None
processed_image = None
yolo_results = [] yolo_results = []
# https://medium.com/@Mert.A/how-to-segment-objects-with-yolov11-68593eb49fa8 # Lock for thread-safe access to shared variables
lock = threading.Lock()
# List of class names (example for COCO dataset)
yolo_classes = list(model.names.values()) yolo_classes = list(model.names.values())
def on_message(client, userdata, message): def on_message(client, userdata, message):
global kobuki_message, latest_image, yolo_results global kobuki_message, latest_image, processed_image, yolo_results
if message.topic == "kobuki/data": if message.topic == "kobuki/data":
kobuki_message = str(message.payload.decode("utf-8")) kobuki_message = str(message.payload.decode("utf-8"))
elif message.topic == "kobuki/cam": elif message.topic == "kobuki/cam":
latest_image = np.frombuffer(message.payload, np.uint8) with lock: # Lock the shared variables between threads so they can't be accessed at the same time and you cant have half processed images
latest_image = cv2.imdecode(latest_image, cv2.IMREAD_COLOR) latest_image = np.frombuffer(message.payload, np.uint8)
# Process the image with YOLO latest_image = cv2.imdecode(latest_image, cv2.IMREAD_COLOR)
results = model(latest_image) # Process the image with YOLO
yolo_results = [] results = model(latest_image)
for result in results: yolo_results = []
for box in result.boxes: processed_image = latest_image.copy() # Create a copy for processing
class_id = int(box.cls.item()) # Convert to integer for result in results:
class_name = yolo_classes[class_id] for box in result.boxes:
yolo_results.append({ class_id = int(box.cls.item())
"class": class_name, class_name = yolo_classes[class_id]
"confidence": box.conf.item(), yolo_results.append({
"bbox": box.xyxy.tolist() "class": class_name,
}) "confidence": box.conf.item(),
# Draw bounding box on the image "bbox": box.xyxy.tolist()
x1, y1, x2, y2 = map(int, box.xyxy[0]) })
cv2.rectangle(latest_image, (x1, y1), (x2, y2), (0, 255, 0), 2) # Draw bounding box on the processed image
cv2.putText(latest_image, f"{class_name} {box.conf.item():.2f}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) x1, y1, x2, y2 = map(int, box.xyxy[0])
cv2.rectangle(processed_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(processed_image, f"{class_name} {box.conf.item():.2f}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# Create an MQTT client instance # Create an MQTT client instance
mqtt_client = mqtt.Client() mqtt_client = mqtt.Client()
@@ -48,20 +55,21 @@ mqtt_client.loop_start()
mqtt_client.subscribe("kobuki/data") mqtt_client.subscribe("kobuki/data")
mqtt_client.subscribe("kobuki/cam") mqtt_client.subscribe("kobuki/cam")
mqtt_client.on_message = on_message # this lines needs to be under the function definition otherwise it cant 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
@app.route('/') @app.route('/')
def index(): def index():
return render_template('index.html') return render_template('index.html')
@app.route('/control', methods=["GET","POST"]) @app.route('/control', methods=["GET", "POST"])
def control(): def control():
if request.authorization and request.authorization.username == 'ishak' and request.authorization.password == 'kobuki': if request.authorization and request.authorization.username == 'ishak' and request.authorization.password == 'kobuki':
return render_template('control.html') return render_template('control.html')
else: else:
return ('Unauthorized', 401, {'WWW-Authenticate': 'Basic realm="Login Required"'}) return ('Unauthorized', 401, {'WWW-Authenticate': 'Basic realm="Login Required"'})
@app.route('/move', methods=['POST']) @app.route('/move', methods=['POST'])
def move(): def move():
data = request.get_json() data = request.get_json()
@@ -78,19 +86,24 @@ def move():
def data(): def data():
return kobuki_message return kobuki_message
@app.route('/image') @app.route('/image')
def image(): def image():
global latest_image global processed_image
if latest_image is not None: with lock: # Lock the shared variables between threads so they can't be accessed at the same time and you cant have half processed images
_, buffer = cv2.imencode('.jpg', latest_image) if processed_image is not None:
return Response(buffer.tobytes(), mimetype='image/jpeg') _, buffer = cv2.imencode('.jpg', processed_image)
else: return Response(buffer.tobytes(), mimetype='image/jpeg')
return "No image available", 404 else:
return "No image available", 404
@app.route('/yolo_results', methods=['GET']) @app.route('/yolo_results', methods=['GET'])
def yolo_results_endpoint(): def yolo_results_endpoint():
global yolo_results global yolo_results
return jsonify(yolo_results) with lock:
return jsonify(yolo_results)
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True, port=5000) app.run(debug=True, port=5000)