How to solve 'file 'rviz2.launch.py' was not found in the share directory of package'?
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~
Just making sure: have you
source
d the correctsetup.bash
file after you built your workspace?Also make sure you have something like this
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).
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.
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?
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 tocolcon build
again, otherwise the share directory is not updated (at least this is my understanding). If you docolcon 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 normalcolcon 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.You can btw also make a config folder in one of your packages where you put the .rviz file and then do
in the CMakeLists.txt. In your launch file you can then do something like
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 ^^
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.