Simulation with Python Remote API

Typically: "How do I... ", "How can I... " questions
Post Reply
mash2612
Posts: 6
Joined: 28 Jan 2018, 10:21

Simulation with Python Remote API

Post by mash2612 » 19 Feb 2018, 07:54

Hello,

I am trying to run a simulation where position of tip dummy is set via Python remote API, and once the position is received the threaded child script will implement IK module and compute relevant positions.
I am able to send the data via remote API, however the IK module doesn't seem to get implement.

I have saved the scene in following location:
https://drive.google.com/open?id=1vb_fn ... HPs6_KdRKO

And the python code is as follows:

Code: Select all

#Standard Imports
import array
import vrep
vrep.simxFinish(-1) # just in case, close all opened connections
def Set_Pose():
    #vrep.simxPauseCommunication(clientID,1)
    #x = float(input('Enter the position x'))
    #y = float(input('Enter the position y'))
    #z = float(input('Enter the position z'))
    vrep.simxSynchronous(clientID, 1)
    alpha = vrep.simxStartSimulation(clientID, vrep.simx_opmode_oneshot)
    vrep.simxSynchronousTrigger(clientID)
    beta=vrep.simxSetObjectPosition(clientID,dummyHandle,-1,(10,0.3,0.4),vrep.simx_opmode_oneshot)
    print(beta)
    vrep.simxSynchronousTrigger(clientID)
    beta = vrep.simxSetObjectPosition(clientID, dummyHandle, -1, (2.5, 3, 4), vrep.simx_opmode_oneshot)
    #vrep.simxSynchronousTrigger(clientID)
    beta = vrep.simxSetObjectPosition(clientID, dummyHandle, -1, (20, 3, 4), vrep.simx_opmode_oneshot)
    vrep.simxSynchronous(clientID, 0)
    #vrep.simxPauseCommunication(clientID,0)
    #beta=vrep.simxGetInMessageInfo(clientID,vrep.simx_headeroffset_server_state)
    gamma=vrep.simxStopSimulation(clientID,vrep.simx_opmode_oneshot_wait)
clientID=vrep.simxStart('127.0.0.1',19999,True,True,5000,5)
if clientID!=-1:
    code,dummyHandle = vrep.simxGetObjectHandle(clientID,'UR5_target#', vrep.simx_opmode_oneshot_wait)
    print(dummyHandle)
    #dummyHandle = vrep.simxGetObjectHandle(clientID, 'Cuboid', vrep.simx_opmode_oneshot_wait)
    Set_Pose()

Lastly on printing I find that the value in end is getting printed twice when I request Object Position from the child script. I could not understand the logic for it.

Thanks!

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

Re: Simulation with Python Remote API

Post by coppelia » 20 Feb 2018, 14:29

Hello,

your threaded child script is run and exists right after. Shouldn't there be a loop in there? And I think you have not enabled the synchronous mode in your python remote API.

Cheers

mash2612
Posts: 6
Joined: 28 Jan 2018, 10:21

Re: Simulation with Python Remote API

Post by mash2612 » 28 Feb 2018, 12:11

Hello admin,

Thanks for your response. As per your comments:

1. I think I have enabled synchronous mode in vrep remote API :
vrep.simxSynchronous(clientID, True)
alpha = vrep.simxStartSimulation(clientID, vrep.simx_opmode_oneshot)
vrep.simxSynchronousTrigger(clientID)
beta=vrep.simxSetObjectPosition(clientID,dummyHandle,-1,pos,vrep.simx_opmode_oneshot)
Is this not the right way to enable synchronous mode?(pos is a tupple which I am accepting from the user.)

2. I am not sure I follow your comment on loop in child script. Do you mean it should be something like a while(userinput) it should execute IKmovement()? Could you please elaborate a little or give some idea via example.

Thanks

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

Re: Simulation with Python Remote API

Post by coppelia » 01 Mar 2018, 09:41

Your threaded script should have a loop, similar to something like:

Code: Select all

applyJoints=function(jointHandles,joints)
    for i=1,#jointHandles,1 do
        sim.setJointPosition(jointHandles[i],joints[i])
    end
end

function sysCall_threadmain()
    sim.setThreadAutomaticSwitch(false)
    jointHandles={-1,-1,-1,-1,-1,-1}
    for i=1,6,1 do
        jointHandles[i]=sim.getObjectHandle('UR5_joint'..i)
    end
    while sim.getSimulationState()~=sim.simulation_advancing_abouttostop do
        local q=sim.getStringSignal("desiredJoints")
        if q then
            q=sim.unpackFloatTable(q)
            -- You received a new desired configuration. Do something with it. e.g.:
            applyJoints(jointHandles,q)
        end
        sim.switchThread() -- resume in next simulation step
    end
    sim.setThreadAutomaticSwitch(true)
end

function sysCall_cleanup()

end
Above code is for V-REP version 3.5.0 and onwards.

Cheers

Post Reply