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.





Friday, June 27, 2014

Real time 2D visualization on Matlab for GY-521 MPU-6050 IMU



Watch the demo video:








Circuit used





I used "GY-521 which has "MPU-6050" sensor which contains an accelerometer and a gyro on one chip. 


Connections:

VCC on GY-521 ---->  to 3.3v pin on Arduino
GND on GY-521 ---->  to GND  pin on Arduino
SCL  on GY-521 ---->  to PinA5 on Arduino
SDA on GY-521 ---->   to PinA4 on Arduino


Codes

I used 2 codes. The first one should be uploaded to the Arduino board. It is responsible for detecting one of the 3 angles of the sensor, and sending its value to the computer via USB. 

The other code is a Matlab code responsible for receiving the angle from the Arduino and visualizing it on a 2D object.


Arduino Code, Matlab code, and Libraries needed:

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

Move "I2Cdev" & "MPU6050" folders to 

Documents > Arduino > libraries 




Real time plotting of distance on Matlab using Arduino Uno & PING)))



Watch the demo video:







Circuit used





I used "PING)))" ultrasonic sensor that has only 3 pins.



Connections:

GND  Pin on PING)))  ----> to GND pin on Arduino 
5V      Pin on PING)))  ----> to 5V pin on Arduino
SIG     Pin on PING)))  ----> to digital pin  7 on Arduino (You may use any other digital pin)


Codes

There are 2 codes. The first one should be uploaded to the Arduino board. It is responsible for detecting the distance of the closest object in front of the sensor, and sending the distance to the computer via USB. 

The other code is a Matlab code responsible for receiving the distance from the Arduino and plotting it.




Arduino & Matlab codes: