V-rep学习笔记:外部函数调用方式
The remote API functions are interacting with V-REP via socket communication in a way that reduces lag and network load to a great extent. The remote API can let one or several external applications interact with V-REP.
A remote API function is called in a similar fashion as a regular API function, however with 2 major differences:
- most remote API functions return a similar value: a return code.
- most remote API functions require two additional argument: the operation mode, and the clientID (identifier returned by the simxStart function)
由于客户端和服务器使用socket套接字进行通信,因此就涉及到网络编程时常见的同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)等调用方式。The need for an operation mode and a specific return code comes from the fact that the remote API function has to travel via socket communication to the server (V-REP), execute a task, then return to the caller (the client). A naive (or regular) approach would be to have the client send a request, and wait until the server processed the request and replied: in most situations this would take too much time and the lag would penalize the client application. Instead, the remote API lets the user chose the type of operation mode and the way simulation advances by providing four main mechanisms to execute function calls or to control the simulation progress:
- Blocking function calls(a blocking function call is the naive or regular approach)
- Non-blocking function calls
- Data streaming
- Synchronous operation
1. Blocking function calls(阻塞调用)
这种方式在调用结果返回之前,当前线程会被挂起。函数只有在得到结果之后才会返回。比如下面代码获取物体句柄,操作模式采用阻塞模式simx_opmode_blocking(A command is sent to the server for execution, and the function waits for the reply from the server)
// Following function (blocking mode) will retrieve an object handle:
if (simxGetObjectHandle(clientID,"myJoint",&jointHandle,simx_opmode_blocking)==simx_return_ok)
{
// here we have the joint handle in variable jointHandle!
}
下图反映了阻塞调用的流程:
2. Non-blocking function calls(非阻塞调用)
非阻塞指在不能立刻得到结果之前,该函数不会阻塞当前线程,而会立刻返回。非阻塞调用适用于不需要服务端应答的场合(A non-blocking function call is meant for situations when we simply want to send data to V-REP without the need for a reply)。如下面例子,只是调用函数设置关节位置而不需要服务端返回数据,就可以使用非阻塞模式simx_opmode_oneshot。
// Following function (non-blocking mode) will set the position of a joint:
simxSetJointPosition(clientID,jointHandle,jointPosition,simx_opmode_oneshot);
下图反映了非阻塞调用的流程:

阻塞/非阻塞,是指程序在等待消息时的状态。简单理解为需要做一件事能不能立即得到应答返回。如果不能立即获得返回,需要等待,那就阻塞了;否则就可以理解为非阻塞。详细区别如下图所示:

有时为了同时发送多条指令给服务端响应,我们可以先暂停通信。In some situations, it is important to be able to send various data within a same message, in order to have that data also applied at the same time on the server side (e.g. we want the 3 joints of a robot to be applied to its V-REP model in the exact same time, i.e. in the same simulation step). In that case, the user can temporarily halt the communication thread in order to achieve this, as shown in following example:
simxPauseCommunication(clientID,); //Allows to temporarily halt the communication thread from sending data.
simxSetJointPosition(clientID,joint1Handle,joint1Value,simx_opmode_oneshot);
simxSetJointPosition(clientID,joint2Handle,joint2Value,simx_opmode_oneshot);
simxSetJointPosition(clientID,joint3Handle,joint3Value,simx_opmode_oneshot);
simxPauseCommunication(clientID,); // Above's 3 joints will be received and set on the V-REP side at the same time
Following diagram illustrates the effect of temporarily halting the communication thread:

3. Data streaming(数据流模式)
这一模式下,客户端可以向服务端发起请求连续的数据流。可以将其看做一种客户与服务端之间的命令/信息订阅模式(也可以类比模拟信号采集卡的连续采样功能:Analog Input在采样过程中每相邻两个采样点的时间相等,采集过程中不停顿,连续不间断的采集数据,直到用户主动停止采集任务)。客户端发起数据流请求及读取的代码如下所示:
// Streaming operation request (subscription) (function returns immediately (non-blocking)):
simxGetJointPosition(clientID,jointHandle,&jointPosition,simx_opmode_streaming); // The control loop:
while (simxGetConnectionId(clientID)!=-) // while we are connected to the server..
{
// Fetch the newest joint value from the inbox (func. returns immediately (non-blocking)):
if (simxGetJointPosition(clientID,jointHandle,&jointPosition,simx_opmode_buffer)==simx_return_ok)
{
// here we have the newest joint position in variable jointPosition!
}
else
{
// once you have enabled data streaming, it will take a few ms until the first value has arrived. So if
// we landed in this code section, this does not always mean we have an error!!!
}
} // Streaming operation is enabled/disabled individually for each command and
// object(s) the command applies to. In above case, only the joint position of
// the joint with handle jointHandle will be streamed.
simx_opmode_streaming: non-blocking mode. Similar to simx_opmode_oneshot, but with the difference that the command will be stored on the server side , continuously executed, and continuously sent back to the client. This mode is often used with "get-functions" (e.g. simxGetJointPosition), where the user requires a specific value constantly.
simx_opmode_buffer: non-blocking mode. No command is sent to the server, but just a reply to a same command, previously executed. This mode is often used in conjunction with the simx_opmode_streaming mode: first, a constant command execution is started with a streaming command, then only command replies fetched.
Following diagram illustrates data streaming:

