Model a Rigid Body in MATLAB
This example shows how to create a rigid body by using the MATLAB classes of Simscape Multibody. In this example, you construct a link in a four-bar system, and define the geometry, mass distribution, and visual properties of the link. You then add frames and connectors to the link, which you can use to connect other frames and components.
The link has a rectangular cross-section with rounded ends and two frame connectors at the link ends.
To avoid typing the namespace name for the classes, use the import
function.
import simscape.Value simscape.op.* simscape.multibody.*;
First, create a simscape.multibody.RigidBody
object to represent the link. A RigidBody
object is a hierarchical container that can contain multiple rigidly connected frames and component objects. In this example, the RigidBody
object includes one rigid solid with two end frame connectors.
To create a RigidBody
object named link, use the simscape.multibody.RigidBody
class.
link = RigidBody
link = RigidBody: No connectors. Frames: Frame Parent Source Connector? ___________ ______ ______ __________ "reference" -- -- No No components. RigidBody with properties: FrameNames: "reference" ComponentNames: [0×1 string] DoVisualize: 1 FrameConnectors: [0×1 string]
The object includes one reference frame and no connectors.
Construct Link Body
To construct the body of the link, use the simscape.multibody.Solid
class. You can specify the geometry, mass distribution, and visual properties of the body. As shown in the image, the link has a length of 10 cm, a width of 2 cm, and a height of 1 cm, with the length oriented along the x-axis of the local reference frame.
To define the dimensions, use simscape.Value
objects.
length = Value(10,"cm"); width = Value(2,"cm"); height = Value(1,"cm");
The link has a rectangular cross-section with rounded ends. To define the geometry of the body, use the simscape.multibody.GeneralExtrusion
class with a custom cross-section specified by the roundedRect
function.
geometry = GeneralExtrusion(roundedRect(length,width),height); function cross_section = roundedRect(length,width) angles = (-90:10:+90)' * pi/180; semi = width/2 * [cos(angles) sin(angles)] + repmat([length/2 0], size(angles)); cross_section = [semi; -semi]; end
The example sets the width and height of the link to 2 cm and 1 cm, respectively, using hard-coded values.
The link is made of aluminum and has a blue color. To specify the mass distribution and color for the link, use the simscape.multibody.UniformDensity
and simscape.multibody.SimpleVisualProperties
classes.
mass = UniformDensity(Value(2700,"kg/m^3"));
color = SimpleVisualProperties([0 0 1]);
body = Solid(geometry,mass,color);
To add the body to the reference frame of the link
object, use the addComponent
method.
addComponent(link,"Body","reference",body);
Add Frames and Connectors
Next, add frames and connectors to the RigidBody
object. RigidBody
objects use frame connectors to connect to other frames or components.
The link
object has a reference frame located at the centroid of the body, which serves as the root of its tree structure. When adding new frames to the link object, you must position them relative to the reference frame by using the simscape.multibody.RigidTransform
class. Ensure each frame has a unique name.
Use the addFrame
method to add a frame to each end of the link, and name them neg_end
and pos_end
. The axis of the link is along the x-axis of the local reference frame.
offset = length/2; addFrame(link,"neg_end","reference",RigidTransform(StandardAxisTranslation(offset, Axis.NegX))); addFrame(link,"pos_end","reference",RigidTransform(StandardAxisTranslation(offset, Axis.PosX)));
To connect to other frames or components, the frames must function as frame connectors. However, a newly created RigidBody
object initially has no connectors. To expose a frame as a frame connector, use the addConnector
method.
addConnector(link,"neg_end"); addConnector(link,"pos_end");
To view the structure tree of the link
object, use the plotStructure
method.
plotStructure(link);
The plot shows that the link
object has one solid body and three frames.
See Also
simscape.multibody.Solid
| simscape.multibody.RigidBody