【ROS2】节点

ROS2 节点

机器人的每一项功能,都被称为是一个节点。

  • 每个节点都是一个独立的可执行程序;
  • 每个节点都可以用不同的编程语言来编写、编译得到;
  • 一个机器人的众多节点可能分布在不同的计算机上(分布式);
  • 每个节点都需要一个唯一的命名。

示例:创建并运行成功一个节点

1. 创建功能包

cd ~/dev_ws/src
ros2 pkg create --build-type ament_cmake learning_node

2. 编写源文件、CMakeLists.txt、package.xml

learning_node/src/node_helloworld.cpp

#include <unistd.h>
#include "rclcpp/rclcpp.hpp"

/**
 * 创建一个 HelloWorld 节点, 初始化时输出 “hello world” 日志
 */
class HelloWorldNode : public rclcpp::Node
{
public:
    HelloWorldNode()
        : Node("node_helloworld_class")                      
    {
        while(rclcpp::ok())                                  
        {
            RCLCPP_INFO(this->get_logger(), "Hello World");  
            sleep(1);                                        
        }
    }
};

// ROS2 节点主入口 main 函数
int main(int argc, char *argv[])                               
{

    // ROS2 C++ 接口初始化
    rclcpp::init(argc, argv);        
    
    // 创建 ROS2 节点对象并进行初始化                 
    rclcpp::spin(std::make_shared<HelloWorldNode>()); 
    
    // 关闭 ROS2 C++ 接口
    rclcpp::shutdown();                               
    
    return 0;
}

learning_node/src/CMakeLists.txt

cmake_minimum_required(VERSION 3.8)
project(learning_node_cpp)

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)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)

find_package(rclcpp REQUIRED)                                              # 人为添加

add_executable(node_helloworld_class src/node_helloworld_class.cpp)        # 人为添加
ament_target_dependencies(node_helloworld_class rclcpp)                    # 人为添加

install(TARGETS
  node_helloworld_class
  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()

learning_node/src/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>learning_node_cpp</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="wynhelloworld@gmail.com">wyn</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

3. 编译功能包

cd ~/dev_ws
colcon build   # 编译工作空间的所有功能包

4. 设置环境变量

cd ~/dev_ws
source install/local_setup.sh # 仅在当前终端生效
echo " source ~/dev_ws/install/local_setup.sh" >> ~/.bashrc # 所有终端均生效

5. 运行节点

ros2 run learning_node node_helloworld

image-20240512170215675

6. 查看节点

  • ros2 node list:查看正在运行的节点

    image-20240512170714292

  • ros2 node info [options] node_name:显示节点信息

    image-20240512170756327

参考链接

https://book.guyuehome.com/ROS2/2.%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5/2.3_%E8%8A%82%E7%82%B9/

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云朵c

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值