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

news2024/9/20 6:15:50

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

学习笔记流水账-推荐阅读原书。

第二章主要就是一些ROS的基本概念,其实ROS1和ROS2的基本概念很多都是类似的。

ROS2机器人个人教程博客汇总(2021共6套)


如何更好更快的熟悉ROS2?多操作多练习就好。

比如命令行接口CLI。不会就用-h,帮助一下即可。

比如ros2 -h

zhangrelay@LAPTOP-5REQ7K1L:~$ ros2 -h
usage: ros2 [-h] [--use-python-default-buffering] Call `ros2 <command> -h` for more detailed usage. ...

ros2 is an extensible command-line tool for ROS 2.

options:
  -h, --help            show this help message and exit
  --use-python-default-buffering
                        Do not force line buffering in stdout and instead use the python default buffering, which might be affected by PYTHONUNBUFFERED/-u and depends on
                        whatever stdout is interactive or not

Commands:
  action     Various action related sub-commands
  bag        Various rosbag related sub-commands
  component  Various component related sub-commands
  daemon     Various daemon related sub-commands
  doctor     Check ROS setup and other potential issues
  interface  Show information about ROS interfaces
  launch     Run a launch file
  lifecycle  Various lifecycle related sub-commands
  multicast  Various multicast related sub-commands
  node       Various node related sub-commands
  param      Various param related sub-commands
  pkg        Various package related sub-commands
  run        Run a package specific executable
  security   Various security related sub-commands
  service    Various service related sub-commands
  topic      Various topic related sub-commands
  wtf        Use `wtf` as alias to `doctor`

  Call `ros2 <command> -h` for more detailed usage.

英文如果不熟悉多用翻译软件查一查即可。

比如节点:

ros2 node -h

zhangrelay@LAPTOP-5REQ7K1L:~$ ros2 node -h
usage: ros2 node [-h] Call `ros2 node <command> -h` for more detailed usage. ...

Various node related sub-commands

options:
  -h, --help            show this help message and exit

Commands:
  info  Output information about a node
  list  Output a list of available nodes

  Call `ros2 node <command> -h` for more detailed usage.

ros2命令支持tab键自动完成。键入ros2,然后按tab键两次以查看可能的关键词。关键词的参数也可以用tab键发现。

比如cpp可执行示例:

ros2 pkg executables demo_nodes_cpp

zhangrelay@LAPTOP-5REQ7K1L:~$ ros2 pkg executables demo_nodes_cpp
demo_nodes_cpp add_two_ints_client
demo_nodes_cpp add_two_ints_client_async
demo_nodes_cpp add_two_ints_server
demo_nodes_cpp allocator_tutorial
demo_nodes_cpp content_filtering_publisher
demo_nodes_cpp content_filtering_subscriber
demo_nodes_cpp even_parameters_node
demo_nodes_cpp list_parameters
demo_nodes_cpp list_parameters_async
demo_nodes_cpp listener
demo_nodes_cpp listener_best_effort
demo_nodes_cpp listener_serialized_message
demo_nodes_cpp one_off_timer
demo_nodes_cpp parameter_blackboard
demo_nodes_cpp parameter_event_handler
demo_nodes_cpp parameter_events
demo_nodes_cpp parameter_events_async
demo_nodes_cpp reuse_timer
demo_nodes_cpp set_and_get_parameters
demo_nodes_cpp set_and_get_parameters_async
demo_nodes_cpp talker
demo_nodes_cpp talker_loaned_message
demo_nodes_cpp talker_serialized_message

如果要运行demo_nodes_cpp中的talker。

ros2 run demo_nodes_cpp talker

这样就ok!

zhangrelay@LAPTOP-5REQ7K1L:~$ ros2 run demo_nodes_cpp talker
[INFO] [1673837054.383326900] [talker]: Publishing: 'Hello World: 1'
[INFO] [1673837055.383390600] [talker]: Publishing: 'Hello World: 2'
[INFO] [1673837056.383153600] [talker]: Publishing: 'Hello World: 3'
[INFO] [1673837057.382968400] [talker]: Publishing: 'Hello World: 4'
[INFO] [1673837058.383033500] [talker]: Publishing: 'Hello World: 5'
[INFO] [1673837059.383368900] [talker]: Publishing: 'Hello World: 6'
[INFO] [1673837060.382784200] [talker]: Publishing: 'Hello World: 7'
[INFO] [1673837061.383054200] [talker]: Publishing: 'Hello World: 8'
[INFO] [1673837062.383405500] [talker]: Publishing: 'Hello World: 9'
[INFO] [1673837063.382757200] [talker]: Publishing: 'Hello World: 10'
[INFO] [1673837064.383403400] [talker]: Publishing: 'Hello World: 11'
[INFO] [1673837065.383398900] [talker]: Publishing: 'Hello World: 12'
[INFO] [1673837066.383364000] [talker]: Publishing: 'Hello World: 13'
[INFO] [1673837067.383122300] [talker]: Publishing: 'Hello World: 14'

