rs_multi_camera_launch.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Copyright 2023 Intel Corporation. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # DESCRIPTION #
  15. # ----------- #
  16. # Use this launch file to launch 2 devices.
  17. # The Parameters available for definition in the command line for each camera are described in rs_launch.configurable_parameters
  18. # For each device, the parameter name was changed to include an index.
  19. # For example: to set camera_name for device1 set parameter camera_name1.
  20. # command line example:
  21. # ros2 launch realsense2_camera rs_multi_camera_launch.py camera_name1:=D400 device_type2:=l5. device_type1:=d4..
  22. """Launch realsense2_camera node."""
  23. import copy
  24. from launch import LaunchDescription, LaunchContext
  25. import launch_ros.actions
  26. from launch.actions import IncludeLaunchDescription, OpaqueFunction
  27. from launch.substitutions import LaunchConfiguration, ThisLaunchFileDir
  28. from launch.launch_description_sources import PythonLaunchDescriptionSource
  29. import sys
  30. import pathlib
  31. sys.path.append(str(pathlib.Path(__file__).parent.absolute()))
  32. import rs_launch
  33. local_parameters = [{'name': 'camera_name1', 'default': 'left', 'description': 'camera1 unique name'},
  34. {'name': 'camera_name2', 'default': 'right', 'description': 'camera2 unique name'},
  35. {'name': 'camera_namespace1', 'default': 'left', 'description': 'camera1 namespace'},
  36. {'name': 'camera_namespace2', 'default': 'right', 'description': 'camera2 namespace'},
  37. {'name': 'serial_no1', 'default': "'323622271323'", 'description': 'choose device1 by serial number'},
  38. {'name': 'serial_no2', 'default': "'315122270868'", 'description': 'choose device2 by serial number'} # camera_1_ID,camera_2_ID換成對應數字
  39. ]
  40. def set_configurable_parameters(local_params):
  41. return dict([(param['original_name'], LaunchConfiguration(param['name'])) for param in local_params])
  42. def duplicate_params(general_params, posix):
  43. local_params = copy.deepcopy(general_params)
  44. for param in local_params:
  45. param['original_name'] = param['name']
  46. param['name'] += posix
  47. return local_params
  48. def launch_static_transform_publisher_node(context : LaunchContext):
  49. # dummy static transformation from camera1 to camera2
  50. node = launch_ros.actions.Node(
  51. package = "tf2_ros",
  52. executable = "static_transform_publisher",
  53. arguments = ["0", "0", "0", "0", "0", "0",
  54. context.launch_configurations['camera_name1'] + "_link",
  55. context.launch_configurations['camera_name2'] + "_link"]
  56. )
  57. return [node]
  58. def generate_launch_description():
  59. params1 = duplicate_params(rs_launch.configurable_parameters, '1')
  60. params2 = duplicate_params(rs_launch.configurable_parameters, '2')
  61. return LaunchDescription(
  62. rs_launch.declare_configurable_parameters(local_parameters) +
  63. rs_launch.declare_configurable_parameters(params1) +
  64. rs_launch.declare_configurable_parameters(params2) +
  65. [
  66. OpaqueFunction(function=rs_launch.launch_setup,
  67. kwargs = {'params' : set_configurable_parameters(params1),
  68. 'param_name_suffix': '1'}),
  69. OpaqueFunction(function=rs_launch.launch_setup,
  70. kwargs = {'params' : set_configurable_parameters(params2),
  71. 'param_name_suffix': '2'}),
  72. OpaqueFunction(function=launch_static_transform_publisher_node)
  73. ])