Get problem with python API!

Typically: "How do I... ", "How can I... " questions
Post Reply
daniel li
Posts: 6
Joined: 12 Mar 2018, 03:28

Get problem with python API!

Post by daniel li » 27 Nov 2018, 10:54

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?

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

Re: Get problem with python API!

Post by coppelia » 27 Nov 2018, 11:21

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

daniel li
Posts: 6
Joined: 12 Mar 2018, 03:28

Re: Get problem with python API!

Post by daniel li » 27 Nov 2018, 12:40

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!

daniel li
Posts: 6
Joined: 12 Mar 2018, 03:28

Re: Get problem with python API!

Post by daniel li » 28 Nov 2018, 10:44

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!

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

Re: Get problem with python API!

Post by coppelia » 03 Dec 2018, 06:55

That's a possibility. Or you have a loop that waits there until the user want to quit.

Cheers

Post Reply