Ball Balancing PID System

95,850

773

193

About: Hi, I'm Johan Link, a 18 years old student living in Switzerland. I love robotics, computers, 3D printing, photography and skate.

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.

Step 1: PCB

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.

Step 2: 3D Printing

Warning : inferieur.stl, superieur.stl and plateau.stl files are not 3D printable. inferieur.stl and superieur.stl are made of acrylic. You can use a CNC to make these parts, but you can also cut them yourself in an acrylic sheet because these parts don't have a very complicated design. plateau.stl can be cut in cardboard.

Step 3: Parts Assembly

Step 4: PCB Programming

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.

Step 5: Python Program

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.

Step 6: Conclusion

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.

PCB Contest

First Prize in the
PCB Contest

Share

    Recommendations

    • Backyard Contest

      Backyard Contest
    • Frozen Treats Challenge

      Frozen Treats Challenge
    • Pets Challenge

      Pets Challenge

    193 Discussions

    0
    None
    Nnkye

    Question 9 days ago

    So may I ask, what is the objective of this proyect? Or your own objective if you have any

    0
    None
    anagabriellanc

    Question 11 days ago

    Hi!
    i really like your proyect, in Fact i like it so much that I wish to construct it. My question is, for this proyect to work Do i have to use the same USB Webcam that you used? If so then I might have a problem, because I don't think i can buy it online and I don't think there are any places here in my country that sell that same one.
    That was my question, Sorry if I had english faults english is not my first language.
    Thanks!

    1 answer
    0
    None
    eabbasov

    17 days ago

    Hi Johan,Thank you for the beautiful project. I made this model for my museum. Works fine.One issue is that although I am using the same camera setup image is not rectified and the blue circle goes outside the camera image
    Algorithm sees the boundaries of the red platform as bigger than it is and because of that the balancing is not very well
    Can you tell me how I can fix these?

    balcam.jpgbalkomp.jpg
    9 replies
    1
    None
    Johan Linkeabbasov

    Reply 17 days ago

    Awesome! Thank you so much for the pictures! I love it!

    0
    None
    eabbasovJohan Link

    Reply 16 days ago

    John, İs the view on screen looks normal?

    0
    None
    Johan Linkeabbasov

    Reply 16 days ago

    It doesn't look normal ? Did you change anything in the code?

    0
    None
    eabbasovJohan Link

    Reply 16 days ago

    Haven't changed anything related to this. The issue I am facing is about the boundaries of the blue circle, it falls outside the camera image.
    Did you change the resolution of the camera? I am using the same camera as you have, and also using Windows 10

    shar.jpg
    0
    None
    Johan Linkeabbasov

    Reply 16 days ago

    It is a strange problem. The blue circles are correctly positioned and they have the right size. The program python changes the resolution of the camera on lines 22 and 23. Which version of OpenCV do you use?

    1
    None
    Johan Linkeabbasov

    Reply 15 days ago

    Try to remove line 396 of the code python. (img = img[0:int(camHeight),int((camWidth-camHeight)/2):int(camWidth-((camWidth-camHeight)/2))])

    0
    None
    eabbasoveabbasov

    Reply 12 days ago

    Result whith :
    camHeight = 480
    camWidght = 1280
    Here the size turned out to big, until it reached a normal rezult.

    15602755938209159080243774394347.jpg
    0
    None
    HergiE

    17 days ago

    hello there! if it is possible to give me your number so I can speak directly via whatspp because I need to submit this project tomorrow!

    2 answers
    0
    None
    aibra16

    17 days ago

    Hello!
    I would like to ask a question about the wings rising. I have a problem that when the button to rise the wings is pressed, the motors indeed do move to raise the arms, BUT the problem is of them not stopping , so the wings keep moving to the opposite side. Is there something I am missing or does it have to do with the code . At what point does the system know that it should stop rising.
    And its kinda urgent

    0
    None
    HergiE

    Question 18 days ago

    Why when we connect the Arduino and after we run the programa and press the buttons(Test the servomotors),(Set up the tray) it brings out a warning: The tray must be in place! and Remember to remove the tray!.What do these warning mean? I've translated them in English from french