V-REP与C++初步通信测试
打开vrep,在上⽅操作栏到help选项打开,选择help topics。此时浏览器打开了vrep的操作⼿册user manual。
在user manual左侧⽬录中到writing code in and around V-REP,⼦⽬录选择V-REP API framework,⼦⽬录选择Remote API,从Enabling the Remote API - client side开始学习。
To use the remote API functionality in your C/C++ application, just include following C-language files in your project:
extApi.h
extApi.c
extApiPlatform.h (contains platform specific code)
extApiPlatform.c (contains platform specific code)
Above files are located in V-REP's installation directory, under programming/remoteApi.
把vrep安装⽬录内的programming/remoteApi⾥⾯所有头⽂件和源⽂件复制到C++⼯程⾥。
Make sure you have defined NON_MATLAB_PARSING and MAX_EXT_API_CONNECTIONS=255 (and optionally
DO_NOT_USE_SHARED_MEMORY) as a preprocessor definition.
预处理器定义NON_MATLAB_PARSING和MAX_EXT_API_CONNECTIONS=255。如果编译器有报错关于shared memory,那么还要定义DO_NOT_USE_SHARED_MEMORY,然后在⼯程中写⼀个空的main测试error,根据编译器的错误提⽰修改。
To enable the remote API on the client side (i.e. your application), call simxStart. See the bubbleRobClient project in the programming directory for an example. This page lists and describes all supported C/C++ remote API functions. V-REP remote API functions can easily be recognized from their "simx"-prefix.
调⽤函数simxStart以启动远程API。但⽬前做不到,点击这个函数可以查看其使⽤⽅法。vrep函数的特点就是都带有simx前缀。simxStart:
Description
Starts a communication thread with the server (i.e. V-REP). A same client may start several communication threads (but only one communication thread for a given IP and port). This should be the very first remote API function called on the client side. Make sure to start an appropriate remote API server service on the server side, that will wait for a connection. See also simxFinish. This is a remote API helper function.
启动与服务器的通信线程(即V-REP)。同⼀客户端可以启动多个通信线程(但只有⼀个通信线程⽤于给定的IP和端⼝)。这应该是客户端调⽤的第⼀个远程API函数。确保在服务器端启动适当的远程API服务器服务,该服务将等待连接。另见simxFinish。这是⼀个远程API 辅助函数。
C synopsis
simxInt simxStart(const simxChar* connectionAddress, simxInt connectionPort, simxUChar waitUntilConnected, simxUChar doNotReconnectOnceDisconnected, simxInt timeOutInMs,simxInt commThreadCycleInMs)
调⽤⽅式,基本上只需要关注connectionPort,这个是⾃定义的参数(端⼝号)。
C parameters
connectionAddress: the ip address where the server is located (i.e. V-REP)
直接使⽤字符串“127.0.0.1”即可
connectionPort: the port number where to connect. Specify a negative port number in order to use shared memory, instead of socket communication.
端⼝号在哪⾥连接。指定负端⼝号以使⽤共享内存,⽽不是套接字通信。端⼝号是⾃定义的。
waitUntilConnected: if different from zero, then the function blocks until connected (or timed out).
若⾮零,则功能将阻塞直到连接建⽴或超时。设置为true或1。
doNotReconnectOnceDisconnected: if different from zero, then the communication thread will not attempt a second connection if a connection was lost.
若⾮零,那么如果连接丢失,通信线程将不会尝试第⼆次连接。设置为true或1。
timeOutInMs:
if positive: the connection time-out in milliseconds for the first connection attempt. In that case, the tim
e-out for blocking function calls is 5000 milliseconds.
if negative: its positive value is the time-out for blocking function calls. In that case, the connection time-out for the first connection attempt is 5000 milliseconds.
若为正值:第⼀次连接尝试的连接超时(以毫秒为单位)。在这种情况下,阻塞函数调⽤的超时为5000毫秒。
若为负值:它的绝对值是阻塞函数调⽤的超时时间。在这种情况下,第⼀次连接尝试的连接超时为5000毫秒。
设置为-5000到5000内任意值。
进程间通信 共享内存commThreadCycleInMs: indicates how often data packets are sent back and forth. Reducing this number improves responsiveness, and a default value of 5 is recommended.
表⽰数据包来回发送的频率。减少此数字可提⾼响应速度,建议默认值为5。
C return value
the client ID, or -1 if the connection to the server was not possible (i.e. a timeout was reached). A call to simxStart should always be followed at the end with a call to simxFinish if simxStart didn't return -1.
客户端ID,如果⽆法连接到服务器,则返回-1(即达到超时)。如果simxStart没有返回-1,则应始终在调⽤simxFinish时调⽤simxStart。
返回,继续学习Enabling the Remote API - server side。只需关注⼀个内置函数simRemoteApi.start。调⽤⽅式也很简单,直接调⽤,并且只要⼀个参数,就是⾃定义的端⼝号。使⽤vrep⾃带场景就可以测试,这⾥使⽤tutorials\BubbleRob⾥⾯的,建议建⽴⼀个副本。打开这个场景,在左侧模块⽬录中双击bubbleRob右侧第⼀个⼩图标,在空⽩⾏调⽤这个函数simRemoteApi.start(20172)。(20172是
⾃定义的,建议端⼝号设为20000以上的值以免与其它进程冲突)
编写测试程序,直接写在主函数所在源⽂件:
#include<iostream>
#include"extApi.h"
void main()
{
  using namespace std;
  int Port = 20172;
  int clientID = simxStart("127.0.0.1", Port, 1, 1, 1000, 5);
  if (clientID != -1)
  {
    cout << "V-rep connected.";
    simxFinish(clientID);
  }
  else
  {
    cout << "V-rep can't be connected.";
  }
  ();
  return;
}
测试开始时,先在vrep界⾯运⾏仿真,然后再运⾏c++⼯程。命令窗⼝显⽰V-rep connected表⽰测试成功。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。