## Torque control

Typically: "How do I... ", "How can I... " questions
sam3891
Posts: 6
Joined: 23 Oct 2015, 10:16

### Re: Torque control

Hi,

How does this work then in the joint space, where a torque vector is only one value?

I am trying to generate a custom torque controller in MATLAB by following your suggestion, but on the VREP side, the function simAddForceAndTorque requires 3 coordinates per joint, whereas MATLAB generates one joint torque only. (I am using KUKA_LBR_14_R820)

Also, what should be the handle for this function ('simAddForceAndTorque') for this model, let's say for joint3? I have chosen:
handle = simGetObjectHandle('KUKA_LBR_14_R820_link3'), is this right?
coppelia wrote:yes, that is correct. You could for instance do:

On the remote API client side (e.g. in C/C++):

Code: Select all

float forceVectorToApply[3]; // a force vector initialized to what you want
simxSetStringSignal(clientId,"myVector",(simxChar*)forceVectorToApply,3*sizeof(float),simx_opmode_oneshot);
Inside of a non-threaded child script:

Code: Select all

if (sim_call_type==sim_childscriptcall_initialization) then
lastVector={0,0,0}
end

if (sim_call_type==sim_childscriptcall_actuation) then
local signal=simGetStringSignal('myVector')
if signal then
lastVector=simUnpackFloats(signal)
end

end
Cheers

erik
Posts: 5
Joined: 03 Dec 2014, 15:54

### Re: Torque control

Hi Sam,

if you send me an e-mail I can reply with a sample file where I tried different methods to apply joint torques, including a Matlab file that sends the desired torques. The case study I did: simple 3dof arm where I wanted to compute and apply the torques to compensate for gravity (i.e. the arm should do nothing when running the simulation).

Erik

coppelia
Posts: 7936
Joined: 14 Dec 2012, 00:25

### Re: Torque control

In next release, there will be a much more in-depth description how to control a joint. We have uploaded the page already, it can be found here, but some html links might not yet be working.

Cheers

sam3891
Posts: 6
Joined: 23 Oct 2015, 10:16

### Re: Torque control

Hi,

I want to implement an application on the "KUKA_LBR_iiwa_14_R820", where I calculate torques on the MATLAB side and send them across to VREP using simxSetJointForce and simxSetJointVelocity.

Code: Select all

for i=1:7
if( torque(i)>0.0 )
velocity(i) = 9999.0;
elseif (torque(i) <0.0)
velocity(i) = -9999.0;
else
velocity(i) = 0.0;
end

end

for i=1:7
vrep.simxSetJointTargetVelocity(clientID,jointHandles(i),velocity(i),vrep.simx_opmode_oneshot);
end

for i=1:7
vrep.simxSetJointForce(clientID,jointHandles(i),abs(torque(i)),vrep.simx_opmode_oneshot);
end

On the V-Rep side, I have enabled a custom joint control loop in the joint dynamic properties, in order to add damping:

Code: Select all

cVel=simGetObjectFloatParameter(jointHandle,2012)
forceOrTorqueToApply=maxForceTorque-(-300*cVel)

return forceOrTorqueToApply,targetVel

1. Is this the right way to do it?

2. Also, in order to ensure that the torque computed by MATLAB is accurately set in VREP, I would like to run the joint control callback script a few times and then communicate with MATLAB. For this I have set the ODE time step to 1ms and the VREP simulation time step to 5ms. Is this correct? Or is there another way to go about this?

I would really appreciate some help.

Thanks!

coppelia
Posts: 7936
Joined: 14 Dec 2012, 00:25

### Re: Torque control

Hello,

please read this section first, there are many things you can do wrong if you try to control a joint from an external application.

Cheers

sam3891
Posts: 6
Joined: 23 Oct 2015, 10:16

### Re: Torque control

Hi,

I have the code as per what you said. So my question again is:
sam3891 wrote:Hi,

I want to implement an application on the "KUKA_LBR_iiwa_14_R820", where I calculate torques on the MATLAB side and send them across to VREP using simxSetJointForce and simxSetJointVelocity.

Code: Select all

for i=1:7
if( torque(i)>0.0 )
velocity(i) = 9999.0;
elseif (torque(i) <0.0)
velocity(i) = -9999.0;
else
velocity(i) = 0.0;
end

end

for i=1:7
vrep.simxSetJointTargetVelocity(clientID,jointHandles(i),velocity(i),vrep.simx_opmode_oneshot);
end

for i=1:7
vrep.simxSetJointForce(clientID,jointHandles(i),abs(torque(i)),vrep.simx_opmode_oneshot);
end

vrep.simxSynchronousTrigger(clientID);

On the V-Rep side, I have enabled a custom joint control loop in the joint dynamic properties, in order to add damping:

Code: Select all

cVel=simGetObjectFloatParameter(jointHandle,2012)
forceOrTorqueToApply=maxForceTorque-(-300*cVel)

return forceOrTorqueToApply,targetVel

1. Is this the right way to do it?

2. Also, in order to ensure that the torque computed by MATLAB is accurately set in VREP, I would like to run the joint control callback script a few times and then communicate with MATLAB. For this I have set the ODE time step to 1ms and the VREP simulation time step to 1ms. Is this correct? Or is there another way to go about this? Is there a possibility to modify the variable 'totalPasses' in the joint control callback script?

I would really appreciate some help.

Thanks!

coppelia
Posts: 7936
Joined: 14 Dec 2012, 00:25

### Re: Torque control

1) That sounds correct (if the other requirements discussed in the link I sent previously are respected)

2) Why would you like the joint control callback to be run a few times before applying values from Matlab? Not sure I understand . But as you mention it, it is good to set the same value for the simulation time step and the physics engine time step.

In a first step, I would try without the callback script that you use for additional damping. Currently you are mixing things, and many things can go wrong. Try to go step-by-step. And see until what point it works, or at which point it stops working.

Cheers

sam3891
Posts: 6
Joined: 23 Oct 2015, 10:16

### Re: Torque control

Hi,

I want to ensure that the torque being communicated over by MATLAB to VREP is applied without lag. So I want to run the joint script several times per simulation step.

coppelia
Posts: 7936
Joined: 14 Dec 2012, 00:25

### Re: Torque control

I still don't understand it. Things communicated from Matlab to V-REP will be applied with a lag. Always. Because Matlab is an external application. But in terms of simulation time, if you enabled the synchronous mode, then you can make sure that things are applied in each simulation step. e.g.:
• send torque value
• trigger next simulation step
• send torque value
• trigger next simulation step
• send torque value
• trigger next simulation step
• send torque value
• ...
Cheers

sam3891
Posts: 6
Joined: 23 Oct 2015, 10:16