"

17 Ball Balancing Integral Control with Kalman Observer

Ball Balancing Project Background

In this chapter, we will discuss an open source project available on  Hackday. The project is about controlling a ball Balancing on a resistive touchscreen surface. The project link is listed in below.
Ball Balancing Project hackday Link
https://hackaday.io/project/175294-adventures-in-state-space-control-arduino-edition
The project is well done in a few aspects. First, the project is closely related to the state space applications in practical engineering control. Second, the author has released all the important project files to the control engineering community, which I believe it as very useful and beneficial to control engineering applications.
The project overall goal is to achieve the control of ball balancing on the resistive touch screen. The author has put a YouTube video link in below for explaining the project in a good detail.
Ball Balancing Project hackday Youtube Link
https://www.youtube.com/watch?v=IjByGk3S7Fg&t=38s
Here is my explain of the code
https://youtu.be/zj92io9M25Q

 

Overall, we want to achieve the control of the system using the integral control technique. In contrast to the full state feedback controller, which suffers from the steady state error problem, the integral control is able to reduce the  steady state error of the system. As such, it becomes an effective approach for controlling the balance of the ball to enable it to stay at the center of the resistive touch screen.

 

Ball Balancing Project Control Theory

Luenberger Observer (Observer Taught In Class for Two Weeks and Lab 4)

Overall, the system involves the use of Kalman observer to estimate the states and then remove the noises from the system. Kalman observer is similar to Luenberger observer. In the class, we have learnt the Luenberger observer in the previous chapter of our class. Hope you have understood it. But lets review the Luenberger observer again.
It should be noted that in class, we have not called it Luenberger observer much. Instead,we just called it observer for convenience. Hope this has not confused you for suddenly introducing a new terminology.  Luenberger observer is the default observer introduced in many textbooks. As such, people just tend to call it observer for convenience. Luenberger is one of the simplest observers available in teaching control class.
Full state feedback control is shown in below.
Figure 1: The full state feedback control. The state space representation matrix includes A,B,and C. It also includes the gain of K.
Luenberger observer is shown in below with the control diagram.
Figure 2: The summary of observer (L) and observer-based controller (L). It shows that we use full state feedback with an observer to realize the control using observer. Note that there is an obvious benefit to use observer \hat{x} rather than the original state to achieve the control. First, we can save some sensors to not measure every single state. Instead, we just use observer to estimate these states. Second, we can remove noises from the measured states.
The overall purpose of the Luenberger observer is to estimate the state so that we do not have to use many sensors to measure the states. The Luenberger observer shows that it is feasible to estimate the state of the plant through the use of the observer gain L matrix shown in Figure below.  With the observer gain L matrix, it becomes feasible to estimate \hat{x}.
Through the feedback process, with the feedback gain K, we do not use the original state x anymore. Instead, we will use the \hat{x} as the feedback.

The question is why we need to use observer state \hat{x} rather than just simply using x as the feedback as shown in the full state feedback control process. Note that x is the physical variables measured through the process such as position, velocity, and acceleration for a mechanical system. The reason that we use \hat{x} rather than x is because we do not suffer the problems associated with x, for example the noise issue related to the x. It also becomes feasible to save the number of sensors needed for the full state feedback process. Without the use of the observer in the control process, for the full state feedback controller to work, we will need to measure every single state to ensure the controller able to work. Now, with the use of observer, we can just use observer to estimate the state such as position, velocity, and acceleration. The benefit is that it will save the number of sensors needed to complete the wonderful control project.

The key equations associated with observer are

This equation is to design observer gain L.

    \[ \dot{e} = (A - LC)e \]

This equation is to show the estimated state \hat{x}.

    \[ \hat{x} = A\hat{x} + Bu + L(y - \hat{y}) \]

 

Full State Feedback Integral Control (Seen Lecture Notes, Last Group Quiz)

Very nice! It looks like that you have understood the observer better now. Now, its time to diver deeper. As we have hinted in the ball balancing project background section that we do need to use the integral control to remove the steady state error. It means that we want the ball stays at the center of the resistive touch screen. For this purpose, with the use of only full state feedback is insufficient. Full state feedback control is not able to ensure zero steady state error. In order to do this, we need use integral control. In order to use integral control, lets review integral control. Note that the integral control is similar to the integral control in the PID controller, but we wrap around of this concept in state space based full state feedback control.

The integral control is very similar to the full state feedback control. However, it includes another outer loop feedback to feedback the Y to the reference. Note that the integral gain Ke is also involved here. With the integral gain, it becomes feasible to reduce the steady state error.

