Introduction: Self Balancing Robot Using PID Algorithm (STM MC)

About: Electrical Engineer from University of Engineering and Technology Lahore. Science enthusiast, aspirant to contribute to the world of science by publishing projects related to science and technology

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

Step 1: Introduction

With the advent of computers and the industrialization of processes, throughout man‘s history, there has always been research to develop ways to refine processes and more importantly, to control them using machines autonomously. The purpose being to reduce man‘s involvement in these processes, thereby reducing the error in these processes. Hence, the Field of ”Control System Engineering” was developed. Control System Engineering can be defined as using various methods to control the working of a process or maintenance of a constant and preferred environment, be it manual or automatic.

A simple example could be of controlling the temperature in a room. Manual Control means the presence of a person at a site who checks the present conditions (sensor), compares it with the desired value (processing) and takes appropriate action to obtain the desired value (actuator). The problem with this method is that it is not very reliable as a person is prone to error or negligence in his work. Also, another problem is that the rate of the process initiated by the actuator is not always uniform, meaning sometimes it may occur faster than required or sometimes it may be slow. The solution of this problem was to use a micro-controller to control the system. The micro-controller is

programmed to control the process, according to given specifications, connected in a circuit (to be discussed later), fed the desired value or conditions and thereby controls the process to maintain the 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

Basic Control System

The previous diagram shows a very simplified version of a Control System. The microcontroller is at the heart of any Control System. It is a very important component therefore, its choice of selection should be made carefully based on the requirements of the System. The micro-controller receives an input from the user. This input defines the desired condition of the System. The micro-controller also receives a feedback input from the sensor. This sensor is connected to the output of the System, the information of which is fed back to the input. The microprocessor, based on its programming, performs various calculations and gives an output to the actuator. The actuator, based on the output, controls the plant to try to maintain those conditions. An example could be a motor driver driving a motor where the motor driver is the actuator and the motor is the plant. The motor, thus rotates at a given speed. The sensor connected reads the condition of the plant at the present time and feeds it back to the micro-controller. The micro-controller again compares, makes calculations and thus, the cycle repeats itself. This process is repetitive and endless whereby the micro-controller maintains the desired conditions

Step 2: PID Based Control System

The PID Algorithm is an efficient method of designing a Control System.

Definition

PID stands for Proportional, Integral and Derivative. In this algorithm, the error signal received is the input. And the following equation is applied onto the error signal

U(t) = Kp∗e(t) + Kd∗d/dt(e(t)) + Ki∗integral(e(t)) (1.1)

Brief Explanation

As can be seen in the above equation, the integral and derivative of the error signals are calculated, multiplied with their respective constants and added along with the constant Kp multiplied with e(t). The output is then fed to the actuator which makes the system run. Now lets look at each part of the function in turn. This function directly effects the rise time, fall time, peak over shoot, settling time and steady state error.

Proportional part: The proportional part reduces the rise time and decreases the steady state error. This 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 increases the peak overshoot.

Derivative part: The derivative part reduces the overshoot and the settling time. This means that the transient state of the system will be more damped. Also, the system will reach its steady state in a lesser time. However, it does not have any effect on the rise time or the steady state error.

Integral part: The integral part reduces the rise time and completely eliminates the steady state error. However, it increases the peak overshoot and the settling time.

Tuning: A good Control System will have low rise time, settling time, peak overshoot and steady state error. Therefore, the Kp, Kd, Ki need to be finely tuned to adjust the contribution of the above factors in order to acquire a good Control System.

Figure has been attached showing the effect of changing various parameters in PID algorithm.

Step 3: Self Balancing Robot

A self balancing robot is a multi-layered,two wheel robot.

The robot will try to balance itself on application of any unequal force(s). It will balance itself by application of force opposing the resultant of the forces on the robot.

Methods of Self Balancing

There are four methods of self balancing of robots. These are as follow:

> Self Balancing using Two IR Tilt Sensor

