Reading an object's position using the Python remote API

Typically: "How do I... ", "How can I... " questions
Post Reply
johnrowlay
Posts: 25
Joined: 09 Aug 2017, 18:52

Reading an object's position using the Python remote API

Post by johnrowlay » 11 Feb 2019, 20:22

I have noticed some strange behaviour when trying to get and set object positions using the Python remote API.

As an example, I have created a test scene, which contains a cuboid and a sphere, and nothing else. Both the cuboid and sphere are static and non-respondable. The cuboid is at position [0.1, 0.1, 0.1], and the sphere is at position [0.2, 0.2, 0.2].

My code launches a V-Rep server, loads the scene, and starts the simulation, using the following code:

Code: Select all

port_num = 19990
args = ['vrep.sh', '-gREMOTEAPISERVERSERVICE_' + str(port_num) + '_FALSE_TRUE']
process = Popen(args, preexec_fn=os.setsid)
time.sleep(6)
client_id = vrep.simxStart('127.0.0.1', port_num, True, True, 5000, 5)
vrep.simxSynchronous(client_id, enable=True)
vrep.simxLoadScene(client_id, 'test_scene.ttt', 0, vrep.simx_opmode_blocking)
_, cuboid_handle = vrep.simxGetObjectHandle(client_id, 'Cuboid', vrep.simx_opmode_blocking)
_, sphere_handle = vrep.simxGetObjectHandle(client_id, 'Sphere', vrep.simx_opmode_blocking)
vrep.simxStartSimulation(client_id, vrep.simx_opmode_blocking)
Then, I want to get the positions of the cuboid and sphere. So, I call the following code:

Code: Select all

_, cuboid_position = vrep.simxGetObjectPosition(client_id, cuboid_handle, -1, vrep.simx_opmode_oneshot)
print('Cuboid position = ' + str(cuboid_position))
_, sphere_position = vrep.simxGetObjectPosition(client_id, sphere_handle, -1, vrep.simx_opmode_oneshot)
print('Sphere position = ' + str(sphere_position))
However, rather than telling me that the cuboid and sphere are at [0.1, 0.1, 0.1] and [0.2, 0.2, 0.2], it tells me that both the cuboid and the sphere are at [0, 0, 0]. But if I check the V-Rep GUI which is running on the server, and click on the cuboid and sphere, they are actually at [0.1, 0.1, 0.1] and [0.2, 0.2, 0.2].

So my first question is, why is the server telling me one thing, but the client is telling me another thing?

I then added a couple of lines to this code:

Code: Select all

_, cuboid_position = vrep.simxGetObjectPosition(client_id, cuboid_handle, -1, vrep.simx_opmode_oneshot)
vrep.simxSynchronousTrigger(client_id)
_, cuboid_position = vrep.simxGetObjectPosition(client_id, cuboid_handle, -1, vrep.simx_opmode_oneshot)
print('Cuboid position = ' + str(cuboid_position))
_, sphere_position = vrep.simxGetObjectPosition(client_id, sphere_handle, -1, vrep.simx_opmode_oneshot)
print('Sphere position = ' + str(sphere_position))
And this time, it told me that the cuboid is at [0.1, 0.1, 0.1], but the sphere is at [0, 0, 0]. If I remove either of these two new lines (the first "simxGetObjectPosition()", or "simxSynchronousTrigger()"), then it goes back to telling me that the cuboid is at [0, 0, 0]. And as you can see, the position of the sphere is incorrect either way.

So, it seems that for the client to be able to read the object positions from the server, you need to do the following:

1) Call "simxGetObjectPosition()" on the object
2) Step with "simxSynchronousTrigger()"
3) Call "simxGetObjectPosition()" on the object once more

And therefore, my second question is, why is this necessary? Why can you not just call "simxGetObjectPosition()" the one time?

Thank you!

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

Re: Reading an object's position using the Python remote API

Post by coppelia » 12 Feb 2019, 10:45

Hello,

you cannot get data from V-REP with the operation mode vrep.simx_opmode_oneshot! That mode is meant to be used to send data to V-REP (or execute a command on the V-REP side without waiting for a reply from V-REP).
Make sure to carefully read this page.

Cheers

Post Reply