系统是Ubuntu22.04,之前安装好了ROS 2 Humble Hawksbill,现在测试一下环境:

1、准备工作

1
2
3
# 确保系统已经更新
sudo apt update
sudo apt upgrade

2、验证ROS2的安装与环境配置

检查ROS2版本

1
2
3
# 用以下命令来检查当前使用的ROS2发行版名称
echo $ROS_DISTRO
# 配置正确则输出所安装的ROS2版本,如:humble

查看ROS2安装路径中的版本信息

1
2
lures@lures-Redmi-G-Pro-2024:~$ grep "version" /opt/ros/humble/.rosinstall
grep: /opt/ros/humble/.rosinstall: No such file or directory

缺少.rosinstall文件:/opt/ros/humble/.rosinstall文件不存在是正常的,因为这个文件通常在源代码安装(从源码编译)的情况下才存在。如果你是通过Debian包管理器(例如apt)安装的ROS2,这个文件通常不会存在

查看ROS2的已安装包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
lures@lures-Redmi-G-Pro-2024:~$ ros2 pkg list
action_msgs
action_tutorials_cpp
action_tutorials_interfaces
action_tutorials_py
actionlib_msgs
ament_cmake
ament_cmake_auto
ament_cmake_copyright
ament_cmake_core
ament_cmake_cppcheck
ament_cmake_cpplint
ament_cmake_export_definitions
ament_cmake_export_dependencies
ament_cmake_export_include_directories
ament_cmake_export_interfaces
ament_cmake_export_libraries
ament_cmake_export_link_flags
ament_cmake_export_targets
ament_cmake_flake8
ament_cmake_gen_version_h
ament_cmake_gmock
ament_cmake_gtest
ament_cmake_include_directories
ament_cmake_libraries
ament_cmake_lint_cmake
ament_cmake_pep257
ament_cmake_pytest
ament_cmake_python
ament_cmake_ros
ament_cmake_target_dependencies
ament_cmake_test
ament_cmake_uncrustify
ament_cmake_version
ament_cmake_xmllint
ament_copyright
ament_cppcheck
ament_cpplint
ament_flake8
ament_index_cpp
ament_index_python
ament_lint
ament_lint_auto
ament_lint_cmake
ament_lint_common
ament_package
ament_pep257
ament_uncrustify
ament_xmllint
angles
builtin_interfaces
class_loader
common_interfaces
composition
composition_interfaces
console_bridge_vendor
cv_bridge
demo_nodes_cpp
demo_nodes_cpp_native
demo_nodes_py
depthimage_to_laserscan
desktop
diagnostic_msgs
domain_coordinator
dummy_map_server
dummy_robot_bringup
dummy_sensors
eigen3_cmake_module
example_interfaces
examples_rclcpp_minimal_action_client
examples_rclcpp_minimal_action_server
examples_rclcpp_minimal_client
examples_rclcpp_minimal_composition
examples_rclcpp_minimal_publisher
examples_rclcpp_minimal_service
examples_rclcpp_minimal_subscriber
examples_rclcpp_minimal_timer
examples_rclcpp_multithreaded_executor
examples_rclpy_executors
examples_rclpy_minimal_action_client
examples_rclpy_minimal_action_server
examples_rclpy_minimal_client
examples_rclpy_minimal_publisher
examples_rclpy_minimal_service
examples_rclpy_minimal_subscriber
fastrtps_cmake_module
geometry2
geometry_msgs
image_geometry
image_tools
image_transport
interactive_markers
intra_process_demo
joy
kdl_parser
keyboard_handler
laser_geometry
launch
launch_ros
launch_testing
launch_testing_ament_cmake
launch_testing_ros
launch_xml
launch_yaml
libcurl_vendor
libstatistics_collector
libyaml_vendor
lifecycle
lifecycle_msgs
logging_demo
map_msgs
message_filters
nav_msgs
orocos_kdl_vendor
osrf_pycommon
pcl_conversions
pcl_msgs
pendulum_control
pendulum_msgs
pluginlib
pybind11_vendor
python_cmake_module
python_qt_binding
qt_dotgraph
qt_gui
qt_gui_cpp
qt_gui_py_common
quality_of_service_demo_cpp
quality_of_service_demo_py
rcl
rcl_action
rcl_interfaces
rcl_lifecycle
rcl_logging_interface
rcl_logging_spdlog
rcl_yaml_param_parser
rclcpp
rclcpp_action
rclcpp_components
rclcpp_lifecycle
rclpy
rcpputils
rcutils
resource_retriever
rmw
rmw_dds_common
rmw_fastrtps_cpp
rmw_fastrtps_shared_cpp
rmw_implementation
rmw_implementation_cmake
robot_state_publisher
ros2action
ros2bag
ros2cli
ros2cli_common_extensions
ros2component
ros2doctor
ros2interface
ros2launch
ros2lifecycle
ros2multicast
ros2node
ros2param
ros2pkg
ros2run
ros2service
ros2topic
ros_base
ros_core
ros_environment
ros_workspace
rosbag2
rosbag2_compression
rosbag2_compression_zstd
rosbag2_cpp
rosbag2_interfaces
rosbag2_py
rosbag2_storage
rosbag2_storage_default_plugins
rosbag2_transport
rosgraph_msgs
rosidl_adapter
rosidl_cli
rosidl_cmake
rosidl_default_generators
rosidl_default_runtime
rosidl_generator_c
rosidl_generator_cpp
rosidl_generator_py
rosidl_parser
rosidl_runtime_c
rosidl_runtime_cpp
rosidl_runtime_py
rosidl_typesupport_c
rosidl_typesupport_cpp
rosidl_typesupport_fastrtps_c
rosidl_typesupport_fastrtps_cpp
rosidl_typesupport_interface
rosidl_typesupport_introspection_c
rosidl_typesupport_introspection_cpp
rpyutils
rqt_action
rqt_bag
rqt_bag_plugins
rqt_common_plugins
rqt_console
rqt_graph
rqt_gui
rqt_gui_cpp
rqt_gui_py
rqt_image_view
rqt_msg
rqt_plot
rqt_publisher
rqt_py_common
rqt_py_console
rqt_reconfigure
rqt_service_caller
rqt_shell
rqt_srv
rqt_topic
rttest
rviz2
rviz_assimp_vendor
rviz_common
rviz_default_plugins
rviz_ogre_vendor
rviz_rendering
sdl2_vendor
sensor_msgs
sensor_msgs_py
shape_msgs
shared_queues_vendor
spdlog_vendor
sqlite3_vendor
sros2
sros2_cmake
statistics_msgs
std_msgs
std_srvs
stereo_msgs
tango_icons_vendor
teleop_twist_joy
teleop_twist_keyboard
tf2
tf2_bullet
tf2_eigen
tf2_eigen_kdl
tf2_geometry_msgs
tf2_kdl
tf2_msgs
tf2_py
tf2_ros
tf2_ros_py
tf2_sensor_msgs
tf2_tools
tinyxml2_vendor
tinyxml_vendor
tlsf
tlsf_cpp
topic_monitor
tracetools
trajectory_msgs
turtlesim
uncrustify_vendor
unique_identifier_msgs
urdf
urdf_parser_plugin
visualization_msgs
yaml_cpp_vendor
zstd_vendor

