Page 1 of 1

forceVector a nil value

Posted: 09 Feb 2021, 15:42
by jovan_s
Hello,

I have the following lines in my code (threaded child script):

result, forceVector, torqueVector=sim.readForceSensor(sensorHandle)
print(result)
Fz=forceVector[3]

I print the result and it's 0, but when I try to access the forceVector value, an error appears, and the simulation stops.

158: attempt to index global 'forceVector' (a nil value)

I don't understand why the measurement isn't yet available. In the sensor setting, the sample value is 1.

Cheers

Re: forceVector a nil value

Posted: 12 Feb 2021, 10:29
by coppelia
Hello,

this is because the code that reads the force/torque sensor is executed at the very beginning of a simulation, before even the dynamics was called. So there is no reading available yet. As you correctly mention it, from a non-threaded child script the data is available in the sensing section.

What you can do in your threaded child script is simply wait for next simulation step, before reading the force/torque sensor, e.g.:

Code: Select all

sim.switchThread() -- execution resumes in next simulation step
result, forceVector, torqueVector=sim.readForceSensor(sensorHandle)
Cheers

Re: forceVector a nil value

Posted: 12 Feb 2021, 10:46
by jovan_s
Thanks a lot!
Cheers

Re: forceVector a nil value

Posted: 12 Feb 2021, 13:25
by jovan_s
Just one more tiny question. I have a force sensor connecting 2 objects, and I want to measure the force with which one component is pressing the other. If I want to measure the z component of the force what is the direction of that force? If the sensor returns a negative Fz value, is it negative relative to the sensor's z-axis or the world z-axis?

Cheers

Re: forceVector a nil value

Posted: 16 Feb 2021, 10:04
by coppelia
The direction of the force will be as the second object in the hierarchy exerts on the force/torque sensor.

Cheers