In this tutorial, you create a simple MATLAB® script to simulate a four-bar model at various coupler lengths. The script uses the coupler motion coordinates, obtained using a Transform Sensor block, to plot the resulting coupler curve at each value of the coupler length. For information on how to create the four-bar model used in this tutorial, see Model a Closed-Loop Kinematic Chain.
At the MATLAB command prompt, enter
smdoc_four_bar. A four-bar model opens up.
For instructions on how to create this model, see Model a Closed-Loop Kinematic Chain.
Under the mask of the Binary Link B block, connect a third Outport block as shown in the figure. You can add an Outport block by copying and pasting Conn1 or Conn2. The new block identifies the frame whose trajectory you plot in this tutorial.
Add the following blocks to the model. During simulation, the Transform Sensor block computes and outputs the coupler trajectory with respect to the world frame.
|Frames and Transforms||World Frame||1|
|Frames and Transforms||Transform Sensor||1|
|Simscape™ Utilities||PS-Simulink Converter||2|
In the Transform Sensor block dialog box, select these variables:
Translation > Y
Translation > Z
The block exposes frame ports y and z, through which it outputs the coupler trajectory coordinates.
Connect the blocks as shown in the figure. Be sure to flip the Transform Sensor block so that its base frame port, labeled B, connects to the World Frame block.
In the Mechanism Configuration block, change Uniform
In the Base-Crank Revolute Joint block, specify the following velocity state targets. The targets provide an adequate source of motion for the purposes of this tutorial.
Select State Targets > Specify Velocity.
In State Targets > Specify Velocity > Value, enter
Deselect State Target > Specify Position.
Specify the following link lengths. The coupler link length is
parameterized in terms of a MATLAB variable,
LCoupler, enabling you
change its value iteratively using a simple MATLAB script.
|Binary Link B||Length||LCoupler|
|Binary Link A1||Length||25|
Save the model in a convenient folder, naming it smdoc_four_bar_msensing.
Create a MATLAB script to iteratively run simulation at various coupler link lengths:
On the MATLAB toolstrip, click New Script.
In the script, enter the following code:
% Run simulation nine times, each time % increasing coupler length by 1 cm. % The original coupler length is 20 cm. for i = (0:8); LCoupler = 20+i; % Simulate model at the current coupler link length (LCoupler), % saving the Outport block data into variables y and z. [~, ~, y, z] = sim('smdoc_four_bar_msensing'); % Plot the [y, z] coordinates of each coupler curve % on the x = i plane. i corresponds to the simulation run number. x = zeros(size(y)) + i; plot3(x, y, z, 'Color', [1 0.8-0.1*i 0.8-0.1*i]); view(30, 60); hold on; end
Save the script as sim_four_bar in the folder containing the four-bar model.
Run the sim_four_bar script. In the MATLAB Editor toolstrip, click the Run button or, with the editor active, press F5. Mechanics Explorer opens with a dynamic 3-D view of the four-bar model.
Simscape Multibody™ iteratively runs each simulation, adding the resulting coupler link curve to the active plot. The figure shows the final plot.
You can use the simple approach shown in this tutorial to analyze model dynamics at various parameter values. For example, you can create a MATLAB script to simulate a crank-slider model at different coupler link lengths, plotting for each simulation run the constraint force acting on the piston.