Contenuto principale

Control of Inverted Pendulum on Cart

This example uses systune to design a controller for an inverted pendulum on a cart.

Pendulum/Cart Assembly

The cart/pendulum assembly is modeled in Simulink® using Simscape™ Multibody™.

This system is controlled by exerting a variable force F on the cart. The controller needs to keep the pendulum upright while moving the cart to a new position or when the pendulum is nudged forward (impulse disturbance dF).

Control Structure

The upright position is an unstable equilibrium for the inverted pendulum. The unstable nature of the plant makes the control task more challenging. For this example, you use the following two-loop control structure.

mdl = "rct_pendulum";
open_system(mdl)

The inner loop uses a second-order state-space controller to stabilize the pendulum in its upright position (θ control), while the outer loop uses a Proportional-Derivative (PD) controller to control the cart position. You use a PD rather than PID controller because the plant already provides some integral action.

The plant model for the inverted pendulum is implemented using Simscape Multibody.

open_system(mdl + "/Plant")

Design Requirements

Use TuningGoal requirements to specify the desired closed-loop behavior. Specify a response time of 3 seconds for tracking a setpoint change in cart position x.

req1 = TuningGoal.Tracking('xref','x',3);

To adequately reject impulse disturbances dF on the tip of the pendulum, use an LQR penalty of the following form.

0(16θ2(t)+x2(t)+0.01F2(t))dt

This penalty emphasizes a small angular deviation θ and limits the control effort F.

Qxu = diag([16 1 0.01]);
req2 = TuningGoal.LQG('dF',{'Theta','x','F'},1,Qxu);

For robustness, require at least 6 dB of gain margin and 40 degrees of phase margin at the plant input.

req3 = TuningGoal.Margins("F",6,40);

Finally, constrain the damping and natural frequency of the closed-loop poles to prevent jerky or underdamped transients.

MinDamping = 0.5;
MaxFrequency = 45;
req4 = TuningGoal.Poles(0,MinDamping,MaxFrequency);

Control System Tuning

The closed-loop system is unstable for the initial values of the PD and state-space controllers (1 and 2/s, respectively). You can use systune to jointly tune these two controllers. Use the slTuner interface to specify the tunable blocks and register the plant input F as an analysis point for measuring stability margins.

ST0 = slTuner(mdl,["Position Controller","Angle Controller"]);
addPoint(ST0,"F");

Next, use systune to tune the PD and state-space controllers subject to the performance requirements specified above. Optimize the tracking and disturbance rejection performance (soft requirements) subject to the stability margins and pole location constraints (hard requirements).

rng(0)
Options = systuneOptions(RandomStart=5);
[ST, fSoft] = systune(ST0,[req1,req2],[req3,req4],Options);
Final: Soft = 1.26, Hard = 0.99988, Iterations = 281
Final: Soft = 1.44, Hard = 0.99933, Iterations = 140
Final: Soft = 1.44, Hard = 0.99865, Iterations = 286
Final: Soft = 1.26, Hard = 0.99984, Iterations = 258
Final: Soft = 1.44, Hard = 0.99857, Iterations = 311
Final: Soft = 1.27, Hard = 0.99847, Iterations = 220

The best design achieves a value close to 1 for the soft requirements while satisfying the hard requirements (Hard<1). This means that the tuned control system nearly achieves the target performance for tracking and disturbance rejection while satisfying the stability margins and pole location constraints.

Validation

Use viewGoal to further analyze how the best design fares against each requirement.

figure(Position=[100 100 575 660])
viewGoal([req1,req3,req4],ST)

MATLAB figure

These plots confirm that the first two requirements are nearly satisfied while the last two are strictly enforced. Next, plot the responses to a step change in position and to a force impulse on the cart.

T = getIOTransfer(ST,["xref","dF"],["x","Theta"]);
figure(Position=[100 100 650 420]);
subplot(1,2,1)
stepplot(T(:,1),10)
title("Tracking of set point change in position")
subplot(1,2,2)
impulseplot(T(:,2),10)
title("Rejection of impulse disturbance")

MATLAB figure

The responses are smooth with the desired settling times. Inspect the tuned values of the controllers.

C1 = getBlockValue(ST,"Position Controller")
C1 =
 
               s    
  Kp + Kd * --------
             Tf*s+1 

  with Kp = 5.97, Kd = 2, Tf = 0.052
 
Name: Position_Controller
Continuous-time PDF controller in parallel form.
Model Properties
C2 = zpk(getBlockValue(ST,"Angle Controller"))
C2 =
 
  -1630.5 (s+12.9) (s+4.355)
  --------------------------
     (s+135.7) (s-14.47)
 
Name: Angle_Controller
Continuous-time zero/pole/gain model.
Model Properties

The angle controller has an unstable pole that pairs up with the plant unstable pole to stabilize the inverted pendulum. To see this, obtain the open-loop transfer at the plant input and plot the root locus.

L = getLoopTransfer(ST,"F",-1);
figure
rp = rlocusplot(L);
rp.XLimits = [-25 20];
rp.YLimits = [-20 20];

MATLAB figure

To complete the validation, update the controller parameters in the model and simulate the nonlinear response of the cart/pendulum assembly.

writeBlockValue(ST)

This animation shows the simulation results from Multibody Explorer.

See Also

|

Topics