run.launch.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. from launch import LaunchDescription
  2. from launch.actions import IncludeLaunchDescription, ExecuteProcess, DeclareLaunchArgument, TimerAction, RegisterEventHandler
  3. from launch.substitutions import LaunchConfiguration, PathJoinSubstitution, PythonExpression, EqualsSubstitution
  4. from launch.launch_description_sources import PythonLaunchDescriptionSource
  5. from launch_ros.substitutions import FindPackageShare
  6. from launch_ros.actions import Node
  7. from launch.conditions import IfCondition
  8. from launch.event_handlers import OnProcessStart
  9. def generate_launch_description():
  10. real_robot_arg = DeclareLaunchArgument(
  11. name='real_robot',
  12. default_value='true',
  13. description='Control real robot (true/false)'
  14. )
  15. enable_hand_control_arg = DeclareLaunchArgument(
  16. name='enable_hand_control',
  17. default_value='true',
  18. description='Enable hand control (true/false)'
  19. )
  20. init_delay_arg = DeclareLaunchArgument(
  21. name='init_delay',
  22. default_value='5.0',
  23. description='Delay before initialization in seconds'
  24. )
  25. end_effector_arg = DeclareLaunchArgument(
  26. name='end_effector',
  27. default_value='hand', # hand / gripper
  28. description='End effector type: hand or gripper'
  29. )
  30. inspire_hand_node = ExecuteProcess(
  31. cmd=['ros2', 'run', 'inspire_hand', 'inspire_hand_sub'],
  32. output='screen',
  33. name='inspire_hand_node',
  34. condition=IfCondition(
  35. EqualsSubstitution(
  36. LaunchConfiguration('end_effector'), 'hand'
  37. )
  38. )
  39. )
  40. hand_grasp_state_pub_node = ExecuteProcess(
  41. cmd=['ros2', 'run', 'inspire_hand', 'hand_grasp_state_pub'],
  42. output='screen',
  43. name='hand_grasp_state_pub',
  44. condition=IfCondition(
  45. EqualsSubstitution(
  46. LaunchConfiguration('end_effector'), 'hand'
  47. )
  48. )
  49. )
  50. zhixing_gripper_node = ExecuteProcess(
  51. cmd=['ros2', 'run', 'zhixing_gripper', 'gripper'],
  52. output='screen',
  53. name='zhixing_gripper_node',
  54. condition=IfCondition(
  55. EqualsSubstitution(
  56. LaunchConfiguration('end_effector'), 'gripper'
  57. )
  58. )
  59. )
  60. head_camera_launch = ExecuteProcess(
  61. cmd=[
  62. 'ros2', 'launch', 'start_robot',
  63. 'gemini2L_recorder_launch.py'
  64. ],
  65. output='log',
  66. name='head_camera_launch'
  67. )
  68. set_head_camera_params = ExecuteProcess(
  69. cmd=[
  70. 'bash', '-c',
  71. 'set -e; '
  72. 'echo "[head_cam] waiting for /top/top/set_color_exposure ..."; '
  73. 'until ros2 service type /top/top/set_color_exposure >/dev/null 2>&1; do sleep 0.2; done; '
  74. 'ros2 service call /top/top/set_color_auto_exposure std_srvs/srv/SetBool "{data: false}"; '
  75. 'ros2 service call /top/top/set_color_exposure orbbec_camera_msgs/srv/SetInt32 "{data: 8000}"; '
  76. 'ros2 service call /top/top/set_color_gain orbbec_camera_msgs/srv/SetInt32 "{data: 10}"; '
  77. 'ros2 service call /top/top/get_color_exposure orbbec_camera_msgs/srv/GetInt32 "{}"; '
  78. 'ros2 service call /top/top/get_color_gain orbbec_camera_msgs/srv/GetInt32 "{}"; '
  79. ],
  80. output='screen',
  81. name='set_head_camera_params'
  82. )
  83. head_cam_handler = RegisterEventHandler(
  84. OnProcessStart(
  85. target_action=head_camera_launch,
  86. on_start=[
  87. TimerAction(
  88. period=6.0,
  89. actions=[set_head_camera_params]
  90. )
  91. ]
  92. )
  93. )
  94. return LaunchDescription([
  95. # 参数声明
  96. real_robot_arg,
  97. enable_hand_control_arg,
  98. init_delay_arg,
  99. end_effector_arg,
  100. # 启动硬件及控制器(带参数传递)
  101. IncludeLaunchDescription(
  102. PythonLaunchDescriptionSource([
  103. PathJoinSubstitution([
  104. FindPackageShare('ymbot_d_control'),
  105. 'launch/activate_multiple_groups.launch.py'
  106. ])
  107. ]),
  108. launch_arguments={
  109. 'real_robot': LaunchConfiguration('real_robot')
  110. # 使用动态参数
  111. }.items()
  112. ),
  113. # 初始化关节位置
  114. TimerAction(
  115. period=LaunchConfiguration('init_delay'),
  116. actions=[
  117. ExecuteProcess(
  118. cmd=[
  119. 'ros2',
  120. 'run',
  121. 'remote_operate_pkg',
  122. 'joint_control',
  123. '--ros-args',
  124. '-p',
  125. # 参考src/remote_operate_pkg/remote_operate_pkg/joint_control.py:17
  126. # leftarm rightarm
  127. 'target_positions:=[-0.35, 0.0, 0.0, -0.52, 0.0, 0.0, 0.0, '
  128. '0.35, 0.0, 0.0, 0.52, 0.0, 0.0, 0.0, 0.0, -0.59]'
  129. ],
  130. output='screen',
  131. name='joint_initialization'
  132. )
  133. ]
  134. ),
  135. # 初始化手部位置(带条件判断)
  136. TimerAction(
  137. period=LaunchConfiguration('init_delay'),
  138. actions=[
  139. ExecuteProcess(
  140. cmd=[
  141. 'ros2', 'run', 'remote_operate_pkg', 'hand_control',
  142. '--ros-args',
  143. '-p',
  144. 'left_hand_positions:=[0.0,0.0,0.0,0.0,0.0,0.0]',
  145. '-p',
  146. 'right_hand_positions:=[0.0,0.0,0.0,0.0,0.0,0.0]',
  147. '-p',
  148. 'duration:=3.0'
  149. ],
  150. output='screen',
  151. name='hand_initialization',
  152. condition=IfCondition(EqualsSubstitution(LaunchConfiguration('end_effector'), 'hand'))
  153. )
  154. ]
  155. ),
  156. # 启动末端执行器节点
  157. inspire_hand_node,
  158. hand_grasp_state_pub_node,
  159. # zhixing_gripper_node,
  160. # 启动底盘驱动节点
  161. ExecuteProcess(
  162. cmd=['ros2', 'run', 'chassis_driver_node', 'chassis_driver_node'],
  163. output='screen',
  164. name='chassis_driver_node'
  165. ),
  166. # 启动VR指令发送节点
  167. ExecuteProcess(
  168. cmd=['ros2', 'run', 'vr_receiver_tcp', 'vr_receiver_tcp'],
  169. output='screen',
  170. name='vr_receiver_node'
  171. ),
  172. # ik解 & 碰撞检测
  173. ExecuteProcess(
  174. cmd=['ros2', 'run', 'ik_solver_pkg', 'ik_servo_node_coll'],
  175. output='log'
  176. ),
  177. # 启动腕部相机节点(保持不变)
  178. ExecuteProcess(
  179. cmd=[
  180. 'ros2', 'launch', 'start_robot', 'rs_multi_camera_launch.py'
  181. ],
  182. output='log'
  183. ),
  184. # 启动头部相机节点(保持不变)
  185. head_camera_launch,
  186. # 设置头部相机曝光/增益
  187. head_cam_handler
  188. ])