-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tutorial_7_red_ball_follow_pid.py
90 lines (71 loc) · 2.54 KB
/
Tutorial_7_red_ball_follow_pid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
#This code makes bveeta mini robot to follow red ball with PID control
#Bizbot Technology
#Programmer: Ts. Khairul
import cv2
import serial
import time
import numpy as np
# PID controller parameters
kp = 0.5
ki = 0.0
kd = 0.1
prev_error = 0
integral = 0
# Connect to the serial port to send commands to the robot
ser = serial.Serial('/dev/ttyUSB1', 57600) # Replace with the correct port and baud rate for your robot
time.sleep(2)
# Load the image or video
cap = cv2.VideoCapture(0) # Use 0 for webcam or specify file path for video
# Define the lower and upper bounds of the red color in HSV color space
red_lower = (0, 120, 70)
red_upper = (10, 255, 255)
while True:
# Capture frame-by-frame
ret, frame = cap.read()
if not ret:
break
# Convert the frame to HSV color space
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Threshold the HSV image to get only red colors
mask = cv2.inRange(hsv, red_lower, red_upper)
# Find contours of the red color in the mask image
contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Draw a rectangle around the ball if it is detected
if len(contours) > 0:
# Find the largest contour (assuming it is the ball)
largest_contour = max(contours, key=cv2.contourArea)
(x, y, w, h) = cv2.boundingRect(largest_contour)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Compute the center of the ball
cx = int(x + w/2)
cy = int(y + h/2)
# Calculate the error between the center of the ball and the center of the frame
error = cx - 320
# Update the integral and derivative terms of the PID controller
integral += error
derivative = error - prev_error
prev_error = error
# Compute the output of the PID controller
output = kp*error + ki*integral + kd*derivative
# Send commands to the robot to follow the ball
if output < 0:
# Turn left
ser.write(b"m 0 -30\r\n")
elif output > 0:
# Turn right
ser.write(b"m -30 0\r\n")
else:
# Move forward
ser.write(b"m -80 -80\r\n")
else:
# Stop the robot if the ball is not detected
ser.write(b"m 0 0\r\n")
# Display the resulting image
cv2.imshow('frame', frame)
# Exit the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the capture and destroy all windows
cap.release()
cv2.destroyAllWindows()