系统相关
首页 > 系统相关> > Ubuntu16 基于qt C++与vrep/coppeliasim通讯

Ubuntu16 基于qt C++与vrep/coppeliasim通讯

作者:互联网

Ubuntu16 基于qt C++与vrep/coppeliasim通讯

1.系统环境和软件

2.安装支撑库

$ wget -O boost_1_65_1.tar.gz https://sourceforge.net/projects/boost/files/boost/1.65.1/boost_1_65_1.tar.gz/download
$ tar xzvf boost_1_65_1.tar.gz
$ cd boost_1_65_1/
$ ./bootstrap.sh --prefix=/usr/
$ ./b2
$ sudo ./b2 install
$ sudo apt-get install build-essential libgl1-mesa-dev
$ sudo apt-get install libzmq3-dev

3.将支撑文件和库添加到工程

4.将支撑文件和库引用到工程中

INCLUDEPATH += $$PWD/FPR_Simulator/include
INCLUDEPATH += $$PWD/FPR_Simulator/include/bindings
INCLUDEPATH += $$PWD/FPR_Simulator/include/include
LIBS += -L$$PWD/FPR_Simulator/dependents/ -lb0
SOURCES += FPR_Simulator/src/b0RemoteApi.cpp

5.打开vrep端的scene

6.测试通讯

使用如下代码替换工程的主函数进行测试:

#include "b0RemoteApi.h"

bool doNextStep=true;
bool runInSynchronousMode=true;
int sens1,sens2;
b0RemoteApi* cl=NULL;

void simulationStepStarted_CB(std::vector<msgpack::object>* msg)
{
    float simTime=0.0;
    std::map<std::string,msgpack::object> data=msg->at(1).as<std::map<std::string,msgpack::object>>();
    std::map<std::string,msgpack::object>::iterator it=data.find("simulationTime");
    if (it!=data.end())
        simTime=it->second.as<float>();
    std::cout << "Simulation step started. Simulation time: " << simTime << std::endl;
}

void simulationStepDone_CB(std::vector<msgpack::object>* msg)
{
    float simTime=0.0;
    std::map<std::string,msgpack::object> data=msg->at(1).as<std::map<std::string,msgpack::object>>();
    std::map<std::string,msgpack::object>::iterator it=data.find("simulationTime");
    if (it!=data.end())
        simTime=it->second.as<float>();
    std::cout << "Simulation step done. Simulation time: " << simTime << std::endl;
    doNextStep=true;
}

void image_CB(std::vector<msgpack::object>* msg)
{
    std::cout << "Received image." << std::endl;
    std::string img(b0RemoteApi::readByteArray(msg,2));
    cl->simxSetVisionSensorImage(sens2,false,img.c_str(),img.size(),cl->simxDefaultPublisher());
}

void stepSimulation()
{
    if (runInSynchronousMode)
    {
        while (!doNextStep)
            cl->simxSpinOnce();
        doNextStep=false;
        cl->simxSynchronousTrigger();
    }
    else
        cl->simxSpinOnce();
}

int main(int argc,char* argv[])
{
    b0RemoteApi client("b0RemoteApi_c++Client","b0RemoteApi");
    cl=&client;

    client.simxAddStatusbarMessage("Hello world!",client.simxDefaultPublisher());
    std::vector<msgpack::object>* reply=client.simxGetObjectHandle("VisionSensor",client.simxServiceCall());
    sens1=b0RemoteApi::readInt(reply,1);
    reply=client.simxGetObjectHandle("PassiveVisionSensor",client.simxServiceCall());
    sens2=b0RemoteApi::readInt(reply,1);

    if (runInSynchronousMode)
        client.simxSynchronous(true);

    client.simxGetSimulationStepStarted(client.simxDefaultSubscriber(simulationStepStarted_CB));
    client.simxGetSimulationStepDone(client.simxDefaultSubscriber(simulationStepDone_CB));
    client.simxGetVisionSensorImage(sens1,false,client.simxDefaultSubscriber(image_CB));
//    client.simxGetVisionSensorImage(sens1,false,client.simxCreateSubscriber(image_CB,1,true));
    client.simxStartSimulation(client.simxDefaultPublisher());

    unsigned long st=client.simxGetTimeInMs();
    while (client.simxGetTimeInMs()<st+5000)
        stepSimulation();

    client.simxStopSimulation(client.simxDefaultPublisher());
    std::cout << "Ended!" << std::endl;
    return(0);
}

运行成功会看到vrep端自动开启了仿真,并且两个框内出现了图像。此时代表C++端和vrep的通讯建立成功,可以利用其他api去获取handle,然后控制自己的模型,进而达到仿真的目的。

7.自己的vrep模型

8.参考

[1] https://www.coppeliarobotics.com/helpFiles/en/b0RemoteApiOverview.htm
[2] https://www.youtube.com/watch?v=9lOLyM5siTw

标签:std,b0RemoteApi,qt,vrep,C++,client,CoppeliaSim,include
来源: https://blog.csdn.net/zztt111/article/details/118707593