Vrep基础部分(三)

主要说一下两种子脚本的区别:多线程子脚本和单线程子脚本。

(1)单线程子脚本,它包含四部分:

the initialization function: sysCall_init. This part will be executed just one time (the first time the child script is called). This can be at the beginning of a simulation, but also in the middle of a simulation: remember that objects associated with child scripts can be copy/pasted into a scene at any time, also when a simulation is running. Usually you would put some initialization code as well as handle retrieval in this part.
the actuation function: sysCall_actuation. This part will be executed in each simulation step, during the actuation phase of a simulation step. Refer to the main script default code for more details about the actuation phase, but typically, you would do some actuation in this part (no sensing).
the sensing function: sysCall_sensing. This part will be executed in each simulation step, during the sensing phase of a simulation step. Refer to the main script default code for more details about the sensing phase, but typically, you would only do sensing in this part (no actuation).
the restoration function: sysCall_cleanup. This part will be executed one time just before a simulation ends, or before the script is destroyed.

即初始化函数、驱动函数、检测函数、恢复函数(恢复到初始状态)。仿真开始后,主脚本通过sim.handleChildScripts命令调用子脚本执行初始化函数,此函数在整个仿真过程中,只执行一次;然后,调用驱动函数和检测函数。每仿真一步,这两个函数会执行一次。恢复函数仅在仿真结束或者销毁前执行一次。

(2)多线程子脚本

多线程子脚本包含两部分:

the main part: this part will be executed when the thread starts, until shortly before the thread ends. This can be at the beginning of a simulation, but also in the middle of a simulation: remember that objects associated with child scripts can be copy/pasted into a scene at any time, also when a simulation is running. Usually you would put some initialization code as well as the main loop in this part: the code in the loop is in charge of handling a specific part of a simulation (e.g. handle the automatic sliding door). In above specific example, the loop is wasting precious computation time and is running asynchronously with the main simulation loop. See further down for a better example.
the restoration part: this part will be executed one time just before a simulation ends, or before the thread ends.

即主函数和恢复部分。仿真开始后,主脚本通过sim.launchThreadedChildScripts命令开启一个新的进程运行子脚本,当下一个采样周期到达时,当子脚本属性中未选择execute once且子脚本程序都运行完,才能再次启动该子脚本。

总结:在一般应用中,通常选单线程子脚本。

猜你喜欢

转载自blog.csdn.net/qq6689500/article/details/79990906