# VRep: How to get the dynamic parameters of the robots

V-REP has a lot of robot models already available to be used. However, when controlling a robot, it is useful to have its model, both kinematic and dynamic. In this tutorial, we are going to show how to get the dynamic model of a generic robot in V-REP.

An important thing to know is that each link of the robot has a coordinate frame, but this coordinate is different when considering the inertias. To better understand this, take a look at the following images. The left one shows the frame of one link, and the right one shows the frames of the inertias.

To see the inertia frames, one can right click with the mouse and then click on 'View' and then in 'Visualize inertias'.

Once both coordinates are different, in order to get the inertia tensors, it is necessary to know the inertia coordinate frames. However, this information is not easy to find.

One way to get the inertia tensor of each link is looking at the V-REP interface. In the 'Rigid Body Dynamic Property' of the link, there is a part showing the mass, the center of mass and the principal moments of inertia. However, the principal moments of inertia are related to the inertia coordinate frames, and we use the link coordinate frame to do all the rest, as for example, do the kinematic model, or to create control laws. In that way, it is important to also know the moment of inertia related to the link frame. A way to do that is to see the 'Set inertia matrix and COM relative to absolute frame'. Once it is easy to get the relation between the absolute frame and the link frames, one can calculate the inertia matrices and COM related to the links' frames.

But as to get the values from the V-REP interface is an irksome manual work, a better way is to use the available commands. Unlike most of the V-REP commands, the command to get the inertia matrices, as well as the mass and the center of mass, is not available for Matlab, but it is in Lua. In this way, one can use this method inside V-REP, and then send the results to Matlab, using a child script.

## How to add a child script

To add a child script, just right click on the object you want in the V-REP scene and then choose 'Add', 'Associated child script', 'Non thread'.

Once the script was added, one can code to get the dynamic properties. The following image shows an example of code using the function 'simGetShapeMassAndInertia'. This function returns the mass, the inertia matrix and the center of mass of the desired object.

After coding the child script, it is necessary to send the information to Matlab, in order to use it to perform the control etc. In the Matlab side, one can use the 'simxCallScriptFunction' to get all information. The following code is an example on how to use it.

First, it is necessary to pass the 'client_id', then the name of the child script, then the name of the function you have created inside the child script, and the handle of the object you want to get information from.

[resM, ~, retFloatsM, ~, ~] = vrep.simxCallScriptFunction(client_id,'remoteApiCommandServer', ... vrep.sim_scripttype_childscript,'getMass_function',link_handle,[],[], ... [],vrep.simx_opmode_blocking); if (resM == vrep.simx_return_ok) mass = retFloatsM; end [resC, ~, retFloatsC, ~, ~] = vrep.simxCallScriptFunction(client_id,'remoteApiCommandServer', ... vrep.sim_scripttype_childscript,'getCom_function',link_handle,[],[], ... [],vrep.simx_opmode_blocking); if (resC == vrep.simx_return_ok) com = retFloatsC'; end [resI, ~, retFloatsI, ~, ~] = vrep.simxCallScriptFunction(client_id,'remoteApiCommandServer', ... vrep.sim_scripttype_childscript,'getInertia_function',link_handle,retFloatsC,[], ... [],vrep.simx_opmode_blocking); if (resI == vrep.simx_return_ok) inertia(1,:) = retFloatsI(1:3); inertia(2,:) = retFloatsI(4:6); inertia(3,:) = retFloatsI(7:9); end