ROS2 how to launch rviz2 with config file
I'm coming from ROS1 background and I'm new with ROS2. I want to launch rviz with config file in ROS2 by specifying relative path from package package_name
ROS1 equivalent would be:
<launch>
<node type="rviz" name="rviz" pkg="rviz" args="-d $(find package_name)/config/config_file.rviz" />
</launch>
What I have in ROS2 is:
from launch import LaunchDescription
from ament_index_python.packages import get_package_share_directory
from launch_ros.actions import Node
import os.path
def generate_launch_description():
return LaunchDescription([
Node(
package='rviz2',
namespace='',
executable='rviz2',
name='rviz2',
arguments=['-d' + os.path.join(get_package_share_directory('package_name'), 'config', 'config_file.rviz')]
)
])
But this approach has 2 problems:
- get_package_share_directory returns path to ...install/package_name/share... but I want .../src/package_name/config/config_file.rviz (just like in ROS1 what $(find package_name) would return) because there's no config_file.rviz in install directory. Even if it was
- I want to save new rviz settings to src directory not to install because install is not tracked by git
I searched documentation, answers.ros.org and github but couldn't find the answer. So what is equivalent of $(find package_name) in ROS2? Or maybe all files should be installed in shared directory if so how to deal with problem 2.