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且子脚本程序都运行完,才能再次启动该子脚本。

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

 

 

V-REP(Virtual Robot Experimentation Platform)是一款用于机器人仿真的开源软件平台。它提供了一个可视化的环境,可以模拟各种机器人和传感器,并且支持多种编程语言进行控制,其中包括Python。 V-REP的Python接口允许用户使用Python编写控制代码,与V-REP进行通信并控制仿真场景中机器人。通过Python接口,你可以实现机器人的运动控制、传感器数据的读取和处理、路径规划等功能。 使用V-REP的Python接口,你可以通过以下步骤来开始使用: 1. 安装V-REP软件:你可以从官方网站下载并安装V-REP软件。 2. 导入vrep模块:在Python代码中,你需要导入vrep模块来使用V-REP的功能。可以使用以下代码导入vrep模块: ```python import vrep ``` 3. 连接到V-REP:使用以下代码连接到V-REP仿真环境: ```python clientID = vrep.simxStart('127.0.0.1', 19997, True, True, 5000, 5) ``` 这里的参数'127.0.0.1'表示连接到本地主机,19997是V-REP的默认端口号。函数返回一个clientID,用于后续的通信。 4. 控制机器人:通过调用V-REP提供的函数,你可以控制机器人的运动、读取传感器数据等。例如,可以使用以下代码获取机器人的位置: ```python errorCode, position = vrep.simxGetObjectPosition(clientID, robotHandle, -1, vrep.simx_opmode_blocking) ``` 这里的robotHandle是机器人在V-REP中的句柄,position是一个包含机器人位置的列表。 5. 断开连接:在完成操作后,记得使用以下代码断开与V-REP的连接: ```python vrep.simxFinish(clientID) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值