## forceVector a nil value

Typically: "How do I... ", "How can I... " questions
jovan_s
Posts: 10
Joined: 01 Dec 2020, 10:19

### forceVector a nil value

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

coppelia
Site Admin
Posts: 8371
Joined: 14 Dec 2012, 00:25

### Re: forceVector a nil value

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

jovan_s
Posts: 10
Joined: 01 Dec 2020, 10:19

### Re: forceVector a nil value

Thanks a lot!
Cheers

jovan_s
Posts: 10
Joined: 01 Dec 2020, 10:19

### Re: forceVector a nil value

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

coppelia
Site Admin
Posts: 8371
Joined: 14 Dec 2012, 00:25

### Re: forceVector a nil value

The direction of the force will be as the second object in the hierarchy exerts on the force/torque sensor.

Cheers