Design of a controller for a Modelica Model from Wolfram SystemModeler

SystemModel;WSM`$WSMVersion

Define the model

model = "Conference2018.ElevatorModel"

sm = SystemModel[model]

Simulate the model to Steady State and grab the model from WSM

sim =SystemModelSimulate[model,All,500,Method->{"StopAtSteadyState"->True}]

lsm=WSM`LocalizedSystemModel[sim,"StopTime"]

lsm["Properties"]

Now get the simplified Equations

simp = lsm["SelectOutputs",{"HeightOut"}]

simp2 = simp["Simplify"]

simp2["Properties"]

Examine the Equations, Operating Point and Parameters

simp2["DisplayEquations"]

initialCond =simp2["OperatingPoint"];
parameterVals = simp2["ParameterValues"];

Insert Parameter Values

simp2["DisplayEquations"]/. parameterVals

equations = simp2["Equations"]

vars = simp2["Variables"]

inputvars = simp2["Inputs"]

outputvars = simp2["Outputs"]

LQG Servo Design Demo

Descriptor State Space System Model of Elevator

demoSysDescriptor = simp2["Linearization"]/. parameterVals

Transform the demo model from descriptor form to regular state-space:

demoSys=
StateSpaceModel[
demoSysDescriptor,
StateSpaceRealization->"ControllableCompanion"]

Create and merge an Integrator into the system

Add a second input identical to the first; one for u-feedback, one for u-w for compatibility with LQGRegulator[ ] convention.

sumDualIn=StateSpaceModel[{{{}},{{}},{{}},{{1,1}}}]
demoSys2In=SystemsModelSeriesConnect[sumDualIn,demoSys]

Create a state space model of a simple integrator with a single  input, and no output.
A slight bit of damping added

intUo = StateSpaceModel[{{{-0.0001}},{{1}},{{}},{{}}}]

Merge the integrator (intUo) into the demo system (demoSys2In)

demoSysUo=SystemsModelMerge[{intUo,demoSys2In}]

Connect the feedback to the integrator, Next design the LQG Regulator

Add feedback from the output of the simple system to the input of the integrator. The regulator will try to zero this first input by adjusting the feedback position. Use a plus sign on the feedback so the regulated system will travel in the desired direction.

demoSysUoFb=SystemsModelFeedbackConnect[demoSysUo,{{1,1,1}}]

Generate an LQG regulator for compSysUoFb, note that the first input will be our servo reference.

demoSysUoFbReg=
LQGRegulator[
{demoSysUoFb,1,2,1},
{{{1}},{{0.001}}},
{DiagonalMatrix[{10,1,1,1,1}],{{.01}}}]

Now we can test the new regulator by simulating it in Mathematica, First Connect the regulator and the system

Cascade the regulator simpSysUoFbReg with the un-modified (not simpSysUo) simple system,
connecting the output of the regulator (port 1) to the input of simpSys (port 1)

demoSysUoFbRegDemoSys=SystemsModelSeriesConnect[demoSysUoFbReg,demoSys,{{1,1}}]

Hook the  output of the serial combined system (port 1) simpSysUoFbRegSimpSys to the regulator sensor input (port 2)

demoSysServ=SystemsModelFeedbackConnect[demoSysUoFbRegDemoSys,{1,2,-1}]

Simulate and plot the response

Original open-loop system and closed-loop LQG servo system

tmaxDemo=5;
demoSysOut=OutputResponse[demoSys2In,{1,0}*UnitStep[t],{t,0,tmaxDemo}];
demoSysServOut = OutputResponse[demoSysServ,{1,0}*UnitStep[t],{t,0,tmaxDemo}];
Grid[{{Prepend[Sort[Eigenvalues[Normal[demoSys][[1]]]],"   EigenValues:"]//TableForm,Plot[demoSysOut,{t,0,tmaxDemo},GridLines->Automatic,PlotRange->All,ImageSize->Large,PlotLabel->"Open-Loop Response"]}}]
Grid[{{Prepend[Sort[Eigenvalues[Normal[demoSysServ][[1]]]],"   EigenValues:"]//TableForm,Plot[demoSysServOut,{t,0,tmaxDemo},PlotRange->All,GridLines->Automatic,ImageSize->Large,PlotLabel->"Closed-Loop LQG Servo Response"]}}]

Load the regulator into WSM

regmodel=CreateSystemModel["Conference2018.Regulator",demoSysUoFbReg];