当前位置:首页 » 《随便一记》 » 正文

ROS2机器人编程简述humble-第二章-DEVELOPING THE FIRST NODE .2

3 人参与  2023年02月03日 11:06  分类 : 《随便一记》  评论

点击全文阅读


0.1ROS2机器人编程简述新书推荐-A Concise Introduction to Robot Programming with ROS2

1.1ROS2机器人编程简述humble-第一章-Introduction

2.1ROS2机器人编程简述humble-第二章-First Steps with ROS2 .1


2.2主要内容是全手工创建一个最简单的自定义节点,其实没啥具体功能,主要是对ROS2节点结构有个更好的理解吧。

使用ros2 pkg create这个命令。

其实-h看一下非常清晰明了。

 ros2 pkg create -husage: ros2 pkg create [-h] [--package-format {2,3}] [--description DESCRIPTION]                       [--license LICENSE]                       [--destination-directory DESTINATION_DIRECTORY]                       [--build-type {cmake,ament_cmake,ament_python}]                       [--dependencies DEPENDENCIES [DEPENDENCIES ...]]                       [--maintainer-email MAINTAINER_EMAIL]                       [--maintainer-name MAINTAINER_NAME] [--node-name NODE_NAME]                       [--library-name LIBRARY_NAME]                       package_nameCreate a new ROS 2 packagepositional arguments:  package_name          The package nameoptions:  -h, --help            show this help message and exit  --package-format {2,3}, --package_format {2,3}                        The package.xml format.  --description DESCRIPTION                        The description given in the package.xml  --license LICENSE     The license attached to this package; this can be an                        arbitrary string, but a LICENSE file will only be generated                        if it is one of the supported licenses (pass '?' to get a                        list)  --destination-directory DESTINATION_DIRECTORY                        Directory where to create the package directory  --build-type {cmake,ament_cmake,ament_python}                        The build type to process the package with  --dependencies DEPENDENCIES [DEPENDENCIES ...]                        list of dependencies  --maintainer-email MAINTAINER_EMAIL                        email address of the maintainer of this package  --maintainer-name MAINTAINER_NAME                        name of the maintainer of this package  --node-name NODE_NAME                        name of the empty executable  --library-name LIBRARY_NAME                        name of the empty library

创建一个需要 rclcpp std_msgs,节点名为simple_node,功能包名为my_package的命令如下:

ros2 pkg create my package --dependencies rclcpp std_msgs --node-name simple_node

ros2 pkg create my_package --dependencies rclcpp std_msgs --node-name simple_nodegoing to create a new packagepackage name: my_packagedestination directory: /home/zhangrelay/ros_ws/book_ros2/srcpackage format: 3version: 0.0.0description: TODO: Package descriptionmaintainer: ['zhangrelay <zhangrelay@todo.todo>']licenses: ['TODO: License declaration']build type: ament_cmakedependencies: ['rclcpp', 'std_msgs']node_name: simple_nodecreating folder ./my_packagecreating ./my_package/package.xmlcreating source and include foldercreating folder ./my_package/srccreating folder ./my_package/include/my_packagecreating ./my_package/CMakeLists.txtcreating ./my_package/src/simple_node.cpp

这些都是自动生成的,非常方便,当然也有图形化IDE更加方便使用,后续补充。

tree my_package/my_package/├── CMakeLists.txt├── include│   └── my_package├── package.xml└── src    └── simple_node.cpp3 directories, 3 files

package.xml

<?xml version="1.0"?><?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?><package format="3">  <name>my_package</name>  <version>0.0.0</version>  <description>TODO: Package description</description>  <maintainer email="zhangrelay@todo.todo">zhangrelay</maintainer>  <license>TODO: License declaration</license>  <buildtool_depend>ament_cmake</buildtool_depend>  <depend>rclcpp</depend>  <depend>std_msgs</depend>  <test_depend>ament_lint_auto</test_depend>  <test_depend>ament_lint_common</test_depend>  <export>    <build_type>ament_cmake</build_type>  </export></package>

simple_node.cpp

#include <cstdio>int main(int argc, char ** argv){  (void) argc;  (void) argv;  printf("hello world my_package package\n");  return 0;}

参考案例:

#include "rclcpp/rclcpp.hpp"int main(int argc, char * argv[]) {    rclcpp::init(argc, argv);        auto node = rclcpp::Node::make_shared("simple_node");        rclcpp::spin(node);        rclcpp::shutdown();        return 0;}

•#include“rclcpp/rrcpp.hpp”允许访问C++中的大多数ROS2类型和函数。

•rclcpp::init(argc,argv)从启动此进程的参数中提取ROS2应考虑的任何选项。

•第6行创建ROS2节点。node是名称为simplenode的ROS2节点的std::shared_ptr。

rclcpp::Node类配备了许多别名和静态函数,以简化代码。SharedPtr是std::shared-ptr<rclcppNode>的别名,

make shared是std::make shared<rclcpp::Node>的静态方法。

以下行是等效的:

std::shared_ptr<rclcpp::Node> node = std::shared_ptr<rclcpp::Node>(new rclcpp::Node("simple_node"));std::shared_ptr<rclcpp::Node> node = std::make_shared<rclcpp::Node>("simple_node");rclcpp::Node::SharedPtr node = std::make_shared<rclcpp::Node>("simple_node");auto node = std::make_shared<rclcpp::Node>("simple_node");auto node = rclcpp::Node::make_shared("simple_node");

•在此代码中,spin阻止程序的执行,因此不会立即终止。其重要功能将在后续示例中解释。

•shutdown管理在下一行程序结束之前节点的关闭。

CMakelist.txt

cmake_minimum_required(VERSION 3.8)project(my_package)if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")  add_compile_options(-Wall -Wextra -Wpedantic)endif()# find dependenciesfind_package(ament_cmake REQUIRED)find_package(rclcpp REQUIRED)find_package(std_msgs REQUIRED)add_executable(simple_node src/simple_node.cpp)target_include_directories(simple_node PUBLIC  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>  $<INSTALL_INTERFACE:include>)target_compile_features(simple_node PUBLIC c_std_99 cxx_std_17)  # Require C99 and C++17ament_target_dependencies(  simple_node  "rclcpp"  "std_msgs")install(TARGETS simple_node  DESTINATION lib/${PROJECT_NAME})if(BUILD_TESTING)  find_package(ament_lint_auto REQUIRED)  # the following line skips the linter which checks for copyrights  # comment the line when a copyright and license is added to all source files  set(ament_cmake_copyright_FOUND TRUE)  # the following line skips cpplint (only works in a git repo)  # comment the line when this package is in a git repo and when  # a copyright and license is added to all source files  set(ament_cmake_cpplint_FOUND TRUE)  ament_lint_auto_find_test_dependencies()endif()ament_package()

编译:colcon build --symlink-install

[ 50%] Building CXX object CMakeFiles/simple_node.dir/src/simple_node.cpp.o[100%] Linking CXX executable simple_node[100%] Built target simple_node

测试(执行):ros2 run my_package simple_node

一个最简单的ROS2功能包完工。


点击全文阅读


本文链接:http://zhangshiyu.com/post/52420.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1