VRep: How to use V-Rep in Torque/Force mode
The V-REP allows the user to choose how to control the robot, if in inverse kinematic mode or torque/force mode. The configuration in the V-REP environment is different for each case, and sending torque to V-REP can be a little bit tricky. Thereat, this tutorial will cover the control of a V-REP robot using the torque/force mode.
The first step is to set the V-REP environment to work in torque/force mode. When clicking on any joint of the robot, the Scene Object Properties dialog will open, and in the "Mode" part one can select the "Torque/force mode".
Then, in the "Dynamic properties" part, click on "Show dynamic properties dialog". Make sure that the "Motor enabled" is ticked and that the "Control loop enabled" is not. Also, to facilitate the future control, one can set the maximum torque and the target velocity to zero, and tick the "Lock motor when target velocity is zero".
Repeat this action for every joint of the robot.
Now that the properties of the joints are set, one needs to set the properties of the links. Click on one link of the robot, and then click on "Show dynamic properties dialog".
When the new dialog is open, make sure that the "Body is dynamic" is ticked.
Repeat this process to all links of the robot.
Now that the V-REP environment is all setted up, one needs to know how to send the torque from Matlab to V-REP, in order to control the robot.
According to V-REP documentation, one needs to specify a target velocity and a maximum torque for each joint. If the current velocity is below the target velocity, the maximum torque is applied. So if you want to control the joints in force/torque, just specify a target velocity very high (e.g. that will never be reached) and then modulate the torque/force using the 'simxSetJointForce' function.
One important thing is that the function that sets the maximum torque of the joints, does not accept a negative value. So in order to change the sign of the applied torque, one needs to change the sign of the target velocity and to send the absolute value of the torque.
Using Matlab, one can create a function with a code like the following one:
vrep.simxPauseCommunication(client_id, 1); for i = 1:number_of_joints vrep.simxSetJointTargetVelocity(client_id, joints_handles(i), sign(u(i))*10e10, vrep.simx_opmode_oneshot); vrep.simxSetJointForce(client_id, joints_handles(i), abs(u(i)), vrep.simx_opmode_oneshot); end vrep.simxPauseCommunication(client_id,0);
The 'simxPauseCommunication' allows sending multiple commands at once, and then the V-REP will wait for the receipt of all commands before to execute them. The commands 'simxSetJointTargetVelocity' and 'simxSetJointForce' send the target velocity, that needs to be high (eg.: 10e10), and the maximum torque, that is the desired torque to be applied to the joints (u). Both commands must be used for all joints of the robot, and that is the reason why they are inside a loop. The 'vrep' and 'client_id' are objects necessaries to do the communication with V-REP. And 'joints_handles' are the handles of the robot's joints. For more information about this objects, take a look at.