Self Balancing Robot Using PID Algorithm (STM MC) : Inverted Pendulum

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

instructables

Self Balancing Robot Using PID Algorithm (STM MC)

by tahirulhaq

Recently a lot of work has been done in the self terminologies, with addition to the motivations for the
balancing of objects. The concept of self balancing project. Experimentation and observations have been
started with the balancing of inverted pendulum. taken, merits and demerits described with ending at
This concept extended to design of aircrafts as well. the future improvements. A model of self balancing
In this project, we have designed a small model of robot was developed to understand the e ectiveness
self balancing robot using the PID(Proportional, of PID in the world of control system. Going through
Integral, Derivative) Algorithm. Since then, this some rigorous tests and experiments, the merits and
method is the new face of the industrial process demerits of the PID control system were discovered.
control systems. This report reviews the methods It was found that in spite of many advantages of PID
involved in self balancing of objects. This project was control over past methods, still this system requires a
conducted as a semester project to understand the lot of improvements. It is hoped that the reader gets a
correlation of PID on the e ciency of various industrial good understanding of the importance of self
processes. Here we focus only at providing a brief balancing, the e ectiveness and deficiencies of PID
review on the e ectiveness and application of the PID control
control. This paper has been developed by providing
a brief introduction to control systems and related

Self Balancing Robot Using PID Algorithm (STM MC): Page 1


Step 1: Introduction

With the advent of computers and the industrialization processes and more importantly, to control them
of processes, throughout man‘s history, there has using machines autonomously. The purpose being to
always been research to develop ways to refine reduce man‘s involvement in these processes,

thereby reducing the error in these processes. Hence, Basic Control System
the Field of ”Control System Engineering” was
developed. Control System Engineering can be The previous diagram shows a very simplified version
defined as using various methods to control the of a Control System. The microcontroller is at the
working of a process or maintenance of a constant heart of any Control System. It is a very important
and preferred environment, be it manual or automatic. component therefore, its choice of selection should
be made carefully based on the requirements of the
A simple example could be of controlling the System. The micro-controller receives an input from
temperature in a room. Manual Control means the the user. This input defines the desired condition of
presence of a person at a site who checks the the System. The micro-controller also receives a
present conditions (sensor), compares it with the feedback input from the sensor. This sensor is
desired value (processing) and takes appropriate connected to the output of the System, the
action to obtain the desired value (actuator). The information of which is fed back to the input. The
problem with this method is that it is not very reliable microprocessor, based on its programming, performs
as a person is prone to error or negligence in his various calculations and gives an output to the
work. Also, another problem is that the rate of the actuator. The actuator, based on the output, controls
process initiated by the actuator is not always the plant to try to maintain those conditions. An
uniform, meaning sometimes it may occur faster than example could be a motor driver driving a motor
required or sometimes it may be slow. The solution of where the motor driver is the actuator and the motor
this problem was to use a micro-controller to control is the plant. The motor, thus rotates at a given speed.
the system. The micro-controller is The sensor connected reads the condition of the plant
at the present time and feeds it back to the micro-
programmed to control the process, according to controller. The micro-controller again compares,
given specifications, connected in a circuit (to be makes calculations and thus, the cycle repeats itself.
discussed later), fed the desired value or conditions This process is repetitive and endless whereby the
and thereby controls the process to maintain the micro-controller maintains the desired conditions
desired value. The advantage of this process is that
no human intervention is required in this process.
Also, the rate of the process is uniform

Self Balancing Robot Using PID Algorithm (STM MC): Page 2


Step 2: PID Based Control System

The PID Algorithm is an e cient method of designing As can be seen in the above equation, the integral
a Control System. and derivative of the error signals are calculated,
multiplied with their respective constants and added
Definition along with the constant Kp multiplied with e(t). The
output is then fed to the actuator which makes the
PID stands for Proportional, Integral and Derivative. system run. Now lets look at each part of the function
In this algorithm, the error signal received is the input. in turn. This function directly e ects the rise time, fall
And the following equation is applied onto the error time, peak over shoot, settling time and steady state
signal error.

