Self Balancing Robot Using PID Algorithm (STM MC) : Inverted Pendulum
Self Balancing Robot Using PID Algorithm (STM MC) : Inverted Pendulum
Self Balancing Robot Using PID Algorithm (STM MC) : Inverted Pendulum
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
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
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.
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.
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.
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
> MPU6050
> 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
• 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.
> Limitations
https://drive.google.com/file/d/1wJmNStqOgdYEJy_UnWfoDeX3FBTJ6KeG/view?usp=drivesdk