V-Rep:RosInterface

启用RosInterface

http://www.coppeliarobotics.com/helpFiles/en/rosTutorialIndigo.htm

  1. 打开终端执行roscore
  2. 启动vrep

当在终端看到下列代码即表示成功加载RosInterface插件

Plugin 'RosInterface': loading...
Plugin 'RosInterface': load succeeded.

并且在终端输入以下命令应看到可用节点如下:

$ rosnode list
/rosout
/vrep_ros_interface

若没有成功加载,执行以下步骤:

  1. 在vrep安装目录下搜索libv_repExtRosInterface.so文件
  2. ibv_repExtRosInterface.so文件放到安装目录的根目录即可

官方例程

可自行参阅
http://www.coppeliarobotics.com/helpFiles/en/rosTutorialIndigo.htm
应注意,在需要发布topic或者trasform等等有时间栈的东西时,应在前添加判断ROSInterface是否在该时间点运行
if simROS then
且应尽量使用sim.getSystemTime()输出时间栈,否则使用‘’‘simROS.getTime()```会使时间不一致(利用的ros::Time::now())
TODO:翻译例程步骤

API

主要使用一下API:

-- 发布器初始化
pub=simROS.advertise('/image', 'sensor_msgs/Image')
-- 订阅器初始化
sub=simROS.subscribe('/image', 'sensor_msgs/Image', 'imageMessage_callback')

-- 发布节点
-- data为tables/array数据类型,亦为所需要发布的数据
simROS.publish(pub,data)

类比ros

pub=n.advertise<sensor_msgs::Image>("/image",1);

sub=n.subscriber("/image",1,imageMessage_callback);

pub.publish(data);

可以从官方例程rosInterfaceTopicPublisherAndSubscriber.ttt里得代码,了解具体的使用方法

-- This illustrates how to publish and subscribe to an image using the ROS Interface.
-- An alternate version using image transport can be created with following functions:
--
-- simROS.imageTransportAdvertise
-- simROS.imageTransportPublish
-- simROS.imageTransportShutdownPublisher
-- simROS.imageTransportShutdownSubscriber
-- simROS.imageTransportSubscribe

function sysCall_init()
        
    -- Get some handles:
    -- 转臂上的摄像头
    activeVisionSensor=sim.getObjectHandle('Vision_sensor')
    -- 世界上的摄像头
    passiveVisionSensor=sim.getObjectHandle('PassiveVision_sensor')
    
    -- Enable an image publisher and subscriber:
    -- 类似pub=n.advertise<sensor_msgs::Image>("/image",1);
    pub=simROS.advertise('/image', 'sensor_msgs/Image')
    -- 对发布器进行格式转换,使得数组转换为字符串,使通讯更快
    simROS.publisherTreatUInt8ArrayAsString(pub) -- treat uint8 arrays as strings (much faster, tables/arrays are kind of slow in Lua)
    -- 类似sub=n.subscriber("/image",1,imageMessage_callback);
    sub=simROS.subscribe('/image', 'sensor_msgs/Image', 'imageMessage_callback')
    -- 对发布器进行格式转换,使得数组转换为字符串,使通讯更快
    simROS.subscriberTreatUInt8ArrayAsString(sub) -- treat uint8 arrays as strings (much faster, tables/arrays are kind of slow in Lua)
    
end
-- 订阅者sub的回调函数
function imageMessage_callback(msg)
    -- Apply the received image to the passive vision sensor that acts as an image container
    sim.setVisionSensorCharImage(passiveVisionSensor,msg.data)
end

-- 摄像头的回调函数
function sysCall_sensing()
    -- Publish the image of the active vision sensor:
    local data,w,h=sim.getVisionSensorCharImage(activeVisionSensor)
    -- 生成tables/arrays类型的数据类型,tables/arrays只要数据中的成员与希望的数据类型一致即可作成任何类型的数据类型。
    -- 因此可以将tables/arrays类型看作数据载体
    d={}
    d['header']={seq=0,stamp=simROS.getTime(), frame_id="a"}
    d['height']=h
    d['width']=w
    d['encoding']='rgb8'
    d['is_bigendian']=1
    d['step']=w*3
    d['data']=data
    simROS.publish(pub,d)
end

function sysCall_cleanup()
    -- Shut down publisher and subscriber. Not really needed from a simulation script (automatic shutdown)
    simROS.shutdownPublisher(pub)
    simROS.shutdownSubscriber(sub)
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值