[ROS2] Launch same node several times using same launch file
Hello everyone!
Perhaps, a bit confusing title, I will try to explain what I want to do:
I am using ROS2 Foxy Fitzroy. I have a C++ node named 'data_processor' which is essentially just a publisher. I have written a launch file takes in arguments and launches the 'data_processor' with a user-defined namespace, named 'data_processor_launch.py'. Now, I want to write a another launch file 'data_processors_all_launch.py', which launches 2 instances of 'data_processor' using the child launch file, but under different namespaces.
I succeed in launching 2 instances of 'data_processors' separately (using the 'data_procesor_launch.py' with namespace 'tm_lm' and using parent launch file 'data_processors_all_launch.py' with just one instance there under namespace 'tb_lm'). But when I try to Include the same launch description twice with different it fails. I believe it should be possible to launch it from one single launch file, because these essentially become different objects.
This is "data_processor_launch.py":
import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.substitutions import LaunchConfiguration
def generate_launch_description():
value = LaunchConfiguration('namespace', default = 'tb_lm')
ld = LaunchDescription()
config = os.path.join(
get_package_share_directory('loading_motor_dt'),
'config',
'params.yaml'
)
data_processor=Node(
package = 'loading_motor_dt',
namespace = value,
name = 'data_processor',
executable = 'currentVoltageFlow',
parameters = [config]
)
d.add_action(data_processor)
return ld
This is "data_processor_all_launch.py": import os
from click import launch
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
def generate_launch_description():
ld = LaunchDescription()
package_prefix = get_package_share_directory('loading_motor_dt')
src = PythonLaunchDescriptionSource([package_prefix,'/launch/data_processor_launch.py'])
launch_tm = IncludeLaunchDescription(src,launch_arguments = {'namespace': 'tm_lm'}.items())
launch_tb = IncludeLaunchDescription(src,launch_arguments = {'namespace': 'tb_lm'}.items())
print(launch_tb)
print(launch_tm)
ld.add_action(launch_tm)
ld.add_action(launch_tb)
return ld
And the following is the error I get when I launch the "data_processor_all_launch.py"
[INFO] [launch]: All log files can be found below /home/sejego/.ros/log/2022-04-08-21-37-47-943060-pop-os-21257
[INFO] [launch]: Default logging verbosity is set to INFO
<launch.actions.include_launch_description.IncludeLaunchDescription object at 0x7f3e72e0c9a0>
<launch.actions.include_launch_description.IncludeLaunchDescription object at 0x7f3e73791880>
Task exception was never retrieved
future: <Task finished name='Task-2' coro=<LaunchService._process_one_event() done, defined at /opt/ros/foxy/lib/python3.8/site-packages/launch/launch_service.py:226> exception=RuntimeError("ExecuteProcess action 'currentVoltageFlow-1': executed more than once: <launch_ros.actions.node.Node object at 0x7f3e723b2700>")>
Traceback (most recent call last):
File "/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_service.py", line 228, in _process_one_event
await self.__process_event(next_event)
File "/opt/ros/foxy/lib/python3.8/site-packages/launch/launch_service.py", line 248, in __process_event
visit_all_entities_and_collect_futures(entity, self.__context))
File "/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 45, in visit_all_entities_and_collect_futures
futures_to_return += visit_all_entities_and_collect_futures(sub_entity, context)
File "/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 45, in visit_all_entities_and_collect_futures
futures_to_return += visit_all_entities_and_collect_futures(sub_entity, context)
File "/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 45, in visit_all_entities_and_collect_futures
futures_to_return += visit_all_entities_and_collect_futures(sub_entity, context)
[Previous line repeated 3 more times]
File "/opt/ros/foxy/lib/python3.8/site-packages/launch/utilities/visit_all_entities_and_collect_futures_impl.py", line 38, in ...