Stabilising an Inverted Pendulum with PID Controller

Inverted pendulum is a system in which the centre of the mass is above the pivot point, where the mass can freely rotate. The inverted pendulum has a unique trait; it is unpredictable, non-linear and consists of multiple variables. Balancing by PID controller is a continuous process where it corrects the feedback system error from the difference between the measured value and the desired value. This research mainly focusses on balancing an inverted pendulum with reaction wheel. The research objectives are to construct a self-balanced inverted pendulum and using PID controller to control the stability of the pendulum. The PID configuration is then evaluated based on the response of the system. The idea is to use the reaction torque generated by the motor to counter balance the inverted pendulum. The factor which governs the amount of torque generated is the height of the pendulum and the mass of the wheel. To balance the pendulum, tuning the PID gain is essential. Proportional gain is tuned first to get oscillation, next is to tune the integral and derivative gain to get a smoother and quicker response. Idea is to get short settling time, and minimum overshoot percentage. Hypothesis is that higher proportional gain will give a faster response rate and the acceleration of the motor is the key on generating torque. A simulation of the pendulum falling is simulated and the results are recorded in term of the response of the pendulum against time. At initial point, proportional gain, integral gain and derivative gain are set to zero to validate the simulation. The finding in this research is that torque is generated by the acceleration of the reaction wheel. Higher acceleration gives a high torque. Others findings is the PID parameter; Proportional gain increases the response rate; Integral gain is used to eliminate steady state error; Derivative gain is used to lessen the overshoot.


Introduction
Inverted pendulum is a system in which the centre of mass is above the pivot point, where the mass can freely rotate. For an example, balancing a long stick on a palm is considered as inverted pendulum. The inverted pendulum has a unique trait where it is unpredictable, non-linear and consists of multiple variables. Which is why, inverted pendulum is usually used to benchmark a control algorithm such as PID controllers, state space representation, neural networks, fuzzy control and genetic algorithms. To balance an inverted pendulum, torque must be applied on the pivot to counter balance the falling pendulum. This can done by moving the pivot point horizontally and vertically by using a cart or a reaction wheel which generate a reaction torque from spinning in opposite direction [1].
The concept of inverted pendulum is very common in the world either in man-made structure or natural things. One of the great examples of an inverted pendulum that can be seen in natural things is human being. To stand up right, human body must be constantly balance itself. Putting an unconscious person upright will lead to the person falling. A humanoid robot is also one of the example where the concept of inverted pendulum can be applied in [2]. Inverted pendulum can also be seen in advance technology such as rockets and missile where the centre of gravity is placed behind the centre of drag, results in aerodynamic instability [3]. As for the latest technology, it would be the Segway [4].
As mentioned above, there are many ways to balance an inverted pendulum. The most common way is to use a moving cart to move the pivot point to create counter moment. Another uncommon way of balancing is by rotating clockwise and anticlockwise to generate moment. Out of the many variations out there, the focus here is to balance an inverted pendulum by using reaction wheel. Unlike other type of inverted pendulum such as rotary or cart they require a runway or platform to balance itself because it is balanced by motion of the whole pendulum. However, reaction wheel inverted pendulum is balance by torque generated by the motor. This requires no platform or runway making it compact and rigid.
Reaction wheel is mainly used in the satellite to provide precise rotation. Newton 3rd law states that for every action there is an equal and opposite reaction, thus the name of reaction wheel. To rotate the satellite in space, satellite rotated by spinning the reaction in the opposite direction. This allows the satellite to have rotational motion through its rotational axis. However, reaction does not generate translation motion, it cannot move from one point to another [5]. Because the wheel has mass, and it is moving in circular motion, the wheel itself has angular momentum. The angular momentum is conserved as long as there is no external torque applies to the system. Other than that, object with mass also subjected to moment of inertia. By relating the relation above, theoretically a reaction wheel inverted pendulum can be model out [6].
Control is one of the most essential elements nowadays, such as control in a power plant. A power plant without control is disastrous. A common control system consists of the controller, sensor and an actuator. Actuator is the component where it converts electrical signal to mechanical energy to change the physical environment. Sensor reads the change in physical environment and sends the electrical signal to the controller. Controller acts as the processor, which processes the value from the feedback sensor. It calculates the difference of the environmental value and target value, which also known as response error. This value is then processed by the controller and the correct signal is sent to the actuator. A target value is the value which the user desire to achieve. Below in Fig. 1 is a basic control diagram [7]. Where, r = input e = response error G(s) = Controller G p (s) = actuator