当完成任务后,客户端需要主动发送请求停止数据流(otherwise the server will continue to stream unessesary data and eventually slow down)。使用simx_opmode_discontinue操作模式来停止数据流。simx_opmode_discontinue: non-blocking mode. This mode is used to interrupt streaming commands.
参考:
https://www.zhihu.com/question/19732473
V-rep学习笔记:外部函数调用方式的更多相关文章
- stm32学习笔记——外部中断的使用
stm32学习笔记——外部中断的使用 基本概念 stm32中,每一个GPIO都可以触发一个外部中断,但是,GPIO的中断是以组为一个单位的,同组间的外部中断同一时间只能使用一个.比如说,PA0,PB0 ...
- 树莓派学习笔记—— 源码方式安装opencv
0.前言 本文介绍怎样在树莓派中通过编译源码的方式安装opencv,并通过一个简单的样例说明怎样使用opencv. 很多其它内容请參考--[树莓派学习笔记--索引博文] 1.下载若干依 ...
- Hive 学习笔记(启动方式,内置服务)
一.Hive介绍 Hive是基于Hadoop的一个数据仓库,Hive能够将SQL语句转化为MapReduce任务进行运行. Hive架构图分为以下四部分. 1.用户接口 Hive有三个用户接口: 命令 ...
- Tornado学习笔记(三) 请求方式/状态码
本章我们来学习 Tornado 支持的请求方式 请求方式 Tornado支持任何合法的HTTP请求(GET.POST.PUT.DELETE.HEAD.OPTIONS).你可以非常容易地定义上述任一种方 ...
- Android学习笔记-listview实现方式之BaseAdapter
listview是Android开发中最为常用的组件,这里我们就学习一下用BaseAdapter的方式实现listview, 主布局activity_main.xml是这样的: <LinearL ...
- hibernate学习笔记6--Criteria查询方式、完整小练习(开发步骤)
一.Criteria查询方式没有sql语了,因此更加面向对象一些.Criteria是一种比HQL更面向对象的查询方式:Criteria的创建方式: Criteria c = s.createCrite ...
- Linux学习笔记4——函数调用栈空间的分配与释放
一.函数执行时使用栈空间作为自己的临时栈,3种方式决定编译器清空栈的方式:__stdcall. __fastcall.__cdecl 1.__stdcall表示每个调用者负责清空自己调用的函数的临时栈 ...
- AM335x(TQ335x)学习笔记——使用dtb方式启动内核
老式的u-boot使用ATAGS的方式启动linux内核,本文使用新式的dtb方式启动内核. 我使用的内核是linux-3.17.2版本,下面开始编译内核. (1) 解压内核 [php] view p ...
- Mybatis进阶学习笔记——动态代理方式开发Dao接口、Dao层(推荐第二种)
1.原始方法开发Dao Dao接口 package cn.sm1234.dao; import java.util.List; import cn.sm1234.domain.Customer; pu ...
随机推荐
- Jenkins 快速搭建
Jenkins 介绍 Jenkins 作为持续集成的重要工具,在DevOps整个工具链中有重要的地位.Jenkins 一般作为独立的应用运行在Java Servlet容器中如Jetty,也可以运行在其 ...
- information_schema系列七
information_schema系列七 这个系列的文章主要是为了能够让自己了解MySQL5.7的一些系统表,统一做一下备注和使用,也希望分享出来让大家能够有一点点的受益. 第七篇主要看一下一下 ...
- Statistical Artifact (error)
In natural science and signal processing, an artifact is any error in the perception or representati ...
- Spring Scheduler定时任务 + Quartz
原文地址: https://blog.csdn.net/revitalizing/article/details/61420556 版权声明:本文为博主原创文章,未经博主允许不得转载. https:/ ...
- go语言之进阶篇有缓冲channel
1.有缓冲channel 示例: 有缓存会阻塞,当读取完其中数值时,又可以写入. package main import ( "fmt" "time" ) f ...
- go语言之进阶篇error接口应用
1.error接口应用 示例: package main import "fmt" import "errors" func MyDiv(a, b int) ( ...
- C#中byte[] 转 double[] 或 int[] 或 struct结构体
方法:使用C#调用C++ memcpy实现各种参数类型的内存拷贝 using System.Runtime.InteropServices; public class GlbWSGridDataset ...
- 基于Deep Learning的中文分词尝试
http://h2ex.com/1282 现有分词介绍 自然语言处理(NLP,Natural Language Processing)是一个信息时代最重要的技术之一,简单来讲,就是让计算机能够理解人类 ...
- ueditor插入自定义内容和样式
UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点 通过UEditor提供的API接口可以很方便的读写操作内容并设置编辑器里的样式 页 ...
- Return array from functions in C++
C++ does not allow to return an entire array as an argument to a function. However, you can return a ...