【V-REP Learning Record 04】Interacting with V-REP using ROS topics

【V-REP Learning Record 01】Setting up V-REP with ROS

【V-REP Learning Record 02】Understanding the vrep_plugin

【V-REP Learning Record 03】Interacting with V-REP using ROS services

        Now we will discuss how to communicate with V-REP using topics. This is useful when we want to send information to a simulated object or retrieve data generated by a robot. Although the service is enabled when V-REP starts, topic communication only occurs when needed, and publisher and subscriber variables are initialized in the simulation scenario.

        The most common way to write V-REP simulation scenarios is through Lua scripts. Each object in the scene can be associated with a script that is automatically called at the start of the simulation and executed in a loop during the simulation.

        In the next example, we will create a scene with two objects. One of them will be programmed to receive floating data from a specific topic, while the other will republish the same data on another topic.

        Using the drop-down menu on the Scene Hierarchy panel, select the entry: Add - Dummy. We can create two objects, a dummy_publisher and a dummy_subscriber, and associate a script with each object. Use the right mouse button on the created object and select the "Add | Associated child script | Non-threaded" entry, as shown in the following figure:

 

Alternatively, we can directly load the file demo_publisher_subscriber.ttt located in the vrep_demo_pkg/scene directory. Let's see what the Lua script looks like with

dummy_subscriber object:

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

        

In the initialization part, we check whether vrep_plugin is installed in the system, otherwise an error will appear:

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

        This will activate subscribers on the /vrep_demo/float_in topic for input floating point values. Parameters to the simextras_enablessubscriberber function include the name of the topic, the desired queue size, the desired stream type, and three enabling parameters. These parameters specify the items to which data should be applied. For example, if we want to set the position of a joint object, the first parameter will be the object handle, and the other parameters will not be used. In our case, we want to save the value received from the topic into the variable "in".

        In the syscb_init system callback function, first check whether the required plug-in (Ros) exists. Get all loaded modules by iterating and if a module named 'Ros' is found, set the pluginNotFound flag to false. If the plugin is not found, no action is taken. Otherwise, when the first script executes the count, enable the subscriber for the topic named "/vrep_demo/float_in".

        The purpose of this code is to perform specific operations in different callback functions in the V-REP simulation environment. It first checks whether the required plugin exists in the initialization callback function, and then performs the appropriate actions based on the result. There is no operation in the actuation callback function and the sensing callback function, and there is no operation in the cleanup callback function.

Each Lua script linked to a V-REP object contains the following four parts:

●sim_childscriptcall_initialization: This part is only executed when the simulation is started for the first time.

●sim_childscriptcall_actuation: This part is called cyclically at the same frame rate as the simulation. Here the user can enter the code to control the robot driver.

●sim_childscriptcall_sensing: This part will be executed in each simulation step, in the sensing phase of the simulation step.

●sim_childscriptcall_cleanup: This part is called before the simulation ends.

demo_publisher object

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

The code is explained below:

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

        In this line, after checking that vrep_plugin is properly installed, we enable floating values. After this line, the script continuously publishes the value of the variable "out"

        Finally, we set the value of the out variable to the value received from the /vrep_demo/float_in topic, stored in the in variable. Note that in and out are special global variables accessible from all scripts in the locale. These variables are called signals in V-REP.

        After running the emulator, we can check that everything is working properly and publish the required numbers on the input topic and monitor the output of the output topic, using the following commands:

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

rostopic echo /vrep_demo/float_out

 

Guess you like

Origin blog.csdn.net/cz_include/article/details/131391729