$ 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
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
使用如下代码替换工程的主函数进行测试:
#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,然后控制自己的模型,进而达到仿真的目的。
对于自己的模型(scene),只需要将scene打开,然后将如下文件拖拽并释放到scene里就行了:
CoppeliaSim/models/tools/B0 remote Api server.ttm
C++的api:
https://www.coppeliarobotics.com/helpFiles/en/b0RemoteApi-cpp.htm
[1] https://www.coppeliarobotics.com/helpFiles/en/b0RemoteApiOverview.htm
[2] https://www.youtube.com/watch?v=9lOLyM5siTw