# Sensors¶

Sensors are used to compute the kinematics of a point of the system. They can compute the following information for a given point:

• The absolute position vector

• The absolute rotation matrix of the body on which the point is attached

• The absolute velocity vector

• The absolute angular velocity vector of the body

• The absolute acceleration

• The absolute angular acceleration vector of the body

• The Jacobian matrix of the sensor, i.e. the relation between generalized velocities and the sensor velocity and angular velocity

There are two kinds of sensors:

• S sensor: sensor attached to a given point;

• F sensor: sensor used to compute external force (see the external force);

Note that there also exist Gen sensor: it is equivalent to S sensors attached on each joint of the system.

## Back to the pendulum-spring example¶

The goal is to capture the vertical acceleration of the point located at the middle of the blue rod.

Position of the sensor

### Step 1: Draw your multibody system¶

Open the existing *.mbs file project with MBsysPad:

• Set back the pendulum joint to independent and the crank joint to dependent;

• Add an anchor point on the rod and enter its coordinates;

• Add a sensor to the anchor point:

• Click on the Sensor button;

• Click on the anchor point at the end of the rod;

• A “S” is added next to the anchor point;

• Click on the “S” to edit its properties and give it a name;

### Step 2: Generate your multibody equations¶

Regenerate the multibody equations with the same options as previously.

### Step 3: Write your user function¶

Edit the user_DrivenJoints function to remove the imposed trajectory of the crank. You can delete or comment the part which imposes the trajectory of the crank.

WARNING:

Imposing the trajectory of an independent or dependent joint will lead to inconsistent results.

Edit the user_dirdyn.py and do your computation in the function `user_dirdyn_loop()`:

```#...
from MBsysPy import MbsSensor

#...

def user_dirdyn_loop(mbs_data, mbs_dirdyn):
#...
# Creating the sensor
my_sensor = MbsSensor(mbs_data)
# Computing the sensor with number 1
id_sensor = 1
my_sensor.comp_s_sensor(id_sensor)
# Add the sensor vertical acceleration as output.
mbs_data.set_output(my_sensor.A[3], "sensor")

#...
```

The saved value will be accessible in the results (`MbsDirdyn.results`) instance in the dictionary `outputs`.

Computing multiple sensor

If you need to use the results of multiple sensors you just have to repeat the previous code. For example in `user_dirdyn_loop()` function:

```#...
from MBsysPy import MbsSensor

#...

def user_dirdyn_loop(mbs_data, mbs_dirdyn):
#...
# Creating the sensors
my_sensor_1 = MbsSensor(mbs_data)
my_sensor_2 = MbsSensor(mbs_data)

# Defining the sensor ids as 1 and 4 (assuming your project has 4 sensors).
id_sensor_1 = 1
id_sensor_2 = 4

# Computing the sensors
my_sensor_1.comp_s_sensor(id_sensor_1)
my_sensor_2.comp_s_sensor(id_sensor_2)

mbs_data.set_output(my_sensor_1.A[3], "sensor_1_vert_acc")  # The vertical acceleration of the first defined sensor
mbs_data.set_output(my_sensor_2.A[3], "sensor_4_vert_acc")  # The vertical acceleration of the second defined sensor
mbs_data.set_output(my_sensor_2.P[3] - my_sensor_1.P[3], "sensors_vert_distance")  # The vertical distance between the sensors

#...
```

If you want, you can compute your sensor via the list `MbsData.sensors`. This list contains (by default) one allocated sensor (`MbsData.sensors[0]`) that is kept during the whole simulation. Doing so avoids creation and destruction of the MbsSensor at each time step (faster).
If you need to compute more than one sensor at the same time you can add them in the list `MbsData.sensors`. Calling `mbs_data.sensors.append(MBsysPy.MbsSensor(mbs_data))` in the function `user_dirdyn_init(mbs_data, mbs_dirdyn)` will allocate a second sensor in the list. Pay attention that the sensors will be available for all the simulation, except if you delete them in the function `user_dirdyn_finish(mbs_data, mbs_dirdyn)` with `mbs_data.sensors.pop(index)`.
When creating a sensor instance you can specify the sensor id with `my_sensor = MBsysPy.MbsSensor(mbs_data, id_=sensor_id)`. Then calling my_sensor.comp_s_sensor() without specifying any number will compute the sensor with the id specified at creation.