Wednesday, July 2, 2014

Balancing of a Ball on Beam using Arduino as a PID controller

Video





I used Arduino Uno as a PID controller to balance a small ping-pong ball on a 4-bar mechanism. And used Matlab to plot the position of the ball vs time.

In this video:
  • setpoint = 15 cm
  • Kp= 2.5
  • Ki=0
  • Kd=1


Control Loop:




  • An ultrasonic sensor detects the actual position of the ball (the "output ball position" in the above figure). 
  • The output ball position is compared to the "setpoint" (the position where we want the ball to be)
  • A PID algorithm on the arduino is used to compute the angle of the servo motor based on the difference between the output ball position and the setpoint (the error).



Parts used:


  • Arduino Uno
  • Ping))) ultrasonic sensor
  • 20 Kg.cm Digital Servo Motor
  • 5 V DC power supply


Connections:




The dimensions of my 4-bar mechanism 






You can change the dimensions of the mechanism, the weight of the ball, or any other parameter. But then you will have to change the Kp, Ki, and Kd values in the code to get a good system performance.



Arduino code, Matlab code and PID library:

https://drive.google.com/folderview?id=0Bx4uoJmyE27YdGRMYi16dnUxVk0&usp=sharing



  • You must download "PID_v1" library for arduino and move its whole folder to:
        Documents > arduino > libraries 



       





Update:



How to get the values of Kp, Ki, & Kd?


- You can try to guess the values, then apply it in the code and monitor the performance of the system as you change theses values. Don't worry too much about  Ki in this system, you can set it to zero. Firstly, guess the value of Kp, then keep changing the value of Kd until you reach a satisfying performance for the system. This is called manual tuning and it takes a long time. 

- Another thing you can do, is to build a mathematical model on SIMULINK and use its automatic tuner that can give you three values for Kp, Ki, and Kd.  Of course the mathmatical model will not 100% represent the real physical system, therefore, you will have to slightly change  the 3 obtained values, which means you'll have to do some manual tuning anyway.



- If you are familiar with Simulink, you can check out these detailed tutorials: 

LINK 1

LINK 2



The tutorial will teach you how to build a model on simulink for the system and how to tune the Kp, Ki, & Kd values.