专栏首页机器人课程与技术CoppeliaSim(V-Rep)和ROS2的使用说明

CoppeliaSim(V-Rep)和ROS2的使用说明

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/ZhangRelay/article/details/103353528

这篇教程简要说明,在CoppeliaSim仿真环境中,使用ROS2接口,如果是ROS 2 Dashing,直接使用安装包中的compiledRosPlugins文件夹下的libsimExtROS2Interface.so。

如果使用其他版本的ROS 2,需要手动编译生成.so文件,否则会出错。当然也支持ROS 1参考之前博文。

重新编译ROS 2之CoppeliaSim插件的过程如下所示:

下载simExtROS2Interface,这个功能包在编译成功后会生成.so文件。

更多案例参考示例和controlTypeExamples下文件

所有文件需要复制到如ros2_ws/src的文件夹下,然后依次输入下面指令:

$ export COPPELIASIM_ROOT_DIR=~/path/to/coppeliaSim/folder
$ ulimit -s unlimited #otherwise compilation might freeze/crash
$ colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=Release -DLIBPLUGIN_DIR=$COPPELIASIM_ROOT_DIR/programming/libPlugin

OK,如上是重新编译的全部过程。

启动CoppeliaSim的指令如下:

$ ./coppeliaSim.sh

会发现如下接口启动,说明ROS 2接口一切正常。

无需担心warning……

开启一个案例玩耍起来(ros2InterfaceTopicPublisherAndSubscriber.ttt):

使用终端查看一下消息:

添加一个仿真时间:

选择sphere,添加子代码如下:

function subscriber_callback(msg)
    -- This is the subscriber callback function
    sim.addStatusbarMessage('subscriber receiver following Float32: '..msg.data)
end

function getTransformStamped(objHandle,name,relTo,relToName)
    -- This function retrieves the stamped transform for a specific object
    t=sim.getSystemTime()
    p=sim.getObjectPosition(objHandle,relTo)
    o=sim.getObjectQuaternion(objHandle,relTo)
    return {
        header={
            stamp=t,
            frame_id=relToName
        },
        child_frame_id=name,
        transform={
            translation={x=p[1],y=p[2],z=p[3]},
            rotation={x=o[1],y=o[2],z=o[3],w=o[4]}
        }
    }
end

function sysCall_init()
    -- The child script initialization
    objectHandle=sim.getObjectAssociatedWithScript(sim.handle_self)
    objectName=sim.getObjectName(objectHandle)
    ros2InterfacePresent=simROS2

    -- Prepare the float32 publisher and subscriber (we subscribe to the topic we advertise):
    if ros2InterfacePresent then
        publisher=simROS2.advertise('/simulationTime','std_msgs/Float32')
        subscriber=simROS2.subscribe('/simulationTime','std_msgs/Float32','subscriber_callback')
    end
end

function sysCall_actuation()
    -- Send an updated simulation time message, and send the transform of the object attached to this script:
    if ros2InterfacePresent then
        simROS2.publish(publisher,{data=sim.getSimulationTime()})
        simROS2.sendTransform(getTransformStamped(objectHandle,objectName,-1,'world'))
        -- To send several transforms at once, use simROS2.sendTransforms instead
    end
end

function sysCall_cleanup()
    -- Following not really needed in a simulation script (i.e. automatically shut down at simulation end):
    if rosInterfacePresent then
        simROS.shutdownPublisher(publisher)
        simROS.shutdownSubscriber(subscriber)
    end
end

这时,可以使用:

$ ros2 topic echo /simulationTime

查看开启仿真的时间。

换一个案例(controlledViaRos2):

更多介绍,查看官方文档。使用版本为CoppeliaSim_Edu_V4_0_0_Ubuntu18_04。


附录(英文文档原文):

ROS 2 tutorial

This tutorial will try to explain in a simple way how you can manage to have CoppeliaSim ROS 2 enabled, based on ROS 2 Dashing.

First of all you should make sure that you have gone through the official ROS tutorials, at least the beginner section. Then, we assume that you have the latest Ubuntu running, that ROS is installed, and that the workspace folders are set. Here also refer to the official documentation regarding the ROS 2 installation.

The general ROS functionality in CoppeliaSim is supported via the ROS Interface (libsimExtROS2Interface.so). The Linux distribution should include that file already compiled in CoppeliaSim/compiledROSPlugins, but it first needs to be copied to CoppeliaSim/, otherwise it won't be loaded. You might however experience plugin load problems, depending on your system specificities: make sure to always inspect the terminal window of CoppeliaSim for details about plugin load operations. Plugins are loaded when CoppeliaSim is launched. Also make sure to source the ROS environment prior to running CoppeliaSim.

