## How to check if sim is paused/running/stopped (remote API)

Typically: "How do I... ", "How can I... " questions
nettercm
Posts: 11
Joined: 22 Sep 2014, 15:04

### How to check if sim is paused/running/stopped (remote API)

My remote API client is running in synchronous mode. I like the fact that I can continue to use the V-REP UI to switch to different views etc. I also want to be able to pause the simulation at various points in time so that I can take time to inspect graphs etc.

When the simulation is paused or stopped, I don't really want my remote API client to continue running through the actual control logic. Instead, I just want it to wait until the user resumes or restarts the simulation.

What's the best way for my remote API client to become aware of the fact that the user has paused (or stopped) the simulation? When I pause or stop the simulation, I noticed that calls to simxSynchronousTrigger() fail. I also noticed that when the simulation is paused, simxGetLastCmdTime() is frozen at the previous value and when the simulation is stopped, simxGetLastCmdTime() goes to 0.

I suppose I can use the results from simxGetLastCmdTime() and simxSynchronousTrigger(), but if there is a recommended way, let me know.

Thanks.

Eric
Posts: 186
Joined: 11 Feb 2013, 16:39

### Re: How to check if sim is paused/running/stopped (remote AP

hi

have a look at simxGetInMessageInfo function, especially the simx_headeroffset_server_state

Cheers

Eric

mfloyd
Posts: 21
Joined: 29 Oct 2014, 23:06

### Re: How to check if sim is paused/running/stopped (remote AP

I'm trying to do the same and when I was using simxGetInMessageInfo, when going from paused to stopped, it always went to "play" for a few times while v-rep did some cleanup. The interval while "getting ready to stop" is happening can be significant when there is a lot of cleanup happening (I think this especially happens with the Playback tool). In lua scripts there is a way to check whether the simulation is getting ready to stop, but I don't seem to have this information available from the external c++ api.

I also found that the simxGetInMessageInfo seemed to not update when called frequently in a loop. In the following code, it would never leave the pause loop:

Code: Select all

	while (simxGetConnectionId(clientID) != -1)
{
int ret = 0;
int message = -1;
ret = simxGetInMessageInfo(clientID, simx_headeroffset_server_state, &message);
while ((message & 2) == 2)//if it has been paused
{
message = -1;
ret = simxGetInMessageInfo(clientID, simx_headeroffset_server_state, &message);
cout << "paused" << endl;
}
if ((message & 1) == 0)
{
cout << "stopped" << endl;
break;
}
cout << "running" << endl;
...
}

but would work if some other simx command was called between calls: e.g simxGetObjectPosition or simxGetIntegerParameter:

Code: Select all

	while (simxGetConnectionId(clientID) != -1)
{
int ret = 0;
int message = -1;
ret = simxGetInMessageInfo(clientID, simx_headeroffset_server_state, &message);
while ((message & 2) == 2)//if it has been paused
{
message = -1;
ret = simxGetInMessageInfo(clientID, simx_headeroffset_server_state, &message);
int paramIdentifier = sim_intparam_event_flags_read_clear;
int paramValue = 0;
ret = simxGetIntegerParameter(clientID, paramIdentifier, &paramValue, simx_opmode_oneshot_wait);
cout << "paused" << endl;
}
if ((message & 1) == 0)
{
cout << "stopped" << endl;
break;
}
cout << "running" << endl;
...
}

using simxGetIntegerParameter can get the run/stop state with frequent calls in a loop instead of simxGetInMessageInfo, but doesn't have pause info.

Is there a way to get the "getting ready to stop" info from the external api?
Why won't the simxGetInMessageInfo call update alone inside the loop without some other api function call?

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

### Re: How to check if sim is paused/running/stopped (remote AP

Hello,

yes, that is correct: the simxGetInMessageInfo will only retrieve the values of last received reply from the server. If the server didn't send any reply, then the values read with simxGetInMessageInfo will not be up-to-date. But the documentation is not clear about this, this will be corrected.

What you should do is start at least one streaming command, so that at least one reply will be received on a regular basis.
Maybe:

Code: Select all

int dummyValue;
simxGetIntegerParameter(clientID,sim_intparam_program_version,&dummyValue,simx_opmode_streaming);
Then, when going from pause to stop, simulation will shortly be running again (i.e. the simulation state won't go directly from paused to stopped), you can see this here.

Finally, if you want to intercept an aboutToStop message from the server, you could write a small non-threaded child script that would be in charge of that:

Code: Select all

if (sim_call_type==sim_childscriptcall_sensing) then
simSetIntegerSignal('simulationState',simGetSimulationState())
end
Then on the client side you could simply continuously stream that value and check the state.

By the way, if there is no threaded child script in the scene, the simulation state will go from paused, to running, to stopped, with only one running step. If you have threaded child scripts running, then the simulator will first wait to let the threads react by themselves to a stop command. If the threaded child scripts do not have that checking mechanism, V-REP will force the stop after 1-2 seconds.

Cheers