会列出当前已安装的所有ROS2软件包,说明ROS2环境已经安装并配置成功

验证ROS2功能

可以进行下图所示的功能测试:

image-20241008193939561

显示:

1
2
3
4
lures@lures-Redmi-G-Pro-2024:~$ ros2 topic list
/chatter
/parameter_events
/rosout

3、安装Gazebo

1
2
3
4
5
6
7
8
9
10
# 安装Gazebo相关包
sudo apt update
sudo apt install ros-humble-gazebo-ros-pkgs ros-humble-gazebo-ros ros-humble-ros-gz-sim

# 启动gazebo的方式
ign gazebo
# 或者对于ROS2和Gazebo集成的情况,可以使用ROS2的launch文件来启动Gazebo仿真
# 分别启动Gazebo的服务器端和客户端
ros2 launch gazebo_ros gzserver.launch.py
ros2 launch gazebo_ros gzclient.launch.py

也可以在管理器中找到对应个的应用图标打开:

image-20241008194640425

4、安装NVIDIA驱动和CUDA

此处,我已经在配置深度学习环境时配置了,当前PyTorchTensorflow支持的CUDA版本是11.8,所以自己的驱动及下载的CUDA及相关工具包注意版本要一致,要不很麻烦~

通过下面命令查看GPUCUDA版本信息,说输出如下图类似,则说明NVIDIA驱动和CUDA安装成功

image-20241008194943437

