Tutorial: Working with Model Hierarchy and Parameterization

Learn how to add a PID controller and dynamics to a water tank model. Learn how to parameterize the model with context variables and mask super blocks to hide details of the model hierarchy.

parametrization

Files for This Tutorial

Watertank.scm

A finished version of the model you build in this tutorial along with any files required to complete the tutorials are available from the Demo Browser: Tutorial Models > Learning the basics or at this location: <installation_directory>/Tutorial Models/Learning the basics.

Overview of a Watertank Problem

In a water tank model, a PID controller is added to regulate the flow of water into the tank.

The following flow equations represent the water tank model:

q out =m× 2gh R h ˙ = q in q out h ˙ = q in R m R × 2gh MathType@MTEF@5@5@+= feaahGart1ev3aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLn hiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr 4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq=Jc9 vqaqpepm0xbba9pwe9Q8fs0=yqaqpepae9pg0FirpepeKkFr0xfr=x fr=xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGceaqabeaacaWGXb WaaSbaaSqaaiaad+gacaWG1bGaamiDaaqabaGccqGH9aqpcaWGTbGa ey41aq7aaOaaaeaacaaIYaGaam4zaiaadIgaaSqabaaakeaacaWGsb GabmiAayaacaGaeyypa0JaamyCamaaBaaaleaacaWGPbGaamOBaaqa baGccqGHsislcaWGXbWaaSbaaSqaaiaad+gacaWG1bGaamiDaaqaba aakeaaceWGObGbaiaacqGH9aqpdaWcaaqaaiaadghadaWgaaWcbaGa amyAaiaad6gaaeqaaaGcbaGaamOuaaaacqGHsisldaWcaaqaaiaad2 gaaeaacaWGsbaaaiabgEna0oaakaaabaGaaGOmaiaadEgacaWGObaa leqaaaaaaa@5938@

By controlling the inflow rate qin of water into the tank, the water level h can be reached within a given time. The given system model has only one input and one output, qin and qout, and therefore requires the addition of a classic PID controller to control the flow rate of water.

The PID controller is implemented as a super block that is comprised of basic arithmetic and integration blocks. The coefficients of the Proportional, Integral and Derivative terms of the PID controller are defined as context variables, which you can edit with OML commands.

The controller in the super block is masked and looks like a regular Twin Activate block. Though the mask hides the implementation details of the super block, the PID parameters are accessible by double-clicking the super block.

Constructing a PID Controller

Create a model with a PID controller to regulate water flow.

  1. From the ribbon, in the the Files tool group, click .
  2. Save the new model as watertank_practice.scm.
  3. From the Palette Browser, add the following blocks into your diagram:
    • From Activate > MathOperations > Gain, drag and drop 3 Gain blocks.
    • From Activate > Dynamical, drag and drop 1 Integral block.
    • From Activate > Dynamical, drag and drop 1 Derivative block.
    • From Activate > MathOperations, drag and drop 1 Sum block.
  4. Position the blocks as you see in the following figure:
  5. For each Gain block, double-click, enter a value for the Gain parameter, and then click OK.
    • For the upper Gain block, enter P.
    • For the middle Gain block, enter I.
    • For the lower Gain block , enter D.
    Note: Assigning a value P, I, or D to a parameter indicates that the value represents a context variable that you will define later in the tutorial.
    Your blocks should look something like this:


  6. Double-click the Sum block. For Number of inputs, enter 3. In the Signs table, assign “+” to all inputs:
  7. Connect the blocks and adjust the links as you see in the following figure:
  8. Click and drag your cursor to box-select all of the blocks:
  9. From the ribbon, select the tool, Create Super Block .
    The blocks in the diagram convert into a super block:
  10. Double-click the SuperBlock, and the diagram opens.
  11. From the Palette Browser > Activate > Ports, drag and drop one Input block and one Output block into the diagram.
  12. Connect the blocks as you see in the following figure:
    The Input and Output blocks serve as the interface to exchange signals from within the super block to the blocks outside of the super block.
  13. To view the top diagram, in an empty area of the modeling window, double-click.
    The input and output ports should be attached to the super block:
  14. To name the super block, select it, press F2, then enter Controller.
    The name of the super block changes to Controller:

Modeling the Water Tank Dynamics

Construct the dynamics of a water tank based on three flow equations.

  1. From the Palette Browser, add the following blocks to your diagram:
    • From Activate > MathOperations, drag and drop two Gain blocks.
    • From Activate > MathOperations, drag and drop one Sum block.
    • From Activate > MathOperations, drag and drop one MathFunc block.
    • From Activate > Dynamical, drag and drop one Integral block.
  2. After adding the blocks, select the MathFunc block, right-click, and select Flip. Do the same for one of the Gain blocks.
  3. Arrange the blocks as you see in the following figure:
  4. For the upper and lower Gain blocks, double-click, enter a value for the parameter, Gain, and then click OK.
    • For the upper Gain block, enter b/R.
    • For the lower Gain block, enter a/R.
  5. On the MathFunc block, double-click. For the Function parameter, enter “sqrt”, and then click OK.
  6. Connect the blocks:


  7. From the Palette Browser, add the following blocks into your diagram:
    • From Activate > MathOperations, drag and drop one Sum block.
    • From Activate > SignalGenerators, drag and drop one Constant block.
    • From Activate > SignalViewers, drag and drop one Scope block.
  8. On the Constant block, double-click. For the parameter, Constant, enter 10, and then click OK.
  9. Arrange and connect the blocks:


  10. On the diagram, click and drag to box-select the blocks you see in the following figure:


  11. Right-click and select Super Block.
    In the diagram, the blocks in the selected area convert into a super block. The Super Block operation automatically links the new super block to the existing blocks in the diagram.
  12. In the Property Editor, select the new super block. Double-click the name field and enter the name, Watertank.
  13. Modify the connections of the Watertank block to the other blocks in the diagram:


  14. Save your model.

Defining Context Variables

On the Context tab of the Diagram dialog, enter values for the main and local context variables.

  1. In an empty area of the root diagram, right-click, and then select Context, or from the menubar, select the Diagram tool .
    The Diagram dialog opens.
  2. In the Context tab, enter the OML commands as you see in the following figure, and then click OK.
    While the context variables P, I and D are defined in the context of the main diagram, the variables used elsewhere in the model also receive these values unless the values are defined in a local context.
  3. On the Watertank super block, double-click.
  4. In an empty area of the main diagram, right-click, and then select Context.
  5. On the Context tab, enter the OML commands as you see in the following figure, and then click OK.
    The context variables R, a and b are defined in the diagram inside of the super block. The scope of these variables is semi-global, meaning they are visible only in the current diagram and any super block in the current diagram. Since the Gain blocks are in the current diagram and contain the variables R, a and b, the variables are defined based on the values specified on the Context tab.
  6. From the ribbon, select Run.
    The simulation starts.
  7. In the main diagram, on the Scope block, double-click.
    A Scope window appears.
  8. To fit the view of the Scope in the window, middle-mouse click.
    The curve shows the water tank reaches and remains at 10, the designated level.

Masking the Super Block

Mask the Controller super block and tune the P, I, and D parameters.

Though the details of the Controller super block are hidden when masked, the process of editing parameters in a block parameter dialog is the same as for a regular block.
  1. In the root diagram, select the Controller super block. From the ribbon, select the Auto Mask tool .
    The super block is masked automatically.
  2. On the masked super block, Controller, double-click.
    A parameter dialog appears. As input entries, the parameter values I, D, and P are context variables defined in the context of the root diagram.
  3. Select the super block, Controller, and then from the ribbon, select the Edit Mask tool.
  4. In the Masking Editor dialog, in the Short Description field, enter Basic PID Controller.
  5. For each parameter, click . In the Parameters Creator dialog, enter the information as you see in the following figures and click OK.
  6. After entering the parameter information, your table should populate like this:
  7. On the Controller super block, double-click. The parameter descriptions should be updated. Enter numeric values for the parameters as you see in the following figure:
  8. From the ribbon, select Run.


    The simulation starts. A Scope window appears with a plot of the following results:
  9. On the super block, Controller, double-click. In the Proportional Parameter field, enter 8.
  10. Run the simulation again. Your plot should look something like this:
    The Scope window shows that the output increases more rapidly at the beginning, while overshoot is reduced.
  11. Save your model.