U(t) = Kp*e(t) + Kd*d/dt(e(t)) + Ki*integral(e(t)) (1.1) • Proportional part: The proportional part reduces the
rise time and decreases the steady state error. This
Brief Explanation means that the system will take lesser time to reach
its peak value and when it reaches its steady state,

the steady state error will be low. However, it However, it increases the peak overshoot and the
increases the peak overshoot. settling time.

• Derivative part: The derivative part reduces the • Tuning: A good Control System will have low rise
overshoot and the settling time. This means that the time, settling time, peak overshoot and steady state
transient state of the system will be more damped. error. Therefore, the Kp, Kd, Ki need to be finely
Also, the system will reach its steady state in a lesser tuned to adjust the contribution of the above factors in
time. However, it does not have any e ect on the rise order to acquire a good Control System.
time or the steady state error.
Figure has been attached showing the effect of
• Integral part: The integral part reduces the rise time changing various parameters in PID algorithm.
and completely eliminates the steady state error.

Step 3: Self Balancing Robot

Self Balancing Robot Using PID Algorithm (STM MC): Page 3


A self balancing robot is a multi-layered,two wheel > Self Balancing using Accelerometer
robot.
Accelerometer gives us the acceleration of the body
The robot will try to balance itself on application of in 3 axes. Acceleration oriented in the y-axis(upward)
any unequal force(s). It will balance itself by and x-axis(forward) gives us the measure to calculate
application of force opposing the resultant of the the direction of gravity and hence calculate the angle
forces on the robot. of inclination. The angle is calculated as follow:

Methods of Self Balancing = arctan(Ay/Ax) (1.2)

There are four methods of self balancing of robots. The disadvantage of using this method is that during
These are as follow: the motion of the robot, the horizontal acceleration
will also be added to the readings which is a high
> Self Balancing using Two IR Tilt Sensor frequency noise. Hence, the angle of inclination will
be inaccurate.
This is one the most crudest way to balance the robot
as it requires very less hardware and a relatively easy >Self Balancing using Gyroscope
algorithm. In this approach, two tilted IR sensors are
used to measure the distance between the ground An gyroscope is used to calculate the angluar
and robot. Based on the distance calculated, PID can velocities along the three axis. The angle of
be used to drive the motors to balance the robot inclination is obtained using the following equation.
accordingly. One disadvantage of this method is that
IR sensor can miss some readings. Another problem p(i) = p(i−1) + 1/6(vali−3 + 2vali−2 + 2vali−1 +
is that an interrupt and loops are required for the vali) (1.3)
calculation of distance which increases the time
complexity of the algorithm. Hence, this method of One big disadvantage of using the gyroscope is that it
balancing robot is not much e cient. has a small DC Bias which is a low frequency noise
and in a few time the values returned are completely

wrong . This, after integration, will cause the zero measurements, minimizing the mean squared error of
point to drift away. As a result of it, the robot will the estimate. It operates in two stages, prediction and
remain in its vertical position for some time and will correction, given the discrete stochastic equations
fall over once the drift comes. describing the system dynamics. However, it is a very
complex algorithm to implement especially on a
> Self Balancing using both Accelerometer and limited hardware of a microcontroller.
Gyroscope
• Complementary Filter: This algorithm primarily uses
As discussed above, using only accelerometer or the data obtained from the gyroscope and integrates
gyroscope will not give us the correct angle of it over time to get the angle of inclination. It also uses
inclination. To account for that, both accelerometer a small proportion of accelerometer readings.
and gyroscope are used. These are both embedded Complementary filter, in fact, minimizes the high
in MPU6050. In this we obtain the data from both of frequency noise of accelerometer and low frequency
them and then fuse them by either using Kalman noise of gyroscope and then fuses them to give the
Filter or Complementary Filter. best accurate angle of inclination.

• Kalman Filter: The Kalman filter calculates the best


estimate of the state of a dynamic system from noisy

Self Balancing Robot Using PID Algorithm (STM MC): Page 4


Step 4: Design of Robot

We have designed a self balancing robot using implemented using PID Controller which is a
Proportional Derivative controller implemented by Proportional Integral Derivative Controller. We
Complementary Filter for MPU6050. This small model balance the robot by driving its wheels in the direction
of Self Balancing Robot will illustrate us the of its fall. In doing that, we are trying to keep the
usefulness of Control Systems in Self Balancing of centre of gravity of the robot above the pivot point. To
robots. drive the wheels in the direction of its fall, we should
know where the robot is falling and the speed at
System Implementation: which it is falling. This data is obtained using
MPU6050 which has an accelerometer and a
The system is a self balancing robot. It is gyroscope. MPU6050 measures the angle of

inclination and give its output to the Micro-Controller. Filter here for MPU6050 which is a math filter to
MPU6050 is interfaced with the STM Board through merge the outputs of accelerometer and gyroscope.
I2C. In I2C, one wire is for the clock which is named After obtaining the data from MPU6050, the
as SCL. The other one is for the data transfer which microcontroller will perform calculations to know
is SDA. In this, master slave communication is used. where it is falling. Based on the calculations, STM
Starting address and ending address is specified to micro-controller will give commands to the motor
know from where the data is starting and where it is driver to drive the vehicles in the direction of falling
ending. We have implemented the Complementary which will balance the robot.

Self Balancing Robot Using PID Algorithm (STM MC): Page 5


Step 5: Project Components

The following components were used in the self MPU6050 is used to get the information about where
balancing robot project: the robot is falling. It measures the angle of inclination
with respect to the zero inclination point which is the
> STM32F407 position of the MPU6050 when the program starts
running.
A micro-controller designed by ST Microelectronics. It
works on the ARM Cortex-M Architecture. The MPU6050 has a 3-axis accelerometer and a 3-
axis gyroscope. The accelerometer measures
> Motor Driver L298N acceleration along the three axes and the gyroscope
measures angular rate about the three axes. In order
This IC is used to run the motor. It gets two external to combine the output, we must filter out the noises of
inputs. One from the microcontroller which supplies it both. To filter out the noises, we have Kalman and
a PWM signal. By adjusting the width of the pulse, Complementary filter. We have implemented
the motor speed can be adjusted. Its second input is Complementary filter in our project.
the voltage source required to drive the motor which
is a 12V battery in our case. > Opto Couple 4N35

> DC Motor An optocoupler is a device used to isolate the low


voltage part and the high voltage part of the circuit.
A DC Motor runs on a DC supply. In this experiment, As the name suggests, it works on the basis of light.
the DC Motor is running using the optocouplers When the low voltage part gets a signal, current flows
connected to the motor driver. To drive the motor we in the high voltage part
have used the Motor Drive L298N.

> MPU6050

Self Balancing Robot Using PID Algorithm (STM MC): Page 6


Self Balancing Robot Using PID Algorithm (STM MC): Page 7
Step 6: Structure of Robot

The structure of robot is explained as follow: > Second Layer

> Physical Structure In the upper layer of the robot, we have placed the
STM Board on Perf Board. Another perf board of 4
The self-balancing robot consist of two layers made opto couplers is placed on the top layer. Gyroscope is
up of transparent plastic glass. The details of two also placed on the top layer of the robot from the
layers are given below: lower side. Both of the components are placed in the
middle part so that the centre of gravity is kept as low
> First Layer as possible.

