QT5 端
1.lib文件
打开路径 C:\Program Files\CoppeliaRobotics\CoppeliaSimEdu\programming\remoteApiBindings\lib
打开QT工程文件 remoteApiSharedLib.pro, release 编译后,将release文件中的以下文件拷贝出来
放在remoteAPI文件夹中(也可以是其它文件夹):
2.QT工程准备
QT5 新建工程, 在pro文件中添加:
win32: LIBS += -L'C:/Program Files/CoppeliaRobotics/CoppeliaSimEdu/programming/remoteApi/' -lremoteApi
INCLUDEPATH += 'C:/Program Files/CoppeliaRobotics/CoppeliaSimEdu/programming/common'
DEPENDPATH += 'C:/Program Files/CoppeliaRobotics/CoppeliaSimEdu/programming/common'
INCLUDEPATH += 'C:/Program Files/CoppeliaRobotics/CoppeliaSimEdu/programming/include'
DEPENDPATH += 'C:/Program Files/CoppeliaRobotics/CoppeliaSimEdu/programming/include'
INCLUDEPATH += 'C:/Program Files/CoppeliaRobotics/CoppeliaSimEdu/programming/remoteApi'
DEPENDPATH += 'C:/Program Files/CoppeliaRobotics/CoppeliaSimEdu/programming/remoteApi'
DEFINES += NON_MATLAB_PARSING
DEFINES += MAX_EXT_API_CONNECTIONS=255
3.CoppeliaSim准备
打开CoppeliaSim,从模型库中拖入ABB IRB140模型,在sysCall_init() 中添加
simExtRemoteApiStart(3000)
或
simRemoteApi.start(3000, 1300, false, false)
老版本的Vrep里有个函数是simExtRemoteApiStart(3000),代表打开19999这个端口。现在在CoppeliaSim的帮助文档中已经找不到了,但是仍可以使用。
新的启用服务器的函数为:
Lua synopsis
simRemoteApi.start(number portNumber,number maxPacketSize=1300,Boolean debug=false,Boolean preEnableTrigger=false)
Lua parameters
portNumber: port where to install the server service. Ports above 20000 are preferred. Negative port numbers can be specified in order to use shared memory, instead of socket communication.
maxPacketSize: the maximum size of a socket send-packet. Make sure to keep the value at 1300, unless the client side has a different setting.
debug: if true, a window will display the data traffic on that port.
preEnableTrigger: if true, the server service will be pre-enabled for synchronous trigger signals from the client.
4.测试通信
QT main 函数中添加
#include "mainwindow.h"
#include <QApplication>
#include <QtDebug>
extern "C" {
#include "extApi.h"
#include "extApiPlatform.h"
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
int clientID = simxStart("127.0.0.1", 3000, true, true, 5000, 5);
if (clientID != -1)
{
qDebug() << "success";
}
else
{
qDebug() << "error";
}
simxStartSimulation(clientID, simx_opmode_oneshot);
simxFinish(clientID);
return a.exec();
}
运行CoppeliaSim仿真,运行QT,若连接成功,则打印success。
QT端运行可能出现
[LspTrace]ReadAccConfig returns false!
解决办法:
- 以管理员身份运行命令符窗口。
- 输入netsh winsock reset,然后按下回车键。
- 出现提示,重启电脑即可。
VS2017端
1.lib文件
打开路径 C:\Program Files\CoppeliaRobotics\CoppeliaSimEdu\programming\remoteApiBindings\lib
使用VS2017打开remoteApiSharedLib-64.vcxproj工程, release编译,提示错误:
无法解析的外部符号
解决办法:
在工程属性 中:C/C++ >> 输出文件 >>对象文件名 把 $(IntDir) 修改为: $(IntDir)%(RelativeDir)
编译成功后会生成remoteApi.dll和remoteApiSharedLib-64.lib。
将dll文件路径添加到系统变量中,重启电脑。
2. VS2017配置
-
新建工程
-
进入属性管理器,菜单栏->视图->其他窗口->属性管理器,右击Debug|x64,新建属性表,这是为了以后新建工程直接复制属性表,无需再重新一步步操作了。
-
打开属性表,在VC++目录的库目录中添加remoteApiSharedLib-64.lib文件所在的目录
-
在C/C++ 常规 附加包含目录中添加三个目录
-
在预处理器的预处理器定义中添加以下内容
NON_MATLAB_PARSING MAX_EXT_API_CONNECTIONS=255
3.CoppeliaSim
配置同QT
4.测试通讯
VS main 文件如下
#include "pch.h"
#include <stdio.h>
#include <stdlib.h>
using namespace std;
extern "C" {
#include "extApi.h"
#include "extApiPlatform.h"
}
int main() {
int clientID = simxStart("127.0.0.1", 3000, true, true, 5000, 5);
if (clientID != -1) {
printf("success");
} else {
printf("error");
}
simxStartSimulation(clientID, simx_opmode_oneshot);
simxStopSimulation(clientID, simx_opmode_oneshot);
simxFinish(clientID);
return 0;
}
启动CoppeliaSim仿真,运行VS,窗口print出success则连接成功
感谢以下博文对我的帮助
V-rep与VS2017 C++通信环境配置,远程操控机械臂 http://www.luyixian.cn/news_show_187594.aspx
V-REP运动学仿真笔记(一)QT通过REMOTEAPI的交互 http://www.freesion.com/article/6532188633/
Qt 出现ReadAccConfig returns false!解决方法 https://blog.youkuaiyun.com/zxl_1996/article/details/86333945
V-REP通过C++程序控制仿真 https://blog.youkuaiyun.com/iamqianrenzhan/article/details/88777469