V-rep运动学仿真笔记(一)——qt通过remoteAPI的交互

本文详细介绍了如何使用V-rep进行运动学仿真实验,并通过Qt5进行remoteAPI的交互,包括编译remoteApi.lib库、配置Qt项目、实现与V-rep的连接及控制长方体旋转的示例代码。

V-rep运动学仿真具体使用及介绍大家可以参考如下:

1)官网:http://www.coppeliarobotics.com/index.html

2)博客:https://www.cnblogs.com/21207-iHome/tag/V-rep/

3)博客资料:http://www.360doc.com/content/19/0630/20/32181961_845820949.shtml

这里介绍通过qt5进行remoteAPI的交互的实验,首先win版本下载的v-rep没有remoteApi.lib,只有dll没有办法进行交互链接,需要自己编译,整体步骤如下;

1)在V-rep安装文件里找到:...\V-REP3\V-REP_PRO_EDU\programming\remoteApiBindings\lib;在里面找到如下:

双击打开,release编译后,生成的文件里,会有remoteApi.lib等如下文件:

3)测试,在新建qt文件里添加对应的库文件及include;pro文件参考如下,我对应文件在D盘,根据你的v-rep目录调整即可:

win32: LIBS += -L'D:/soft/V-REP3/V-REP_PRO_EDU/programming/remoteApiBindings/build-remoteApiSharedLib-Desktop_Qt_5_12_0_MSVC2015_64bit-Release/release/' -lremoteApi

INCLUDEPATH += 'D:/soft/V-REP3/V-REP_PRO_EDU/programming/common'
DEPENDPATH += 'D:/soft/V-REP3/V-REP_PRO_EDU/programming/common'

INCLUDEPATH += 'D:/soft/V-REP3/V-REP_PRO_EDU/programming/include'
DEPENDPATH += 'D:/soft/V-REP3/V-REP_PRO_EDU/programming/include'

INCLUDEPATH += 'D:/soft/V-REP3/V-REP_PRO_EDU/programming/remoteApi'
DEPENDPATH += 'D:/soft/V-REP3/V-REP_PRO_EDU/programming/remoteApi'

#下面这两句很重要
DEFINES += NON_MATLAB_PARSING
DEFINES += MAX_EXT_API_CONNECTIONS=255

然后在main文件里添加如下:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include<iostream>
extern "C" {
#include "extApi.h"
#include "extApiPlatform.h"
//#include "v_repConst.h"
#include "v_repLib.h"
}
using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    
    int clientID = simxStart("127.0.0.1", 19997, true, true, 5000, 5);
    if (clientID != -1)
    {
        printf("success");
    }
    else
    {
        printf("error");
    }
    simxStartSimulation(clientID, simx_opmode_oneshot);    
    simxFinish(clientID);
    
    return a.exec();
}

打开v-rep后,运行qt,出现succes即标识链接成功;

4)简单旋转控制,在文件里添加一个长方体,add>>primitive shape>>cuboid,在对话框中修改尺寸如下:

生成一个长方体后,在add>>joint>>revolute,然后把Revolute_joint移动到长方体一处;再添加一个平面:add>>primitive shape>>plane,直接在树状图里拖动对象,形成如下结构:

5)修改Revolute_joint动力学参数,双击Revolute_joint,弹出对话框,如下操作:

此时点击仿真,是不会转动的,如下:

6)在qt中添加main函数代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include<iostream>
extern "C" {
#include "extApi.h"
#include "extApiPlatform.h"
//#include "v_repConst.h"
#include "v_repLib.h"
}
using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    int clientID = simxStart("127.0.0.1", 19997, true, true, 5000, 5);
    if (clientID != -1)
    {
        printf("success");

        cout << "V-rep connected." << endl;
        simxInt Revolute_joint;//设置一个变量存储关节句柄
        if (simxGetObjectHandle(clientID, "Revolute_joint", &Revolute_joint, simx_opmode_blocking) == simx_return_ok)//获得关节句柄,若成功
        {
            // here we have the joint handle in variable jointHandle!
            cout << "Successfully got joint handle." << endl;

            simxFloat tar_velo = 10;//设置一个变量存储关节目标转速
            if (simxSetJointTargetVelocity(clientID, Revolute_joint, tar_velo, simx_opmode_blocking) == simx_return_ok)//设置关节目标转速
                cout << "target velocity: " << tar_velo << endl;
            else
                cout << "angle set failed." << endl;
        }
        else
            cout << "Geting object handle failed." << endl;
    }
    else
    {
        printf("error");
    }
    simxStartSimulation(clientID, simx_opmode_oneshot);
    simxFinish(clientID);

    return a.exec();
}

让v-rep在仿真状态,然后qt编译后运行即可看到如下:

长方体在按照设定速度旋转;

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值