I introduce you to…
Ball Tracking Robot
A self-driving robot controlled by a Raspberry Pi 4 on 3 wheels, and propelled by 2 motors. It uses a Pi camera to detect and steer towards the designated red ball, and uses 3 pairs of Adafruit ultrasonic sensors to detect the distance from the ball.
Engineer | School | Area of Interest | Grade |
---|---|---|---|
Kyuhyun K | Leigh High School | Software Engineering | Incoming Sophomore |
First Milestone
For my first milestone, I set up all the software parts of the base project, including the Raspberry Pi 4, and got my computer to remote SSH into it.
On the mechanical side, I connected one of the ultrasonic sensors through a breadboard, 2 DC motors, and a Pi camera to my Raspberry Pi using the provided dupont wires.
I got the motors to turn, print the distance detected by the ultrasonic sensor, and capture pictures through the Pi camera using the 3 programs listed below.
My biggest challenge so far was getting the camera to take a photo, because at first I got a Segementation Fault error, which was was extremely unsual to happen in Python, as pointed out by my helpful and patient instructor, Daniel Lin.
After many loops of uninstalling and reinstalling extensions, and even reflashing the Pi, I got the camera to take a photo.
Daniel also provided me with some code that converts the red parts of the image to white and the rest to black.
The code is supposed to help distinguish the red ball from the rest of the environment.
I reached this milestone at the end of my 1st week, and next week, I hope to finish the base project.
That means I need to finish putting together the robot, and integrating the Pi camera into my program.
Code for testing components
Motors
from gpiozero import Motor
import time
rmotor = Motor(18, 23)
lmotor = Motor(13,24)
rmotor.backward()
lmotor.forward()
time.sleep(2)
rmotor.reverse()
lmotor.reverse()
time.sleep(2)
lmotor.stop()
rmotor.stop()
Ultrasonic Sensor
from gpiozero import DistanceSensor
ultrasonic = DistanceSensor(echo = 17, trigger = 4)
#Only use one of these while loops at a time and comment out the other one
while True:
print(ultrasonic.distance)
while True:
ultrasonic.wait_for_in_range()
print("In range")
ultrasonic.wait_for_out_of_range()
print("Out of range")
Camera
from picamera2 import Picamera2
import cv2
import time
# Initialize and configure the camera
picam2 = Picamera2()
picam2.configure(picam2.create_preview_configuration(main={"format": "RGB888", "size": (640, 480)}))
picam2.start()
time.sleep(0.5)
# Capture one frame
frame = picam2.capture_array()
if frame is None or frame.size == 0:
raise ValueError("Empty frame captured!")
# Convert from RGB to BGR for OpenCV compatibility
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
# Save to file
cv2.imwrite("frame.jpg", frame)
picam2.close()
print("Frame saved as frame.jpg")
Schematic
Bill of Materials
Part | Note | Price | Link |
---|---|---|---|
Raspberry Pi Kit | What the item is used for | $95.19 | Link |
Robot Chassis | What the item is used for | $18.99 | Link |
Screwdriver Kit | What the item is used for | $5.94 | Link |
Ultrasonic Sensor | What the item is used for | $9.99 | Link |
H Bridges | What the item is used for | $8.99 | Link |
Pi Cam | What the item is used for | $12.86 | Link |
Electronics Kit | What the item is used for | $11.98 | Link |
Motors | What the item is used for | $11.98 | Link |
SD Card Adapter | What the item is used for | $9.99 | Link |
DMM | What the item is used for | $11 | Link |
Champion sports ball | What the item is used for | $16.73 | Link |
AA batteries | What the item is used for | $18.74 | Link |
USB power bank & cable | What the item is used for | $16.19 | Link |