# 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.

### 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. You also need to regenerate the user models files.

### 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 dependant joint will lead to inconsistent results.

c section:

In C there are 2 options in order to store the result of a sensor:

1. During the simulation in the user_dirdyn_loop function (defined in the user_dirdyn.c file);
2. After the simulation, during the post-process.
• This method is not explained in the tutorial.

The acceleration is saved in the resultsR/ folder.

Option 1: on-line sensor calculation

• Open the user_dirdyn.c file:
• In the user_dirdyn_init function, do nothing:
• The MbsAux structure initializes one MbsSensor structure, we will use this one.
• If you need informations from multiple sensors you can either:
1. Compute one sensor and store the useful information; repeat the operation for the other sensors.
2. Or declare enough sensor strucure int the UserIO structure (user_IO.h file), initialized in the mbs_new_user_IO() function (user_IO.c file).
• In the user_dirdyn_loop, calculate the sensor kinematics using the sensor function and save the desired value.
• You have to include set_output.h, user_all_id.h and mbs_project_interface.h
//...
#include "set_output.h"
#include "user_all_id.h"
#include "mbs_project_interface.h"
//...

void user_dirdyn_loop(MbsData *mbs_data, MbsDirdyn *mbs_dd)
{
//...

int id = RodSensor_id;

// retrieve the pointer to the sensor structure defined in mbs_aux
MbsSensor *PtrSensor = mbs_dd->mbs_aux->psens;

// compute the sensor (position, velocity...)
mbs_sensor(PtrSensor, mbs_data, id);

// save the vertical acceleration
set_output(PtrSensor->A[3], "Vertical_Acc");

//...
}


REMARK:

The calculation of sensor kinematics can be done in any user function if it is useful for defining the constitutive law, see hereafter. Note that this way of doing is not very efficient.

/* You maybe have to include the "mbs_sensor.h"
or (not and) the "mbs_project_interface.h" header file.*/
MbsSensor psens[1];                        // Creation of a pointer to a sensor struct.
allocate_sensor(psens, mbs_data->njoint);  // Allocate the Jacobian at the correct dimension
init_sensor(psens, mbs_data->njoint);      // Initialize all value to zero
mbs_sensor(psens, mbs_data, RodSensor_id); // Compute the sensor
printf("%f", psens->P[1])                  // Print the X position (for example)
free_sensor(psens);                        // Free the memory (always better)


### Check the results¶

Plot the graph of the sensor acceleration (results ares avilaible in resultsR/ folder) and check your results with the following graph. The evolution of the system is the same as for the Cuts part of the tutorial