Evaluating the stability of system in general
There is numerous way to test the stability of a system. One of the most common way is to test the linear system of the transfer function [8].
Consider the system has two main block-diagrams G p (s) and G(s). The transfer function in loop: The output can be expressed as, y = G p G The response error can be expressed as, e = r -y The ratio of the output and response error is the closed loop function, The transfer function above is in complex form. Once the function is converted into Laplace form, the data can be presented graphically on a complex s -plane. A closed loop function is always in the fraction form as shown in equation (1). The numerator in a function is also known as zero whereas the denominator is known as pole. The 'circle' mark on the s-plane shown in Fig. 2 represents the zero, whereas the 'cross' mark represents the pole. Both of these factors govern the stability of the system. If a pole exists on the right plane, it means that there is an output component which gradually increases as time progress, which result in instability in the system. In other words, to achieve stability, the pole needed to be on the left half plane. Fig. 3 shows a clearer response of the system depending which point the function located. In the centre of Fig. 3 shows the general response of the system, with either gradually decrease, increase or remain constant. Yaxis represents the frequency of the response, where the higher the magnitude of the imaginary number indicates higher frequency.

PID controller
PID is a very well-known feedback controller, due to its efficiency and reliability. PID consists of three parameters, proportional gain, integrated and derivative. Like most of the controller, it calculates the differences between the measured value and the desired value as shown in Fig. 4. One of the examples of PID in daily life is air conditioning. First air conditioning is set to a desire temperature by using the remote. Most of the air conditioning has a built-in temperature sensor to feedback the ambient temperature. The purpose of the PID is to constantly receive feedback from the ambient temperature and send the suitable voltage to the system to lower down the temperature. Once the desire temperature is achieved, the PID controller will then try to maintain the temperature within a range. This range is known as the steady state error. A good controller has a low range of steady state error, also achieve the desire value in a shorter time with low overshooting. The proportional gain acts as multiplier which multiplies the error response. It is also one the factor to make the response quicker, however too much will result in oscillation and eventually unstable. The function of an integral gain is sum up all the errors produce to reduce the steady state error, whereas the derivative gain is product of rate of change of the error. The function is to increase the reaction magnitude when there is a change in error. However, it is sensitive to noises such as disturbance in the electrical signal. torque. By using this theory, it is possible to balance an inverted pendulum by placing the reaction wheel at the end of the pendulum. Balancing is done by rotating and stopping the reaction wheel against the falling direction of the pendulum. Stopping the reaction wheel in a short duration, result in high deceleration, hence producing a counter force to balance the inverted pendulum. The best configuration of the PID controller in this research can be obtained by comparing the result in terms of performance and stability [8].

Design of the inverted pendulum
The inverted pendulum on this research is very similar to reaction wheel discussed earlier.
Instead of balancing on one axis, the purpose of this research is to construct a two-axis reaction wheel inverted pendulum. The main components of the design consist of a pendulum body, two reaction wheels, two motors, gyroscope and a microcontroller. The overall design is as shown in Fig. 5. The pendulum body must be made from light material such as aluminium. Lighter body require lesser force to counter the falling thus, weaker motor can be used. Stronger motor is expensive and big in size. Furthermore, it adds on weight into the system.
Due to budget limitation, smaller motor is used to fit into the budget. The limitation of small motor is that, the torque generated by the motor is small. Thus, a lighter and smaller reaction wheel is used. The wheel has a dimension of 6 cm diameter and 2 cm depth. It is 3D printed with PLA (polylactic acid) filament. The density is set to 30% because of light weight requirement. The height of the pendulum is designed to be approximately 30 cm. This height is decided based on the amount of counter torque generated by the motor.
The torque generated by the motor is 14.7 x 10 -3 Nm, this value is based on the specification of the motor. This number is bigger than the torque generated by the weight which is 5.11 x 10 -3 Nm. The weight is calculated by multiplying the weight of the pendulum and distance of the pivot point to centre of gravity. The whole pendulum including motor has a mass of 150 grams. The distance of the centre of gravity to the pivot point is approximately 20 cm. Assumption here is the maximum angle the pendulum can be tilted before falling is 10 degrees. Thus, after resolve the weight in vertical component the maximum torque generated by weight is 5.11 x 10 -3 Nm. A bigger value is needed because overshoot better than having insufficient enough torque to counter the balance.