书中最大的特色就是图示非常赞!

其中:

频率1hz,topic:/chatter;node:/talker。

源码如下:

#include <chrono>
#include <cstdio>
#include <memory>
#include <utility>

#include "rclcpp/rclcpp.hpp"
#include "rclcpp_components/register_node_macro.hpp"

#include "std_msgs/msg/string.hpp"

#include "demo_nodes_cpp/visibility_control.h"

using namespace std::chrono_literals;

namespace demo_nodes_cpp
{
// Create a Talker class that subclasses the generic rclcpp::Node base class.
// The main function below will instantiate the class as a ROS node.
class Talker : public rclcpp::Node
{
public:
  DEMO_NODES_CPP_PUBLIC
  explicit Talker(const rclcpp::NodeOptions & options)
  : Node("talker", options)
  {
    // Create a function for when messages are to be sent.
    setvbuf(stdout, NULL, _IONBF, BUFSIZ);
    auto publish_message =
      [this]() -> void
      {
        msg_ = std::make_unique<std_msgs::msg::String>();
        msg_->data = "Hello World: " + std::to_string(count_++);
        RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", msg_->data.c_str());
        // Put the message into a queue to be processed by the middleware.
        // This call is non-blocking.
        pub_->publish(std::move(msg_));
      };
    // Create a publisher with a custom Quality of Service profile.
    rclcpp::QoS qos(rclcpp::KeepLast(7));
    pub_ = this->create_publisher<std_msgs::msg::String>("chatter", qos);

    // Use a timer to schedule periodic message publishing.
    timer_ = this->create_wall_timer(1s, publish_message);
  }

private:
  size_t count_ = 1;
  std::unique_ptr<std_msgs::msg::String> msg_;
  rclcpp::Publisher<std_msgs::msg::String>::SharedPtr pub_;
  rclcpp::TimerBase::SharedPtr timer_;
};

}  // namespace demo_nodes_cpp

RCLCPP_COMPONENTS_REGISTER_NODE(demo_nodes_cpp::Talker)

频率1hz,

 timer_ = this->create_wall_timer(1s, publish_message);

topic:/chatter;

pub_ = this->create_publisher<std_msgs::msg::String>("chatter", qos);

node:/talker。

explicit Talker(const rclcpp::NodeOptions & options)
  : Node("talker", options)

要熟悉各类命令行使用。如

  • node list

  • topic list

  • topic info

  • interface list

  • interface show

  • topic echo

按照这个过程继续启动listener。

这样就有订阅啦。

#include "rclcpp/rclcpp.hpp"
#include "rclcpp_components/register_node_macro.hpp"

#include "std_msgs/msg/string.hpp"

#include "demo_nodes_cpp/visibility_control.h"

namespace demo_nodes_cpp
{
// Create a Listener class that subclasses the generic rclcpp::Node base class.
// The main function below will instantiate the class as a ROS node.
class Listener : public rclcpp::Node
{
public:
  DEMO_NODES_CPP_PUBLIC
  explicit Listener(const rclcpp::NodeOptions & options)
  : Node("listener", options)
  {
    // Create a callback function for when messages are received.
    // Variations of this function also exist using, for example UniquePtr for zero-copy transport.
    setvbuf(stdout, NULL, _IONBF, BUFSIZ);
    auto callback =
      [this](const std_msgs::msg::String::SharedPtr msg) -> void
      {
        RCLCPP_INFO(this->get_logger(), "I heard: [%s]", msg->data.c_str());
      };
    // Create a subscription to the topic which can be matched with one or more compatible ROS
    // publishers.
    // Note that not all publishers on the same topic with the same type will be compatible:
    // they must have compatible Quality of Service policies.
    sub_ = create_subscription<std_msgs::msg::String>("chatter", 10, callback);
  }

private:
  rclcpp::Subscription<std_msgs::msg::String>::SharedPtr sub_;
};

}  // namespace demo_nodes_cpp

