Main Content


Replace body on robot



replaceBody(robot,bodyname,newbody) replaces the body in the robot model with the new body. All properties of the body are updated accordingly, except the Parent and Children properties. The rest of the robot model is unaffected.


collapse all

To use dynamics functions to calculate joint torques and accelerations, specify the dynamics properties for the rigidBodyTree object and rigidBody.

Create a rigid body tree model. Create two rigid bodies to attach to it.

robot = rigidBodyTree('DataFormat','row');
body1 = rigidBody('body1');
body2 = rigidBody('body2');

Specify joints to attach to the bodies. Set the fixed transformation of body2 to body1. This transform is 1m in the x-direction.

joint1 = rigidBodyJoint('joint1','revolute');
joint2 = rigidBodyJoint('joint2');
setFixedTransform(joint2,trvec2tform([1 0 0]))
body1.Joint = joint1;
body2.Joint = joint2;

Specify dynamics properties for the two bodies. Add the bodies to the robot model. For this example, basic values for a rod (body1) with an attached spherical mass (body2) are given.

body1.Mass = 2;
body1.CenterOfMass = [0.5 0 0];
body1.Inertia = [0.001 0.67 0.67 0 0 0];

body2.Mass = 1;
body2.CenterOfMass = [0 0 0];
body2.Inertia = 0.0001*[4 4 4 0 0 0];


Compute the center of mass position of the whole robot. Plot the position on the robot. Move the view to the xy plane.

comPos = centerOfMass(robot);

hold on

Figure contains an axes. The axes contains 6 objects of type patch, line. These objects represent base, body1, body2.

Change the mass of the second body. Notice the change in center of mass.

body2.Mass = 20;

comPos2 = centerOfMass(robot);
hold off

Figure contains an axes. The axes contains 7 objects of type patch, line. These objects represent base, body1, body2.

Input Arguments

collapse all

Robot model, specified as a rigidBodyTree object. The rigid body is added to this object and attached at the rigid body specified by bodyname.

Body name, specified as a string scalar or character vector. This body must be on the robot model specified in robot.

Data Types: char | string

Rigid body, specified as a rigidBody object.

Extended Capabilities

Introduced in R2016b