Ball Balancing PID System

66,914

640

111

About: Hi, I'm Johan Link, a 18 years old student living in Switzerland. I love robotics, computers, 3D printing, photography and skate. https://www.thingiverse.com/JOHLINK/about

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.

bottom.step

Top.step

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

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

    • Warm and Fuzzy Contest

      Warm and Fuzzy Contest
    • Paper Contest

      Paper Contest
    • Epilog X Contest

      Epilog X Contest

    111 Discussions

    0
    None
    NicolasO43

    Question 1 day ago on Step 1

    Hi Johan, your project is so creative that I decided to reproduce it. I am particularly attracted by the level of complexity of the different parts. I already 3D-printed most of them except superieur.stl and inferieur.stl cause in order to CNC these ones, I've been asked for the "STEPS" files. A group of college students are in fact manufacturing the parts for me. They have access to 3D printers, CNC, milling machines, who knows what else.
    I don't wanna cut these parts myself since I can get them done for free. You got me?
    Since, you don't provide any 'step' files, is there a way I can get the two parts fabricated without the step files? It seems that only the 'step' files contain certain information that they need. They tried to do some kind of conversion but it does not work.

    Thank you for sharing this brilliant project.
    Porte toi bien.

    1 answer
    0
    None
    Johan LinkNicolasO43

    Answer 10 hours ago

    Hi,
    Thanks for the comment. I just added two .step files in step 2. I hope it will work.
    Good luck for the construction of this project. :)

    0
    None
    DenisG71

    Question 1 day ago

    Johan,
    I am looking for the python program to generate the look-up file "data.txt". This is a great project. Thank you from Montreal, Qc.

    1 answer
    0
    None
    mimax

    1 day ago

    Is there a requirement for the arm size? Cou'l'd I use a small size?

    0
    None
    power000

    3 days ago

    THis is wonderfull!!!!!! Well done mate ,it is really nice very quick response ,just great!!
    I was sure from the first video that i am going to made it , but those magnets and camera is really expensive. But it is great

    0
    None
    jbumstead

    8 days ago

    Very impressive work!

    0
    None
    Jerryolsen

    10 days ago

    Hi Johan, it seems the camera orientation to the table is important. Which two angles if the camera base set between? From the pictures it looks like the camera is aligned opposite of angel B is that correct?

    0
    None
    Jerryolsen

    12 days ago

    Hi Johan, I have mine assembled and working. I’m using Windows 10 and an Arduino Nano. I found that I need to use the middle mouse button on Windows to set the color target for the ball. I'm having trouble with it keeping the ball on the table.
    Do you have recommendations on the startup sequence and PID parameters?

    Thanks for any help you can give me.

    2 replies
    0
    None
    Johan LinkJerryolsen

    Reply 11 days ago

    Hi,
    At the beginning you have to put each coefficients (pid) to 0. Then you can slightly increase the coefficient p. At this point you have to put the ball in different places on the board and check if the board tilts in the right direction (no need to throw the ball, you can just hold it on top of the board). If the board moves in the right directions, you can increase the coefficient d. Then you have to do some tests by varying the value of p and d. Once this works you can increase the value of the coefficient i which is very sensitive. (the coefficient i is not necessary. You can set it at 0).
    I hope it helps!
    Do not hesitate to send me a picture of your construction. I'm interested :)

    0
    None
    JerryolsenJohan Link

    Reply 10 days ago

    Thanks Johan, I'll try that and send you some pictures soon.

    0
    None
    WannaDuino

    13 days ago

    awesome, finalist for sure.
    Ben je nederlands zo ja, contact me of reageer.

    1 reply
    0
    None
    Wrrr 10-GWannaDuino

    Reply 10 days ago

    Ik had het ook fout: Johan Link is een Franstalige Zwitser.
    Je kunt bovenaan elke instructable op de naam van de auteur klikken..

    0
    None
    Wrrr 10-G

    10 days ago

    VERY impressive, both the 'homework' (the math alone is breathtaking, wow!) behind it, pcb design, 'industrial' design... It looks like a nicely rounded and well finished product.
    Hat's off to you sir!

    0
    None

    Go to the PCB contest in contests page, and find his instructable. Just hit the vote button.

    0
    None
    호성김

    12 days ago

    와우 이거 마침 관심있던 부분인데 좋은 정보네요 으리가 넘칩니다.

    0
    None
    VajkF

    12 days ago

    You can do this without a custom pcb...

    0
    None
    DougL2

    14 days ago on Step 3

    I see in the assembly the magnetic balls attach to the ends of the arms(with set screws) and I see what looks like the print3times.stl but what is the part below the print3times.stl part? Aslo how are these attached to the plateau.stl? Thank.