## Simulate multiple steps

Typically: "How do I... ", "How can I... " questions
Phlogiston90
Posts: 5
Joined: 12 Jun 2019, 16:37

### Simulate multiple steps

Hi,

I would like to simulate exactly 20 timesteps, using the legacy remote API. If I write a for loop around vrep.simxSynchronousTrigger, it takes quite long, probably because I have to send the trigger command 20 times to the V-rep server.
Therefore, I would like to write the for loop in a lua (child/customization)-script. However, there is no lua function like vrep.simxSynchronousTrigger.
I then found the simulation_stepper model in the model folder after it was suggested in a forum thread, but for me this only works if I press the UI button, but not if called from Python via vrep.simxCallScriptFunction (it returns, but doesn't move the objects in the GUI).

Any help would be much appreciated!

coppelia
Posts: 7918
Joined: 14 Dec 2012, 00:25

### Re: Simulate multiple steps

Hello,

if you want to step a simulation from within V-REP, then you will have to loop into the callback function sysCall_beforeMainScript used inside of a customization script, as was done in the simulation stepper tool.

That function will basically be called at the beginning of every simulationstep. If you return {doNotRunMainScript=true}, then the main script won't be called this time. THis is how you control the stepping.

So simply do something like:

Code: Select all

function stepSimulation(inInts,inFloats,inStrings,inBuffer)
runOneStep=true;
return {},{},{},''
end

function sysCall_beforeMainScript()
local retValue={doNotRunMainScript=runOneStep}
runOneStep=false
return retValue
end

and call stepSimulation from your remote API client.

Cheers

Phlogiston90
Posts: 5
Joined: 12 Jun 2019, 16:37

### Re: Simulate multiple steps

Thanks for your answer! But it doesn't work or I don't understand it.

So I have a Python program that does something like:

Code: Select all

for _ in range(20):
vrep.simxSynchronousTrigger(client_id)
return_code, ping_time = vrep.simxGetPingTime(client_id)

This is slow (~240ms), so I want to speed it up by calling a lua script function, where I can give 20 as a parameter and then it performs 20 steps. It does not have to be 20 steps but e.g. 20ms - I just want to have it deterministic.

So what I did now was to copy paste your code into a) my previously used remote_api customization script and b) as a new customization script attached to a dummy object 'Dummy'.

Both did not work, i.e. if I call

Code: Select all

return_code, _,_,_,_ = vrep.simxCallScriptFunction(client_id, 'Dummy', vrep.sim_scripttype_customizationscript,
'stepSimulation', [], [], [], bytearray(), vrep.simx_opmode_blocking)

it does not run any step: If I start the simulation with

Code: Select all

return_code = vrep.simxSynchronous(client_id, enable=True)
return_code = vrep.simxStartSimulation(client_id, vrep.simx_opmode_blocking)

it doesn't do anything and if I start it without vrep.simxSynchronous then the simulation runs without any halt.

Furthermore, I would like to have a for-loop in the customization script as written before, but I don't know how to add it to your example code.

Best regards,
Mark

coppelia
Posts: 7918
Joined: 14 Dec 2012, 00:25

### Re: Simulate multiple steps

With following in your customization script, you can run 20 simulation steps at a time:

Code: Select all

function sysCall_init()
stepsToRun=0
end

function start20StepRun()
stepsToRun=20
end

function sysCall_beforeMainScript()
return {doNotRunMainScript=(stepsToRun==0)}
end

function sysCall_actuation()
stepsToRun=stepsToRun-1
end

you just need to call start20StepRun to trigger the 20 steps. Since a customization script runs non-threaded, you can't make a for-loop to step through simulation steps.
Also, do not mix the remote API synchronous mode, an any synchronous mode you might create yourself (like in above example), otherwise you'll have two mechanisms blocking (and you'll have to activate two different triggers).

Cheers

Phlogiston90
Posts: 5
Joined: 12 Jun 2019, 16:37

### Re: Simulate multiple steps

Thank you a lot! This worked for me, and reduced the time needed to simulate 20 steps from 210ms to 10ms!

Best,
Mark