-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrialWithThreading.py
More file actions
149 lines (116 loc) · 4.72 KB
/
trialWithThreading.py
File metadata and controls
149 lines (116 loc) · 4.72 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import time
import threading
from picamera2 import Picamera2
from ultralytics import YOLO
import RPi.GPIO as GPIO
# Setup GPIO
GPIO.setmode(GPIO.BCM) # Use BCM pin numbering
GPIO.setup(17, GPIO.OUT) # Arm 1 & Arm 2
GPIO.setup(27, GPIO.OUT) # LASER control
GPIO.setup(23, GPIO.OUT) # HEAD control
# Initialize PWM for head speed control
head_pwm = GPIO.PWM(23, 1000) # Head control (Pin 23), frequency 100 Hz
head_pwm.start(0) # Start with 0% duty cycle (stopped)
# Initialize the camera
picam2 = Picamera2()
picam2.preview_configuration.main.size = (640, 640)
picam2.preview_configuration.main.format = "RGB888"
picam2.preview_configuration.align()
picam2.configure("preview")
picam2.start()
# Load the YOLO model
model = YOLO("best.pt")
print(model.names)
# Class IDs for object detection
birds_and_flock = [1, 2] # Bird and flock
humans_and_fake_birds = [0, 3] # Fake bird and human
# Shared detection variable
detected_objects = []
detection_lock = threading.Lock()
# Interval mode cycle durations
HEAD_ROTATE_TIME = 3 # 3 seconds
DETERRENT_TIME = 5 # 5 seconds
# Global Flags
in_interval_mode = True
bird_response_running = False # Prevent multiple deterrent threads
def detect_objects():
"""Continuously detects objects and updates the global variable."""
global detected_objects
while True:
frame = picam2.capture_array()
start_time = time.time()
results = model(frame, imgsz=640)
end_time = time.time()
inference_time = end_time - start_time
# Update detected objects safely
with detection_lock:
detected_objects = results[0].boxes.cls.tolist()
print(f"Inference Time: {inference_time:.2f} seconds")
print(f"Detected Objects: {[model.names[int(cls)] for cls in detected_objects]}")
time.sleep(0.5) # Small delay to avoid excessive processing
def bird_detected_response():
"""Activates deterrents when a bird is detected and stops when birds leave."""
global in_interval_mode, bird_response_running
with detection_lock:
bird_response_running = True # Mark deterrent as active
in_interval_mode = False # Stop interval mode
print("Bird detected! Activating deterrents.")
# Activate deterrents
GPIO.output(27, GPIO.HIGH) # Turn on laser
GPIO.output(17, GPIO.HIGH) # Turn on arms
head_pwm.ChangeDutyCycle(0) # Stop head rotation
# # Keep deterrents ON while birds are present
# time_elapsed = 0
# while time_elapsed < 20: # Max 20 seconds deterrent time
# with detection_lock:
# if not any(cls in birds_and_flock for cls in detected_objects):
# print("Birds left, stopping deterrents early.")
# break # Exit early if birds are gone
# else:
# print("Still detecting...")
time.sleep(5)
# time_elapsed += 1
# Turn off deterrents
GPIO.output(27, GPIO.LOW)
GPIO.output(17, GPIO.LOW)
print("Deterrents turned off. Returning to interval mode.")
with detection_lock:
bird_response_running = False # Allow future deterrent activations
in_interval_mode = True # Resume interval mode
def interval_mode_cycle():
"""Runs the interval mode cycle indefinitely, but pauses when birds are detected."""
while True:
if in_interval_mode:
print("Interval Mode: Rotating Head.")
head_pwm.ChangeDutyCycle(90) # Rotate head
time.sleep(HEAD_ROTATE_TIME)
print("Interval Mode: Stopping head, activating deterrents.")
head_pwm.ChangeDutyCycle(0) # Stop head rotation
GPIO.output(27, GPIO.HIGH) # Turn on laser
GPIO.output(17, GPIO.HIGH) # Turn on arms
time.sleep(DETERRENT_TIME)
print("Interval Mode: Turning off deterrents.")
GPIO.output(27, GPIO.LOW)
GPIO.output(17, GPIO.LOW)
time.sleep(1) # Small delay to prevent CPU overload
# Start detection thread
detection_thread = threading.Thread(target=detect_objects, daemon=True)
detection_thread.start()
# Start interval mode thread (ALWAYS RUNNING)
interval_thread = threading.Thread(target=interval_mode_cycle, daemon=True)
interval_thread.start()
# Main control loop
try:
while True:
with detection_lock:
if any(cls in birds_and_flock for cls in detected_objects):
if not bird_response_running: # Prevent multiple deterrent activations
threading.Thread(target=bird_detected_response, daemon=True).start()
time.sleep(0.5) # Small delay to balance CPU usage
except KeyboardInterrupt:
print("Program interrupted by the user.")
finally:
picam2.close()
head_pwm.stop()
GPIO.cleanup()
print("Resources cleaned up!")