The main script
By default, each scene in V-REP will have one main script. The main script contains the basic code that allows a simulation to run. Without main script, a running simulation won't do anything. The default main script is segmented in 3 parts: Following is the typical main script: if (simGetSimulationState()==sim_simulation_advancing_firstafterstop) then -- Initialization part: simOpenModule(sim_handle_all) simHandleMechanism(sim_handle_all_except_explicit) simHandleIkGroup(sim_handle_all_except_explicit) simHandleMill(sim_handle_all_except_explicit) simHandleCollision(sim_handle_all_except_explicit) simHandleDistance(sim_handle_all_except_explicit) simHandleProximitySensor(sim_handle_all_except_explicit) simHandleVisionSensor(sim_handle_all_except_explicit) simHandleGraph(sim_handle_all_except_explicit,simGetSimulationTime()) end -- "Actuation"-part: simResumeThreads(1) simHandleChildScript(sim_handle_all_except_explicit) simHandleModule(sim_handle_all,false) simResumeThreads(2) simHandleJoint(sim_handle_all_except_explicit,simGetSimulationTimeStep()) simHandlePath(sim_handle_all_except_explicit,simGetSimulationTimeStep()) simHandleMechanism(sim_handle_all_except_explicit) simHandleIkGroup(sim_handle_all_except_explicit) simHandleDynamics(simGetSimulationTimeStep()) simHandleVarious() simHandleMill(sim_handle_all_except_explicit) -- "Sensing"-part: workThreadCount=simGetIntegerParameter(sim_intparam_work_thread_count) The main script is not supposed to be modified. The reason for this is following: one of V-REP's strength is that any model (robot, mechanism, etc) can be copied into a scene and is immediately operational. When modifying the main script, you run the risk that models won't perform as expected anymore (e.g. if your main script lacks the command simHandleChildScript(sim_handle_all_except_explicit) then all models copied into the scene won't operate at all). Another reason is that keeping a default main script allows old scenes to easily adjust for new functionality (e.g. if a new V-REP version introduces a neat command simDoMagic(), then old scenes will automatically be updated to have that command also automatically called in their main script). If however, for a reason or another you really need to modify the main script of a scene, you can do this by opening the script dialog [Menu bar --> Tools --> Scripts], then double-clicking the main script in the list. You can also open the main script by double-clicking the light-red script icon at the top of the scene hierarchy: [Main script icon] From the moment when you opened the main script, it will be marked as customized and won't be automatically updated anymore. Refer to the script editor section for more information on script edition. Most commands in the main script behave or operate in a similar way. If we take for example the collision detection functionality, we have in the regular part: Any new collision object will automatically be handled with above two commands (as long as it is not marked as explicit handling). The exact same mechanism is applied to minimum distance calculations, proximity sensor and vision sensor simulations, inverse kinematics, etc. This is a powerful mechanism that allows running simple simulations without writing a single line of code. Notice that in the restoration part of the main script, all command arguments have changed to sim_handle_all. If we consider the case of the collision detection module, the command is: simResetCollision(sim_handle_all). This will reset all collision objects without any exception (also those marked as explicit handling). The reason is that just before ending a simulation, we want to make sure that all collision objects are reset (some child scripts might not have been written correctly and might not reset their explicitely handled collision objects). This allows for a clean restoration of a scene to its initial state. The most important command in the main script is simHandleChildScript. Without this command no child script will be executed. Refer to script calling methodology for more information. A similar command is simHandleSensingChildScripts: if you look at the default main script and focus at the regular part, you can notice that the first section (the actuation section) allows actuating or modifying the scene content (e.g. simHandleChildScript, simHandleJoint, simHandlePath, simHandleMechanism, etc.), while the second section (the sensing or probing section) allows sensing and probing the scene content (e.g. simHandleCollision, simHandleDistance, simHandleProximitySensor, etc.). Following illustrates what happens in the default main script when a mobile robot equipped with a proximity sensor is simulated: [Default actuation - sensing - display sequence] With above's sequence in mind, child scripts will always read (with simReadProximitySensor) the proximity sensor's state from previous sensing (which happened at the end of previous simulation pass with simHandleProximitySensor), then react to obstacles. Sometimes above sequence doesn't work well. Suppose you have a 2D laser scanner model that should scan (i.e. measure) a series of directions then display detected points, and this in each simulation pass. The default proximity sensor handling in the main script (simHandleProximitySensor(sim_handle_all_except_explicit)) doesn't allow for such an automatic behavior. So the user could do the sensing in a child script in the actuation part. Following figure illustrates this way of doing: [Sensing - actuation - display sequence] The simulation would still run fine, however the display would appear biased: indeed, in above figure, the robot's proximity sensor clearly intersects with the obstacle, however the latter one does not seem to be detected (no detection point visible). To correct for this, one could modify the main script, but this is not recommended for compatibility reasons (e.g. you still want the 2D laser scanner model to be operating correctly when copy-pasted into a different scene). The solution in that case is to mark the child script that handles the sensing as to be executed in the sensing phase. Refer to the script dialog section. The main script code is of type pass through, this means that at each simulation step, the main script is executed, then control is returned to the simulator, without spending too much time in complicated loops. Recommended topics |