Essential Steps for Constructing a Physical Model
Workflow Overview
The table lists the essential major steps for building and simulating
a physical model, along with related documentation topics that provide
background information on each step. Detailed step descriptions follow.
The graphic illustrates the key workflow steps as applied to
the Mass-Spring-Damper
with Controller example model. Step descriptions below include
detailed instructions on how to create this model.
Step 1: Create New Model Using ssc_new
Using ssc_new
is the best way to start
building Simscape™ models. It helps to ensure that you use recommended default settings
for your model. When you use ssc_new
, it automatically:
Creates a new Simscape model, with required and
commonly used blocks already on the model canvas
Selects recommended solver and tolerance settings
Enables data logging for the whole model
Detailed Instructions
In the MATLAB® Command Window, type:
The new untitled model, with the default solver set to VariableStepAuto
,
contains a Solver Configuration block, a
Simulink-PS Converter block, and a
PS-Simulink Converter block connected to a
Scope block.
The resources section at the bottom of the model window provides links to the
relevant block libraries and documentation topics.
Step 2: Assemble Physical Network
To model your system, you add blocks from the Simscape libraries
to a model, and then connect them into a physical network. The lines
connecting the blocks in the schematic you create represent the physical
connections that exist between the components in a real system that
you are modeling. In other words, Simscape diagrams mimic the
physical system layout.
When constructing your network, it is important to include domain-specific reference blocks,
such as Electrical Reference,
Mechanical Translational Reference, and so on.
Depending on domain, these blocks represent connection to ground, frame, or
atmosphere. For more information, see Grounding Rules.
Detailed Instructions
Open the Simscape > Foundation Library > Mechanical
> Translational Elements library.
Drag the Mass,
Translational Spring,
Translational Damper, and
Mechanical Translational Reference
blocks into the model window.
Connect the Translational Spring,
Translational Damper, and
Mass blocks to the
Mechanical Translational Reference
block as shown in the next illustration. To rotate a block, select it
and press Ctrl+R.
To adjust visual presentation of the model, shorten the names of the
Spring and Damper blocks, as shown. To have the name of the
Mass block appear along with Spring
and Damper, right-click the Mass block
and, from the context menu, select
> >
.
Step 3: Adjust Block Parameters and Variable Targets
Simscape blocks represent generic components that have
default initial values for block parameters and variables. You can
adjust these values to suit your application or match the manufacturer
data sheet.
Detailed Instructions
In this model, you will adjust parameters that control the mass,
the spring stiffness, and the damping coefficient. You will also specify
the initial velocity of the mass by adjusting the respective variable
target.
Double-click the Spring block. Set
its Spring rate to 400 N/m
.
Double-click the Damper block. Set
its Damping coefficient to 100 N/(m/s)
.
Double-click the Mass block. Set its Mass to 3.6
kg
.
To specify the desired initial value for mass velocity,
click the Variables tab. The Velocity variable
already has its Priority specified as High
.
In the Beginning Value field, type 10
,
then click Apply.
Note that the Override check
box next to the Velocity variable is now selected,
which means that you have overridden the default variable initialization
value. This variable has high priority, therefore, the solver will
try to exactly satisfy this beginning value when it computes the initial
conditions to determine the simulation starting point.
Step 4: Add Sources
You can drive Simscape models using input signals. This
technique allows you to represent physical effects, such as forces,
voltages, or pressures, that act on your system. You can also specify
other quantities that flow through your system, such as current, mass
flow rate, and heat flux. You add signal input connections to your
physical network by using Simscape source blocks.
Detailed Instructions
To add the representation of the force acting on the mass, use
the Ideal Force Source block.
Open the Simscape > Foundation Library > Mechanical
> Mechanical Sources library.
Add the Ideal Force Source block to your diagram. Shorten the block name, as
shown.
To reflect the correct direction of the force shown in the original
schematic, flip the block orientation. With the Force
Source block selected, on the Format
tab of the Simulink® Toolstrip, under Arrange, click
Flip up-down.
Copy the Mechanical Translational Reference block
by right-clicking it and dragging to a new location. Then flip the
orientation of the new block using the same technique as with the Force
Source block.
Connect port C of the Force Source block
to this second Mechanical Translational Reference block
and port R to the Mass block, as shown below.
The input signal for the force profile will be supplied through
port S, after you connect the physical network to a Simulink source
(see Step 6: Connect to Simulink with Interface Blocks).
A positive signal at port S will specify a force that acts from port
C to port R.
Step 5: Add Sensors
You can measure quantities from your physical network and use
them in other locations in your model. Some common uses of those
quantities include feedback for a control algorithm, modeling physical
components whose behavior depends on other physical quantities (such
as temperature-dependent resistor), or simply viewing the results
during simulation.
You measure quantities using sensor blocks, connected in series
or in parallel depending on the measured value. To measure a quantity
defined by a Through variable (such as current, flow rate, force),
connect the sensor in series. To measure a quantity defined by an
Across variable (such as voltage, pressure, velocity), connect the
sensor in parallel. For more information on Through and Across variables,
see Variable Types.
Detailed Instructions
To measure spring deformation, connect an Ideal Translational Motion
Sensor block in parallel with the spring.
Open the Simscape > Foundation Library > Mechanical
> Mechanical Sensors library.
Add the Ideal Translational Motion Sensor block
to your diagram.
To rotate the block, select it and press Ctrl+R.
Connect the block as shown in the next illustration. Shorten the block name, as shown.
Step 6: Connect to Simulink with Interface Blocks
Equations in a Simscape network are solved simultaneously,
while Simulink blocks are evaluated sequentially. Interface blocks,
such as Simulink-PS Converter and PS-Simulink
Converter, handle the boundary between these two modeling
conventions. You need interface blocks when Simulink signals
specify quantities in a Simscape network, or when passing Simscape quantities
to Simulink for control design or other purposes. Every time
you connect a Simulink block to a Simscape physical network,
you have to use an appropriate converter block.
Detailed Instructions
You are now going to connect your physical network to a controller
built out of regular Simulink blocks. First, prepare the physical
network to be connected to Simulink signals:
Delete the Scope block.
While you can use scopes to view simulation results, they add
clutter to the block diagram. A more efficient way to view and analyze
simulation results is to use the Simscape Results Explorer, as
described in Step 8: View Simulation Results.
Connect the physical signal output port of the Simulink-PS
Converter block to port S of the Force Source block.
Connect the output port P of the Motion Sensor block
to the physical signal input port of the PS-Simulink Converter block.
Connect the Solver Configuration block
to the circuit and hide the converter block names. Your diagram now
looks like this.
Then, build and connect the controller:
Open the Simulink > Sources library and drag the Pulse
Generator block into the model. Change the block name to Position
Command. Set the block parameters as shown.
Open the Simulink > Math Operations library and
drag the Sum block into the model. In the List
of signs, replace the second plus sign with a minus sign,
as shown.
Open the Simulink > Continuous library and drag the PID Controller block
into the model. Set the Proportional (P),
Integral (I), and Derivative
(D) parameter values as shown.
Connect the blocks as shown in the following diagram.
The control Simulink signal goes to the
input port of the Simulink-PS Converter block, where
it is converted into a physical signal driving the force profile of
the Ideal Force Source block.
The output port P of the Ideal Translational Motion Sensor block,
which measures the spring deformation, connects to the PS-Simulink
Converter block. This block converts the physical signal into
a feedback Simulink signal for the controller.
To compare the input and feedback signals, connect them to a
signal viewer:
Right-click the Simulink signal that goes from
the Pulse Generator block to the Sum block.
From the context menu, select > > .
Then, right-click the Simulink signal that goes
from the PS-Simulink Converter block to the Sum block.
From the context menu, select > .
Step 7: Simulate Model
You run simulations by clicking the Run button
in the Simulink Toolstrip (at the top of the model window) or in a Scope Viewer
toolbar. The Simscape solver evaluates the model, calculates the initial conditions, and
runs the simulation. For a detailed description of this process, see How Simscape Simulation Works. The message in the bottom-left corner of the model window provides the status
update.
Detailed Instructions
To run the simulation, click
in the Simulink Toolstrip.
The Scope Viewer displays overlaid plots of the input and feedback
signals.
Step 8: View Simulation Results
The Simscape Results Explorer lets you view and analyze
simulation data by using the data logging functionality. For example,
you can compare two simulation runs to analyze how changing the mass
affects the spring deformation.
Detailed Instructions
Right-click the Spring block.
From the context menu, select > > .
Note
simlog
is the default name of the log variable.
If you change the variable name using the Workspace variable
name model configuration parameter, the context menu lists
that name as the log variable associated with the block.
The Simscape Results Explorer window opens, with the node
corresponding to the Spring block highlighted in the
left pane. The right pane displays the simulation data plots for the
three variables associated with the block.
Expand the Spring
node and select
node x
. The right pane displays the spring deformation
data plotted over time.
Double-click the Mass block. Set its Mass to 7.2
kg
.
Rerun the simulation.
To reload the logged data, click the
button located in the
toolbar of Simscape Results Explorer window. In the pop-up window
displaying the variable name, click OK.
Notes
To have the data reload automatically after each simulation
run, in the Configuration Parameters dialog box, in the Simscape pane,
select the Open viewer after simulation check
box.
To explore the simulation results side by side, in
two separate Simscape Results Explorer windows, use the
button after the first
simulation run. For more information, see About the Simscape Results Explorer.