RCLCPP_COMPONENTS_REGISTER_NODE(demo_nodes_cpp::Listener)

图形化工具也非常棒!(๑•̀ㅂ•́)و✧

ros2 run rqt_graph rqt_graph

现在,只需在运行程序的终端中按Ctrl+C,即可停止所有程序。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/165810.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Linux chgrp 命令

Linux chgrp&#xff08;英文全拼&#xff1a;change group&#xff09;命令用于变更文件或目录的所属群组。与 chown 命令不同&#xff0c;chgrp 允许普通用户改变文件所属的组&#xff0c;只要该用户是该组的一员。在 UNIX 系统家族里&#xff0c;文件或目录权限的掌控以拥有…

(一)Jenkins部署、基础配置

目录 1、前言 1.1、Jenkins是什么 1.2、jenkins有什么用 2、 Jenkins安装 2.1、jdk安装 2.2、安装Jenkins 3、Jenkins配置 3.1、解锁Jenkins 3.2、插件安装 3.3、创建管理员 3.4、实例配置 4、汉化 4.1、下载Locale插件 4.2、设置为中文 5、设置中文失效解决步骤 1…

U-Boot 之零 源码文件、启动阶段(TPL、SPL)、FALCON、设备树

最近&#xff0c;工作重心要从裸机开发转移到嵌入式 Linux 系统开发&#xff0c;在之前的博文 Linux 之八 完整嵌入式 Linux 环境、&#xff08;交叉&#xff09;编译工具链、CPU 体系架构、嵌入式系统构建工具 中详细介绍了嵌入式 Linux 环境&#xff0c;接下来就是重点学习一…

【Spring6源码・AOP】代理对象的创建

前三篇Spring IOC的源码解析与这一章的AOP是紧密相连的&#xff1a; 【Spring6源码・IOC】BeanDefinition的加载 【Spring6源码・IOC】Bean的实例化 【Spring6源码・IOC】Bean的初始化 - 终结篇 首先介绍我们本章的demo&#xff1a; 一个接口&#xff0c;一个实现&#xf…

【论文速递】ECCV2022 - 开销聚合与四维卷积Swin Transformer_小样本分割

【论文速递】ECCV2022 - 开销聚合与四维卷积Swin Transformer_小样本分割 【论文原文】&#xff1a;Cost Aggregation with 4D Convolutional Swin Transformer for Few-Shot Segmentation 获取地址&#xff1a;https://arxiv.org/pdf/2207.10866.pdf博主关键词&#xff1a; …

紧聚焦涡旋光束app设计-VVB2.0

紧聚焦涡旋光束app设计-VVB2.0前言界面预览功能演示写在最后前言 时隔几个月&#xff0c;花了点时间&#xff0c;将之前用matlab设计的app紧聚焦涡旋光束matlab gui设计进行一次修改&#xff0c;这次发布2.0版本&#xff0c;本次修改的范围主要是将原来的界面进行重做&#xf…

软件设计师中级复习小总结

软件设计师中级复习小总结 计算机与体系结构 K 1024 k 1000 B 字节 b 位 1字节 8位 8bit(位)1Byte(字节) 1024Byte(字节)1KB KB&#xff0c;MB&#xff0c;GB之间的换算关系是&#xff1a;1024KB1MB&#xff0c;1024MB1GB&#xff0c;1024GB1TB K&#xff0c;M&#x…

DevOps 实战概述

一、背景越来越多的团队使用DevOps&#xff0c;个人觉得原因有二&#xff0c;其一市场需求&#xff0c;从瀑布到敏捷的过程能看出市场就是需要团队响应快&#xff0c;小步快跑&#xff0c;风险低效率高&#xff0c;但是敏捷只解决了开发团队的问题并没有解决运维团队的问题&…

16、Javaweb_ajax的JSjQuery实现方式JSON_Java对象互转用户校验案例

AJAX&#xff1a; 1. 概念&#xff1a; ASynchronous JavaScript And XML 异步的JavaScript 和 XML 1. 异步和同步&#xff1a;客户端和服务器端相互通信的基础上 * 客户端必须等待服务器端的响应。在等待的期间客户端不能做其他操作。 * 客户端不需要…

[LeetCode周赛复盘] 第 328 场周赛20230115

