Accelerating the pace of engineering and science

# Documentation

## Orienting a Body and Its Coordinate Systems

### About the Body Orientation Examples

This section shows you a set of examples of orienting a test body and its attached coordinate systems (CSs). It makes detailed use of the rotation representations and conversions explained in the preceding section, Representations of Body Orientation, and shows how to use MATLAB® workspace variables in your SimMechanics™ block dialogs.

This sequence of examples assumes you are familiar with the basics of setting up and visualizing bodies and machines.

### Setting Up the Test Body

The later examples require a configured body to work with. Here you set up a simple body with one Body coordinate system (CS), located at the center of gravity (CG).

#### Initializing the Body

First, create, connect, and visualize the initial body:

1. Open the SimMechanics block library and a new Simulink® model window.

2. Drag and drop a Machine Environment, a Ground, and a Body block, as well as any Joint block you want, into your model window.

3. Open the Body dialog. In the Body coordinate systems area, delete the Body CS named CS2.

Leave the other defaults. The CG CS is located at Origin position vector[0 0 0]. Select Show port for the CG CS and deselect it for CS1. Delete the CS1 entry. Click Apply.

4. Open the Ground and select Show Machine Environment port. Click OK. Connect Ground to Machine Environment at the new Machine Environment port.

5. Connect the Body block at its CG CS port, through the Joint block, to Ground at its grounded CS port.

6. From the Simulation menu, open Configuration Parameters to the SimMechanics node. In the Visualization area, select the Display machines after updating diagram check box. Click OK.

7. From the Edit menu, select Update Diagram. When the window opens, open the Model menu and select Body Geometries > Ellipsoids. The window now displays the body as a sphere.

#### Configuring the Body

Now configure the body's mass and geometric properties:

1. In the Body dialog's Mass properties area, enter 11.5 kg for the mass and [18 0 0; 0 56 0; 0 0 56] kg*m2 for the inertia. Click Apply.

2. Update your diagram. The body in the window changes to an ellipsoid, with (x,y,z) axes of length 2.02, 0.885, and 0.885 meters (m), respectively.

Switch from ellipsoid display to displaying individual body settings. This defaults back to convex hull display. But here, the CG CS triad alone displays. (There are no other CSs.) The x-, y-, and z-axes are red, green, and blue, respectively.

Turn the ellipsoid back on.

Test Body Ellipsoid: Initial Orientation, with Detail of CG CS Axes

### Rotating the Body and Its CG CS Relative to World

In this example, you rotate the body, along with its CG CS axes, with respect to the World CS. The CG CS axes continue to have the same orientation with respect to the body shape. The rotation is a positive turn of 75 deg (degrees) about the axis (1,1,1) in World. In this example, you rotate with a quaternion.

#### Computing the Rotation as a Quaternion

The unit vector n in the (1,1,1) direction is (1,1,1)/$\sqrt{3}$. The rotation angle is θ = 75 deg = 1.3090 rad. At the MATLAB command line, define th = pi*75/180 and compute the quaternion components:

```q = [sin(th/2)/sqrt(3) sin(th/2)/sqrt(3) sin(th/2)/sqrt(3) ...
cos(th/2)]

q =
0.3515    0.3515    0.3515    0.7934```

#### Rotating the Body and Its CG CS Axes with the Quaternion

To rotate the body and the CG CS coordinate axes together by this rotation,

1. In the Body dialog, click the Orientation tab in the Body coordinate systems area. Under the Specified using convention pull-down menu, select Quaternion.

2. Under Orientation vector, enter q. Leave the other defaults. The Relative to coordinate system field indicates that the rotation represented by q is oriented with respect to the World axes. Click Apply.

3. Update the diagram. The body and its CG CS rotate together relative to World:

Test Body Ellipsoid: Body and Its CG CS Axes Rotated Together

4. Finally rotate the body and its CG CS back to the original orientation by entering [0 0 0 1] under Orientation vector and clicking Apply. Update your diagram to refresh the visualization.

### Rotating the Body Relative to Its Center of Gravity

You can also rotate the body without rotating its CG CS axes. To accomplish this requires leaving the Body CSs unchanged while rotating the body's inertia tensor relative to the CG CS. Here you use the same rotation as in the preceding example, but represent it as a rotation matrix.

#### Computing the Rotation as a Rotation Matrix

The unit vector n in the (1,1,1) direction is (1,1,1)/$\sqrt{3}$. The rotation angle is θ = 75 deg = 1.3090 rad. Compute the rotation matrix:

```nDotJ = [0 -1/sqrt(3) 1/sqrt(3); 1/sqrt(3) 0 -1/sqrt(3); ...
-1/sqrt(3) 1/sqrt(3) 0]
nDotJ =
0   -0.5774    0.5774
0.5774         0   -0.5774
-0.5774    0.5774         0

R = eye(3) + nDotJ*sin(th) + nDotJ^2*(1-cos(th))
R =
0.5059   -0.3106    0.8047
0.8047    0.5059   -0.3106
-0.3106    0.8047    0.5059```

#### Rotating the Body's Inertia Tensor