If the plugin cannot be loaded, then you should recompile it by yourself. It is open source and can be modified as much as needed in order to support a specific feature or to extend its functionality. If specific messages/services/etc. need to be supported, make sure to edit files located in simExtROSInterface/meta/, prior to recompilation. There are 2 packages:

  • simExtROS2Interface: this package is the ROS 2 Interface that will be compiled to a ".so" file, and that is used by CoppeliaSim.
  • ros2_bubble_rob: this is the package of a very simple robot controller that connects to CoppeliaSim via the ROS Interface. This node will be in charge of controlling the red robot in the demo scene controlTypeExamples/controlledViaRos.ttt

Above packages should be copied to your ros2_ws/src folder.

In order to build the packages, navigate to the ros2_ws folder and type:

$ export COPPELIASIM_ROOT_DIR=~/path/to/coppeliaSim/folder
$ ulimit -s unlimited #otherwise compilation might freeze/crash
$ colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=Release -DLIBPLUGIN_DIR=$COPPELIASIM_ROOT_DIR/programming/libPlugin

That's it! The packages should have been generated and compiled to an executable or library. Copy and paste the created files to the CoppeliaSim installation folder. The plugins are now ready to be used.

Now open a terminal, move to the CoppeliaSim installation folder and start CoppeliaSim. This is what you should have (or similar):

$ ./coppeliaSim.sh
...
Plugin 'ROS2Interface': loading...
Plugin 'ROS2Interface': load succeeded.
...

Upon succesful ROS2 Interface load, checking the available nodes gives this:

$ ros2 node list
/sim_ros2_interface

In an empty CoppeliaSim scene, select an object, then attach a non-threaded child script to it with [Menu bar --> Add --> Associated child script --> non threaded]. Open the script editor for that script and replace the content with following:

function subscriber_callback(msg)
    -- This is the subscriber callback function
    sim.addStatusbarMessage('subscriber receiver following Float32: '..msg.data)
end

function getTransformStamped(objHandle,name,relTo,relToName)
    -- This function retrieves the stamped transform for a specific object
    t=sim.getSystemTime()
    p=sim.getObjectPosition(objHandle,relTo)
    o=sim.getObjectQuaternion(objHandle,relTo)
    return {
        header={
            stamp=t,
            frame_id=relToName
        },
        child_frame_id=name,
        transform={
            translation={x=p[1],y=p[2],z=p[3]},
            rotation={x=o[1],y=o[2],z=o[3],w=o[4]}
        }
    }
end

function sysCall_init()
    -- The child script initialization
    objectHandle=sim.getObjectAssociatedWithScript(sim.handle_self)
    objectName=sim.getObjectName(objectHandle)
    ros2InterfacePresent=simROS2

    -- Prepare the float32 publisher and subscriber (we subscribe to the topic we advertise):
    if ros2InterfacePresent then
        publisher=simROS2.advertise('/simulationTime','std_msgs/Float32')
        subscriber=simROS2.subscribe('/simulationTime','std_msgs/Float32','subscriber_callback')
    end
end

function sysCall_actuation()
    -- Send an updated simulation time message, and send the transform of the object attached to this script:
    if ros2InterfacePresent then
        simROS2.publish(publisher,{data=sim.getSimulationTime()})
        simROS2.sendTransform(getTransformStamped(objectHandle,objectName,-1,'world'))
        -- To send several transforms at once, use simROS2.sendTransforms instead
    end
end

function sysCall_cleanup()
    -- Following not really needed in a simulation script (i.e. automatically shut down at simulation end):
    if rosInterfacePresent then
        simROS.shutdownPublisher(publisher)
        simROS.shutdownSubscriber(subscriber)
    end
end

Above script will publish the simulation time, and subscribe to it at the same time. It will also publish the transform of the object the script is attached to. You should be able to see the simulation time topic with:

$ ros2 topic list

To see the message content, you can type:

$ ros2 topic echo /simulationTime

Now load the demo scene ros2InterfaceTopicPublisherAndSubscriber.ttt, and run the simulation. The code in the child script attached to Vision_sensor will enable a publisher to stream the vision sensor's image, and also enable a subscriber to listen to that same stream. The subscriber applies the read data to the passive vision sensor, that is only used as a data container. So CoppeliaSim is streaming data, while listening to the same data! This is what is happening:

[Image publisher and image subscriber demo]

Try experimenting a little bit with the code. You can also visualize the image that CoppeliaSim streams with following command:

$ ros2 run image_view image_view image:=/visionSensorData

Had you been streaming simpler data, then you could also have visualized it with:

$ ros2 topic echo /visionSensorData

Now stop the simulation and load the demo scene controlTypeExamples/controlledViaRos2.ttt, and run the simulation. The robot is simplistic, and also behaving in a simplistic way for simplification purposes. It is controlled via the ROS2 Interface:

[External client application controlling the robot via ROS]