5
Furthermore, the speed of the wheel can be controlled to reduce the amount of overshooting.
As mentioned above the height is designed to be 30 cm because shorter height only generates small amount of torque to counter balance the pendulum. Although greater height generates more torque, however the torque generated by the weight is also increases as the height increases. Thus, 30 cm is the optimum height for the inverted pendulum.

Electronic components of the inverted pendulum
Electrical components are the vital of the inverted pendulum. Table 1 shows the description of each component.
Direct current (DC) motor is used due to the ability to provide high RPM. The importance of reaction wheel is to create torque, and this is proportional to the acceleration of the wheel. High acceleration can be obtained by changing the velocity in a short time. This can be achieved by DC motor; hence DC motor is used. Since this is the dual axis system, a gyroscope is a better sensor to measure the angular position of the pendulum. Potentiometer is not a suitable selection because it can only read one axis and having two in the system, only result in complex. As for microcontroller, Arduino Uno is selected due to its reliability and low cost. Other alternative such as raspberry pi does not have the same quality as Uno. Furthermore, the software used to tune the PID is Arduino as well, thus more compatible. Mega Arduino is not chosen because of the high cost. With the number of sensors and motors, Uno is sufficient to do the job. In Fig. 6, shows the connections of all the electrical components to the microcontroller.

Modelling using vectorial dynamic
The structure of the system can breakdown into forces to further understand the system. Following Fig. 7 is the schematic of the system. Fig. 8 is the free body diagram of the system showing the point where the force is acting. Here is a list of abbreviation used below, G p = pendulum weight L = length of pivot to wheel m = wheel mass L p = length of pivot to centre of gravity of pendulum T m = motor torque M p = pendulum mass G = weight of wheel   Where R is the radius of the reaction wheel. In this setup, the momentum force, F can be assumed to be approximately same as the torque generated by the motor due to negligible mechanical loses from the motor to the reaction wheel [8]. Thus, Eq. (2) can be written as (6)

Modelling using analytical dynamics
To obtain the equation of the system, alternative way such as analytical dynamics is reliable.
The total energy of the system can be break down into the kinetic and potential energy. The difference in kinetic (T) and potential energy (V) is also known as Lagrangian equation.
(7) Equation of motion expressed in Lagrangian form. This is a general formula, (8) Where Q is dissipative force. The force in the system is conservative, thus   13), the potential energy is in term of cosine. This is because when dealing with the potential energy, only the height is considered, which is the vertical axis.

Results and Discussions
The purpose of this research is to identify the effect of PID configuration on the response rate of inverted pendulum. The expected outcome of this research is fluctuation should be considerably low, but the settling time will be high. This is most likely due to the charge up time for the reaction wheel to deliver a counter moment. Unlike the other system where the moment is instantaneous generated by the motor, reaction wheel need to take up speed to generate counter moment.
A simulation of the inverted pendulum is designed by using Simulink. The purpose of the simulation is to theoretically simulate the outcome of the pendulum. The simulated result is then compared with the practical result to evaluate the difference between the theory and practical performance. The Simulink design is shown below in Fig. 9. A basic PID control system consists of PID, plant, actuator and sensor to feedback the output. In Fig. 10, the loop first starts by having a constant with 0 magnitude going through the PID controller. The PID controller is then produce an output as motor voltage to give specific amount of torque to balance the pendulum. After that the change in angle position is then measured by the sensor and feedback to the PID controller. 'Step' shown in Fig. 9 is the source of the disturbance which made the pendulum to be unbalance. The scope shown in the Simulink design is the result of the response rate and there are three entries to the scope which produces three variables in graph as shown in Fig. 10.   Fig. 10. System response when K p = 0, K i = 0 and K d = 0.

