SimMechanics 2nd Generation Introduction Example
SimMechanics 2nd Generation Introduction Example
R2015a
www.mathworks.com
www.mathworks.com/sales_and_services
User community:
www.mathworks.com/matlabcentral
Technical support:
www.mathworks.com/support/contact_us
Phone:
508-647-7000
Trademarks
MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand
names may be trademarks or registered trademarks of their respective holders.
Patents
MathWorks products are protected by one or more U.S. patents. Please see
www.mathworks.com/patents for more information.
Revision History
March 2012
September 2012
March 2013
September 2013
March 2014
October 2014
March 2015
Online only
Online only
Online only
Online only
Online only
Online only
Online only
Contents
1-2
1-2
1-3
1-3
1-3
1-3
1-5
1-6
1-6
1-9
1-11
1-14
1-14
1-14
1-16
1-17
1-18
1-19
1-19
1-20
1-21
1-21
1-21
1-21
1-22
1-22
iii
iv
Contents
1-23
1-23
1-24
1-25
1-28
1-31
1-35
1
Introduction to SimMechanics
Software
SimMechanics Product Description on page 1-2
Required and Related Products on page 1-3
Start New Multibody Model on page 1-5
Multibody Model Anatomy on page 1-6
Model Simple Link on page 1-14
Model Simple Pendulum on page 1-19
Analyze Simple Pendulum on page 1-23
SimMechanics First and Second Generation Comparison on page 1-35
Key Features
Blocks and modeling constructs for simulating and analyzing 3D mechanical systems
in Simulink
Rigid body definition using standard geometry and custom extrusions defined in
MATLAB
Automatic calculation of mass and inertia tensor
Simulation modes for analyzing motion and calculating forces
Visualization and animation of multibody system dynamics with 3D geometry
SimMechanics Link utility, providing an interface to Pro/ENGINEER, SolidWorks,
and Autodesk Inventor, and an API for interfacing with other CAD platforms
Support for C-code generation (with Simulink Coder)
1-2
Related Products
You can extend the capability of SimMechanics using other physical modeling products
found in the Simscape family. Each physical modeling product gives you a set of block
libraries with which you can model common components found in industry and academia:
rigid bodies, gears, valves, solenoids, etc.
With the physical modeling products, you can model not only mechanical systems, but
also electrical, hydraulic, and power systems. You can model each system separately,
and then integrate the systems into a single multiphysics model where you can analyze
combined system performance.
Physical Modeling Product Family
The physical modeling family includes five products:
1-3
1-4
1-5
1-6
While important, rigid body subsystem and joint blocks are not sufficient to represent the
four-bar linkage. Other blocks serve important purposes. These include World Frame,
Rigid Transform, Mechanism Configuration, and Solver Configuration blocks. The table
summarizes their functions in a multibody model.
Block
Function
World Frame
Rigid Transform
Mechanism Configuration
1-7
Block
Function
Solver Configuration
The figure breaks the four-bar model into its logical components. These are the physical
components and abstract entities that you need in order to represent this system.
Each rigid body subsystem contains SimMechanics blocks that represent solids and
their spatial relationships. The blocks are Solid and Rigid Transform. The figure shows
the blocks that model one of the binary links. Three Solid blocks represent the three
solid sections of this rigid bodymain, peg, and hole sections. Two Rigid Transform
blocks represent the fixed spatial relationships between the three solids. You use them to
position the peg and hole sections at the ends of the main section.
1-8
Model Actuation
You can actuate a model by applying a force or torque to a rigid body or to a joint. To
represent forces and torques acting on a rigid body, SimMechanics provides a Forces and
Torques library. Drag a block from this library and connect it to the rigid body frame(s)
that you want to apply the force or torque to.
Block
Function
Internal Force
Gravitational Field
The figure shows a four-bar model with an External Force and Torque block for force and
torque prescription at a crank link frame.
1-9
1-10
Dynamical Sensing
You can sense various dynamical variables between frame pairs, e.g., for analysis or
control design. Sensing outputs can be of two types:
Motion Compute and output the relative position, velocity, or acceleration between
two SimMechanics frames. You can sense motion between joint frames, by using
the sensing capability of joint blocks, or between arbitrary frames, by using the
Transform Sensor block.
Force or torque Compute and output the forces and torques acting between two
SimMechanics frames. You can sense force and torque between the port frames of
certain Forces and Torques blocks, such as the Inverse Square Law Force block, or
between the port frames of a joint block.
1-11
Joint blocks enable you to sense different types of forces and torques between their
respective port frames, including:
Actuation force or torque acting at a given joint primitive.
Constraint force and torque acting joint-wide to prevent motion normal to the joint
degrees of freedom.
Total force and torque, including constraint and joint primitive actuation
contributions, acting joint-wide.
The figure shows a four-bar model with a Transform Sensor block for trajectory
coordinate sensing between a coupler link frame and the world frame.
1-12
Related Examples
1-13
Model Overview
Mechanical links are common building blocks in linkages, mechanisms, and machines.
The simple pendulum is an example with one link. In this tutorial, you model a simple
link with two end frames that you can later connect to joints. Rigid Transform blocks
provide the end frames, while a Solid block provides geometry, inertia, and color. For
simplicity, the model assumes the link has a brick shape.
Build Model
1-14
At the MATLAB command line, enter smnew. The SimMechanics block library and a
model template with commonly used blocks open up.
Make a copy of the Rigid Transform block and paste it in the model. The Rigid
Transform blocks enable you to create new frames to which you can connect joints
during multibody assembly.
Delete the blocks Simulink-PS Converter, PS-Simulink Converter, and Scope. You do
not need these blocks in this tutorial.
Connect the remaining blocks as shown in the figure. Ensure that the base frame
ports (B) of the Rigid Transform blocks both face the Solid block frame port. Since
each Rigid Transform block applies a spatial transformation with respect to its base
frame, switching port connections generally changes the spatial relationship between
the two frames.
In the Solid block dialog box, specify the following parameters. Later, you define
the MATLAB variables shown using a Subsystem block that contains the Solid and
Rigid Transform blocks. Among its advantages, this approach enables you to update
variables used in multiple blocks from a single placethe Subsystem block dialog
box.
Parameter
Value
Units
Change to cm
rho
Default units
rgb
Not applicable
In the dialog boxes of the Rigid Transform and Rigid Transform1 blocks, specify the
following parameters. These parameters encode the offset between the base and
1-15
follower port frames of the Rigid Transform blocks, located at the link ends, with
respect to the Solid reference port frame.
Parameter
Rigid Transform1
Rigid Transform
Units
Translation >
Method
Standard Axis
Standard Axis
Not applicable
Translation >
Axis
-X
+X
Not applicable
Translation >
Offset
L/2
L/2
Change to cm
Generate Subsystem
1-16
Select the Solid block and the two Rigid Transform blocks.
Right-click the highlighted region and select Create Subsystem from Selection.
Simulink adds a new Subsystem block containing the Solid and Rigid Transform
blocks. At the end of the tutorial, this will be a custom block representing the simple
link rigid body.
Right-click the Subsystem block, and select Mask > Create Mask. A mask editor
opens up, enabling you to specify the numerical values of the MATLAB variables you
entered in the Solid and Rigid Transform block dialog boxes.
In the Parameters & Dialog tab of the Mask Editor window, add five edit fields
to the Parameters folder. You can find this folder in the Dialog box pane. In
the edit fields, specify the following parameters and click OK. Prompt is the desired
text for each parameter in the Subsystem block dialog box. Name is the MATLAB
variable associated with each Subsystem block parameter.
Prompt
Name
Length (cm)
Width (cm)
Thickness (cm)
Density (kg/m^3)
rho
Color [R G B]
rgb
Double-click the Subsystem block dialog box and enter the following numerical
values. These are the values of the MATLAB variables that you entered in the Solid
and Rigid Transform block dialog boxes.
Parameter
Value
Length (cm)
20
Width (cm)
Thickness (cm)
Density (cm)
2700
Color [R G B]
Visualize Model
Update the block diagram. You can do this by selecting, in the Simulink menu bar,
Simulation > Update Diagram. Mechanics Explorer opens with a front view of the
simple link model. In the Mechanics Explorer toolstrip, select the isometric view button
to obtain the 3-D view shown below. To view the frames present in the model
including those you created using the Rigid Transform blocksselect View > Show
Frames in the Mechanics Explorer menu bar.
1-17
1-18
Model Overview
The pendulum is the simplest mechanical system you can model. This system contains
two rigid bodies, a link and a fixed pivot, connected by a revolute joint. In this tutorial,
you model and simulate a pendulum using the custom link block you created in Model
Simple Link. A Revolute Joint block provides the rotational degree of freedom between
the link and the world frame.
1-19
Build Model
1-20
At the MATLAB command prompt, enter smnew. The SimMechanics block library
and a model template with commonly used blocks open up.
Drag the Simple Link custom block you created in the tutorial Model Simple Link
on page 1-14 into the model.
Drag a Revolute Joint block into the model. You can find this block in the
SimMechanics Second Generation > Joints library. This block provides one
rotational degree of freedom between its port frames.
Connect the blocks as shown in the figure. The port orientation of the Revolute
Joint block becomes important when you specify joint state targets, prescribe
joint actuation inputs, or sense joint dynamic variables. The Revolute Joint block
interprets each quantity as that applied to the follower frame with respect to
the base frame, so switching the port connections can affect model assembly and
simulation.
In the Solid block dialog box, specify the following parameters. This block connects
rigidly to the World frame and therefore has no effect on model dynamics. You can
leave the inertia parameters in their default values.
Parameter
Value
Units
Change to cm
Not applicable
[0.80 0.45 0]
Specify Gravity
The Revolute Joint block uses the common Z axis of the base and follower frames as the
joint rotation axis. To ensure the pendulum oscillates under the effect of gravity, change
the gravity vector so it no longer aligns with the Z axis. To do this, in the Mechanism
Configuration block dialog box, set the Uniform Gravity > Gravity parameter to [0
-9.81 0].
Configure Solver
1
In the Simulink Editor menu bar, select Simulation > Model Configuration
Parameters.
In the Solver tab, set the Solver parameter to ode15s (stiff/NDF). This solver
is the recommended choice for physical models.
Set Max step size to 0.01 and click OK. The small step size increases the
simulation accuracy and produces a smoother animation in Mechanics Explorer.
Small step sizes can have a detrimental effect on simulation speed but, in such a
simple model, a value of 0.01 provides a good balance between simulation speed and
accuracy.
Assemble Model
Update the block diagram. You can do this in the Simulink Editor menu bar, by selecting
Simulation > Update diagram. Mechanics Explorer opens with a 3-D view of the
model in its initial configuration.
1-21
In the Mechanics Explorer toolstrip, check that the View convention parameter is set
to Y up (XY Front). This view convention ensures that gravity is vertically aligned
on your screen. Select a standard view button to refresh the Mechanics Explorer display.
The figure shows a front view of the model. Save the visualization settings by clicking the
Save explorer configuration to model button
Simulate Model
Run the simulation. You can do this through the Simulink Editor menu bar, by selecting
Simulation > Run. Mechanics Explorer plays a physics-based animation of the
pendulum model.
Save Model
Save the model in a convenient folder under the name simple_pendulum. You reuse this
model in the tutorial Analyze Simple Pendulum on page 1-23.
1-22
Overview
In this tutorial, you explore the various forces and torques that you can add to a model.
Then, using blocks with motion sensing capability, you analyze the resulting dynamic
response of the model. The end result is a set of time-domain and phase plots, one for
each combination of forces and torques. You create these plots using MATLAB commands
with SimMechanics motion outputs as arguments.
Your starting point is the simple pendulum model that you built in Model Simple
Pendulum on page 1-19. By adding forces and torques to this model, you incrementally
1-23
change the pendulum from undamped and free to damped and driven. The forces and
torques that you apply include:
Gravitational force (Fg) Global force, acting on every rigid body in direct proportion
to its mass, that you specify in terms of the acceleration vector g. You specify this
vector using the Mechanism Configuration block.
Joint damping (Fb) Internal torque, between the pendulum and the joint fixture,
that you parameterize in terms of a linear damping coefficient. You specify this
parameter using the Revolute Joint block that connects the pendulum to the joint
fixture.
Actuation torque (FA) Driving torque, between the pendulum and the joint fixture,
that you prescribe directly as a Simscape physical signal. You prescribe this signal
using the Revolute Joint block that connects the pendulum to the joint fixture.
Open the simple_pendulum model that you created in tutorial Model Simple
Pendulum on page 1-19.
In the Sensing menu of the Revolute Joint block dialog box, select the following
variables:
Position
Velocity
The block exposes two additional physical signal ports, labeled q and w, that output
the angular position and velocity of the pendulum with respect to the world frame.
1-24
Drag the following blocks into the model. You use them to output the joint position
and velocity to the MATLAB base workspace.
Library
Block
Quantity
PS-Simulink Converter
To Workspace
Change the Variable name parameters in the To Workspace block dialog boxes
to q and w. These variables make it easy to identify the joint variables that the To
Workspace blocks output during simulationposition, through the Revolute Joint
block port q, and velocity, through the Revolute Joint block port w.
Connect the blocks as shown in the figure. Ensure that the To Workspace block with
variable name q connects, through the PS-Simulink Converter block, to the Revolute
Joint block port q, and that the To Workspace block with variable name w connects to
the Revolute Joint block port w.
Run the simulation. You can do this in the SimMechanics Editor menu bar by
selecting Simulation > Run. Mechanics Explorer opens with a 3-D animation of the
simple pendulum model.
Plot the joint position and velocity with respect to time, e.g., by entering the
following code at the MATLAB command prompt:
figure; % Open a new figure
hold on;
plot(q); % Plot the pendulum angle
plot(w); % Plot the pendulum angular velocity
1-25
Plot the joint angular velocity with respect to the angular position, e.g., by entering
the following code at the MATLAB command prompt.
figure;
Plot(q.data, w.data);
The result, shown in the figure, is the phase plot of the joint corresponding to a
starting position of zero degrees with respect to the horizontal plane.
1-26
Try simulating the model using different starting angles. You can change the
starting angle in the State Targets > Position menu of the Revolute Joint block
dialog box. The figure shows a compound phase plot for starting angles of -80, -40, 0,
40, and 80 degrees.
1-27
In the Revolute Joint block dialog box, set Internal Mechanics > Damping to
8e-5 (N*m)/(deg/s). The damping coefficient causes energy dissipation during
motion, resulting in a gradual decay of the pendulum oscillation amplitude.
Ensure that State Targets > Position > Value is set to 0 deg.
Plot the joint position and velocity with respect to time. To do this, at the MATLAB
command prompt, you can enter this code:
figure;
hold on;
plot(q);
plot(w);
The figure shows the resulting plot. Note that the pendulum oscillations decay with
time due to damping. At larger damping values, the pendulum becomes overdamped,
and the oscillations disappear altogether.
1-28
Plot the joint phase plot. To do this, at the MATLAB command prompt, you can enter
this code:
figure;
plot(q.data, w.data);
1-29
Try simulating the model using different starting angles. You can change the
starting angle in the State Targets > Position menu of the Revolute Joint block
dialog box. The figure shows a compound phase plot for starting angles of -240, -180,
-120, -60, 0, and 60 degrees.
1-30
In the Revolute Joint block dialog box, set Actuation > Torque to Provided by
Input. The block exposes a physical signal input port that you can use to prescribe
the joint actuation torque.
Block
Simulink-PS Converter
Sine Wave
The Sine Wave block provides a periodic torque input as a Simulink signal. The
Simulinik-PS Converter block converts the Simulink signal to a Simscape physical
signal compatible with SimMechanics blocks.
3
1-31
In the Sine Wave block dialog box, set Amplitude to 0.06. This amplitude
corresponds to an actuation torque oscillating between -0.06 N and 0.06 N.
In the Revolute Joint block dialog box, ensure that State Targets > Position >
Value is set to 0 deg.
Plot the joint position and velocity with respect to time. To do this, at the MATLAB
command prompt, you can enter this code:
figure;
hold on;
plot(q);
plot(w);
1-32
Plot the joint phase plot. To do this, at the MATLAB command prompt, you can enter
this code:
figure;
plot(q.data, w.data);
1-33
1-34
SimMechanics First
Generation
SimMechanics Second
Generation
Mass/Inertia Calculation
Manual only
Automatic or manual
Solid Geometry
No
Yes
Animation Replay
No
Yes
Limited
Yes
Limited
Yes
Simscape Logging
No
Yes
Code Generation
Yes
Yes
CAD Import
Yes
Yes1
Motion Actuation
Yes
Yes
Force/Torque Sensing
Yes
Yes
Complex Constraints
Yes
Yes3
Variable Mass/Gravity
Yes
Yes4
Gravitational Fields
No
Yes
1-36