The components of the inertia tensor that you enter into the Body dialog are always defined relative to the CG CS axes. If you hold the CG CS axes fixed and rotate the body by a rotation matrix R, the inertia tensor transforms according to Inew = R*Iold*RT. Compute this with MATLAB:

```I = [18 0 0; 0 56 0; 0 0 56]
I =
18     0     0
0    56     0
0     0    56

Irot = R*I*R'
Irot =
46.2753  -15.4698    5.9711
-15.4698   31.3911    9.4987
5.9711    9.4987   52.3336```

#### Rotating the Body with the Rotation Matrix

Symmetrize Irot by entering Irot = (Irot + Irot')/2. Then rotate the body alone relative to World and the CG CS.

1. In the Body dialog's Mass properties area, replace the existing inertia tensor with Irot. Click Apply.

2. Update the diagram. The body rotates again, as in the preceding example. But unlike that example, the CG CS axes do not change.

Test Body Ellipsoid: Body Rotated Relative to Its CG CS Axes

3. Finally rotate the body back to the original orientation by entering I in the Inertia field and clicking Apply. Update the diagram to refresh the visualization.

### Creating and Rotating Body Coordinate Systems

In the preceding examples, you work with only one Body CS, the CG CS. In this example, you set up additional Body CSs and learn how to rotate them. It is common in mechanical applications to require extra Body CSs to locate sensors and actuators on bodies. Their axis orientations do not, in general, align with the orientation of the CG CS axes.

You visualize the body here using convex hulls, instead of ellipsoids, to articulate the Body CSs more clearly. Obtaining a full convex hull, with a surface enclosing a volume, requires at least four non-coplanar Body CSs.

This example also shows you how to obtain Euler angles and rotate with them.

#### Creating and Viewing the New Body CSs

To change the visualization display to convex hulls,

1. Open the Model menu in the visualization window. Select the Body Geometries submenu.

2. Select Convex Hulls.

To create and visualize the new Body CSs,

1. Go to the Body dialog's Body coordinate systems area. Add three new coordinate systems to the CS list. Name them CS1, CS2, and CS3.

2. For these three CSs, change the Origin position vector fields to [1 0 0], [0 1 0], and [0 0 1], respectively. Click Apply.

3. Update the diagram. The visualization window now display the body as a triangular surface. All the Body CS triads are oriented the same way, parallel to the CG CS axes and the World axes.

Test Body Convex Hull with CG CS and Three Body Coordinate Systems

#### Computing the Rotation as a Set of Euler Angles

The preceding examples used two rotation representations, the quaternion and the rotation matrix, based on the axis-angle representation. In this example, you use the same rotation as before, but represented as a set of Euler angles.

The rotation axis sequence convention is Z-X-Z, with Φ, θ, Ψ as the first, second, and third angles, respectively, the same as presented in Converting Rotation Representations preceding. To obtain the angles, you equate the rotation matrix form for that convention:

${R}_{Z}\left(\varphi \right)=\left(\begin{array}{ccc}\mathrm{cos}\varphi & -\mathrm{sin}\varphi & 0\\ \mathrm{sin}\varphi & \mathrm{cos}\varphi & 0\\ 0& 0& 1\end{array}\right)$

to the numerical form of R you computed in Rotating the Body Relative to Its Center of Gravity preceding:

```R =
0.5059   -0.3106    0.8047
0.8047    0.5059   -0.3106
-0.3106    0.8047    0.5059```

and invert the resulting equations. The solution for θ and Ψ is

```theta = acos(R(3,3))
theta =
1.0404

psi = asin(R(3,1)/sin(theta))
psi =
-0.3684```

or about 60 and -21 deg, respectively.

Here is a method that yields a unique solution by using the structure of the Euler convention, R = R1(Φ)*R2(θ)*R3(Ψ). Multiply R on the right by R3(Ψ)T*R2(θ)T, isolating R1(Φ):

```R3 = [cos(psi) -sin(psi) 0; sin(psi) cos(psi) 0; 0 0 1]
R3 =
0.9329    0.3601         0
-0.3601    0.9329         0
0         0    1.0000

R2 = [1 0 0; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)]
R2 =
1.0000         0         0
0    0.5059   -0.8626
0    0.8626    0.5059

R1 = R*R3'*R2'
R1 =
0.3601   -0.9329   -0.0000
0.9329    0.3601   -0.0000
0.0000    0.0000    1.0000

phi = acos(R1(1,1))
phi =
1.2024```

or about 69 deg.

#### Rotating a Body CS Axis Triad

Here you rotate one of the Body CS axis triads, using the Z-X-Z rotation axis sequence convention and, for Euler angles, the Φ, θ, and Ψ you just found.

1. In the Body dialog's Orientation tab, locate the CS2 entry.

Under Orientation vector, enter [phi theta psi]. In the Units pull-down menu, select rad. In the Specified using convention pull-down menu, select Euler Z-X-Z. Click Apply.

2. Update your diagram. The CS2 axis triad, whose origin continues to be located at (0,1,0), now looks like this:

This rotated orientation of the CS2 axis triad is the same as that of the rotated CG CS in Rotating the Body and Its CG CS Relative to World preceding. The two rotations are the same and produce the same result.