0
$\begingroup$

Rosanswers logo

I'm using ROS2 Dashing on ubuntu 18.04 64bits.

I have run:

cd ~/ros2_overlay_ws/src/
ros2 pkg create sam_empty_package_with_rviz2
cd ~/ros2_overlay_ws/src/sam_empty_package_with_rviz2
mkdir -p rviz2/
mkdir -p launch/

vim launch/rviz2.launch.py

import os

from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch_ros.actions import Node


def generate_launch_description():
    rviz_config_dir = os.path.join(
            get_package_share_directory('sam_empty_package_with_rviz2'),
            'rviz2',
            'sam_empty_package_with_rviz2.rviz')

    return LaunchDescription([
        Node(
            package='rviz2',
            node_executable='rviz2',
            node_name='rviz2',
            arguments=['-d', rviz_config_dir],
            output='screen'),
    ])

Compile:

colcon build --packages-select sam_empty_package_with_rviz2

After compile I run:

ros2 launch sam_empty_package_with_rviz2 rviz2.launch.py

And I got errors:

sam@sam-ub1804:~$ ros2 launch sam_empty_package_with_rviz2 rviz2.launch.py
file 'rviz2.launch.py' was not found in the share directory of package 'sam_empty_package_with_rviz2' which is at '/home/sam/ros2_dashing_overlay_ws/install/sam_empty_package_with_rviz2/share/sam_empty_package_with_rviz2'
sam@sam-ub1804:~$ 

How to solve it?

Thank you~


Originally posted by sam on ROS Answers with karma: 2570 on 2019-12-09

Post score: 3


Original comments

Comment by gvdhoorn on 2019-12-09:
Just making sure: have you sourced the correct setup.bash file after you built your workspace?

Comment by MCornelis on 2019-12-09:
Also make sure you have something like this

install(DIRECTORY launch
  DESTINATION share/${PROJECT_NAME}) 

in your CMakeLists.txt. Otherwise "colcon build" does not actually place your launchfiles in the share folder (which is where ros2 launch looks for launchfiles).

Comment by MCornelis on 2019-12-09:
If you plan on changing the .rviz2 file and launchfile a lot I recommend building with --symlink-install. This way you don't have to source after every change to these 2 files.

Comment by sam on 2019-12-10:
I'm sure I have already sourced that setup.bash on my ROS2 workspace. And it works when I use that install. Thank you very much! Please post as an answer! Another simple question is why if I use --symlink-install, it will cause my rviz2 could not save file anymore?

Comment by MCornelis on 2019-12-10:
A normal colcon build will copy the launch directory to the share directory. So if you then change something in the launch folder you have to colcon build again, otherwise the share directory is not updated (at least this is my understanding). If you do colcon build --symlink-install a link to the launch folder is placed in the share directory. So if you then update something in the launch folder even if you look in the share directory you will get the updated version. I am not aware of --symlink-install preventing you from saving the .rviz2 config. This is new to me, my bad I think :P? You are free to do a normal colcon build if that works for you, but like I said it will require rebuilding every time you change something to your .launch.py or your .rviz2/.rviz file.

Comment by MCornelis on 2019-12-10:
You can btw also make a config folder in one of your packages where you put the .rviz file and then do

install(DIRECTORY config
  DESTINATION share/${PROJECT_NAME})

in the CMakeLists.txt. In your launch file you can then do something like

rviz_config_dir = os.path.join(
            get_package_share_directory('<my_package_name>'),
            'config',
            '<my_rviz_config_file_name>.rviz')

Comment by MCornelis on 2019-12-10:
You can btw also just hit the up-arrow next to a comment. I believe this also gives me karma? You might even be able to accept a comment as an answer, I'm not entirely sure how this all works :P Just glad I was able to help ^^

Comment by sam on 2019-12-11:
Thanks for very detail information. I'm not able to set the comment as an answer. Please post your comment as an answer and I will mark it.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

As per request of the questioin poster I'll highlight the answer in the comments that worked for him here:

In CMakeLists.txt you need to put an install() command which basically tells colcon build to put either a copy or a link to a certain folder in the share directory. The share directory is the default place where ros2 launch will look for things. So if you add

install(DIRECTORY launch
  DESTINATION share/${PROJECT_NAME})

To your CMakeLists.txt and then do a colcon build your share directory, so for instance ros2_ws/install/<pkg_name>/share/<pkg_name>/, will have a copy of your launch directory in it.

If you do colcon build --symlink-install, then instead of a copy of the launch file it will put a symbolic link to the launchfile. This means that if you change something to the launch file in your package you don't have to rebuild the package for the changes to take effect. This is only true for files that don require compilation, so --symlink-install will work for config files or python code / launch files etc.


Originally posted by MCornelis with karma: 331 on 2019-12-12

This answer was ACCEPTED on the original site

Post score: 5

$\endgroup$

Your Answer

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