RosPlugin publishers

While RosPlugin services in V-REP are enabled as soon as V-REP starts (given that ROSCORE was running at that time), RosPlugin topic publishing and data streaming happens only on demand (i.e. publishers need to be individually enabled). Moreover, this can only happen while a simulation is running (one exception to this is the topic info: data will be streamed to that topic as long as V-REP is running).

V-REP offers two equivalent means of enabling and disabling RosPlugin publishers: via the simRosEnablePublisher and simRosDisablePublisher services, or via custom Lua commands that can be used from within a script. In either case a topic name, a command ID and 3 enabling parameters need to be provided:

  • The topic name will be the string under which data will be streamed (e.g. "robotPosition")
  • The command ID (hereafter streamCmd) specifies the type of data that we wish to be streamed (e.g. simros_strmcmd_get_object_pose if we wish to stream an object's position & orientation)
  • The 3 enabling parameters fully specify the item whose data we wish to stream (e.g. if we require to stream the joint position of a joint object, then one enabling parameter will be the object handle, while the 2 other parameters will not be used. The first 2 parameters are integer values, the third parameter is a string value. Most of the time only one parameter is required for a streamCmd, sometimes none, and othertimes 2.
  • If you wish to activate/deactivate data streaming via a Lua script, then following 3 functions will be needed:

    simExtROS_enablePublisher

    Description Enables a publisher (i.e. V-REP will be streaming data on a specific topic). Publishers can only be enabled while simulation is running. If a same publisher (i.e. of the same type and applied to the same item) was already enabled previously, then a reference counter will be incremented and the topic name of that publisher is returned. Publishers can be disabled with simExtROS_disablePublisher. At simulation end, all publishers are automatically disabled. A publisher can also be enabled using following ROS service: simRosEnablePublisher. See also simExtROS_enableSubscriber.
    Lua synopsis string effectiveTopicName=simExtROS_enablePublisher(string topicName,number queueSize,number streamCmd,number auxInt1,number auxInt2,string auxString,number publishCnt=0)
    Lua parameters
    topicName: the desired topic name
    queueSize: the desired queue size
    streamCmd: the desired type of data to stream
    auxInt1: the first enabling parameter.
    auxInt2: the second enabling parameter
    auxString: the third enabling parameter
    publishCnt: the number of times you wish to publish the data, before going to sleep. Specify -1 to start in sleep mode, 0 for an unlimited number of publishing, and n to publish exactly n times the data. Can be omitted, in which case 0 is used. See also simExtROS_wakePublisher.
    Lua return values
    effectiveTopicName: the effective topic name that will be used to stream the desired data, or nil if there was an error. If the desired topic name is already in use for another type of publisher, then a new topic name is generated (i.e. "_2" appended). If the desired publisher is already enabled, then the topic name of that existing publisher is returned

    simExtROS_disablePublisher

    Description Disables a publisher (i.e. V-REP will stop streaming data on a specific topic) previously enabled with simExtROS_enablePublisher. If a same publisher was enabled several times (e.g. from different nodes), then this command will only decrement a reference counter (a publisher is effectively disabled when its reference counter reaches zero). At simulation end, all publishers are automatically disabled. A publisher can also be disabled using following ROS service: simRosDisablePublisher. See also simExtROS_disableSubscriber.
    Lua synopsis number referenceCounter=simExtROS_disablePublisher(string topicName)
    Lua parameters
    topicName: the topic name previously returned by simExtROS_enablePublisher.
    Lua return values
    referenceCounter: the value of the reference counter. If the counter is >0, then the publisher is still enabled. If the counter is 0, then the publisher was just disabled. If referenceCounter is -1, there was an error.

    simExtROS_wakePublisher

    Description Wakes up a publisher, puts it to sleep, or queries its publish count state. The publisher must previously have been enabled with simExtROS_enablePublisher.
    Lua synopsis number result=simExtROS_wakePublisher(string topicName,number publishCnt)
    Lua parameters
    topicName: the topic name previously returned by simExtROS_enablePublisher.
    publishCnt: the number of times you wish to publish the data, before going to sleep. Specify -1 to put the publisher to sleep, 0 for an unlimited number of publishing, and n to publish exactly n times the data. Specify -2 to query the current publish count state.
    Lua return values
    result: -1 if there was an error, or n, the current publish count state if publishCnt argument was -2.

    As an example, imagine you wish to activate streaming of the pose of object "myObject" under the topic name "myObjectPose", then, you could write following code in a Lua script:

    local handle=simGetObjectHandle('myObject')
    local effectiveTopicName=simExtROS_enablePublisher('myObjectPose',1,
                                simros_strmcmd_get_object_pose,handle,-1,'')

    The above example can also be executed directly in the console:

    $ rosservice call vrep/simRosGetObjectHandle 'myObject'
    handle: 5
    $ rosservice call vrep/simRosEnablePublisher 'myObjectPose' 1 8193 5 -- -1 ''
    effectiveTopicName: myObjectPose

    The first line retrieves the object handle (i.e. 5), and the second line enables streaming of the desired data. '8193' is the value of the variable simros_strmcmd_get_object_pose defined in the file 'programming/include/v_repConst.h'

    To check if the data is effectively streaming, try one of following two lines:

    $ rostopic list
    $ rostopic echo myObjectPose

    You can also have V-REP act as publisher and subscriber at the same time. For instance, following code (to be executed just once at simulation start) will automatically match object "myObject2"'s pose with the pose of "myObject1", as long as the simulation is running:

    local object1Handle=simGetObjectHandle('myObject1')
    local effectiveTopicName=simExtROS_enablePublisher('myObjectPose',1,
                                simros_strmcmd_get_object_pose,object1Handle,-1,'')
    
    local object2Handle=simGetObjectHandle('myObject2')
    local subscriberID=simExtROS_enableSubscriber(effectiveTopicName,1,
                                simros_strmcmd_set_object_pose,object2Handle,-1,'')

    Recommended topics

  • RosPlugin publisher types
  • RosPlugin subscribers
  • RosPlugin services