Page 1 of 1

Get problem with python API!

Posted: 27 Nov 2018, 10:54
by daniel li
I'm working on controlling UR5.But weird thing happend.
When I use the code like following:

Code: Select all

import vrep

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

vrep.simxStartSimulation(clientID, vrep.simx_opmode_oneshot)
RC, UR5_joint1_Handle = vrep.simxGetObjectHandle(clientID, 'UR5_joint1', vrep.simx_opmode_blocking)
vrep.simxSetJointTargetVelocity(clientID, UR5_joint1_Handle, 0.1, vrep.simx_opmode_oneshot)

vrep give me nothing.
But if I add a timesleeping,vrep works as expected.

Code: Select all

import vrep
import time

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

vrep.simxStartSimulation(clientID, vrep.simx_opmode_oneshot)
RC, UR5_joint1_Handle = vrep.simxGetObjectHandle(clientID, 'UR5_joint1', vrep.simx_opmode_blocking)
vrep.simxSetJointTargetVelocity(clientID, UR5_joint1_Handle, 0.1, vrep.simx_opmode_oneshot)
time.sleep(1)

Why is that happend?Is there somethin wrong with operation mode?

Re: Get problem with python API!

Posted: 27 Nov 2018, 11:21
by coppelia
Hello,

this is normal: when you send a command and forget about it (i.e. non-blocking operation), control returns directly to your program. In the mean time, the command will have to be fetched by another thread, send via socket to the server, etc.
If you do not add a sleep, then your client will immediately end and free resources (including the thread that is supposed to send your command).

Cheers

Re: Get problem with python API!

Posted: 27 Nov 2018, 12:40
by daniel li
Thanks your reply!

So what is the correct way to control motors(i.e. using simxSetJointTargetVelocity)?
Now I can only use time.sleep() to run my scene.Using time.sleep() feels a little bit werid.I think the way I code is not correct.I've read the user manual,but I can't find out what is wrong.

Could you show me a correct code example?For example set all 6 joint's TargetVelocity to 0.1.
Here's my code

Code: Select all

import vrep
import time

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

time.sleep(1)

vrep.simxStartSimulation(clientID, vrep.simx_opmode_oneshot)
RC, UR5_joint1_Handle = vrep.simxGetObjectHandle(clientID, 'UR5_joint1', vrep.simx_opmode_blocking)
RC2, UR5_joint2_Handle = vrep.simxGetObjectHandle(clientID, 'UR5_joint2', vrep.simx_opmode_blocking)
RC3, UR5_joint3_Handle = vrep.simxGetObjectHandle(clientID, 'UR5_joint3', vrep.simx_opmode_blocking)
RC4, UR5_joint4_Handle = vrep.simxGetObjectHandle(clientID, 'UR5_joint4', vrep.simx_opmode_blocking)
RC5, UR5_joint5_Handle = vrep.simxGetObjectHandle(clientID, 'UR5_joint5', vrep.simx_opmode_blocking)
RC6, UR5_joint6_Handle = vrep.simxGetObjectHandle(clientID, 'UR5_joint6', vrep.simx_opmode_blocking)
vrep.simxSetJointTargetVelocity(clientID, UR5_joint1_Handle, 0.01, vrep.simx_opmode_oneshot)
vrep.simxSetJointTargetVelocity(clientID, UR5_joint2_Handle, 0.1, vrep.simx_opmode_oneshot)
vrep.simxSetJointTargetVelocity(clientID, UR5_joint3_Handle, 0.1, vrep.simx_opmode_oneshot)
vrep.simxSetJointTargetVelocity(clientID, UR5_joint4_Handle, 0.1, vrep.simx_opmode_oneshot)
vrep.simxSetJointTargetVelocity(clientID, UR5_joint5_Handle, 0.1, vrep.simx_opmode_oneshot)
vrep.simxSetJointTargetVelocity(clientID, UR5_joint6_Handle, 0.1, vrep.simx_opmode_oneshot)


Thanks!

Re: Get problem with python API!

Posted: 28 Nov 2018, 10:44
by daniel li
Well,I found a probably correct way to solve my problem.
It seems using vrep.simxGetPingTime(clientID) at the end of the code helps.Refer to file simpleTest.py included in the programming folder.

Thanks a lot!

Re: Get problem with python API!

Posted: 03 Dec 2018, 06:55
by coppelia
That's a possibility. Or you have a loop that waits there until the user want to quit.

Cheers