ROS2编译与运行测试

上一篇:ROS2安装与配置

源码

首先下载ROS2测试代码:

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

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

然后运行编译

1
colcon build

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

运行

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

result

分别在两个终端执行:

1
2
ros2 run examples_rclcpp_minimal_publisher publisher_member_function
ros2 run examples_rclcpp_minimal_subscriber subscriber_member_function

发布端执行输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 59'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 60'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 61'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 62'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 63'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 64'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 65'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 66'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 67'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 68'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 69'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 70'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 71'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 72'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 73'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 74'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 75'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 76'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 77'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 78'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 79'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 80'
[INFO] [minimal_publisher]: Publishing: 'Hello, world! 81'

订阅端输出为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
hyper@ubuntu:~$ ros2 run examples_rclcpp_minimal_subscriber subscriber_menber_function
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 64'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 65'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 66'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 67'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 68'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 69'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 70'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 71'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 72'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 73'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 74'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 75'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 76'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 77'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 78'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 79'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 80'
[INFO] [minimal_subscriber]: I heard: 'Hello, world! 81'

错误

我遇到的错误

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

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

1
sudo apt install python3-colcon-common-extensions

分支错误

编译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
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

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

使用git branch查看当前examples的分支为mastergit branch -a查看所有分支为:

1
2
3
4
5
6
7
8
9
10
11
12
13
* 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文件夹的内容,然后继续编译。

Wall问题

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

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

编译报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Starting >>> kalman_filter_localization
Finished <<< examples_rclpy_minimal_service [0.56s]
Starting >>> serial
Finished <<< examples_rclpy_minimal_subscriber [0.58s]
Starting >>> usb_camera_driver
--- stderr:serial
/home/hyper/ros2_Ws/src/serial/src/impl/list_ports/list_ports_linux.Cc:In function ‘std::vector<std::__cxx11::basic_string<char> >glob(const std::vector<std::__cxx11::basic_string<char> >&)’:
/home/hyper/ros2_WS/src/serial/src/impl/list_ports/list_ports_linux.Cc:66:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int path_index =0; path_index < glob_results.gl_pathc; path_index++)

/home/hyper/ros2_Ws/src/serial/src/impl/list_ports/list_ports_linux.Cc:57:9: warning: variable ‘glob_retval'set but not used[-Wunused-but-set-variable]
int glob_retval = glob(patterns[0].c_str() , 0, NULL, &glob_results);

/home/hyper/ros2_Ws/src/serial/src/impl/list_ports/list_ports_linux.CC: In function ‘std::__cxx11::string format(const ch r*,...):
/home/hyper/ros2_Ws/src/serial/src/impl/list_ports/list_ports_linux.CC:246:31: warning:comparison between signed and unsigned integer expressions [-Wsign-compare]
else if( return_value >= buffer_size_bytes )
---
Finished <<< serial [2.37s]
Finished <<< examples_rclcpP_multithreaded_executor [7.75s]
Finished <<< usb_camera_driver [8.28s]
Finished <<< kalman_filter_localization [23.5s]

Summary:19 packages finished [46.6s]
1 package had stderr output:serial

报错为:-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-***。

然后编译。


ROS2编译与运行测试
https://blog.jackeylea.com/ros2/cimpile-run-test-ros2/
作者
JackeyLea
发布于
2020年12月8日
许可协议