[LeetCode周赛复盘] 第 328 场周赛20230115 一、本周周赛总结二、 [Easy] 6291. 数组元素和与数字和的绝对差1. 题目描述2. 思路分析3. 代码实现三、[Medium] 6292. 子矩阵元素加 11. 题目描述2. 思路分析3. 代码实现四、[Medium] 6293. 统计好子数组的数目1. 题目描述2. 思路分…

文献阅读总结--合成生物学工程促进大肠杆菌中莽草酸的高水平积累

题目&#xff1a;Systems engineering of Escherichia coli for high-level shikimate production (ME 2022) 0 前言 本版块内容为记录阅读的文献内容总结经典方法手段。本文内容来自相关文献&#xff0c;在文末做来源进行详细说明对文献中内容不做真实性评价。 1 具体内容 …

标准化和归一化概念澄清与梳理

标准化和归一化是特征缩放(feature scalingscaling)的主要手段&#xff0c;其核心原理可以简单地理解为&#xff1a;让所有元素先减去同一个数&#xff0c;然后再除以另一个数&#xff0c;在数轴上的效果就是&#xff1a;先将数据集整体平移到有某个位置&#xff0c;然后按比例…

【C进阶】动态内存管理

家人们欢迎来到小姜的世界&#xff0c;<<点此>>传送门 这里有详细的关于C/C/Linux等的解析课程&#xff0c;家人们赶紧冲鸭&#xff01;&#xff01;&#xff01; 客官&#xff0c;码字不易&#xff0c;来个三连支持一下吧&#xff01;&#xff01;&#xff01;关注…

Spring 中最常用的 11 个扩展点

目录 1.自定义拦截器 2.获取Spring容器对象 2.1 BeanFactoryAware接口 2.2 ApplicationContextAware接口 3.全局异常处理 4.类型转换器 5.导入配置 5.1 普通类 5.2 配置类 5.3 ImportSelector 5.4 ImportBeanDefinitionRegistrar 6.项目启动时 7.修改BeanDefiniti…

MySQL高级【MVCC原理分析】

1&#xff1a;MVCC1.1&#xff1a;基本概念1). 当前读 读取的是记录的最新版本&#xff0c;读取时还要保证其他并发事务不能修改当前记录&#xff0c;会对读取的记录进行加 锁。对于我们日常的操作&#xff0c;如&#xff1a;select ... lock in share mode(共享锁)&#xff0c…

技术人员和非技术人员如何写出优质博客?-涵子的个人想法

大家好&#xff0c;我是涵子。今天&#xff0c;我们来沉重的聊聊一个大家都很关心的一个问题&#xff1a;技术人员和非技术人员如何写出优质博客&#xff1f; 目录 前言 初写博客&#xff0c;仰望大师 中段时期&#xff0c;无粉无赞 优质博客&#xff0c;涨粉涨赞 优质内容…

前端编写邮件html各邮箱兼容及注意事项

近期由于项目需要&#xff0c;第一次编写邮件html模板&#xff0c;发现各种邮箱兼容问题&#xff0c;尤其是windows自带的邮箱outlook兼容性极差&#xff0c;在此简单做下记录。 注意事项&#xff08;全局样式规则&#xff09; 使用越垃圾的样式越好&#xff0c;绝大部分css3…

Spring面试题

Spring概述&#xff08;10&#xff09; https://blog.csdn.net/zhang150114/article/details/90478753 什么是spring? Spring是一个轻量级JavaEE开发框架&#xff0c;最早有Rod Johnson创建&#xff0c;目的是为了解决企业级应用开发的**业务逻辑层和其他各层的耦合问题。*…

Eclipse 连接 SQL Server 数据库教程

&#x1f388; 作者&#xff1a;Linux猿 &#x1f388; 简介&#xff1a;CSDN博客专家&#x1f3c6;&#xff0c;华为云享专家&#x1f3c6;&#xff0c;Linux、C/C、云计算、物联网、面试、刷题、算法尽管咨询我&#xff0c;关注我&#xff0c;有问题私聊&#xff01; &…

JUC面试(一)——JUCJMMvolatile 1.0

JUC&JMM JMM JUC&#xff08;java.util.concurrent&#xff09; 进程和线程 进程&#xff1a;后台运行的程序&#xff08;我们打开的一个软件&#xff0c;就是进程&#xff09;&#xff0c;资源分配单位线程&#xff1a;轻量级的进程&#xff0c;并且一个进程包含多个线程…