ROS2编译与运行测试

上一篇:ROS2安装与配置

首先下载ROS2测试代码:

1
git clone https://github.com/ros2/examples

将其放到ros2的工作空间ros2_ws中。

然后运行编译

1
colcon build

然后会在工作空间文件夹自动生成buildinstalllog三个文件夹。

可能错误1

执行之后什么都没有,没有报错也没有输出。

解决,缺点东西没有安装:

1
sudo apt install python3-colcon-common-extensions

可能错误2

编译examples时报错:

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
hyper@ubuntu:~/ros2_ws$ colcon build
Starting >>> examples_rclcpp_minimal_action_client
Starting >>> examples_rclcpp_minimal_action_server
Starting >>> examples_rclcpp_minimal_client
Starting >>> examples_rclcpp_minimal_composition
Finished <<< examples_rclcpp_minimal_action_server [1.32s]
Starting >>> examples_rclcpp_minimal_publisher
Finished <<< examples_rclcpp_minimal_composition [2.00s]
Starting >>> examples_rclcpp_minimal_service
--- stderr: examples_rclcpp_minimal_client
/home/hyper/ros2_ws/src/examples/rclcpp/services/minimal_client/main.cpp: In function ‘int main(int, char**)’:
/home/hyper/ros2_ws/src/examples/rclcpp/services/minimal_client/main.cpp:41:13: error: ‘rclcpp::FutureReturnCode’ has not been declared
rclcpp::FutureReturnCode::SUCCESS)
^~~~~~~~~~~~~~~~
make[2]: *** [CMakeFiles/client_main.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/client_main.dir/all] Error 2
make: *** [all] Error 2
---
Failed <<< examples_rclcpp_minimal_client [2.55s, exited with code 2]
Aborted <<< examples_rclcpp_minimal_action_client [2.76s]
Aborted <<< examples_rclcpp_minimal_publisher [2.50s]
Aborted <<< examples_rclcpp_minimal_service [1.90s]

Summary: 2 packages finished [4.12s]
1 package failed: examples_rclcpp_minimal_client
3 packages aborted: examples_rclcpp_minimal_action_client examples_rclcpp_minimal_publisher examples_rclcpp_minimal_service
2 packages had stderr output: examples_rclcpp_minimal_action_client examples_rclcpp_minimal_client
11 packages not processed
</code></pre>

代码有问题,但是这是官方代码而且还在更新,那么只可能我们用错了代码。

使用<code>git branch</code>查看当前examples的分支为<code>master</code>,<code>git branch -a</code>查看所有分支为:

<pre><code class="language-bash line-numbers">* master
remotes/origin/HEAD -> origin/master
remotes/origin/ardent
remotes/origin/bouncy
remotes/origin/check_parameters
remotes/origin/crystal
remotes/origin/dashing
remotes/origin/eloquent
remotes/origin/executor_refactor/add_wait_set
remotes/origin/foxy
remotes/origin/jacob/rclpy_action_client_sync
remotes/origin/master
remotes/origin/ros1_shim

我使用的是eloquent版本,所以使用:

1
git checkout eloquent

清空install文件夹的内容,然后继续编译。

可能错误3

编译其他的代码时,比如说:

1
git clone https://github.com/jinmenglei/serial.git

编译报错:

error

报错为:-Wsign-compare -Wunused-but-set-variable

有符号和无符号比较,定义但未使用,不是什么大错,但是编译器认为是错误,修改一下配置文件就可以了。

在源码CMakeLists.txt文件中找到下面的内容:

1
2
3
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
endif()

-W表示启用警告,-Wall表示启用all类型警告。

我们加上:

1
2
3
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Wno-sign-compare -Wno-unused-but-set-variable")
endif()

报什么错就在错误类型前面加上-Wno-***。

然后编译。

运行

所有可执行文件都在install文件夹下,

result

分别在两个终端执行:

1
2
ros2 run examples_rclcpp_minimal_publisher publisher_member_function
ros2 run examples_rclcpp_minimal_subscriber subscriber_member_function

执行效果:

test