5、配置Gazebo使用GPU加速

image-20241008195120276

image-20241008195211632

-v 4使Gazebo以详细模式启动,这样可以在终端中看到Gazebo的加载日志,启动后,查看日志中是否有NVIDIA GPU的信息。同时在另一个窗口运行nvidia-smi,检查GPU使用情况:

1
2
watch -n 1 nvidia-smi
# 会每秒刷新一次 GPU 使用情况。如果 Gazebo 正在使用 GPU,你应该能够看到 GPU 的使用率上升。

6、使用Gazebo和ROS2的简单测试

image-20241008200105175

image-20241008200237575

Gazebo 服务端(gzserver)成功启动:ROS 2 与 Gazebo 集成部分没有报错,gzserver 进程已经运行,并输出了进程 ID。

ROS 2 主题列表:列出了当前活跃的 ROS 2 主题,显示了一些基本的 ROS 2 主题(如 /clock/rosout 等)。不过,没有看到与 Gazebo 仿真模型、传感器数据相关的主题。

image-20241008200322757

新建一个demo.sdf文件,文件内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?xml version="1.0" ?>
<sdf version="1.6">
<model name="test_camera_box">
<static>false</static>
<link name="link">
<inertial>
<mass>1.0</mass>
</inertial>
<visual name="visual">
<geometry>
<box>
<size>0.5 0.5 0.5</size>
</box>
</geometry>
<material>
<ambient>1 0 0 1</ambient>
<diffuse>1 0 0 1</diffuse>
</material>
</visual>
<collision name="collision">
<geometry>
<box>
<size>0.5 0.5 0.5</size>
</box>
</geometry>
</collision>
<sensor name="camera" type="camera">
<camera>
<horizontal_fov>1.047</horizontal_fov>
<image>
<width>640</width>
<height>480</height>
<format>R8G8B8</format>
</image>
<clip>
<near>0.1</near>
<far>100</far>
</clip>
</camera>
<always_on>true</always_on>
<update_rate>30</update_rate>
<plugin name="camera_controller" filename="libgazebo_ros_camera.so">
<ros>
<namespace>/my_camera</namespace>
<remapping>image_raw:=/camera/image_raw</remapping>
</ros>
<camera_name>camera</camera_name>
</plugin>
</sensor>
</link>
</model>
</sdf>

格式化SDF内容为命令行字符串:XML内容的双引号需要进行转义:

1
2
3
4
5
6
7
ros2 service call /spawn_entity gazebo_msgs/SpawnEntity "{
name: 'test_camera_box',
xml: '<?xml version=\"1.0\" ?><sdf version=\"1.6\"><model name=\"test_camera_box\"><static>false</static><link name=\"link\"><inertial><mass>1.0</mass></inertial><visual name=\"visual\"><geometry><box><size>0.5 0.5 0.5</size></box></geometry><material><ambient>1 0 0 1</ambient><diffuse>1 0 0 1</diffuse></material></visual><collision name=\"collision\"><geometry><box><size>0.5 0.5 0.5</size></box></geometry></collision><sensor name=\"camera\" type=\"camera\"><camera><horizontal_fov>1.047</horizontal_fov><image><width>640</width><height>480</height><format>R8G8B8</format></image><clip><near>0.1</near><far>100</far></clip></camera><always_on>true</always_on><update_rate>30</update_rate><plugin name=\"camera_controller\" filename=\"libgazebo_ros_camera.so\"><ros><namespace>/my_camera</namespace><remapping>image_raw:=/camera/image_raw</remapping></ros><camera_name>camera</camera_name></plugin></sensor></link></model></sdf>',
robot_namespace: '',
initial_pose: { position: { x: 0.0, y: 0.0, z: 0.5 }, orientation: { w: 1.0 } },
reference_frame: 'world'
}"

启动Gazebo的服务端和客户端,确保启动

1
2
3
# 这样会启动 Gazebo 的图形界面和物理模拟服务
ros2 launch gazebo_ros gzserver.launch.py
ros2 launch gazebo_ros gzclient.launch.py

在终端运行之前的XML命令,显示结果:

image-20241008203224178

如果希望查看摄像头的图像输出,可以使用rqt_image_view,命令如下:

1
ros2 run rqt_image_view rqt_image_view

rqt_image_view 界面中选择 /my_camera/image_raw 主题,能看到相机的实时图像。

image-20241008203452719