This is one the most crudest way to balance the robot as it requires very less hardware and a relatively easy algorithm. In this approach, two tilted IR sensors are used to measure the distance between the ground and robot. Based on the distance calculated, PID can be used to drive the motors to balance the robot accordingly. One disadvantage of this method is that IR sensor can miss some readings. Another problem is that an interrupt and loops are required for the calculation of distance which increases the time complexity of the algorithm. Hence, this method of balancing robot is not much efficient.

> Self Balancing using Accelerometer

Accelerometer gives us the acceleration of the body in 3 axes. Acceleration oriented in the y-axis(upward) and x-axis(forward) gives us the measure to calculate the direction of gravity and hence calculate the angle of inclination. The angle is calculated as follow:

θ = arctan(Ay/Ax) (1.2)

The disadvantage of using this method is that during the motion of the robot, the horizontal acceleration will also be added to the readings which is a high frequency noise. Hence, the angle of inclination will be inaccurate.

>Self Balancing using Gyroscope

An gyroscope is used to calculate the angluar velocities along the three axis. The angle of inclination is obtained using the following equation.

θp(i) = θp(i−1) + 1/6(vali−3 + 2vali−2 + 2vali−1 + vali) (1.3)

One big disadvantage of using the gyroscope is that it 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 point to drift away. As a result of it, the robot will remain in its vertical position for some time and will fall over once the drift comes.

> Self Balancing using both Accelerometer and Gyroscope

As discussed above, using only accelerometer or gyroscope will not give us the correct angle of inclination. To account for that, both accelerometer and gyroscope are used. These are both embedded in MPU6050. In this we obtain the data from both of them and then fuse them by either using Kalman Filter or Complementary Filter.

Kalman Filter: The Kalman filter calculates the best estimate of the state of a dynamic system from noisy measurements, minimizing the mean squared error of the estimate. It operates in two stages, prediction and correction, given the discrete stochastic equations describing the system dynamics. However, it is a very complex algorithm to implement especially on a limited hardware of a microcontroller.

Complementary Filter: This algorithm primarily uses the data obtained from the gyroscope and integrates it over time to get the angle of inclination. It also uses a small proportion of accelerometer readings. Complementary filter, in fact, minimizes the high frequency noise of accelerometer and low frequency noise of gyroscope and then fuses them to give the best accurate angle of inclination.

Step 4: Design of Robot

We have designed a self balancing robot using Proportional Derivative controller implemented by Complementary Filter for MPU6050. This small model of Self Balancing Robot will illustrate us the usefulness of Control Systems in Self Balancing of robots.

System Implementation:

The system is a self balancing robot. It is implemented using PID Controller which is a Proportional Integral Derivative Controller. We balance the robot by driving its wheels in the direction of its fall. In doing that, we are trying to keep the centre of gravity of the robot above the pivot point. To drive the wheels in the direction of its fall, we should know where the robot is falling and the speed at which it is falling. This data is obtained using MPU6050 which has an accelerometer and a gyroscope. MPU6050 measures the angle of inclination and give its output to the Micro-Controller. MPU6050 is interfaced with the STM Board through I2C. In I2C, one wire is for the clock which is named as SCL. The other one is for the data transfer which is SDA. In this, master slave communication is used. Starting address and ending address is specified to know from where the data is starting and where it is ending. We have implemented the Complementary Filter here for MPU6050 which is a math filter to merge the outputs of accelerometer and gyroscope. After obtaining the data from MPU6050, the microcontroller will perform calculations to know where it is falling. Based on the calculations, STM micro-controller will give commands to the motor driver to drive the vehicles in the direction of falling which will balance the robot.

Step 5: Project Components

The following components were used in the self balancing robot project:

> STM32F407

A micro-controller designed by ST Microelectronics. It works on the ARM Cortex-M Architecture.

> Motor Driver L298N

