Model a Simple Pendulum
The pendulum is the simplest mechanical system you can model. This system contains two 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 a Simple Link. A Revolute Joint block provides the rotational degree of freedom between the link and the world frame.
At the MATLAB® command prompt, enter
smnew. The Simscape™ Multibody™ block library and a model template with commonly used blocks open up.
Delete blocks Simulink-PS Converter, PS-Simulink Converter, Scope, and Rigid Transform. You do not need them in this tutorial.
Add to the model the Simple Link custom block created in Model a Simple Link.
Add a Revolute Joint block to the model. You can find this block in the Simscape > Multibody > 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 Geometry > Dimensions
[4 4 4]
Graphic > Visual Properties > Color
[0.80 0.45 0]
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].
Set Pendulum Starting Position
You can specify the desired joint angle using the State Targets menu in the Revolute Joint block dialog box. To do this, select State Targets > Position and enter the desired joint angle. For this tutorial, you can leave the angle in its default value, which corresponds to a horizontal pendulum starting position.
Open the Configuration Parameters. In the Modeling tab, click Model Settings.
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.01and 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.01provides a good balance between simulation speed and accuracy.
Update the block diagram. In the Modeling tab, click Update Model. Mechanics Explorer opens with a 3-D view of the model in its initial configuration.
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
Run the simulation. Mechanics Explorer plays a physics-based animation of the pendulum model.
Save the model in a convenient folder under the name simple_pendulum. You reuse this model in the tutorial Analyze a Simple Pendulum.