Synchronous operation with Python using remote API

Typically: "How do I... ", "How can I... " questions
Post Reply
3mayuge3
Posts: 2
Joined: 22 Oct 2018, 08:12

Synchronous operation with Python using remote API

Post by 3mayuge3 » 05 Dec 2018, 10:01

Hi,

I'd like to use synchronous operation with python, remote API. So I read this page (http://www.coppeliarobotics.com/helpFil ... erandi.htm) and at first wrote a simple code:

Code: Select all

import time

try:
    import vrep
except:
    print ('----------------------------------------------------------------')
    print ('"vrep.py" could not be imported. This means very probably that')
    print ('either "vrep.py" or the remoteApi library could not be found.')
    print ('Make sure both are in the same folder as this file,')
    print ('or appropriately adjust the file "vrep.py"')
    print ('--------------------------------------------------------------')
    print ('')


print('Program started')

vrep.simxFinish(-1) # just in case, close all opened connections
clientID=vrep.simxStart('127.0.0.1',19998,True,True,5000,5) # Connect to V-REP


if clientID!=-1:
    print ('Connected to remote API server')

    # start the simulation
    _ = vrep.simxSynchronous(clientID, 1)
    print(_)

    _, obj_handle = vrep.simxGetObjectHandle(clientID, 'Cuboid', vrep.simx_opmode_blocking)

    start_time = time.time()

    vrep.simxStartSimulation(clientID,vrep.simx_opmode_blocking)
    while (time.time() - start_time) <= 10:
        _, obj_pos = vrep.simxGetObjectPosition(clientID, obj_handle, -1, vrep.simx_opmode_buffer)
        # print(obj_pos)
        vrep.simxSynchronousTrigger(clientID)
        vrep.simxGetPingTime(clientID)

    vrep.simxAddStatusbarMessage(clientID,'simulation stop!',vrep.simx_opmode_oneshot)
    vrep.simxStopSimulation(clientID,vrep.simx_opmode_oneshot)
With this code, I just got "8". I guess it means "error code is 8", so "simx_return_remote_error_flag" was return.
I couldn't find something wrong, so would anyone tell me what is mistake(s)?
(I'm sorry if there are similar question.)

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

Re: Synchronous operation with Python using remote API

Post by coppelia » 06 Dec 2018, 08:00

hello,

you haven't enabled data streaming. You should first call the command with operation mode simx_opmode_streaming before trying to read from the buffer.

Cheers

3mayuge3
Posts: 2
Joined: 22 Oct 2018, 08:12

Re: Synchronous operation with Python using remote API

Post by 3mayuge3 » 10 Dec 2018, 08:03

Hello,

Thank you for your quick response.
Oh, I didn't notice. I tried that and maybe it's done well.

Actually, my final goal is to control joints of a robot at 5 ms period with remote API. For example, while python code is calculating, it is desirable that the simulation step wait. Is it possible?

Thank you.

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

Re: Synchronous operation with Python using remote API

Post by coppelia » 10 Dec 2018, 15:09

Probably 5ms is a bit too fast. Also because you want to run the remote API in synchronous mode. Make a few tests to see.

Cheers

Post Reply