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

ROS2 how to launch rviz2 with config file

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

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:

<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:

  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, 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.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
4

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

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

Comments

1

This following is the python way for a) add the config folder in your package to the data_files in setup.py (just like the launch folder)

import os
from glob import glob
from setuptools import setup

package_name = 'my_package'

setup(
    # Other parameters ...
    data_files=[
        # ... Other data files
        (os.path.join('share', package_name), glob('launch/*.launch.py'))
         # Include all config files.   
        (os.path.join('share', package_name), glob('config/*config.rviz'))  
    ]
)
longhongc gravatar image longhongc  ( 2021-11-30 15:10:08 -0600 )edit
2

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

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

Comments

1

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 -0600 )edit
1

This is how I include it in the launch file:


from launch import LaunchDescription
from launch_ros.actions import Node
import os

def generate_launch_description():

    pkg_name = 'package_name'
    pkg_dir = os.popen('/bin/bash -c "source /usr/share/colcon_cd/function/colcon_cd.sh && \
        colcon_cd %s && pwd"' % pkg_name).read().strip()


    return LaunchDescription([
        Node(
            package='rviz2',
            namespace='',
            executable='rviz2',
            name='rviz2',
            arguments=['-d', [os.path.join(pkg_dir, 'config', 'config_file.rviz')]]
        )
    ])


LSD gravatar image LSD  ( 2021-11-06 13:01:32 -0600 )edit

Question Tools

3 followers

Stats

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

Seen: 18,006 times

Last updated: May 09 '21