ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Using multithreading to initialize & run several nodes with rospy.

asked 2021-11-30 02:25:27 -0500

Jacob G. gravatar image

Using: Ros Melodic on Ubuntu 18.04.

I'm trying to give navigation goals to several robots simultaneously using rospy.

As of right now, every robot in the "task queue" will wait until the one before it has completed its task.

To fix that, I thought multithreading would be useful (if you have better suggestions, please shoot!)

When running the following line (without threading):

def function(x, y, robot):
    rospy.init_node('movebase_client', anonymous=True)

Everything works....

But when theading is introduced, the system breaks.

Here is a totally stripped down version of the code (This can't even run on its own.)

#!/usr/bin/env python
import rospy
import threading

def function(x, y, robot):
    rospy.init_node('movebase_client', anonymous=True)
    print(x, y, robot)

testing = threading.Thread(target=function, args=[3, 3, "Bot1"])
testing.start()

When running this code, I get the following error:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/jubuntu/multiple_costs/P5/src/deploy/launch/jacobtest.py", line 6, in function
    rospy.init_node('movebase_client', anonymous=True)
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/client.py", line 285, in init_node
    rospy.core.register_signals() # add handlers for SIGINT/etc...
  File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/core.py", line 623, in register_signals
    _signalChain[signal.SIGTERM] = signal.signal(signal.SIGTERM, _ros_signal)
ValueError: signal only works in main thread

Any help would be highly appreciated!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-11-30 19:45:38 -0500

Mike Scheutzow gravatar image

You can only have 1 ROS node per linux process. The rule is you should only call rospy.init_node(...) once.

If you call it a second time, the first ros-node instance is shut down and start a new ros-node instance begins.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-11-30 02:25:27 -0500

Seen: 207 times

Last updated: Nov 30 '21