In the bottom part of first layer, we have placed a cell > Centre of Gravity of Robot
to power STM board. Also two motors of 4 volt each
have been placed on each side with tyres connected The center of gravity is maintained as low as
for the robot to move. In the upper part of first layer, possible. For this purpose, we have placed heavy
two batteries of 4 volt each (8 volts total) and motor batteries on the bottom layer and light components
driver IC(L298N) have been placed for the operation such as STM board and optocouplers on the upper
of motors. layer.

Step 7: Code

Code was compiled on Atollic TrueStudio. STM studio was used for debugging purposes.

Download
http://www.instructables.com/ORIG/FSJ/MUEH/JF344LDO/FSJMUEHJF344LDO.txt

Step 8: Conclusion

Self Balancing Robot Using PID Algorithm (STM MC): Page 8


After a lot of experimentation and observation, we provides a core clock of 168MHz. Though dealt with
finally come to the point where we summarize our this problem in this project, there is an overall notion
results and discuss how far we succeeded in about this model that it does not exactly provide such
implementing and working out the e ectiveness of the high frequency.
system.
The open loop speed provides a very smooth line
> General Review with only a few unexpected values. The PID
Algorithm is also working fine providing a very low
During the experimentation, the speed of the motor settling time of the motor. The motor’s PID Algorithm
was successfully controlled using the PID Algorithm. was tested under various voltages keeping the
The curve however is not exactly a smooth straight reference speed constant. The voltage change does
line. There are many reasons for that: not change the speed of the motor showing that the
PID Algorithm is working fine
• The sensor though connected to a low pass filter
still provides certain finite debouncings; these are due > E ectiveness
to the non linear resistances and some inevitable
reasons of the analog electronics. Here we discuss the e ectiveness of the PID
controller that we observed during the
• The motor does not rotate smoothly under small experimentation.
voltage or PWM. It provides jerks that might cause
some wrong values fed to the system. > Simple Implementation

• Due to wobbling, the sensor might miss some slits We have seen in the experimentation and
providing higher values. • Another major reason for observations section that a PID controller is very easy
errors can be the core clock frequency of the STM to implement. It only requires three parameters or
micrcontroller. This model of STM microcontroller constants that have to be set in order to have a speed

control system torque for the motor. So the motor in some trials does
not run or in other trials provides a large overshoot
> Unmatched E ciency for Linear Systems and a longer settling time.

Linear PID controller is the most e cient in the family


of controllers because the logic is very simple and
application is widespread in case of linear or fairly
linear applications.

> Limitations

We explained in the abstract about the Limitations of


this system. Here we discuss a few of them that we
observed.

> Selection of Constants

We have seen that, though a PID controller is easy to


implement, still it is a major drawback of the system
that the step of selecting the value of constants is a
laborious one; as one has to make di cult
calculations. The other way is the hit and trial method
but that is also not e cient.
Self Balancing Robot Using PID Algorithm (STM MC): Page 9
> Constants not always constant

The experimental results showed that for di erent


values of the reference speed for the motor, the PID
controller malfunctioned for the same values of the
PID constants. For di erent speeds, the constants had
to be selected di erently and this increases the
computational cost exponentially.

> Non Linear

The PID controller used in our case is linear,


therefore, it can be applied to linear systems only. For
non-linear systems, the controller has to be
implemented di erently. Although di erent non linear
methods of PID are available, they require more
parameters to be selected. This again makes the
system undesirable due to high computational cost.

> Initial Push Required

We showed in the experimentation section that for a


fairly small reference speed where the error is fairly
small in the start, the PWM supplied by the PID is so
small that it does not generate the required starting

Step 9: Special Thanks

Special Thanks to my group members who helped me through this project.

I will upload the link to the video soon.

I hope you find this instructable interesting.

This is Tahir Ul Haq from UET signing off. Cheers !!!

Following is the link to project video.

https://drive.google.com/file/d/1wJmNStqOgdYEJy_UnWfoDeX3FBTJ6KeG/view?usp=drivesdk

Self Balancing Robot Using PID Algorithm (STM MC): Page 10

You might also like