0
$\begingroup$

Rosanswers logo

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.


Originally posted by Combinacijus on ROS Answers with karma: 65 on 2021-03-29

Post score: 1

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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


Originally posted by shonigmann with karma: 1567 on 2021-03-29

This answer was ACCEPTED on the original site

Post score: 4


Original comments

Comment by longhongc on 2021-11-30:
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'))  
    ]
)
$\endgroup$
0
$\begingroup$

Rosanswers logo

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.* `

Originally posted by LSD with karma: 98 on 2021-05-09

This answer was NOT ACCEPTED on the original site

Post score: 2


Original comments

Comment by morten on 2021-09-16:
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

Comment by LSD on 2021-11-06:
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')]]
        )
    ])

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.