This IC is used to run the motor. It gets two external inputs. One from the microcontroller which supplies it a PWM signal. By adjusting the width of the pulse, the motor speed can be adjusted. Its second input is the voltage source required to drive the motor which is a 12V battery in our case.

> DC Motor

A DC Motor runs on a DC supply. In this experiment, the DC Motor is running using the optocouplers connected to the motor driver. To drive the motor we have used the Motor Drive L298N.

> MPU6050

MPU6050 is used to get the information about where the robot is falling. It measures the angle of inclination with respect to the zero inclination point which is the position of the MPU6050 when the program starts running.

The MPU6050 has a 3-axis accelerometer and a 3-axis gyroscope. The accelerometer measures acceleration along the three axes and the gyroscope measures angular rate about the three axes. In order to combine the output, we must filter out the noises of both. To filter out the noises, we have Kalman and Complementary filter. We have implemented Complementary filter in our project.

> Opto Couple 4N35

An optocoupler is a device used to isolate the low voltage part and the high voltage part of the circuit. As the name suggests, it works on the basis of light. When the low voltage part gets a signal, current flows in the high voltage part

Step 6: Structure of Robot

The structure of robot is explained as follow:

> Physical Structure

The self-balancing robot consist of two layers made up of transparent plastic glass. The details of two layers are given below:

> First Layer

In the bottom part of first layer, we have placed a cell to power STM board. Also two motors of 4 volt each have been placed on each side with tyres connected for the robot to move. In the upper part of first layer, two batteries of 4 volt each (8 volts total) and motor driver IC(L298N) have been placed for the operation of motors.

> Second Layer

In the upper layer of the robot, we have placed the STM Board on Perf Board. Another perf board of 4 opto couplers is placed on the top layer. Gyroscope is also placed on the top layer of the robot from the lower side. Both of the components are placed in the middle part so that the centre of gravity is kept as low as possible.

> Centre of Gravity of Robot

The center of gravity is maintained as low as possible. For this purpose, we have placed heavy batteries on the bottom layer and light components such as STM board and optocouplers on the upper layer.

Step 7: Code

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

Step 8: Conclusion

After a lot of experimentation and observation, we finally come to the point where we summarize our results and discuss how far we succeeded in implementing and working out the effectiveness of the system.

> General Review

During the experimentation, the speed of the motor was successfully controlled using the PID Algorithm. The curve however is not exactly a smooth straight line. There are many reasons for that:

• The sensor though connected to a low pass filter still provides certain finite debouncings; these are due to the non linear resistances and some inevitable reasons of the analog electronics.

• The motor does not rotate smoothly under small voltage or PWM. It provides jerks that might cause some wrong values fed to the system.

• Due to wobbling, the sensor might miss some slits providing higher values. • Another major reason for errors can be the core clock frequency of the STM micrcontroller. This model of STM microcontroller provides a core clock of 168MHz. Though dealt with this problem in this project, there is an overall notion about this model that it does not exactly provide such high frequency.

The open loop speed provides a very smooth line with only a few unexpected values. The PID Algorithm is also working fine providing a very low settling time of the motor. The motor’s PID Algorithm was tested under various voltages keeping the reference speed constant. The voltage change does not change the speed of the motor showing that the PID Algorithm is working fine

> Effectiveness

Here we discuss the effectiveness of the PID controller that we observed during the experimentation.

> Simple Implementation

We have seen in the experimentation and observations section that a PID controller is very easy to implement. It only requires three parameters or constants that have to be set in order to have a speed control system

> Unmatched Efficiency for Linear Systems

Linear PID controller is the most efficient 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 difficult calculations. The other way is the hit and trial method but that is also not efficient.

> Constants not always constant

The experimental results showed that for different values of the reference speed for the motor, the PID controller malfunctioned for the same values of the PID constants. For different speeds, the constants had to be selected differently 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 differently. Although different 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 torque for the motor. So the motor in some trials does not run or in other trials provides a large overshoot and a longer settling time.

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 !!!