The main script
A main script is a simulation script. By default, each scene in CoppeliaSim will have one main script. It contains the basic code that allows a simulation to run. Without main script, a running simulation won't do anything. The main script contains functions that are appropriately called by the system. If a given function is not defined, the call will be ignored. Except for the initialization function, all other functions are optional. The default main script is typically segmented in 4 functions: Following is the typical main script, slightly simplified: function sysCall_init() -- Initialization part: sim.handleSimulationStart() sim.openModule(sim.handle_all) sim.handleGraph(sim.handle_all_except_explicit,0) end function sysCall_actuation() -- Actuation part: sim.resumeThreads(sim.scriptthreadresume_default) sim.resumeThreads(sim.scriptthreadresume_actuation_first) sim.launchThreadedChildScripts() sim.handleChildScripts(sim.syscb_actuation) sim.resumeThreads(sim.scriptthreadresume_actuation_last) sim.handleCustomizationScripts(sim.syscb_actuation) sim.handleAddOnScripts(sim.syscb_actuation) sim.handleSandboxScript(sim.syscb_actuation) sim.handleModule(sim.handle_all,false) sim.resumeThreads(2) sim.handleMechanism(sim.handle_all_except_explicit) sim.handleIkGroup(sim.handle_all_except_explicit) sim.handleDynamics(sim.getSimulationTimeStep()) end function sysCall_sensing() -- Sensing part: sim.handleSensingStart() sim.handleCollision(sim.handle_all_except_explicit) sim.handleDistance(sim.handle_all_except_explicit) sim.handleProximitySensor(sim.handle_all_except_explicit) sim.handleVisionSensor(sim.handle_all_except_explicit) sim.resumeThreads(sim.scriptthreadresume_sensing_first) sim.handleChildScripts(sim.syscb_sensing) sim.resumeThreads(sim.scriptthreadresume_sensing_last) sim.handleCustomizationScripts(sim.syscb_sensing) sim.handleAddOnScripts(sim.syscb_sensing) sim.handleSandboxScript(sim.syscb_sensing) sim.handleModule(sim.handle_all,true) sim.resumeThreads(sim.scriptthreadresume_allnotyetresumed) sim.handleGraph(sim.handle_all_except_explicit,sim.getSimulationTime()+sim.getSimulationTimeStep()) end function sysCall_cleanup() -- Clean-up part: sim.resetCollision(sim.handle_all_except_explicit) sim.resetDistance(sim.handle_all_except_explicit) sim.resetProximitySensor(sim.handle_all_except_explicit) sim.resetVisionSensor(sim.handle_all_except_explicit) sim.closeModule(sim.handle_all) end function sysCall_suspend() sim.handleChildScripts(sim.syscb_suspend) sim.handleCustomizationScripts(sim.syscb_suspend) sim.handleAddOnScripts(sim.syscb_suspend) sim.handleSandboxScript(sim.syscb_suspend) end function sysCall_suspended() sim.handleCustomizationScripts(sim.syscb_suspended) sim.handleAddOnScripts(sim.syscb_suspended) sim.handleSandboxScript(sim.syscb_suspended) end function sysCall_resume() sim.handleChildScripts(sim.syscb_resume) sim.handleCustomizationScripts(sim.syscb_resume) sim.handleAddOnScripts(sim.syscb_resume) sim.handleSandboxScript(sim.syscb_resume) end The main script is not supposed to be modified. The reason for this is following: one of CoppeliaSim's strength is that any model (robot, actuator, sensor, 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 sim.handleChildScripts 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 CoppeliaSim version introduces a neat command sim.doMagic(), 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 double-clicking the light-red script icon next to the world 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. Most commands in the main script behave or operate in a similar way. If we take for example the distance calculation functionality, we have in the regular part: Any new distance object will automatically be handled with above command (as long as it is not marked as explicit handling). The exact same mechanism is applied to collision detection, 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. The most important command in the main script is sim.handleChildScripts, which is called at two distinct locations, inside of the actuation function, and inside of the sensing function. Without this command, non-threaded child scripts will not be executed. If you look at the default main script, you can notice that the actuation function allows actuating or modifying the scene content (e.g. sim.handleIkGroup, sim.handleDynamics, etc.), while the sensing function allows sensing and probing the scene content (e.g. sim.handleCollision, sim.handleDistance, sim.handleProximitySensor, 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 sim.readProximitySensor) the proximity sensor's state from previous sensing (which happened at the end of previous simulation pass, inside of the main script, with sim.handleProximitySensor), then react to obstacles. If you need to explicitely handle a sensor, then make sure to always do it while in the sensing section, otherwise you might end-up with situations where the display appears wrong as illustrated in following figure: [Sensing - actuation - display sequence] As does the main script have an actuation and sensing function, so do non-threaded child scripts. Threaded child scripts on the other hand can be squeduled to run while the main script is in the actuation or sensing function, refer to the API function sim.setThreadResumeLocation. Recommended topics |