-
Notifications
You must be signed in to change notification settings - Fork 2
Example: Full State Feedback Controller for an Inverted Pendulum
In this example we will develop a full state feedback controller for an inverted pendulum, which is shown in the following picture.
The inverted pendulum is an example commonly found in textbooks and research literature. Its popularity derives in part from the fact that it is unstable without control. Furthermore, the dynamics of the system are nonlinear. The task of the full state feedback controller is to balance the pendulum by applying a force to the cart that the pendulum is attached to. In order to develop this controller, a system model is needed.
The pendulum is already modeled as an ODESCA_Component. You can find it in the folder Examples->Components with the name OCLib_Pendulum. If you need help creating a custom component, please refer to our Getting Started Section.
Having modeled the pendulum, we are able to create an instance of it.
Pendulum_comp = OCLib_Pendulum('myPendulum');
In the next step we will complete the component by setting all the parameters.
Pendulum_comp.setParam('M0',4); % mass of the cart
Pendulum_comp.setParam('M1',.36); % mass of the pendulum
Pendulum_comp.setParam('l_s',.451); % length from suspension to center of mass of the pendulum
Pendulum_comp.setParam('theta',.08433); % moment of inertia
Pendulum_comp.setParam('Fr',10); % speed proportional friction constant of the cart
Pendulum_comp.setParam('C',.00145); % friction coefficient of the pendulum
Pendulum_comp.setParam('g',9.81); % gravity
After we created and parametrized the component, we can now create an ODESCA_System. The system consists of only one component, which we can already add in the constructor of the ODESCA_System:
Pendulum_sys = ODESCA_System('myPendulumSystem',Pendulum_comp)
If you take a look at the system now, you should see 4 states, 1 input, 2 outputs and 7 parameters.
For the creation of a full state feedback controller, we need a linear approximation of the nonlinear system. The approximation is only accurate close to the center of the series. We want to linearize the system at the unstable equilibrium because we want to use the controller to stay exactly in this position.
First, we need to create an ODESCA_SteadyState which is the base for the creation of a system approximation such as the linearization. If (x0,u0) is no valid steady state, the creation of an ODESCA_SteadyState will display a warning. The function linearize finally creates an instance of the class ODESCA_Linear.
ss = Pendulum_sys.createSteadyState([0,0,0,0],0,'mySteadyState');
Pendulum_sys_lin = ss.linearize();
All the different classes (ODESCA_Component, ODESCA_System, ODESCA_SteadyState, ODESCA_Linear) are still connected to each other so that it is always distinct, which system belongs to which approximation et cetera. For example
Pendulum_sys_lin.steadyState.system.components
will give you the names of all components linked to the system.
ODESCA already provides a function that creates a full state feedback controller with precompastion using the state space model of the linearized system calculated before. The feedback matrix K and the precompensation matrix V are calculated inside the function. Finally, the nonlinear system including the controller is created in Simulink.
Pendulum_sys_lin.createFSF();
However, executing the previous command, we get a warning saying that calculating a precompensation matrix did not succeed due to different I/O dimensions. Having two outputs but only one input in the system, the well known formula for calculating the precompensation matrix fails. So, the Simulink model is created without precompensation, which is not a problem here because the setpoint of the angle is zero, so we only need precompensation for the position.
We already created the whole system for you in Examples->Systems->FSF_Pendulum.
As you can see, the system can perform a step in the position of the cart and the angle still stays zero. Uncontrolled by just applying an impulse on the force to change the position, the pendulum leaves its unstable equilibrium.