ROS subscribersWhile ROS services in V-REP are enabled as soon as V-REP starts (given that ROSCORE was running at that time), ROS topic subscription and listening to streaming data happens only on demand (i.e. subscribers need to be individually enabled). Moreover, this can only happen while a simulation is running. V-REP offers two equivalent means of enabling and disabling subscribers: via the simRosEnableSubscriber and simRosDisableSubscriber 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: If you wish to activate/deactivate topic subscription via a Lua script, then following 2 functions will be needed: simExtROS_enableSubscriber
simExtROS_disableSubscriber
As an example, imagine you wish to activate subscription of the topic "myObjectPose" that is streaming pose data, and you wish to apply that pose data to the object "myObject". Then you could write following code in a Lua script: local handle=simGetObjectHandle('myObject') local subscriberID=simExtROS_enableSubscriber('myObjectPose',1 simros_strmcmd_set_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/simRosEnableSubscriber 'myObjectPose' 1 10243 5 -- -1 '' subscriberID: 0 The first line retrieves the object handle (i.e. 5), and the second line enables listening and setting of the desired data. '10243' is the value of the variable simros_strmcmd_set_object_pose defined in the file 'programming/include/v_repConst.h' You can 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 |