# Creating a Multibody with different joints in MATLAB

This example shows how to build a multibody containing various joints in MATLAB. It also shows a way of setting operating point targets for all the joint primitives for all the joint types. The following sections of code demostrates the approach.

Create a multibody object and add the necessary components like WorldFrame and Gravity.

```import simscape.Value simscape.multibody.*; mb = Multibody; addComponent(mb, 'World', WorldFrame()); sep = Value(20, 'cm'); % Separation between blocks % Translucent ground plane ground = Solid(Brick([[5 4] * sep, Value(1, 'cm')]), UniformDensity, ... SimpleVisualProperties(0.7 *[1 1 1], 0.6)); addComponent(mb, 'Ground', ground); addComponent(mb, 'Ground_Pose', RigidTransform(CartesianTranslation([2 1.5 0] * sep))); connectVia(mb, 'Ground_Pose', 'World/W', 'Ground/R'); % Zero gravity mb.Gravity = Value([0 0 0], 'm/s^2');```

Add all types of joints to the multibody object using the custom function addJoint.

```% Row 1: Single-primitive joints addJoint(mb, 'Revolute', [0 0] * sep, [1 0 0]); addJoint(mb, 'Prismatic', [1 0] * sep, [1 1 0]); addJoint(mb, 'Spherical', [2 0] * sep, [0 .8 0]); addJoint(mb, 'LeadScrew', [3 0] * sep, [0 0 .9]); addJoint(mb, 'ConstantVelocity', [4 0] * sep, [.8 0 .8]); % Row 2: 2-DOF multi-primitive joints addJoint(mb, 'Universal', [0.5 1] * sep, [.95 .4 0]); addJoint(mb, 'Rectangular', [1.5 1] * sep, [.4 .9 0]); addJoint(mb, 'Cylindrical', [2.5 1] * sep, [0 .7 .7]); addJoint(mb, 'PinSlot', [3.5 1] * sep, [.6 0 1]); % Row 3: 3-DOF and 4-DOF multi-primitive joints addJoint(mb, 'Gimbal', [0 2] * sep, [.37 .86 .57]); addJoint(mb, 'Cartesian', [1 2] * sep, [.28 .55 .96]); addJoint(mb, 'Planar', [2 2] * sep, [.91 .63 .10]); addJoint(mb, 'Bearing', [3 2] * sep, [.96 .49 .80]); addJoint(mb, 'Telescoping', [4 2] * sep, [.8 .8 .5]); % Row 4: 0-DOF and 6-DOF joints addJoint(mb, 'Weld', [1 3] * sep, .1 * [1 1 1]); addJoint(mb, 'Bushing', [2 3] * sep, .8 * [1 1 1]); addJoint(mb, 'SixDof', [3 3] * sep, 1 * [1 1 1]); ```

Now in order to create operating points for specifying position or velocity targets to any joint, we will need the path to each of the joint primitives . The Multibody object provides a method jointPrimtivePaths to list the paths to all the joint primitives in the multibody. We use this list as one of the inputs to the custom function radnomOpPoint which creates an operating point specifying random velocities for all the joints.

```jointPrimPaths = jointPrimitivePaths(mb); op = randomOpPoint(jointPrimPaths, Value(90, 'deg/s'), Value(1, 'cm/s'));```

Once the operating point is created we can compile and use the computeState method to view if the above random velocity targets are achieved.

