-DEVELOPING THE FIRST NODE .2

Posted zhangrelay

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了-DEVELOPING THE FIRST NODE .2相关的知识,希望对你有一定的参考价值。

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 -h
usage: 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_name

Create a new ROS 2 package

positional arguments:
  package_name          The package name

options:
  -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_node
going to create a new package
package name: my_package
destination directory: /home/zhangrelay/ros_ws/book_ros2/src
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['zhangrelay <zhangrelay@todo.todo>']
licenses: ['TODO: License declaration']
build type: ament_cmake
dependencies: ['rclcpp', 'std_msgs']
node_name: simple_node
creating folder ./my_package
creating ./my_package/package.xml
creating source and include folder
creating folder ./my_package/src
creating folder ./my_package/include/my_package
creating ./my_package/CMakeLists.txt
creating ./my_package/src/simple_node.cpp

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

tree my_package/
my_package/
├── CMakeLists.txt
├── include
│   └── my_package
├── package.xml
└── src
    └── simple_node.cpp

3 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 dependencies
find_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++17
ament_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功能包完工。

以上是关于-DEVELOPING THE FIRST NODE .2的主要内容,如果未能解决你的问题,请参考以下文章

The first Chapter !

The first one

The First

The first C# program

生活格言 The first blog. Welcome!

ML -- the first week