Simulation

Related API functions

A simulation in V-REP can be started, paused and stopped with [Menu bar --> Simulation --> Start/Pause/Stop simulation] or through the related toolbar buttons:

[Simulation start/pause/stop toolbar buttons]


Internally, the simulator will use additional intermediate states in order to correctly inform scripts or programs about what will happen next. Following state diagram illustrates the simulator's internal states:

[Simulation state diagram]


Scripts and programs should always take into account the current simulation state in order to behave correctly (refer to the simGetSimulationState API function). Each script or application should be segmented into 3 sections, where each section is conditionally run depending on the current simulation state:

  • Initialization section: the initialization section should be run only the first time the script or program is run. One would believe that monitoring the sim_simulation_advancing_firstafterstop state (see the state diagram here above) would be a good idea, however: remember, scene objects as well as child scripts can be copy/pasted into a scene at any time, even when a simulation is running. For that reason, instead of monitoring the sim_simulation_advancing_firstafterstop state, monitor the script execution count (simGetScriptExecutionCount): when zero, execute the initialization code. Threaded child scripts on the other hand do not need to monitor any state for the initialization: the initialization section is simply the first part of the code. Refer to the examples in the child script section for more information.
  • Simulation section: the simulation section should always be run and no specific state monitoring is required except for threaded child scripts. Threaded child scripts should make sure to end execution when the simulation state becomes sim_simulation_advancing_abouttostop. If not, the simulator will wait longer than needed before being able to end a simulation, and the restoration code of the threaded child script might not be run. Refer to the examples in the child script section for more information.
  • Restoration section: the restoration section should be run just before a simulation ends. This can be achieved by monitoring the sim_simulation_advancing_lastbeforestop state (see the state diagram here above). Threaded child scripts on the other hand do not need to monitor any state for the restoration: the restoration section is simply the last part of the code. Just make sure the threaded child script exits its main loop when the state becomes sim_simulation_advancing_abouttostop. Refer to the examples in the child script section for more information.
  • The simulator operates by advancing the simulation time at constant time steps. Following figure illustrates the main simulation loop:

    [Main simulation loop]


    Real-time simulation is supported by trying to keep the simulation time synchronized with the real time:

    [Real-time simulation loop]


    Following represents a very simplified main client application (messaging, plugin handling and other details have been omitted for clarity purpose):

    void initializationCallback
    {
    // do some initialization here } void loopCallback
    {
    if ( (simGetSimulationState()&sim_simulation_advancing)!=0 )
    {
    if ( (simGetRealTimeSimulation()!=1)||(simIsRealTimeSimulationStepNeeded()==1) )
    {
    if ((simHandleMainScript()&sim_script_main_script_not_called)==0)
    simAdvanceSimulationByOneStep();
    }
    }
    } void deinitializationCallback
    {
    // do some clean-up here }

    Depending on the simulation complexity, performance of the computer and simulation settings, real-time simulation might not always be possible.

    In non real-time simulations, the simulation speed (i.e. the perceived speed) is mainly dependent on two factors: the simulation time step and the number of simulation passes for one rendering pass (see the simulation dialog for more details). In the case of a real-time simulation, the simulation speed mainly depends on the real-time multiplication coefficient, but also to a certain degree of the simulation time step (a too small simulation time step might not be compatible with the real-time character of a simulation because of the limited calculation power of the computer). During simulation, the simulation speed can be adjusted with following toolbar buttons:

    [Simulation speed adjustment toolbar buttons]


    The simulation speed is adjusted in a way so that the initial simulation time step is never increased (because this might have as consequence the breaking of a mechanism for example). Following two figures illustrate the simulation speed adjustment mechanisms:

    [Simulation speed adjustment mechanism for non real-time simulations]


    [Simulation speed adjustment mechanism for real-time simulations]


    Recommended topics

  • Simulation settings dialog
  • Scripts
  • The main client application
  • Plugins