Sometimes cannot succeed to switch scene through python remote api

Typically: "How do I... ", "How can I... " questions
Post Reply
lyz
Posts: 22
Joined: 07 Mar 2019, 07:34

Sometimes cannot succeed to switch scene through python remote api

Post by lyz » 28 Aug 2019, 09:53

Hello, everyone
I have a problem about the subject. I write the python remote api code to control vrep start、stop、load scene and so on. I want switch some scenes through python remote api, But sometimes it is invalid. Here is my main code, if I add sleep some seconds, the scene switch will be better, but not absolute.

Code: Select all

                    
if (switch_map) :
    vrep.simxStopSimulation(clientID, vrep.simx_opmode_oneshot)
    vrep.simxCloseScene(clientID, vrep.simx_opmode_blocking)
    vrep.simxLoadScene(clientID, scene, 0xFF, vrep.simx_opmode_blocking)
    time.sleep(2)

    _, baseHandle = vrep.simxGetObjectHandle(clientID, 'Vehicle', vrep.simx_opmode_blocking)
    # set robot pose
    vrep.simxSetObjectPosition(clientID, baseHandle, -1, jpos, vrep.simx_opmode_blocking)
    vrep.simxSetObjectOrientation(clientID, baseHandle, -1, jyaw, vrep.simx_opmode_oneshot_wait)

    print ("load scene : " + scene)
    # start simulation
    vrep.simxStartSimulation(clientID, vrep.simx_opmode_oneshot)
Does anybody have this problem? please help me, thanks a lot.

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

Re: Sometimes cannot succeed to switch scene through python remote api

Post by coppelia » 28 Aug 2019, 13:26

Hello,

could it be that some sort of timeout was hit? Can you try to increase the timeout that you specify in simxStart?
Could it be that it takes close to 5 seconds?
If that doesn't work, try to first transfer the file via simxTransferFile and then load it locally.

Also, simxStopSimulation will always take more time to execute, even if in blocking mode. This is because a running simulation can't instantaneously stop. It will first try to stop threaded script, then run a last simulation step. So it might also be that simxCloseScene simply fails because the simulation hasn't yet stopped.

Cheers

lyz
Posts: 22
Joined: 07 Mar 2019, 07:34

Re: Sometimes cannot succeed to switch scene through python remote api

Post by lyz » 30 Aug 2019, 04:41

OK, thank you very much. I add one second time sleep under the stop simulation command. And now it is succeed to switch scene every time.

Code: Select all

            
            vrep.simxStopSimulation(clientID, vrep.simx_opmode_oneshot)
            time.sleep(1)
            vrep.simxCloseScene(clientID, vrep.simx_opmode_blocking)
            vrep.simxLoadScene(clientID, scene, 0xFF, vrep.simx_opmode_blocking)

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

Re: Sometimes cannot succeed to switch scene through python remote api

Post by coppelia » 30 Aug 2019, 05:35

Better would be to use simxGetInMessageInfo with simx_headeroffset_server_state to check if simulation has stopped.

Cheers

lyz
Posts: 22
Joined: 07 Mar 2019, 07:34

Re: Sometimes cannot succeed to switch scene through python remote api

Post by lyz » 02 Sep 2019, 11:12

OK, Thanks a lot.

Post Reply