# User constraints¶

The user constraints enable to impose constraints that can not be resolved using classical Robotran cuts.

• The user constraint is expressed in the form: $$h_{usr}(q) = 0$$
• The user has to provide, for a given configuration:
• The value of the constraint: $$h_{usr}(q)$$
• The Jacobian matrix of the constrain: $$J_{usr}(q)$$
• The quadratic term: $$\left(\dot{J}\dot{q}(q, \dot{q})\right)_{usr}$$
• The user constraints are implemented using the user_cons_hJ and user_cons_jdqd functions.

## Back to the pendulum-spring example¶

The goal is to replace the prismatic joint between the pendulum and the slider by a screw joint. Since only simple joints can be implemented in Robotran, the screw joint will be modelled:

• Using a rotoid and a prismatic joint along the same axis;
• Screw pitch : 20 mm.

The constraint can thus be written as: $$p\varphi - 2\pi z = 0$$

• $$\varphi$$: slider rotation along the pendulum;
• $$z$$: slider translation along the pendulum;
• $$p$$: the screw pitch

### Step 1: Draw your multibody system¶

Open the Pendulum Spring model in MBsysPad:

• Insert a R3 joint in the kinematic chain between the pendulum and the slider
• See the tips and tricks if you don’t know how to modify the attach point of a body.
• Give a name to the joint.
• Set the R3 joint as dependent

There is no specific action to do in MBsysPad for the user constraint.

### Step 2: Generate your multibody equations¶

You have to generate the equations because we added a joint. You also need to regenerate the user models files.

However if you add an user constraint on a existing project you don’t have to regenerate the equations because the user constraints are defined in the user functions.

### Step 3: Write your user function¶

C section:

• Edit the user_cons_hJ function:
• Enter the expression of the constraint: $$h_{usr}(q)$$
• Fill the jacobian matrix: $$J_{usr}(q)$$
• Include user_all_id.h.
//...
#include "user_all_id.h"
//...

void user_cons_hJ(double *h, double **Jac, MbsData *mbs_data, double tsim)
{
//...

// declare and define pitch value
double p = 20e-3;

// define the value of the constraint
h[1] = p * mbs_data->q[R3_slider_id] -2 * M_PI * mbs_data->q[T3_slider_id];

// define the value of the jacobian matrix
Jac[1][T3_slider_id] = -2 * M_PI;
Jac[1][R3_slider_id] = p;

//...
}


VERSION EVOLUTION:

In previous version you needed to add 1 to the id retrieved. It’s no more needed.

• Edit the user_cons_jdqd function:
• No modification to do since the quadratic term of the constraint is null in the present case.
• If the quadratic term of the constraint is nonzero you have to define it in this function.

### Step 4: Run your simulation¶

Before running the simulation, you must modify the main script in order to indicate that there is a user constraint.

C section:

Open the main.c file and:

• Include mbs_set.h.
• add the mbs_set_nb_usrc after the program loading in order to set the number of user constraints:
//...
#include "mbs_set.h"
//...