Vrep编程

本文详细介绍了V-REP中的六种编程方式及其应用场景,包括嵌入式脚本、自定义脚本、附加组件等,并对比了不同类型的子脚本的特点。此外,还提供了如何使用Lua编写脚本的建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Vrep scirpt间的简单区分

这有有一篇博客对各种脚本有更为详细的分析可以好好看一下。
Vrep scirpt介绍
这里写图片描述
While the regular API can be accessed from within the simulator (e.g. from an embedded script, an addon, a plugin or the main client application), the remote API, the ROS interfaces and the BlueZero interface can be accessed from almost any possible external application or hardware (including real robots, remotecomputers, etc.). The auxiliary API is not an interface per se, but more a collection of helper functions that can be embedded, and that operate on their own. The other interfaces item groups all possibilities for the user to extend the available interfaces. Above figure illustrates an overview of the various interfaces.

六种编程方式

an embedded script
an add-on
The ROS node
The BlueZero node
Plugin
remote API client

脚本

1.an embedded script:能够自定义仿真,例如一个场景或者模型
An embedded script is a script that is embedded in a scene (or model), i.e. a script that is part of the scene and that will be saved and loaded together with the rest of the scene (or model).

这里写图片描述

1.1Simulation scripts: simulation scripts are scripts that are executed only during simulation, and that are used to customize a simulation or a simulation model. The main simulation loop is handled via the main script, and models/robots are controlled via child scripts.
这里写图片描述

  • Associated scripts form the basis of V-REP’s distributed control architecture.

1.1.1The main script

这里写图片描述
A main script is a simulation script. By default, each scene in V-REP 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. 每一个simulation都需要一个main script.
主脚本包含以下几个函数:

  • the initialization function: sysCall_init
  • the actuation function: sysCall_actuation
  • the sensing function: sysCall_sensing
  • the restoration function: sysCall_cleanup

注意事项: 尽量不要修改主脚本,当你打开主脚本的那一刻,主脚本就变成自定义脚本了,不再能够自动更新。

1.1.2 Child scripts
child script 是一种仿真脚本。可以控制模型或者机器人。每一个scene都支持无数的子脚本。child scripts和scene objects是绑定在一起的。.You can attach a new child script to an object by selecting the object, then navigating to [menu bar –> Add –> Associated child script].
non-threaded child scriptthreaded child script 两种:
这里写图片描述
non-thread script是白色的,不像Customization scripts是全黑的。thread child scripts是绿色的
1.1.2.1 non-thread script
Non-threaded child scripts contain a collection of blocking functions. This means that every time they are called, they should perform some task and then return control. If control is not returned, then the whole simulation halts. Non-threaded child script functions are called by the main script twice per simulation step from the main script’s actuation and sensing functions. The system will also call child scripts when appropriate (e.g. during child script initialization, clean-up, or when a joint callback function or contact callback function is defined). This type of child script should always be chosen over threaded child scriptswhenever possible.(尽可能使用non-thread script)
1.1.2.2 thread script
Threaded child scripts are scripts tha**t will launch in a thread**. The launch of threaded child scripts is handled by the default main script code, via the sim.launchThreadedChildScripts function, in a cascaded way. When a threaded child script’s execution is still underway, it will not be launched a second time. When a threaded child script ended, it can be relaunched only if the execute once item in the script properties is unchecked. A threaded child script icon in the scene hierarchy is displayed in light blue instead of white to indicate that it will be launched in a thread.

Threaded child scripts have several weaknesses compared to non-threaded child scripts if not programmed appropriately: they are more resource-intensive, they can waste some processing time, and they can be a little bit less responsive to a simulation stop command.

1.2 Customization scripts: are embedded scripts that can be used to customize a simulation scene to a great extent. They are attached to (or associated with) scene objects.
它们是黑色的。所以很容易辨认。通过下图这个例子可以很容易了解什么是Customization script.
通过这个脚本,能够方便地了解什么是自定义脚本
– This is a customization script. It is intended to be used to customize a scene in
– various ways, mainly when simulation is not running. When simulation is running,
– do not use customization scripts, but rather child scripts if possible.
自定义脚本使用不同的方式自定义一个场景。主要是仿真不进行时才使用,正在仿真的时候,建议尽量使用child scripts.

