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 source
d 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.