|
@@ -408,15 +408,77 @@ rviz2
|
|
|
|
|
|
### 4.6 在RVIZ2中显示机器人模型
|
|
|
|
|
|
+虽然机器人的轨迹已经在RVIZ中显示出来了,但是并没有机器人的模型,也看不到轮子的转动,小鱼来带你一起解决这个问题。
|
|
|
|
|
|
+前面小鱼介绍过,要发布机器人模型我们所使用的节点是`robot_state_publisher`,所以我们在`gazebo.launch.py`中加入这个节点,同时再加上rviz2的启动节点,最终的`gazebo.launch.py`内容如下:
|
|
|
|
|
|
+```python
|
|
|
|
|
|
+import os
|
|
|
+from launch import LaunchDescription
|
|
|
+from launch.actions import ExecuteProcess
|
|
|
+from launch_ros.actions import Node
|
|
|
+from launch_ros.substitutions import FindPackageShare
|
|
|
|
|
|
|
|
|
+def generate_launch_description():
|
|
|
+ robot_name_in_model = 'fishbot'
|
|
|
+ package_name = 'fishbot_description'
|
|
|
+ urdf_name = "fishbot_gazebo.urdf"
|
|
|
|
|
|
+ ld = LaunchDescription()
|
|
|
+ pkg_share = FindPackageShare(package=package_name).find(package_name)
|
|
|
+ urdf_model_path = os.path.join(pkg_share, f'urdf/{urdf_name}')
|
|
|
|
|
|
+ # Start Gazebo server
|
|
|
+ start_gazebo_cmd = ExecuteProcess(
|
|
|
+ cmd=['gazebo', '--verbose', '-s', 'libgazebo_ros_factory.so'],
|
|
|
+ output='screen')
|
|
|
|
|
|
+ # Launch the robot
|
|
|
+ spawn_entity_cmd = Node(
|
|
|
+ package='gazebo_ros',
|
|
|
+ executable='spawn_entity.py',
|
|
|
+ arguments=['-entity', robot_name_in_model, '-file', urdf_model_path ], output='screen')
|
|
|
+
|
|
|
+ # Start Robot State publisher
|
|
|
+ start_robot_state_publisher_cmd = Node(
|
|
|
+ package='robot_state_publisher',
|
|
|
+ executable='robot_state_publisher',
|
|
|
+ arguments=[urdf_model_path]
|
|
|
+ )
|
|
|
|
|
|
+ # Launch RViz
|
|
|
+ start_rviz_cmd = Node(
|
|
|
+ package='rviz2',
|
|
|
+ executable='rviz2',
|
|
|
+ name='rviz2',
|
|
|
+ output='screen',
|
|
|
+ # arguments=['-d', default_rviz_config_path]
|
|
|
+ )
|
|
|
+
|
|
|
+ ld.add_action(start_gazebo_cmd)
|
|
|
+ ld.add_action(spawn_entity_cmd)
|
|
|
+ ld.add_action(start_robot_state_publisher_cmd)
|
|
|
+ ld.add_action(start_rviz_cmd)
|
|
|
+
|
|
|
+
|
|
|
+ return ld
|
|
|
+```
|
|
|
+
|
|
|
+保存编译启动
|
|
|
+
|
|
|
+```shell
|
|
|
+colcon build
|
|
|
+```
|
|
|
+
|
|
|
+```shell
|
|
|
+ros2 launch fishbot_description gazebo.launch.py
|
|
|
+```
|
|
|
+
|
|
|
+然后继续启动键盘控制,Enjoy It!
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
|