Ziegler-Nichols tuning method
One of the way to tune an unstable system using PID controller is the Ziegler-Nichols turning method. This method is very common and reliable because tuning can be done without the mathematical models of the system. This method can be used on system with known mathematical modelling as well. Ziegler-Nichols tuning method tune a system by judging the response of the system. To use this method, first, only the K p (Proportional gain) is tuned to certain amount until the response start oscillating. Second is to tune either K i (Integral gain) or K d (Derivative gain) to stabilize the system. Finally, the last step is to tune the last parameter to improve the stability of the system either by reducing the fluctuation or reducing the steady state error [10].
When PID gain is zero. The response shows that the system is unstable as it grows exponentially away from stable point as the disturbance introduced into the system. Yellow line indicates the angle position of the pendulum, red line indicates the disturbance and blue line indicates the response of the motor voltage. With the zero gain, the motor is deactivated thus the voltage response is stagnant as shown in Fig. 10. Fig. 11 shows when the K p has a value of 15. The response is similar as the previous response but with a few fluctuations at the beginning. To start balancing an unbalance system, K p is commonly the priority to tune. In this case, the proportional gain is tuned to 15 whereas the integral gain and derivative gain remain zero. With the K p at 15, the angle of the pendulum decreases a slight amount before growing exponentially. This shows that there is a slight improvement to the stability of the system, however the voltage response of the motor has a very steep gradient towards infinity. To counter the pendulum from falling, the motor must generate torque in opposite direction, which is why the voltage response has a mirror pattern against the falling of pendulum. As the voltage response going towards infinity, which means the system require infinite amount of energy to balance the system, practically this is unachievable. Thus, higher K p is required.
Higher K p value is input to the simulation and the result is shown in Fig. 12. More fluctuations occurred before system went unstable. As higher K p is added, the fluctuation occurrences increase at the beginning then gradually went unstable. The rate which the system became unstable is significantly lower with twice amount of K p . Voltage motor also share the similar traits. This shows that the system is more stable when higher proportional gain is input into the system. One of the reason the stable time is longer is because motor voltage has more attempts to balance the pendulum. Fluctuations shows in the motor voltage is the result of motor trying to balance the pendulum. However, the lack of other parameter gains it is still unable to balance the pendulum in a longer duration. Thus, other gains are needed for the system to be balanced. With K p = 30 and K i = 5, the system is no longer unstable. As compared to Fig. 12, this response stays stable after a little fluctuation. The amplitude of the fluctuation is significantly lower as well. On the other hand, the motor voltage has more visible fluctuations and stay constant as the system is stable. The response of the pendulum has a biggest fluctuation when an external force is acted on it. The amplitude of the fluctuation is then reduced gradually until reaches zero, which is the stable point. This indicates that when the external force acted on the pendulum, it swings away from equilibrium. As it is away from equilibrium the motor generates opposite torque to bring the pendulum back to equilibrium, which is why the blue line has a peak when the response of the pendulum has a peak. After the pendulum reaches stability voltage is still supplying the motor to maintain the balance of the system, which explains the motor voltage is not in zero. All the parameters are input into the PID controller, the system became more stable as shown in Fig. 14. Only one visible fluctuation and the system is balanced without overshooting that may cause another oscillation. With addition derivative component of the PID control, the controller is now more sensitive in change in error. The moment change in error is read by the controller, it reacted quickly to neutral the change. Integral is important to make this happen as well. Only with the help of K i , the system managed to stabilise with low stead state error else the pendulum will still swing left and right at equilibrium.
After the simulation, theoretically it is proven it is possible to balance the inverted pendulum by using PID controller. Next step will be testing on an actual inverted pendulum. The actual inverted pendulum has finish manufactured as shown in Fig. 15. Same procedure will be applied on the actual inverted pendulum in future test.

Conclusions
The simulation done by Simulink shows concrete proof that an inverted pendulum can be balanced PID controller. With Ziegler-Nichols tuning method, even if the mathematical modelling unknown it is possible to balance it by evaluating the response of the system and tune each gain until a positive result is achieve. In PID, proportional gain is responsible for amplifying the error; integral gain is to reduce the steady state error; derivative is sensitive in change in error. Based on the simulation done in Simulink, high K p is require to balance the pendulum and the value K i and K d can be as low as 5 and it is sufficient for the system to be balanced.