```cmb = compile(mb); state = computeState(cmb,op)```
```state = State: Status: Valid Assembly diagnostics: x Revolute_Joint Joint successfully assembled Rz Free position value: +0 (deg) High priority velocity target +23.5864 (deg/s) achieved Prismatic_Joint Joint successfully assembled Pz Free position value: +0 (m) High priority velocity target +0.210897 (cm/s) achieved Spherical_Joint Joint successfully assembled S Free position value: [+0 +0 +0], +0 (deg) High priority velocity target [-80.4494 -62.6214 -85.1939] (deg/s) achieved LeadScrew_Joint Joint successfully assembled LSz Free position value: +0 (deg) High priority velocity target +0.0848914 (cm/s) achieved ConstantVelocity_Joint Joint successfully assembled CV High priority position target +90 (deg) achieved High priority velocity target [-5 +10] (deg/s) achieved Universal_Joint Joint successfully assembled Rx Free position value: +0 (deg) High priority velocity target +73.9708 (deg/s) achieved Ry Free position value: +0 (deg) High priority velocity target +10.0194 (deg/s) achieved Rectangular_Joint Joint successfully assembled Px Free position value: +0 (m) High priority velocity target +0.162366 (cm/s) achieved Py Free position value: +0 (m) High priority velocity target +0.00724535 (cm/s) achieved Cylindrical_Joint Joint successfully assembled Rz Free position value: +0 (deg) High priority velocity target -12.8893 (deg/s) achieved Pz Free position value: +0 (m) High priority velocity target +0.984314 (cm/s) achieved PinSlot_Joint Joint successfully assembled Px Free position value: +0 (m) High priority velocity target +0.454274 (cm/s) achieved Rz Free position value: +0 (deg) High priority velocity target +21.4513 (deg/s) achieved Gimbal_Joint Joint successfully assembled Rx Free position value: +0 (deg) High priority velocity target +89.4072 (deg/s) achieved Ry Free position value: +0 (deg) High priority velocity target +28.0298 (deg/s) achieved Rz Free position value: +0 (deg) High priority velocity target +25.9604 (deg/s) achieved Cartesian_Joint Joint successfully assembled Px Free position value: +0 (m) High priority velocity target -0.25679 (cm/s) achieved Py Free position value: +0 (m) High priority velocity target -0.640975 (cm/s) achieved Pz Free position value: +0 (m) High priority velocity target +0.594411 (cm/s) achieved Planar_Joint Joint successfully assembled Px Free position value: +0 (m) High priority velocity target +0.433686 (cm/s) achieved Py Free position value: +0 (m) High priority velocity target +0.215342 (cm/s) achieved Rz Free position value: +0 (deg) High priority velocity target -60.8224 (deg/s) achieved Bearing_Joint Joint successfully assembled Pz Free position value: +0 (m) High priority velocity target +0.908122 (cm/s) achieved Rx Free position value: +0 (deg) High priority velocity target +2.55215 (deg/s) achieved Ry Free position value: +0 (deg) High priority velocity target -63.2583 (deg/s) achieved Rz Free position value: +0 (deg) High priority velocity target -63.7585 (deg/s) achieved Telescoping_Joint Joint successfully assembled S Free position value: [+0 +0 +0], +0 (deg) High priority velocity target [-89.3162 -43.8057 -52.8736] (deg/s) achieved Pz Free position value: +0 (m) High priority velocity target -0.844793 (cm/s) achieved Weld_Joint Joint successfully assembled Bushing_Joint Joint successfully assembled Px Free position value: +0 (m) High priority velocity target +0.66129 (cm/s) achieved Py Free position value: +0 (m) High priority velocity target +0.148752 (cm/s) achieved Pz Free position value: +0 (m) High priority velocity target -0.792454 (cm/s) achieved Rx Free position value: +0 (deg) High priority velocity target -44.7294 (deg/s) achieved Ry Free position value: +0 (deg) High priority velocity target -32.5625 (deg/s) achieved Rz Free position value: +0 (deg) High priority velocity target -71.8616 (deg/s) achieved SixDof_Joint Joint successfully assembled Px Free position value: +0 (m) High priority velocity target +0.664527 (cm/s) achieved Py Free position value: +0 (m) High priority velocity target -0.294531 (cm/s) achieved Pz Free position value: +0 (m) High priority velocity target -0.806505 (cm/s) achieved S Free position value: [+0 +0 +0], +0 (deg) High priority velocity target [-24.8307 -63.8315 -74.1798] (deg/s) achieved ```

Finally, to perform any simulation workflows we can generate the simulink model using the makeBlockDiagram method.

`makeBlockDiagram(mb,op,'jointZooModel');`
```Warning: Unrecognized function or variable 'registerTICCS'. ```
```Warning: Unrecognized function or variable 'customizationticcs'. ```