属性:

  • they run non-threaded.
  • they are attached to (or associated with) scene objects (i.e. they are associated scripts). Associated scripts form the basis of V-REP’s distributed control architecture, and share the convenient property to be automatically duplicated if their associated object is duplicated.
    (关联的对象被复制的时候,关联的脚本也会自动被复制)

3.an add-on:
An add-on in V-REP is quite similar to a plugin: it is automatically loaded at program start-up, and allows V-REP’s functionality to be extended by user-written functionality or functions. Add-on scripts are written in Lua. Two types of add-ons are supported:

  • Add-on functions: add-on functions appear first in the add-on menu. They can be seen as
    functions that will be executed once, when selected by the user. Importers and exporters can
    conveniently be implemented with them.
  • Add-on scripts: add-on scripts are executed constantly while the simulator is running, effectively running in the background. They should only execute minimalistic code everytime they are called, since the whole application would otherwise slow down. Add-on scripts are called differently depending on the simulation state:

怎么使用c++或者python来写脚本
我们不能用python 或者c++来写脚本。但是我们可以使用 regular API来写插件。
Or you can write an external Python program, and use the remote API to interface with V-REP.
If you want to write a script, use Lua. It has a very simple syntax, easy to understand, and you can get the most of the job done by studying the code in the examples.

Access from unassociated code

Unassociated code is code that is not attached to any scene object. This includes all the code written for plugins, add-ons,
remote API clients, external ROS nodes, external BlueZero nodes, and the main script.

Access from associated code
Associated code is code that is associated with a scene object (i.e. attached to a scene object). This includes all the code written for child scripts or customization scripts.

add-on :

05-18
### CoppeliaSim (原名 V-REP) 仿真软件的功能介绍 CoppeliaSim 是一款功能强大的机器人仿真平台,支持多种编程语言和外部工具的集成。它的主要特点包括高度可定制化的场景构建能力以及与 MATLAB/Simulink 的无缝对接[^1]。 #### 主要功能模块 1. **图形化用户界面**: 用户可以通过直观的操作来设计和调整机器人的工作环境。例如,在主界面上可以轻松拖拽组件以搭建复杂的仿真场景[^1]。 2. **机器人模型库**: 提供丰富的预定义机器人模型,允许用户快速启动项目而不必从零开始建模[^2]。 3. **动态物理引擎**: 支持精确的动力学计算,能够模拟真实的力反馈、碰撞检测以及其他重要的物理特性[^4]。 4. **脚本编写支持**: 可通过 Lua 脚本来扩展系统的功能性,从而实现更复杂的行为逻辑控制. 5. **与其他工具链协作的能力**: - 和 MATLAB/Simulink 进行联合调试非常方便,这使得工程师能够在同一平台上完成算法开发至硬件验证全流程的工作流管理[^2]. - 同样也具备同 ROS 系统交互的可能性, 对于现代自动化系统而言至关重要[^3]. 以下是基于 MATLAB 控制机械臂绘制图案的一个简单例子: ```matlab function main() % 初始化客户端连接 simxFinish(-1); % 关闭所有现有的连接 clientID=simxStart('127.0.0.1',19999,true,true,5000,5); if(clientID ~= -1) disp('Connected to remote API server'); % 加载场景文件 res = simxLoadScene(clientID,'your_scene_path.ttt',simx_opmode_blocking); % 获取目标对象句柄... [errCode,jointHandle]=simxGetObjectHandle(clientID,... 'JointName',simx_opmode_blocking); % 设置关节位置命令... desiredPosition = pi / 4; simxSetJointTargetPosition(clientID,jointHandle,desiredPosition,simx_opmode_oneshot); % 断开连接前等待一段时间观察效果 pause(5); simxFinish(clientID); else error('Failed connecting to remote API server!'); end end ``` 此代码片段展示了如何建立到服务器端口 `19999` 上运行的服务程序链接,并加载指定路径下的 `.ttt` 场景文件之后操作某个特定名称关节的角度设定过程[^2]。 ### 安装步骤概述 为了正确设置您的开发环境,请遵循以下通用流程: 1. 前往官方网站下载最新稳定版或者历史版本中的任意一个满足您需求的选择项; 2. 解压压缩包后按照提示执行安装向导直至结束; 3. 如果计划结合 ROS 使用,则需额外配置 RosInterface 插件部分参数以便双方顺利通讯交流数据信息[^3];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值