2024-09-04 00:46:44 -07:00
# Requirements:
# A ZED camera
# Install zed ros2 wrapper package (https://github.com/stereolabs/zed-ros2-wrapper)
# Example:
# $ ros2 launch rtabmap_examples zed.launch.py camera_model:=zed2i
import os
from ament_index_python.packages import get_package_share_directory
2024-09-05 21:05:50 -07:00
from launch import LaunchDescription , LaunchContext
2024-09-04 19:38:05 -07:00
from launch.actions import DeclareLaunchArgument
2024-09-04 00:46:44 -07:00
from launch_ros.actions import Node
2024-09-05 21:05:50 -07:00
from launch.actions import IncludeLaunchDescription , OpaqueFunction
2024-09-04 00:46:44 -07:00
from launch.substitutions import LaunchConfiguration
from launch.launch_description_sources import PythonLaunchDescriptionSource
2024-09-04 19:38:05 -07:00
from launch.conditions import UnlessCondition
2024-09-04 00:46:44 -07:00
import tempfile
2024-09-05 21:05:50 -07:00
parameters = []
remappings = []
def launch_setup ( context : LaunchContext , * args , ** kwargs ):
2024-09-04 00:46:44 -07:00
2026-06-13 21:44:39 -07:00
use_zed_odometry = LaunchConfiguration ( 'use_zed_odometry' ) . perform ( context ) in [ "True" , "true" ]
# Override some ZED parameters without changing any files:
# * grab_resolution: VGA
# * pos_tracking_enabled: disabled when rtabmap computes the odometry, so
# the ZED node does not publish the odom->camera_link TF (which would
# conflict with rtabmap's odometry). We still set publish_tf:=true below
# so the ZED node keeps broadcasting the IMU TF, which rtabmap needs
# (wait_imu_to_init). sensors.publish_imu_tf is ignored when publish_tf
# is false, and the IMU frame is not in the ZED URDF, so this is the only
# way to get the IMU TF while rtabmap owns the odometry.
pos_tracking_enabled = 'true' if use_zed_odometry else 'false'
2024-09-04 00:46:44 -07:00
with tempfile . NamedTemporaryFile ( mode = 'w+t' , delete = False ) as zed_override_file :
zed_override_file . write ( "--- \n " +
"/**: \n " +
" ros__parameters: \n " +
" general: \n " +
2026-06-13 21:44:39 -07:00
" grab_resolution: 'VGA' \n " +
" pos_tracking: \n " +
" pos_tracking_enabled: " + pos_tracking_enabled )
2024-09-04 00:46:44 -07:00
parameters = [{ 'frame_id' : 'zed_camera_link' ,
'subscribe_rgbd' : True ,
'approx_sync' : False ,
'wait_imu_to_init' : True }]
remappings = [( 'imu' , '/zed/zed_node/imu/data' )]
2026-06-13 21:44:39 -07:00
if use_zed_odometry :
2024-09-04 19:38:05 -07:00
remappings . append (( 'odom' , '/zed/zed_node/odom' ))
2024-09-05 21:05:50 -07:00
else :
parameters . append ({ 'subscribe_odom_info' : True })
return [
2024-09-04 00:46:44 -07:00
# Launch camera driver
IncludeLaunchDescription (
PythonLaunchDescriptionSource ([ os . path . join (
get_package_share_directory ( 'zed_wrapper' ), 'launch' ),
'/zed_camera.launch.py' ]),
2024-09-05 21:05:50 -07:00
launch_arguments = { 'camera_model' : LaunchConfiguration ( 'camera_model' ),
'ros_params_override_path' : zed_override_file . name ,
2026-06-13 21:44:39 -07:00
'publish_tf' : 'true' ,
'publish_imu_tf' : 'true' ,
2024-09-05 21:05:50 -07:00
'publish_map_tf' : 'false' } . items (),
2024-09-04 00:46:44 -07:00
),
2024-11-30 17:28:16 -08:00
# Sync rgb/depth/camera_info together
2024-09-04 00:46:44 -07:00
Node (
package = 'rtabmap_sync' , executable = 'rgbd_sync' , output = 'screen' ,
parameters = parameters ,
2026-06-13 21:44:39 -07:00
remappings = [( 'rgb/image' , '/zed/zed_node/rgb/color/rect/image' ),
( 'rgb/camera_info' , '/zed/zed_node/rgb/color/rect/camera_info' ),
2024-09-04 00:46:44 -07:00
( 'depth/image' , '/zed/zed_node/depth/depth_registered' )]),
# Visual odometry
Node (
package = 'rtabmap_odom' , executable = 'rgbd_odometry' , output = 'screen' ,
2024-09-05 21:05:50 -07:00
condition = UnlessCondition ( LaunchConfiguration ( 'use_zed_odometry' )),
2024-09-04 00:46:44 -07:00
parameters = parameters ,
2024-09-04 19:38:05 -07:00
remappings = remappings ,),
2024-09-04 00:46:44 -07:00
# VSLAM
Node (
package = 'rtabmap_slam' , executable = 'rtabmap' , output = 'screen' ,
parameters = parameters ,
remappings = remappings ,
arguments = [ '-d' ]),
# Visualization
Node (
package = 'rtabmap_viz' , executable = 'rtabmap_viz' , output = 'screen' ,
parameters = parameters ,
remappings = remappings )
2024-09-05 21:05:50 -07:00
]
def generate_launch_description ():
return LaunchDescription ([
# Launch arguments
DeclareLaunchArgument (
'use_zed_odometry' , default_value = 'false' ,
description = 'Use zed \' s computed odometry instead of using rtabmap \' s odometry.' ),
2024-11-30 17:28:16 -08:00
DeclareLaunchArgument (
'camera_model' , default_value = '' ,
description = "[REQUIRED] The model of the camera. Using a wrong camera model can disable camera features. Valid choices are: ['zed', 'zedm', 'zed2', 'zed2i', 'zedx', 'zedxm', 'virtual']" ),
2024-09-05 21:05:50 -07:00
OpaqueFunction ( function = launch_setup )
2024-09-04 00:46:44 -07:00
])