【V-REP学习记录04】Interacting with V-REP using ROS topics

【V-REP学习记录01】Setting up V-REP with ROS

【V-REP学习记录02】Understanding the vrep_plugin

【V-REP学习记录03】Interacting with V-REP using ROS services

        现在我们将讨论如何使用主题与V-REP通信。当我们想要向仿真对象发送信息或检索机器人生成的数据时,这是很有用的。虽然在V-REP启动时启用了服务,但主题通信仅在需要时进行,并在模拟场景中初始化发布者和订阅者变量。

        编写V-REP模拟场景的最常见方法是通过Lua脚本。场景中的每个对象都可以关联到一个脚本,该脚本在模拟开始时自动调用,并在模拟期间循环执行。

        在下一个例子中,我们将创建一个有两个对象的场景。其中一个将被编程为接收来自特定主题的浮动数据,而另一个将在另一个主题上重新发布相同的数据。

        使用场景层次面板上的下拉菜单,选择条目:Add——Dummy。我们可以创建两个对象,一个dummy_publisher和一个dummy_subscriber,并为每个对象关联一个脚本。在已创建的对象上使用鼠标右键,选择“Add | Associated child script | Non-threaded”条目,如下图所示:

或者,我们可以通过直接加载位于vrep_demo_pkg/scene目录中的文件demo_publisher_subscriber.ttt。让我们看看与之Lua脚本的内容

dummy_subscriber对象:

if (sim_call_type==sim.syscb_init) then
    -- 检查所需的插件是否存在(libv_repExtRos.so或libv_repExtRos.dylib)
    local moduleName=0
    local moduleVersion=0
    local index=0
    local pluginNotFound=true
    while moduleName do
        moduleName,moduleVersion=sim.getModuleName(index)
        if (moduleName=='Ros') then
            pluginNotFound=false
        end
        index=index+1
    end

    if (pluginNotFound) then
    else        
        -- 启用/vrep_demo/float_in主题的订阅器
        if (sim.getScriptExecutionCount()==0) then
            simExtROS_enableSubscriber("/vrep_demo/float_in",1,simros_strmcmd_set_float_signal,-1,-1,"in")
        end
    end
end


if (sim_call_type==sim.syscb_actuation) then
end


if (sim_call_type==sim.syscb_sensing) then
end


if (sim_call_type==sim.syscb_cleanup) then
end

        

在初始化部分,我们检查系统中是否安装了vrep_plugin,否则会出现错误显示:

simExtROS_enableSubscriber("/vrep_demo/float_in",1,simros_strmcmd_set_float_signal ,-1,-1,"in")

        这将激活/vrep_demo/float_in主题上输入浮点值的订阅者。simextras_enablessubscriberber函数的参数包括主题的名称、所需的队列大小、所需的流类型和三个启用参数。这些参数指定应该应用数据的项。例如,如果我们想要设置一个关节物体的位置,第一个参数将是对象句柄,而其他参数将不被使用。在我们的例子中,我们想将从主题接收的值保存到变量“in”中。

        在syscb_init系统回调函数中,首先检查是否存在所需的插件(Ros)。通过迭代获取所有已加载的模块,如果找到了名为'Ros'的模块,将pluginNotFound标志设置为false。如果找不到插件,则不进行任何操作。否则,在第一个脚本执行计数时,启用名为"/vrep_demo/float_in"的主题的订阅器。

        这段代码的目的是在V-REP仿真环境中的不同回调函数中执行特定的操作。它首先在初始化回调函数中检查所需的插件是否存在,然后根据结果执行相应的操作。在actuation回调函数和sensing回调函数中没有任何操作,而在cleanup回调函数中也没有任何操作。

每个链接到V-REP对象的Lua脚本包含以下四个部分:

●sim_childscriptcall_initialization:该部分仅在模拟第一次启动时执行。

●sim_childscriptcall_actuation:该部分以与模拟相同的帧速率循环调用。用户可以在这里输入控制机器人驱动的代码。

●sim_childscriptcall_sensing:该部分将在每个模拟步骤中执行,在模拟步骤的感知阶段。

●sim_childscriptcall_cleanup:这个部分在模拟结束之前被调用。

demo_publisher对象

if (sim_call_type==sim.syscb_init) then
    -- 检查所需的插件是否存在(libv_repExtRos.so或libv_repExtRos.dylib)
    local moduleName=0
    local moduleVersion=0
    local index=0
    local pluginNotFound=true
    while moduleName do
        moduleName,moduleVersion=sim.getModuleName(index)
        if (moduleName=='Ros') then
            pluginNotFound=false
        end
        index=index+1
    end

    if (pluginNotFound) then
        -- 如果未找到插件,显示错误消息
        sim.displayDialog('Error','ROS plugin was not found.\nSimulation will not run properly',sim.dlgstyle_ok,false,nil,{0.8,0,0,0,0,0},{0.5,0,0,1,1,1})
    else        
        -- 启用类型为float的发布者主题/vrep_demo/float_out
        if (sim.getScriptExecutionCount()==0) then
            simExtROS_enablePublisher("/vrep_demo/float_out",1,simros_strmcmd_get_float_signal,-1,-1,"out")
        end
    end
end

if (sim_call_type==sim.syscb_actuation) then
   --Get value of input signal and publish it on /vrep_demo/float_out topic
   data = sim.getFloatSignal('in')
   if( data ) then
    sim.setFloatSignal("out",data)
    sim.addStatusbarMessage(data)
   end
    
end


if (sim_call_type==sim.syscb_sensing) then
	-- Put your main SENSING code here
end


if (sim_call_type==sim.syscb_cleanup) then
	-- Put some restoration code here
end

代码解释如下:

simExtROS_enablePublisher("/vrep_demo/float_out", 1,simros_strmcmd_get_float_signal, -1,-1,"out"

        在这一行中,在检查了vrep_plugin的正确安装之后,我们将启用浮动值。在这一行之后,脚本连续发布变量"out"的值

        最后,我们将out变量的值设置为从/vrep_demo/float_in主题接收到的值,存储在in变量中。注意,in和out是可以从的所有脚本访问的特殊全局变量现场。这些变量在V-REP中被称为信号。

        运行模拟器后,我们可以检查一切是否正常工作,并在上发布所需的数字输入主题并监控输出主题的输出,使用以下命令:

rostopic pub /vrep_demo/float_in std_msgs/Float32 "data: 2.0" -r 12

rostopic echo /vrep_demo/float_out

猜你喜欢

转载自blog.csdn.net/cz_include/article/details/131391729
今日推荐