Python remote API calls gets slower and slower

Typically: "How do I... ", "How can I... " questions
Post Reply
JoakimMagnusson
Posts: 10
Joined: 09 May 2018, 10:31

Python remote API calls gets slower and slower

Post by JoakimMagnusson » 15 May 2018, 12:44

Hi!

I'm writing a python script using the python remote API. When running my script i noticed it got slower and slower with time so I tried timing a remote API function call and could see the call taking longer and longer time the longer my script was running. Is it possible that I'm doing something wrong and causing this behavior, or this is a known problem perhaps?

For example, I timed my synchronoustrigger call like this:

Code: Select all

	dt = time.time()
        vrep.simxSynchronousTrigger(clientID) #Do one step in simulation
        dt = time.time() - dt
        print(1000*dt) #Convert to milliseconds 
At first the call takes approximately 10 ms but after a while(maybe 10 minutes) it takes 20 ms and it just keep increasing.

Any suggestion or explanation is appreciated.

Edit: I commented out my simxSetJointTargetPosition calls and the problem disappeared. But I need to use this call. Any ideas why this call is causing problems?

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

Re: Python remote API calls gets slower and slower

Post by coppelia » 18 May 2018, 13:51

Hello,

can you show us how and in which context you are calling simxSetJointTargetPosition?

Cheers

JoakimMagnusson
Posts: 10
Joined: 09 May 2018, 10:31

Re: Python remote API calls gets slower and slower

Post by JoakimMagnusson » 24 May 2018, 09:30

In my main script I'm using the remote API to set the servo and then do simulation steps in synchronous mode. Like this:

Code: Select all

while True:
    pos = robot.get_servo_pos(1)
    # Some calculations...
    robot.set_servo_position(1,  0.5)
    robot.step_simulator_nonblocking()
    
    if robot_failed:
    	robot.reset_robot()
    
The set_servo_pos function in the robot class looks like this:

Code: Select all

def set_servo_position(self, servoID, angle):
		#setting position of servos
		assert servoID > 0 and servoID <= 18, 'Commanding unexisting servo'
		errorCode = vrep.simxSetJointTargetPosition(self.clientID, self.servos[servoID-1], angle, vrep.simx_opmode_streaming)
		assert errorCode <= 1, 'Failed to set servo position'
the reset function looks like this:

Code: Select all

reset_robot(self):
	hexapod_handle = self.get_object_handle('hexapod')
	vrep.simxRemoveModel(self.clientID, hexapod_handle, vrep.simx_opmode_blocking)
	errorcode, self.hexapod = vrep.simxLoadModel(self.clientID, 'C:/Users/86008047/Python/phantomx_model_skinny.ttm', 0, vrep.simx_opmode_blocking)
I noticed that the problem with longer API calls doesn't occur if I don't call the reset function. I've also tried stopping and starting the simulation instead but stopping the simulation takes to much time.

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

Re: Python remote API calls gets slower and slower

Post by coppelia » 24 May 2018, 12:48

You should call vrep.simxSetJointTargetPosition with the simx_opmode_discontinue operation mode at the end of simulation or when disconnecting, for each joint you have been getting information from.

By the way, vrep.simx_opmode_streaming is wrong, when just sending information to V-REP (and not expecting any reply). You should use simx_opmode_oneshot instead. In that case you don't need the simx_opmode_discontinue operation mode later on.


Cheers

JoakimMagnusson
Posts: 10
Joined: 09 May 2018, 10:31

Re: Python remote API calls gets slower and slower

Post by JoakimMagnusson » 24 May 2018, 13:42

Using simx_opmode_oneshot instead solved the problem, thanks!

Post Reply