Trybotics Logo

Ball Balancing PID System

DESCRIPTION

This system holds a ball in balance on a plate. A webcam films the system and a python program analyzes the images to find the position of the ball. The python program calculates the tilting of the tray to prevent the ball from falling.

A proportional-integral-derivative (PID) regulator is used to compensate the movements of the ball. The position and speed of the ball are measured by the camera and these measurements are used by the PID regulator in the python program.

Sorry about the English faults, it’s not my mother tongue.

Description:

PCB manufacturing

I drew the pcb with eagle then PCBWay.com manufactured it.


PCB assembly :

I soldered all smd components without paste flux. I don't recommend you to do like me. Instead, follow this tutorial to solder smd components: https://www.instructables.com/id/SMD-Hand-Soldering/


The PCB is powered with a 5-6v 2A power supply.
PCB components :

Role of the PCB

The PCB communicates with the computer continuously. This PCB serves only to control the servomotors.

Attachments

Description:

The PCB contains the same microcontroller as the arduino leonardo. Therefore it can be programmed with Arduino software. The code can be downloaded here.

Before programming the PCB, you have to burn the bootloader: Arduino as ISP and Arduino Bootloaders

Burning the Bootloader

  1. You need an Arduino
  2. Upload the ArduinoISP sketch (arduino software / Examples / ArduinoISP) onto your Arduino board
  3. Make the connections as in the picture
  4. Select "Arduino Leonardo" from the Tools > Board menu.
  5. Select "Arduino as ISP" from Tools > Programmer
  6. Run Tools > Burn Bootloader
  7. The process can take several minutes.
  8. Disconnect all cables.

After this step you have to connect the pcb to the computer with a USB cable and upload this code.

Description:

Python Program
5 More Images

The most important code is in the computer that controls the system. You can find the code here.

How does it work. The first step is to find the position of the ball. The camera sends the computer live video. The python program receives the video and has to process it. I use the OpenCV library to do the image processing. The program detects the ball thanks to its color. Here the ball is orange, the program will then average the position of all the orange pixels of the image to find the position of the ball. With OpenCV all this is quite simple. I recommend this tutorial for color recognition with OpenCV: https://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/

Now that we have the ball position we can calculate the inclination of the board with PID (proportional, integral, and derivative) control. The regulation is done in three stages. The first step is the simplest. Imagine that we want to stabilize the ball in the center of the board. The further away the ball is from the center, the more it will be necessary to tilt the board. Then you have to measure the speed of the ball: the faster the ball moves away from the center, the more you have to tilt the board.

Description:

Thanks for reading! If you have any questions, do not hesitate to write a comment.

You can also vote for me in the PCB contest.


YOU MIGHT ALSO LIKE