Model based control design using MathModelica and Control System Professional

Peter Aronsson 
Jan Brugård 
Erik Andersson

MathCore Engineering AB

Friday 4:00 in Boilermakers

Preface

To run this notebook you need Mathematica, Control System Professional, and MathModelica System Designer Professional.

Please contact your local Wolfram sales representative to get information about Mathematica and Control System Professional.

Please contact your local Wolfram sales representative or MathCore directly to get information about MathModelica System Designer Professional or get a trial.

To purchase MathModelica System Designer Professional please contact your local Wolfram sales representative or MathCore directly.

Contact Information

MathCore Engineering AB
Teknikringen 1F
583 30 LINKÖPING
Sweden

email: sales@mathcore.com

phone: +46 13 328500

web: www.mathcore.com

Background

Model based control design is a powerful tool for optimizing the operation of systems. The development scheme can be summarized in the following steps:

1)	Setting up the system equations (e.g. Ohms law for a resistor)
2)	Deriving the state space representation
3)	Selecting a control structure
4)	Linearizing the state space
5)	Designing control parameters
6)	Testing the complete system

Control System Design in Mathematica

Outline

Setting up the system equations

State space representation

Linearization

Testing and refining the model

Conclusion

System Equations

MathModelica makes it possible to model a system using drag'n'drop of model components. Let us consider a two tank system.

Initialization (load MathModelica and ControlSystems)

<<MathModelica`

<<ControlSystems`

SetControlFormat[];

StartModelEditor["C:\\petar\\presentations\\Wolfram Technology Conference 2007\\DoubleTankSystem.mo"]

State space form from Modelica model

{eqns,initeqns,states,algvars,parameters} =ModelEquations[DoubleTankSystem.DoubleTankOpenLoop]/.HoldPattern[Member[args___,x_]]:>Composition[args][x];

parameters =Join[{ _[g]->g},parameters];

eqns

To convert this to state - space form: (OverDot[x])=f(x,u)
y=h(x,u)  we need to solve for (OverDot[x]) and y

xDot = D[states,t];
sol = Solve[eqns,Join[xDot,algvars]]

f = xDot/.First[sol]

h=y[t] /. First[sol]

Linearize the state-space

states

ss = Linearize[f,h,{{tank1[V][t],1},{reservoir1[V][t],100},{tank2[V][t],1}},{u[t],1}]

Applying parameter settings gives numerical values

%/.parameters /. g->9.81

The state in the reservoir is not interesting, reduce the system.

ss = MinimalRealization[ss]/.parameters

Design a controller

Create transferfunction for PID controller

pid=TransferFunction[s,{{kp+(ki/s)+((kd s)/(1+s τ))}}];

Connect the system.

sc=SeriesConnect[pid, ss]//FeedbackConnect//Simplify  /.parameters

tf=TransferFunction[s,sc]//ExpandRational

den=Denominator[tf[s]][[1,1]]

d1=Expand[(den/(2τ))]

Create polynomial for four poles

d2=(s-p1)(s-p2)(s-p3)(s-p4)

Place the poles

poleRules={p1->4(-0.5+I),p2->4(-0.5-I),p3->-4,p4->-4};

coeff=Solve[CoefficientList[d1-d2,s]==0,{kp,kd,ki,τ}] /. poleRules/. g ->9.81//Chop

Lets make a manipulate to control the poles

Wrap the stuff above in some functions...

PlacePoles[f_,h_,sWorkPoint_,uWorkPoint_,parameters_,poleRules_]:=
Module[{ss,pid,sc,tf,den,d1,d2},
ss = Linearize[f,h,sWorkPoint,uWorkPoint];
ss = MinimalRealization[ss] /. parameters;
pid = TransferFunction[s,{{Kp+(Ki/s)+((Kd s)/(1+s τ))}}];
sc=SeriesConnect[pid, ss]//FeedbackConnect//Simplify  /.parameters;
tf=TransferFunction[s,sc]//ExpandRational;
den=Denominator[tf[s]][[1,1]];
d1=Expand[(den/(2τ))];
d2=(s-p1)(s-p2)(s-p3)(s-p4);
coeff=Solve[CoefficientList[d1-d2,s]==0,{Kp,Kd,Ki,τ}] /. poleRules/. g ->9.81//Chop;
If[coeff ==={},Return[$Failed]];
BodePlot[sc/.parameters/.First[coeff]]
]

