Skip to content

Commit

Permalink
* adjusting object detection parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
JuliusHendrix committed May 8, 2022
1 parent 1c2bda6 commit f3da454
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
14 changes: 10 additions & 4 deletions Main/rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,43 @@ def rectangle(mode, rendering):
state = 'straight'

# parameters
time_to_drive_2_meters = distance_to_time(200)
show_video = True if rendering == 1 else False

# distances to travel (in cm)
distances = [distance_to_time(200), distance_to_time(100), distance_to_time(200), distance_to_time(100)]
time_correction = 0
turns = 0
has_dodged = False

# initialize timer
timer = Timer()
timer.start_timer()

try:
bb.move(1)
bb.set_camera_top_servo_angle(-20)
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
print(f'{state = }')

if mode == 2:
obj, direction = detector.check_for_object(frame=frame.array, distance_to_dodge=25,
if mode == 2 and not has_dodged:
obj, direction = detector.check_for_object(frame=frame.array, distance_to_dodge=200,
show_video=show_video)
if obj is not None:
print("OBJECT DETECTED!")
timer.pause_timer()
bb.move(0)
bb.around_obstacle(direction=direction)
has_dodged = True
time_correction += distance_to_time(80)
timer.resume_timer()
bb.move(1)

if state == 'straight':
if timer.get_timer() > distances[turns]:
if timer.get_timer() > distances[turns] - time_correction:
if turns < 4:
state = 'turn'
turns += 1
time_correction = 0
else:
bb.move(0)
break # back at start
Expand Down
14 changes: 10 additions & 4 deletions Main/straight.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ def straight(mode, rendering):
bb = BounceBot()

# initialize object detector
detector = ObjectDetector()
detector = ObjectDetector(threshold=0.4)

# initialize state
state = 'straight'
has_turned = False
has_dodged = False

# parameters
time_to_drive_2_meters = distance_to_time(200)
time_correction = 0
show_video = True if rendering == 1 else False

# initialize timer
Expand All @@ -48,21 +50,25 @@ def straight(mode, rendering):

try:
bb.move(1)
bb.set_camera_top_servo_angle(-20)
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
print(f'{state = }')

if mode == 2:
obj, direction = detector.check_for_object(frame=frame.array, distance_to_dodge=25,
if mode == 2 and not has_dodged:
obj, direction = detector.check_for_object(frame=frame.array, distance_to_dodge=200,
show_video=show_video)
if obj is not None:
print("OBJECT DETECTED!")
timer.pause_timer()
bb.move(0)
bb.around_obstacle(direction=direction)
has_dodged = True
time_correction += distance_to_time(80)
timer.resume_timer()
bb.move(1)

if state == 'straight':
if timer.get_timer() > time_to_drive_2_meters:
if timer.get_timer() > time_to_drive_2_meters - time_correction:
if not has_turned:
state = 'turn'
has_turned = True
Expand Down
20 changes: 17 additions & 3 deletions ObjectDetection/object_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class ObjectDetector:
def __init__(self, object_names=["CUP", "BOOK", "BOTTLE"], threshold=.45):
def __init__(self, object_names=["CUP", "BOOK", "BOTTLE", "BED"], threshold=.45):
self.class_names = []

file_path = os.path.realpath(__file__)
Expand All @@ -35,9 +35,10 @@ def check_for_object(self, frame, distance_to_dodge=320, show_video=False):
for classId, confidence, box in zip(classIds.flatten(), confs.flatten(), bbox):
if self.class_names[classId - 1].upper() in self.object_names:
color = (0, 255, 0)
found_objects.append((classId, confidence, box))
# found_objects.append((classId, confidence, box))
else:
color = (255, 0, 0)
found_objects.append((classId, confidence, box))
cv2.rectangle(frame, box, color=color, thickness=2)
cv2.putText(frame, self.class_names[classId - 1].upper(), (box[0] + 10, box[1] + 30),
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
Expand All @@ -49,7 +50,20 @@ def check_for_object(self, frame, distance_to_dodge=320, show_video=False):
dodge_directions = []
for object in found_objects:
lower_bounding_line = object[2][1] + object[2][3]
if lower_bounding_line > distance_to_dodge:
upper_bounding_line = object[2][1]

width = object[2][2]
height = object[2][3]

if lower_bounding_line > distance_to_dodge\
and 250 > width > 120 \
and 250 > height > 120:

print(f'{lower_bounding_line = }')
print(f'{upper_bounding_line = }')
print(f'{width = }')
print(f'{height = }')

close_objects.append(object)

left_edge = object[2][0]
Expand Down
16 changes: 12 additions & 4 deletions RobotControl/bouncebot_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def __init__(self):
super().__init__()
# initialize servo control
self.turret_servo = Servo(PWM('P3'))
self.turret_servo.angle(0)

# initialize solenoid control
self.turret_solenoid_1 = Pin("D0")
Expand All @@ -37,9 +36,15 @@ def __init__(self):
self.camera_servo2_angular_speed = 3 # deg / frame

self.turret_servo_current_angle = 0
self.turret_servo_max_angle = 45
self.turret_servo_max_angle = 35
self.turret_servo_angular_speed = 3 # deg / frame

# set all servo angles to 0
self.set_camera_bottom_servo_angle(0)
self.set_camera_top_servo_angle(0)
self.set_turret_servo_angle(0)
self.set_direction_servo_angle(0)

def reset_servo_angles(self):
self.set_camera_bottom_servo_angle(0)
self.set_camera_top_servo_angle(0)
Expand Down Expand Up @@ -155,6 +160,7 @@ def turn_90(self, direction='right'):
time.sleep(1.4)
self.move(0)
self.set_direction_servo_angle(0)
time.sleep(0.5)

def turn_270(self, direction='right'):
"""
Expand Down Expand Up @@ -188,6 +194,7 @@ def turn_270(self, direction='right'):
self.move(1)
time.sleep(0.2)
self.move(0)
time.sleep(0.5)

def u_turn(self, direction='right'):
if direction == 'right':
Expand All @@ -202,13 +209,14 @@ def u_turn(self, direction='right'):
self.set_direction_servo_angle(angle)
time.sleep(1)
self.move(1)
time.sleep(3.9)
time.sleep(4.0)
self.move(0)
self.set_direction_servo_angle(0)
time.sleep(0.5)
self.move(1)
time.sleep(0.4)
time.sleep(0.6)
self.move(0)
time.sleep(0.5)

def around_obstacle(self, direction='right'):
"""
Expand Down

0 comments on commit f3da454

Please sign in to comment.