How to extract running time of VREP by remote API

Typically: "How do I... ", "How can I... " questions
Post Reply
Aboelnaga
Posts: 11
Joined: 12 Apr 2015, 19:51

How to extract running time of VREP by remote API

Post by Aboelnaga » 18 May 2015, 19:11

Is there any function or way to get the simulation time in any point of the simulation ? for example I am controlling a youbot and in some points in path I need to know how much time it took to get to this point.

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

Re: How to extract running time of VREP by remote API

Post by coppelia » 19 May 2015, 10:50

Hello,

have a look at simxGetLastCmdTime. If you have no streaming that was initialized, then that function will only get the simulation time of the last service call to V-REP. So you could simply start streaming some dummy data, then you should always get the freshes value of the simulation time.

Cheers

harrisonmg
Posts: 6
Joined: 16 Feb 2018, 17:53

Re: How to extract running time of VREP by remote API

Post by harrisonmg » 02 Mar 2018, 19:30

Hi,

I'm wondering if there is a particularly fast or preferred type of data to stream as a dummy, purely to enable the use of a command like this.

Also, is simxGetLastCmdTime specific to the client? Or would data being streamed to another client connection work to retrieve current simulation time?

Best,
Harrison

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

Re: How to extract running time of VREP by remote API

Post by coppelia » 05 Mar 2018, 07:11

Hello,

you could use a command like following:

Code: Select all

int dummyValue;
simxGetIntegerParameter(clientId,sim_intparam_program_version,&dummyValue,simx_opmode_streaming);
Each remote API client is isolated from other remote API clients. Yo you would have to enable that data streaming in all of them.

Another more flexible way would be to have a customization script on the V-REP side that constantly updates a few useful values and packs them into a string signal, like:

Code: Select all

function setSignal(name,infoTable)
    sim.setStringSignal(name,sim.packFloatTable(infoTable,0,2)..sim.packInt32Table(infoTable,2,1))
end

function sysCall_init()
    signalName='infoSignal'
    local infos={sim.getSystemTimeInMs(-1),0,0} -- system time, simulation time, run state (0=stopped, 1=running, 2=paused)
    setSignal(signalName,infos)
end

function sysCall_cleanup()
    sim.clearStringSignal(signalName)
end

function sysCall_nonSimulation()
    local infos={sim.getSystemTimeInMs(-1),0,0}
    setSignal(signalName,infos)
end

function sysCall_sensing()
    local infos={sim.getSystemTimeInMs(-1),sim.getSimulationTime(),1}
    setSignal(signalName,infos)
end

function sysCall_suspend()
    local infos={sim.getSystemTimeInMs(-1),sim.getSimulationTime(),2}
    setSignal(signalName,infos)
end

function sysCall_beforeInstanceSwitch()
    sim.clearStringSignal(signalName)
end

function sysCall_afterInstanceSwitch()
    local infos={sim.getSystemTimeInMs(-1),0,0}
    setSignal(signalName,infos)
end
Then simply stream that small set of information.

Cheers

harrisonmg
Posts: 6
Joined: 16 Feb 2018, 17:53

Re: How to extract running time of VREP by remote API

Post by harrisonmg » 05 Mar 2018, 22:41

Thanks for your reply!

Using python, I've initiated streaming like so

Code: Select all

if vrep.simxGetIntegerParameter(self.client_id, vrep.sim_intparam_program_version, vrep.simx_opmode_streaming)[0] > 1:                             
    rospy.logerr('simxGetIntegerParameter has failed with a return code of {}'.format(return_code))
else:
    rospy.logwarn('V-Rep Clock Interface streaming dummy data.')
However every call of simxGetLastCmdTime returns a value of 0, including during a running simulation.

To rule out a client connection issue, here is my remoteApiConnections.txt

Code: Select all

portIndex1_port             = 19997
portIndex1_debug            = true
portIndex1_syncSimTrigger   = true
  
portIndex2_port             = 19998
portIndex2_debug            = true
portIndex2_syncSimTrigger   = true        
and both client connections (both occurring in different ROS nodes)

Code: Select all

client_id = vrep.simxStart('127.0.0.1', 19997, True, False, 2000, 5)
# and
client_id = vrep.simxStart('127.0.0.1', 19998, True, False, 2000, 5)
here is my attempt to call simxGetLastCmdTime and publish the clock as well

Code: Select all

while not rospy.is_shutdown():
    vrep_time = vrep.simxGetLastCmdTime(self.client_id)
    # check for consecutive duplicate time
    if vrep_time != self.last_time:
        rospy.loginfo(vrep_time)
        self.vrep_time_pub.publish(rospy.Time(vrep_time))
        self.last_time = vrep_time
Perhaps it is worth noting that both clients start with a client ID of 0. I am not certain as to how the remote server(s) would be able to discern clients in this case.

Thanks for your help!
Harrison

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

Re: How to extract running time of VREP by remote API

Post by coppelia » 06 Mar 2018, 09:35

You need to retrieve the streamed data, otherwise the last cmd time won't update. Like in following example:

Code: Select all

try:
    import vrep
except:
    print ('--------------------------------------------------------------')
    print ('"vrep.py" could not be imported. This means very probably that')
    print ('either "vrep.py" or the remoteApi library could not be found.')
    print ('Make sure both are in the same folder as this file,')
    print ('or appropriately adjust the file "vrep.py"')
    print ('--------------------------------------------------------------')
    print ('')

import time

print ('Program started')
vrep.simxFinish(-1)
clientID=vrep.simxStart('127.0.0.1',19997,True,True,5000,5)
if clientID!=-1:
    print ('Connected to remote API server')

    startTime=time.time()
    vrep.simxGetIntegerParameter(clientID, vrep.sim_intparam_program_version, vrep.simx_opmode_streaming) # Initialize streaming
    while time.time()-startTime < 5:
        vrep.simxGetIntegerParameter(clientID, vrep.sim_intparam_program_version, vrep.simx_opmode_buffer) # Needed, in order to update the last cmd time
        print ('Simulation time: ',vrep.simxGetLastCmdTime(clientID))
        time.sleep(0.005)

    vrep.simxGetIntegerParameter(clientID, vrep.sim_intparam_program_version, vrep.simx_opmode_discontinue) # Stop streaming
    # Before closing the connection to V-REP, make sure that the last command sent out had time to arrive. You can guarantee this with (for example):
    vrep.simxGetPingTime(clientID)

    # Now close the connection to V-REP:
    vrep.simxFinish(clientID)
else:
    print ('Failed connecting to remote API server')
print ('Program ended')
Also this will return a value different from 0 only if simulation is running.

Cheers

harrisonmg
Posts: 6
Joined: 16 Feb 2018, 17:53

Re: How to extract running time of VREP by remote API

Post by harrisonmg » 06 Mar 2018, 16:46

Now I see! All seems to work now. Thanks!

Post Reply