Ask Your Question

ROS2 how to launch rviz2 with config file

asked 2021-03-29 07:18:15 -0500

Combinacijus gravatar image

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:

  <node type="rviz" name="rviz" pkg="rviz" args="-d $(find package_name)/config/config_file.rviz" />

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([
            arguments=['-d' + os.path.join(get_package_share_directory('package_name'), 'config', 'config_file.rviz')]

But this approach has 2 problems:

  1. 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
  2. I want to save new rviz settings to src directory not to install because install is not tracked by git

I searched documentation, 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.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2021-05-09 13:37:24 -0500

This answer assumes that you've gone through the tutorials of configuring your ROS2 environment and setting up colcon_cd here:

colcon_cd by default finds your ROS2 package located in src/, which I assume is beneficial for your problem.

The command to launch RViz in ROS2 using ROS1 styling:

rviz2 -d $(colcon_cd your_package && pwd)/path/to/config.rviz

Note: Please let me know if this solution doesn't work out for you. If in case it does, feel free to upvote my answer. `

edit flag offensive delete link more


but how do you include this in the launch file arguments field? I've tried a number of variations and it doesn't seem to want to evaluate the colcon_cd package as a bash command

morten gravatar image morten  ( 2021-09-16 06:22:11 -0500 )edit

answered 2021-03-29 21:26:02 -0500

shonigmann gravatar image

I would recommend the following:

a) add the config folder where you keep your rviz config files to the install command in your package's CMakeLists.txt (e.g. install(DIRECTORY ... config DESTINATION share/${PROJECT_NAME}) if its not already there. This will make sure that the config file will exist in the package's shared directory (where get_package_share_directory() expects it to be)

b) build your package with colcon build --symlink-install. You can also selectively rebuild the package in question using colcon build --symlink-install --packages-select PACKAGENAME. This will add a symbolic link to your rviz config file in the install directory that points to the file in your src directory. That way, you avoid the need to build your package every time you change your config file.

There may be "faster" pythonic methods, but I am not sure if they'd be cleaner. Hope that helps

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2021-03-29 07:18:15 -0500

Seen: 659 times

Last updated: May 09