# Difference between revisions of "VRep: How to get the dynamic parameters of the robots"

(6 intermediate revisions by the same user not shown) | |||

Line 3: | Line 3: | ||

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

− | [[File:inertia_frame.png]] | + | [[File:inertia_frame.png|center]] |

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

− | [[File:inertia_visual.png]] | + | [[File:inertia_visual.png|center]] |

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. | 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. | + | 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 (COM) 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. |

− | [[File:inertia_abs.png]] | + | [[File:inertia_abs.png|center]] |

− | 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. | + | 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'. | ||

+ | |||

+ | [[File:child_script.png|center]] | ||

+ | |||

+ | 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. It receives an int, that is the object handle, and a transformation, to tell with whom the output must be related. If nil, the transformation is the absolute frame. | ||

+ | |||

+ | [[File:child_code.png|center]] | ||

+ | |||

+ | == Sending information to Matlab == | ||

+ | |||

+ | 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 of 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. For the mass and COM, no transformation is passed, so all is returned related to the absolute frame. For the intertia matrix, the COM position is passed, so the inertia returned is related to the position of the center os mass, and the orientation of the absolute frame. | ||

+ | |||

+ | [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 | ||

+ | |||

+ | After that, if you want the information related to the Denavit-Hartemberg (DH) frame of each link of the robot, you only need to transform the results, once the DH frames is already known and the absolute frame is easy to get. |

## Latest revision as of 19:48, 22 January 2018

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 (COM) 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. It receives an int, that is the object handle, and a transformation, to tell with whom the output must be related. If nil, the transformation is the absolute frame.

## Sending information to Matlab

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 of 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. For the mass and COM, no transformation is passed, so all is returned related to the absolute frame. For the intertia matrix, the COM position is passed, so the inertia returned is related to the position of the center os mass, and the orientation of the absolute frame.

[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

After that, if you want the information related to the Denavit-Hartemberg (DH) frame of each link of the robot, you only need to transform the results, once the DH frames is already known and the absolute frame is easy to get.