%% Specify Dynamics Properties to Rigid Body Tree
% 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];

addBody(robot,body1,'base');
addBody(robot,body2,'body1');

%
% 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);

show(robot);
hold on
plot(comPos(1),comPos(2),'or')
view(2)

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

body2.Mass = 20;
replaceBody(robot,'body2',body2)

comPos2 = centerOfMass(robot);
plot(comPos2(1),comPos2(2),'*g')
hold off

%
% Copyright 2012 The MathWorks, Inc.