PlacePoles2[po1_,po2_,po3_]:= PlacePoles[f,h,{{tank1[V][t],1},{reservoir1[V][t],100},{tank2[V][t],1}},{u[t],1},parameters,{p1->po1,p2->po2,p3->po3[[1]]+I po3[[2]],p4->po3[[1]]-I po3[[2]]}]

The we make a "manipulate" with four poles (two complex conjugated and two Reals) and a bode plot

DynamicModule[{p={-2,-1},p2={-2,1},p3={-4,0},p4={-4,0},x=Graphics[]},Grid[{{Graphics[{Line[{{-10,-10},{0,0},{-10,10}}]
,
Dynamic[{Locator[Dynamic[p,(p={#[[1]],#[[2]]};p2={#[[1]],-p[[2]]})&]],Locator[Dynamic[p2,(p2={#[[1]],#[[2]]};p={#[[1]],-p2[[2]]})&]],
Locator[Dynamic[p3,(p3={#[[1]],0})&]],
Locator[Dynamic[p4,(p4={#[[1]],0})&]]}]},PlotRange->{{-10,0},{-10,10}},Frame->True,Axes->True],Dynamic[x]},{
Button["Bode plot...",x=PlacePoles2[p3[[1]],p2[[1]],p]]}}]]

Verification

MathModelica makes it possible to verify the control design

We can now test the non-linear system using MathModelica. We give a trapezoid signal as reference signal for the controller and investigate the behavior.

Simulate[DoubleTankSystem.DoubleTankClosedLoop,{t,0,10},ParameterValues->{PID1.kd==0.6239552417668423`,PID1.kp==8.624091685590432`,PID1.ki==19.085470183190278`,PID1.tau == 0.13209074544195226`}];

PlotSimulation[{feedback1.u1[t],tank2.V[t]},{t,0,10}]

If we instead place the poles like this

poleRules2={p1->4(-0.5+0.5I),p2->4(-0.5-0.5I),p3->-4,p4->-4};
coeff2=Solve[CoefficientList[d1-d2,s]==0,{kp,kd,ki,τ}] /. poleRules2/. g ->9.81//Chop

Simulate[DoubleTankSystem.DoubleTankClosedLoop,{t,0,10},ParameterValues->{PID1.kd==0.46475273516093163`,PID1.kp==4.411059020798`,PID1.ki==7.634188073276113`,PID1.tau==0.13209074544195226`}];PlotSimulation[{feedback1.u1[t],tank2.V[t]},{t,0,10}]

How will the system behave if we add a disturbance?

This can easily be done in the graphical editor, where it is possible to select between a wide range of signal sources as disturbance.

What about large complex systems ?

Large complex systems in Modelica

High index DAE (Differential and Algebraic Equation) systems and not so often ODEs

Contain hybrid elements (events, discrete variables, etc)

Contain non-linear equations (e.g. calulated from measurement data and/or external C/Fortran functions)

Can be very large (system models over 100 000 equations have been modelled using Modelica)

All this makes it hard to find a state-space form symbolically in Mathematica.

MathModelica can linearize the model numerically

MathModelica can directly retrieve the linearized state-space for a model, given its operating point.

LinearizeModel[DoubleTankSystem.DoubleTankClosedLoop]

LinearizeModel[DoubleTankSystem.DoubleTankOpenLoop]

Extending the model

With MathModelica model editor it is easy to change the model by drag'n'drop. Lets add another tank and compare the bodeplots

GraphicsGrid[{{BodePlot[LinearizeModel[DoubleTankSystem.DoubleTankOpenLoop]],BodePlot[LinearizeModel[DoubleTankSystem.TrippleTankOpenLoop]]}},ImageSize->800]

Drive line example

Ongoing master thesis work at MathCore/Linköping university by Eric Andersson.

Thesis work includes

Control design of car drive line using MathModelica and Control System Professional (CSP)

Control design of diesel engine using MathModelica and CSP

Investigate different methods incl. optimal design using Lyapunov equations, LQ design, etc.

Identify requirements for working conveniently with MathModelica and CSP

Are you using CSP today and need better tool for building models and verify your system? Don't hesitate to contact us: info@mathcore.com

Conclusions

The combination of MathModelica System Designer Professional and Control System Professional makes it possible to cover the complete control design process:

Build model of a system in a convenient way using drag'n'drop in MathModelica

Analyze model equations to derive a symbolic linear state-space form using Mathematica

Linearize non-linear models numerically using MathModelica

Design Control System using Control System Professional (either symbolically or numerically)

Verify the design on the non-linear model using MathModelica

Acknowledgements

Thanks to Igor Bakshee and Nirmal Malapaka for providing beta version of CSP