V-rep 使用RemoteApi交互C++程序

V-rep是如何通过remoteAPI的交互,只需要一行代码simExtRemoteApiStart()。

以ABB为例,在新场景中直接从现有模型中拖出abb的模型。

1.1 ABB 机械臂

在Scene窗口中Abb元素的右边有一个小本子的标志,双击打开编译脚本。

1.2 程序界面

里面的代码已经编译好啦,V-rep的代码示弱语言,内容主要分为几大块,以if(sim_call_type == xxxx)then  .... end 为分界。

 表示仿真开始,仿真中,仿真暂停,仿真结束等等不同的状态。在不同的状态里写入相应的指令就好啦。

而初始化调用RemoteApi需要在初始化过程中,调用simExtRemoteApiStart(3000)。 其中3000表示端口号。端口号只要不与已有进程冲突就可以了,具体可以查询端口使用的列表。

 

 1.3 添加RemoteApi

这里我们通过C++连接V-rep。首先我们先找到v-rep的根目录里的programming引入include和RemoteApi两个文件夹。想了解更多可以以bubbleRobClient中的例程为例。

我们这里在VS中创建一个自己的C++控制台程序。

这里要添加预处理:

WIN32
NDEBUG
_CONSOLE
NON_MATLAB_PARSING
MAX_EXT_API_CONNECTIONS=255zasda

 

 

 

注意添加附加包含目录,要包括inclue和remoteapi两个文件夹,添加头文件

#include <stdio.h>
#include <stdlib.h>

extern "C" {
#include "extApi.h"

}

其中Port是之前定下的端口号:3000。

使用simxStart函数开启RemoteApi的通信。返回值int,判断是否能够连接。

使用simxGetObjectHandle函数添加需要读取的ObjectHandle。

使用simxGetObjectPosition函数获得handle的位置信息。

 

例程如下:

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

extern "C" {
#include "extApi.h"
}


int main()
{
	int Port = 3000;
	int PositionControlHandle;
	simxChar* Adresse = "127.0.0.1";
	float position[3];


	int clientID = simxStart(Adresse, Port, true,true,2000,5);

	if (clientID != -1) 
	{
		printf("V-rep connected.");
		extApi_sleepMs(300);
		while (simxGetConnectionId(clientID) != -1)
		{
			simxGetObjectHandle(clientID,"IRB140_manipulationSphere", &PositionControlHandle, simx_opmode_oneshot);
			simxGetObjectPosition(clientID,PositionControlHandle,-1,position, simx_opmode_oneshot);
			printf("(%f,%f,%f)\r\n",position[0],position[1],position[2]);
		}

		simxFinish(clientID);
	}
	else {
		printf("V-rep can't be connected.");
		extApi_sleepMs(300);
	}

    return 0;
}

  

更多函数用法及参数见v-rep使用手册,RemoteApi中C++部分及Server部分介绍。

posted @ 2017-11-16 14:15  大G霸  阅读(6060)  评论(1编辑  收藏  举报