At a first glance, this is a simple task: the code is already provided by ROS2 official site... and as usual, there are problems.
rviz_cmd = IncludeLaunchDescription(
PythonLaunchDescriptionSource(os.path.join(launch_dir, 'rviz_launch.py')),
condition=IfCondition(use_rviz),
launch_arguments={'namespace': '',
'use_namespace': 'False',
'rviz_config': rviz_config_file}.items())
The code above, when called as
ld.add_action(rviz_cmd)
... works, but produces warnings:
[rviz2-5] Warning: Invalid frame ID "left_wheel_backside" passed to canTransform argument source_frame - frame does not exist
[rviz2-5] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-5] Warning: Invalid frame ID "left_wheel_frontside" passed to canTransform argument source_frame - frame does not exist
[rviz2-5] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-5] Warning: Invalid frame ID "right_wheel_backside" passed to canTransform argument source_frame - frame does not exist
[rviz2-5] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
[rviz2-5] Warning: Invalid frame ID "right_wheel_frontside" passed to canTransform argument source_frame - frame does not exist
[rviz2-5] at line 133 in /tmp/binarydeb/ros-foxy-tf2-0.13.13/src/buffer_core.cpp
The reason is, RViz node starts before transformations are published. It detects a problem, reports it and then, as transformations become available, warnings stop. But our output already has couple of them and this is not nice. So we need to make a pause before launching RViz, so that other nodes could start.
The solution is simple, it only took me half-day of googling :)
# We reuse RViz node
rviz_cmd = IncludeLaunchDescription(
PythonLaunchDescriptionSource(os.path.join(launch_dir, 'rviz_launch.py')),
condition=IfCondition(use_rviz),
launch_arguments={'namespace': '',
'use_namespace': 'False',
'rviz_config': rviz_config_file}.items())
# We wrap it in the Timer Action
rviz_timer_action = launch.actions.TimerAction( period=3.0, actions=[ rviz_cmd ])
# And we call wrapped node instead of the original one
#ld.add_action(rviz_cmd)
ld.add_action(rviz_timer_action)
Now we do not have warnings in the output of our Terminal. RViz comes up with a model of our robot:
