Ball Tracking 180° Camera

2,168

27

About: I enjoy making projects and tinkering around with electronics.

Intro: Ball Tracking 180° Camera

Welcome to my first project! I'm excited to share what I've made and showing you the steps to build your own tracking camera. This project was made possible using the OpenCV library in conjunction with Python.

Step 1: Gather Materials

  • Raspberry Pi Model B 2 (or any other model)
  • L298N H-Bridge Motor Driver
  • Motor with Gear Housing
  • USB Webcam
  • Jumper Wires
  • Machine Screws with Nuts
  • Gears
  • Epoxy / Hot Glue
  • Optional : Laser

Step 2: Mechanics

Using a scrap piece of wood (the one I have is pretty beat up which is ok), mount the motor onto a spot that is not in the middle. Then, attach a small gear onto the motor. The hole on the gear may have to be enlarged to fit over the motor's fitting.

The next step will be mounting the large gear (which will be loose) so that its teeth connect with the small gear's teeth. This was mounted onto the board using hot glue after roughing up the wood with sandpaper for a better bond.

After the gears are in place, it's time to attach the webcam onto the large gear. Here, I have removed the webcam from its housing and used just the core circuit board of the webcam for easier mounting. The webcam was attached using epoxy glue for a strong bond.

The last component to be mounted is optional - for the L298N H-bridge. This can be mounted by simply drilling four holes in the board and mounting the board using machine screws and hex nuts.

Step 3: Wiring

Now to hook everything up together. The motor's two wires will connect directly to the L298N H-bridge in either one of the two terminal connectors on the left or right side of the board (I chose left). Two wires are needed to connect the 5V and Ground of the L298N to the 5V and Ground of the Raspberry Pi for power. Then, two female-female jumper wires are needed to connect from the L298N to the Pi's pins 17 and 18. The webcam simply connects into one of the Pi's USB ports. That's all the wiring!

Step 4: Code

Now for the most challenging aspect of this project.

I used the OpenCV library with Python for tracking the ball in real time. The program also utilizes the gpiozero library that comes with the Pi to turn the motor according to the x-coordinates of the ball that OpenCV determines. The code is able to determine the ball's position based on its yellow color, which should be unique from the background in order to be effective. A lower and upper color range is supplied to the program for it to determine where the ball is. OpenCV then calls the .inRange() function with the parameters of: the current frame (from the webcam), and the lower and upper color boundaries. After the coordinates of the ball on the frame are determined, the program tells the motor to turn if the ball is not in the center (x coordinate in range of 240 - 400 in a 640 pixel wide frame). The motor will turn more if the ball is more off center, and turn less when the ball is closer to the center.

And that's how the code works.

Note: if you're going to use the code, you must have OpenCV installed. Also, if the motor turns the wrong way, simply reverse the wires going into the L289N, or reverse the controlling gpio wires connected to the Pi.

Step 5: Action!

Check out the video to see it in action.

Share

    Recommendations

    • Optics Contest

      Optics Contest
    • Halloween Contest 2018

      Halloween Contest 2018
    • Plastics Contest

      Plastics Contest

    Discussions