The child script attached to the robot, and running in a non-threaded fashion, is in charge of following:

  • determine some object handles (e.g. motor joint handles and proximity sensor handle)
  • verify if the ROS2 Interface is loaded
  • Launch motor speed subscribers
  • Launch a sensor publisher and a simulation time publisher
  • and finally launch a client application. The application is called with some topic names as arguments, so that it will know which topics to listen to and to subscribe. The client application (ros2BubbleRob) is then taking over the control of the robot via ROS 2.

While simulation is running, copy and paste a few times the robot. Notice that every copy is directly operational and independent. This is one of the many strengths of CoppeliaSim.

Now stop the simulation and open a new scene, then drag following model into it: Models/tools/ros2Interface helper tool.ttm. This model is constituted by a single customization script that offers following topic publishers and subscribers:

  • startSimulation topic: can be used to start a simulation by publishing on this topic a std_msgs::Bool message.
  • pauseSimulation topic: can be used to pause a simulation by publishing on this topic a std_msgs::Bool message.
  • stopSimulation topic: can be used to stop a simulation by publishing on this topic a std_msgs::Bool message.
  • enableSyncMode topic: by publishing a std_msgs::Bool message on this topic, you can enable/disable the synchronous simulation mode.
  • triggerNextStep topic: by publishing a std_msgs::Bool message on this topic, you can trigger the next simulation step, while in the synchronous simulation mode.
  • simulationStepDone topic: a message of type std_msgs::Bool will be published at the end of each simulation pass.
  • simulationState topic: messages of type std_msgs::Int32 will be published on a regular basis. 0 indicates that the simulation is stopped, 1 that it is running and 2 that it is paused.
  • simulationTime topic: messages of type std_msgs::Float32 will be published on a regular basis, indicating the current simulation time.

Have a look at the content of the customization script, that can be fully customized for various purposes. Try generating topic messages from the command line, for instance:

$ ros2 topic pub /startSimulation std_msgs/Bool '{data: true}' --once
$ ros2 topic pub /pauseSimulation std_msgs/Bool '{data: true}' --once
$ ros2 topic pub /stopSimulation std_msgs/Bool '{data: true}' --once
$ ros2 topic pub /enableSyncMode std_msgs/Bool '{data: true}' --once
$ ros2 topic pub /startSimulation std_msgs/Bool '{data: true}' --once
$ ros2 topic pub /triggerNextStep std_msgs/Bool '{data: true}' --once
$ ros2 topic pub /triggerNextStep std_msgs/Bool '{data: true}' --once
$ ros2 topic pub /triggerNextStep std_msgs/Bool '{data: true}' --once
$ ros2 topic pub /stopSimulation std_msgs/Bool '{data: true}' --once

In order to display the current simulation time, you could type:

$ ros2 topic echo /simulationTime

Finally, make sure to have a look at the remote API functionality and the BlueZero framework in CoppeliaSim: similarly to ROS, it allows for remote function execution, fast data streaming back and forth, is quite simple to use, leightweight and cross-platform. The remote API functionality is available for 7 different languages. Both, the remote APi and the BlueZero framework can be interesting alternatives to ROS in some cases.


~Fin~


本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Ubuntu_ROS中应用kinect v2笔记

    http://www.ros.org/news/2014/09/microsoft-kinect-v2-driver-released.html

    zhangrelay
  • ROS机器人项目开发11例-ROS Robotics Projects(6)Matlab和Android

    书中,第8章主要介绍了ROS与Matlab和Android的接口,以及集成使用的方法。

    zhangrelay
  • ROS 2 Eloquent Elusor安装和使用汇总

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 ...

    zhangrelay
  • What is aspnet.config

    今天认真的看了一下1.1和2.0版本的Aspnet.config,发现非常的不同,也许是asp.net 2.0比1.1的修改非常大。在MSDN上也找不到相关的文...

    张善友
  • 在DataGrid中选择,确认,删除多行复选框列表

    在DataGrid中选择,确认,删除多行复选框列表 Selecting, Confirming & Deleting Mul...

    阿新
  • python 脚本生成为可执行文件

    你会发现dist下面只有一个可执行文件,这个单文件就可以发布了,可以运行在你正在使用的操作系统类似的系统的下面。

    Devops海洋的渔夫
  • DAY15:阅读CUDA C runtime之纹理内存

    GPUS Lady
  • kubernetes入门-概念篇

    Kubernetes is an open-source platform for automating deployment, scaling, and op...

    王磊-AI基础
  • 热迁移失败总结

    被热迁移的vm内存读写速度超过了内存同步的速度,让热迁移一直没办法完成内存在源节点和目的节点的同步。

    后端云
  • Dynamic Nginx Router... in Go!

    We needed a specialized load balancer at Nitro. After some study, Mihai Todor an...

    李海彬

扫码关注云+社区

领取腾讯云代金券