## Detecting ext. simulation stop in child script while loop

Typically: "How do I... ", "How can I... " questions
Wilsonator
Posts: 45
Joined: 25 Sep 2015, 19:10

### Detecting ext. simulation stop in child script while loop

Consider a child script in an infinite while loop in the 'sim_childscriptcall_actuation' :

Code: Select all

while(1) do
stuff
if(criteria) then
break
end
end

This simulation is currently run by a synchronous remote API started by vrep.simxStart(...).

Now take the situation where the remote API calls a stop using:

Code: Select all

 vrep.simxStopSimulation(clientID, vrep.simx_opmode_oneshot_wait)
vrep.simxFinish(clientID)

Is there a way to detect that the remote API synchronous simulation has called a stop command to Vrep when inside the 'while' loop in 'sim_childscriptcall_actuation'? I tried 'simGetSimulationState' for sim_simulation_advancing_lastbeforestop, sim_simulation_advancing_abouttostop, and sim_simulation_stopped but it didn't seem to detect that Vrep would be stopping that iteration.

Tobias
Posts: 21
Joined: 08 Jan 2016, 14:35

### Re: Detecting ext. simulation stop in child script while loo

HI,

I am not sure if I got you right but if you use something like:

Code: Select all

while simGetSimulationState()~=sim_simulation_advancing_abouttostop do
the loop should stop with your simulation.

Regards,
Tobi

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

### Re: Detecting ext. simulation stop in child script while loo

If your (infinite) loop is inside of a non-threaded child script, then the server side of the remote API would not be able to handle in-coming commands from the client. Also, the whole simulator will appear frozen.

Cheers

Wilsonator
Posts: 45
Joined: 25 Sep 2015, 19:10

### Re: Detecting ext. simulation stop in child script while loo

Is there a way to send a command or value from a remote API to an (infinite) while loop in a non-threaded child script?

I have tried sending signals (ie. simGetIntegerSignal) but they do not seem to update while in the while loop.

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

### Re: Detecting ext. simulation stop in child script while loo

This is not possible, since remote API commands will only be handled at the end/beginning of each simulation step. And child scripts are only running after/before that.

What do you want to achieve exactly? Did you have a look at simxCallScriptFunction?

Cheers

Wilsonator
Posts: 45
Joined: 25 Sep 2015, 19:10

### Re: Detecting ext. simulation stop in child script while loo

I have a simulation running synchronously from a remote API. I need all data and signals to be synchronously shared between a child script and the external script running the external API. Certain signals are being set by the external API before its call to vrep.simxSynchronousTrigger(clientID).

Just to be absolutely sure that all signals have reached V-Rep before the current steps calculations are executed (without having to call a time costly blocking function call such as vrep.simxGetPingTime(clientID) ), I wanted to send the next iteration number to V-Rep and check it compared to the previous iteration number. If the iteration number is incremented by one then the simulation moves forwards. While this synchronization may already theoretically have to happen, this is just my way of putting in a fail-safe bug check to ensure I don't get any funky behavior.

To do this I was running an infinite while loop that would check a string signal for the new iteration number. Of course it was not updating correctly in the while loop. Additionally since this is in a while loop, if the external simulation ends, there will be no increment and the child script gets stuck in the loop. Therefore I also wanted to check to see if the external simulation had called vrep.simxStopSimulation(...). But once again simxGetInMessageInfo() was not working correctly in the loop. If simulation stop command was detected I would 'break' and allow the simulation to end properly. Otherwise it just hangs in the child script.

I do have another solution, where I open a new TCP/IP socket between V-Rep and the Remote API script. Then as the remote API script ends I just send a stop commend through this socket, and in the while loop check for this occurrence and break the loop if it occurs. I was just looking for a cleaner solution using the V-Rep API!

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

### Re: Detecting ext. simulation stop in child script while loo

I think you can also do this with simxCallScriptFunction, since that call will execute a script function even if simulation is waiting for a trigger. In that function, you can then read other signals, set signals, etc., and also return data if you need. That function can be called in a blocking, one-shot call, but also in a streaming fashion.

Cheers