While integral control looks promising, however, it does not have an observer. It means that it still uses the raw states as the feedback. It therefore can not remove the noises of the states. To improve this situation, we need to use Kalman observer. The reason that we use Kalman observer is because we need to remove the noises from the states. Luenberger Observer is not able to achieve this goal. Instead, we need to use Kalman observer to achieve this goal.

Figure 3: The summary of the full state feedback integral control (K). It includes the feedback control gain K and Ke.

 

Kalman Observer Based Full State Integral Control

Kalman observer is very similar to Luenberger Observer in principle. But it has the capability to remove noises from states. So often, we call it Kalman filter instead of calling it Kalman observer. Details of the mathematics of the Kalman observer can be refereed to the model predicative controller (MPC) chapter. In this case study, we will not focus on explaining the mathematics of Lalman observer. Rather, we will focus on how to implement it with python

Figure 4: The summary of the Kalman observer based full state feedback integral control (K). It includes the Kalman observer gain (L) and feedback control gain K and Ke.

Ball Balancing Project Modeling and Implementation

For this system, it can be described as the following state space.

(1)   \begin{equation*} \dot{x} = A x + b \end{equation*}

Where

    \[ A = \begin{bmatrix} 0 & 1 \\ 0 & 0 \end{bmatrix}, \quad B = \begin{bmatrix} 0 \\ 350 \end{bmatrix}, \quad C = \begin{bmatrix} 1 \\ 0 \end{bmatrix} \]

 

    \[ A_a = \begin{bmatrix} 0 & 1 & 0 \\ 0 & 0 & 0 \\ 1 & 0 & 0 \end{bmatrix}, \quad B_a = \begin{bmatrix} 0 \\ 350 \\ 0 \end{bmatrix}, \quad B_r = \begin{bmatrix} 0 \\ 0 \\ -1 \end{bmatrix}, \quad C_a = \begin{bmatrix} 1 & 0 & 0 \end{bmatrix} \]

With the state space, in python, it can be represented as below.

Figure 5: The state space representation matrix A,B,and C in Python for both initial full state feedback control and integral control.

Specifically, we also explained the correspondence of the python code, the control theory, and the control diagram. It includes the integral state (X_{N}), control input (u), state space, and Kalman observer.

The ball balancing integral control diagram is shown in the following.  Now the question is how to use python to implement the control theory. I have shown the process step by step:

 

Figure 6: The integral state X_{N} using python and the related diagram.

Figure 7: The control input u using python and the related diagram.

Figure 8: The state space representation \dot{x} = A x + b using python and the related diagram.

Figure 9: The Kalaman Observer gain  L using python and the related diagram.

 

Figure 9: The Kalaman Observer design using python and the related diagram.

 

Kalman Observer Based Full State Integral Control Results With Python and Arduino

With the above control theory and python knowledge, it becomes possible that we can simulate the ball balancing system. In this simulation, we assume there is simulation and also assume there is noise condition, where noise can be equal to 0 and 10.

measuredX = x[0,0] + np.random.randn() * noise

After running the simulation, the output is shown in below.  It is obvious that the use of Kalman observer is able to reduce the noises from the system.

Figure 11: The simulation of Kalman Observer Based Full State Integral Control without noises

Figure 12: The simulation of Kalman Observer Based Full State Integral Control with noises

 

Here is the link to the code used to produce the simulation results.
Kalman Observer Based Full State Integral Control Python Code Link
https://drive.google.com/file/d/1qAcrs-XwlM07WHt52FdAaUm3Kaqqie3g/view?usp=drive_link
You also can actually implement the Arduino code using Kalman Observer Based Full State Integral Control to control two servo motors. To make Arduino using the code to do this. We need to calculate the Kalman Gain (L)  and Integral gain (K) first using Python. Then we will use the the K and L computed with Python to do Arduino based control.  The Arduino Code and Results are shown in below.
Arduino Code and Simulation Link
https://wokwi.com/projects/415045376930392065

 

Figure 13: The simulation of Kalman Observer Based Full State Integral Control using Arduino for Ball Balancing. It is very fun and informative, please read the Arduino code carefully for how it is able to use the Kalman Gain (L) and Integral gain (K) to implement real-time Arduino based ball balancing control precisely using Kalman Observer Based Full State Integral Control technique.

License

Icon for the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

Robotics and Controls Engineering Copyright © by Hongbo Zhang; Elissa Ledoux; and Vishwas Bedekar is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, except where otherwise noted.