Ball and Beam: Controlling the Position of a Ball along a Beam

Introduction

This model studies a ball rolling on top of a beam. The ball translational acceleration will be dependent on how the beam is angled. This example studies two different control schemes, the PID regulator and the LQ regulator, which can be used to control the position of the ball along the beam, using the beam angle as input.

In this notebook, we will explore how the Wolfram Language can be used to find optimal parameters for the PID controller and calculate the LQ regulator gains.

Explore the available scenarios by evaluating each cell in order. Evaluation is done by selecting a cell and pressing Shift+Enter.

In order to get the full experience of this example, you need the following:

Wolfram SystemModeler; a free trial download is available at www.wolfram.com/system-modeler

The model that came in the same bundle as this notebook

For more information about Wolfram SystemModeler, visit www.wolfram.com/system-modeler.

Initialize

Define the model names:

mmodel="EducationExamples.Physics.BallAndBeam.Components.BallAndBeamModel";
gravityModel="EducationExamples.Physics.BallAndBeam.Components.GravitationalPull";
partialModel="EducationExamples.Physics.BallAndBeam.Components.BallAndBeamBase";

The Model

You can visualize the model using the Diagram property:

SystemModel[mmodel,{"Diagram",ImageSize-> 570,Frame-> False}]

With the SystemModelLinearize function, you can find a linearized state-space representation of how the ball position and velocity are dependent on the beam angle:

ss=SystemModelLinearize[gravityModel]

Define that you want to use the ball position Ball.s as the output:

ss=ReplacePart[ss,{{2,2,2}->"y",{1,3}->{{1,0}},{1,4}->{{0}}}]

Using the TransferFunctionModel function, you can convert the state-space representation into a time-invariant function:

tf=TransferFunctionModel[ss,s]

PID Controller

Using the transfer function that was calculated in the previous section, you can calculate parameters for the PID controller that place the system poles favorably.

Tuning the Controller

Use the PIDTune function to find the PID parameters:

pid=PIDTune[tf,"PID","PIDData"];

These are the parameters that will be used in the PID controller:

({k,Ti,Td}=pid["FeedbackIdealParameters"])//Column

Connect the Controller

Extend the BallAndBeamBase partial model, this has all the base components required for the control setup.

SystemModel[partialModel,{"Diagram",Frame-> False}]

The actual PID controller is the only additional component that will be required.

components={
"pid"∈"Modelica.Blocks.Continuous.PID"
};

These are the connections between the components:

connections = {
"pulse.y"\[UndirectedEdge]"feedback.u1",
"feedback.y"\[UndirectedEdge]"pid.u",
"pid.y"\[UndirectedEdge]"ballAndBeam.u",
"ballAndBeam.x"\[UndirectedEdge]"feedback.u2"
};

Use the ConnectSystemModelComponents function to connect the components and create a model:

pidModel=ConnectSystemModelComponents["BallAndBeamPID",components,connections,<|"ExtendsModels"-> partialModel|>];
SystemModel[pidModel,{"Diagram",Frame-> False}]

When simulating, use the parameters for the PID that were found in the previous subsection and set the parameters for the pulse:

parameters = {
"pid.k"->k,
"pid.Ti"->Ti,
"pid.Td"->Td,
"amplitude"->0.07,
"period"->10,
"offset"->-0.05
};

Simulate the Controller

Set the model parameters and simulate:

pidUpdatedModel = SetSystemModel[pidModel,<|"ParameterValues"->parameters|>];
pidSim=SystemModelSimulate[pidUpdatedModel,20];

You can study how the controller performs by plotting the reference position and the actual ball position, using the SystemModelPlot function:

SystemModelPlot[pidSim]

LQR Controller

Here an alternative control scheme, the LQ regulator, is explored.

Tuning the Controller

With the LQRegulatorGains function, you can find the gain matrix for the LQ regulator:

K=LQRegulatorGains[ss,{(({{1, 0}, {0, 0}})),(({{1}}))}];

Connect the Controller

Once again, the partial model will be used. The LQR gain is the only additional component that will be required.

components={
"LQRGain"∈"Modelica.Blocks.Math.MatrixGain"
};

These are the connections between the components:

connections = {
"pulse.y"\[UndirectedEdge]"feedback.u1",
"feedback.y"\[UndirectedEdge]"ballAndBeam.u",
"ballAndBeam.x"\[UndirectedEdge]"LQRGain.u[1]",
"ballAndBeam.v"\[UndirectedEdge]"LQRGain.u[2]",
"LQRGain.y[1]"\[UndirectedEdge]"feedback.u2"
};

Connect the components and create a model with the ConnectSystemModelComponents function:

lqrModel=ConnectSystemModelComponents["BallAndBeamLQR",components,connections,<|"ExtendsModels"-> partialModel|>];
SystemModel[lqrModel,{"Diagram",Frame-> False}]

When simulating, use the parameters for the PID that were found in the previous subsection and set the parameters for the pulse:

parameters = {
"LQRGain.K[1,1]"->K[[1,1]],
"LQRGain.K[1,2]"->K[[1,2]],
"amplitude"->0.07,
"period"->10,
"offset"->-0.05
};

Simulate the Controller

Set the model parameters and simulate:

lqrSim = SystemModelSimulate[lqrModel,20,<|"ParameterValues"->parameters|>];

You can study how the controller performs by plotting the reference position and the actual ball position, using the SystemModelPlot function:

SystemModelPlot[lqrSim]

Compare the Controllers

You can plot the output from the two controller simulations together to see how they perform:

Legended[
Show[
SystemModelPlot[lqrSim,{"y","x"},
PlotStyle->{{Black,Dotted},ColorData[97][1]},
PlotLegends->None,
PlotRange->{{0,20},{-0.15,0.15}}],
SystemModelPlot[pidSim,{"x"},
PlotStyle->ColorData[97][2],
PlotLegends->None],ImageSize-> 570
],
Placed[
LineLegend[{
ColorData[97][1],ColorData[97][2],{Black,Dotted}},{"Ball Position (LQR)","Ball Position (PID)","Reference"},
LegendLayout-> "Row"],
Below]
]

Terms and Conditions of Use

This domain example is an informational resource made freely available by Wolfram Research.

Use of This Example

• You may not use this example for any purpose that is unlawful or dangerous.
• You assume total responsibility and risk for your use of this example.
• You may not present this example with any alteration to its trade dress, trademarks, or design.

License

All content in this bundle is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. By accessing the content or using it in any way, you accept and agree to be bound by the terms of this license. If you do not agree to these Terms of Use, you may not use this content. Wolfram Research reserves the right to change, modify, add to, or remove portions of these Terms of Use at any time without notice. Please refer back to www.wolfram.com for the latest Terms of Use.

A summary of the licensing terms can be found at:
http://creativecommons.org/licenses/by-nc-sa/3.0

The full legal code can be found at: 
http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode