Typically: "How do I... ", "How can I... " questions
atoz
Posts: 57
Joined: 18 Oct 2013, 09:02

Is it possible to use ROS subscribers/publishers in the initialisation stage of a threaded script? I currently have a subscriber set up and I can see from the

Code: Select all

rostopic info topic_name
command that Vrep is subscribed to the topic but not acting on the input. Although it works on a non-threaded script perfectly fine.

Thanks

[EDIT] On a similar note, where are the

Code: Select all

streamcmds e.g. simros_strmcmd_set_object_pose
values defined for the python bindings? They seem to be missing from the vrepConst.py file

coppelia
Posts: 7441
Joined: 14 Dec 2012, 00:25

### Re: Threaded scripts and ROS

Yes, you can use a threaded and non-threaded child script in a similar way. The way the current ROS interface works is somewhat semi-automatic: publishers will be automatically triggered in each simulation step, with appropriate data filled-in. The same happens with subscribers. This allows to enable streaming of a vision sensor image (for example) with a single line of code. And allows to enable subscription (and application) of a ROS image to a passive vision sensor also with a single line of code.

In next release, there will be a new (or additional ROS interface) that behaves in a very similar way as the ROS C++ API.

And yes, the stream commands constants are not defined in vrepConst.py, which is (for now) only meant to be used with the remote API. But you could add those constants to the file.

Cheers

atoz
Posts: 57
Joined: 18 Oct 2013, 09:02

### Re: Threaded scripts and ROS

coppelia wrote:Yes, you can use a threaded and non-threaded child script in a similar way. The way the current ROS interface works is somewhat semi-automatic: publishers will be automatically triggered in each simulation step, with appropriate data filled-in. The same happens with subscribers. This allows to enable streaming of a vision sensor image (for example) with a single line of code. And allows to enable subscription (and application) of a ROS image to a passive vision sensor also with a single line of code.
I understand this. Maybe I was unclear in what I was asking. I can't get the subscriber in the threaded script functioning. It works fine in a non-threaded script and updates the value of the object I want it to. However, in a threaded script, even though I can see that V-rep is subscribed to the topic, it doesn't update the value of the object. I've set up the subscriber in the initialisation stage of the threaded script. Could it be that the thread execution in the script does not allow execution of anything outside of thread function?

Another similar issue that I've encountered is that trying to execute a declared function in a threaded script using

Code: Select all

simRosCallScriptFunction 
from Python also does not work and returns -1. However, it works fine when using a non-threaded script. I'm guessing both these issues are linked to the way threaded scripts are executed.

The reason I have all this code in a threaded script is because I would like to use the RML functions for movement and it would be nice to have all relevant code in the same script.

Thanks

coppelia
Posts: 7441
Joined: 14 Dec 2012, 00:25

### Re: Threaded scripts and ROS

Try to load the demo scene rosTopicPublisherAndSubscriber.ttt: it uses a non-threaded child script. Now remove the attached child script, and replace it with a threaded child script with following content:

Code: Select all

-- Retrive the handle of the vision sensor we wish to stream:
visionSensorHandle=simGetObjectHandle('Vision_sensor')

-- Now enable topic publishing and streaming of the vision sensor's data:
topicName=simExtROS_enablePublisher('visionSensorData',1,simros_strmcmd_get_vision_sensor_image,visionSensorHandle,0,'')

-- Retrive the handle of the passive vision sensor. We will use the passive vision sensor
passiveSensorHandle=simGetObjectHandle('PassiveVision_sensor')

-- Now enable a topic subscription:
simExtROS_enableSubscriber(topicName,1,simros_strmcmd_set_vision_sensor_image,passiveSensorHandle,0,'')

It should works as with the non-threaded child script.
As for your second problem, this is a limitation of release 3.3.0, that will be relaxed in release 3.3.1. In addition to this, the new ROS interface will then also be available.

Cheers

slgrajeshwar36172
Posts: 4
Joined: 06 Nov 2019, 04:34
Location: iit patna
Contact:

### Re: Threaded scripts and ROS

can you please post one complete code threaded script for ROS for newer version of vrep and ROS.
thanks for code

coppelia
Posts: 7441
Joined: 14 Dec 2012, 00:25

### Re: Threaded scripts and ROS

The following is for the ROS interface, and a threaded child script (similar to above):

Code: Select all

function sysCall_threadmain()
-- Get some handles:
activeVisionSensor=sim.getObjectHandle('Vision_sensor')
passiveVisionSensor=sim.getObjectHandle('PassiveVision_sensor')

-- Enable an image publisher and subscriber:
if simROS then
print("<font color='#0F0'>ROS interface was found.</font>@html")
simROS.publisherTreatUInt8ArrayAsString(pub) -- treat uint8 arrays as strings (much faster, tables/arrays are kind of slow in Lua)
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)
else
end

if simROS then
-- Publish the image of the active vision sensor:
local data,w,h=sim.getVisionSensorCharImage(activeVisionSensor)
d={}
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
sim.switchThread() -- resume in next simulation step
end
end

function sysCall_cleanup()
if simROS then
-- Shut down publisher and subscriber. Not really needed from a simulation script (automatic shutdown)
simROS.shutdownPublisher(pub)
simROS.shutdownSubscriber(sub)
end
end

function sysCall_init()

end

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_cleanup()
end