-First Steps with ROS2 .1

Posted zhangrelay

tags:

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

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,即可停止所有程序。

以上是关于-First Steps with ROS2 .1的主要内容,如果未能解决你的问题,请参考以下文章

Python - 7 Steps to Mastering Machine Learning With Python

How to setup a slave for replication in 6 simple steps with Percona XtraBackup

ROS2之OpenCV基础代码对比foxy~galactic~humble

关于“ROS2 Topic-Statistics-Tutorial编译出错”的思考

win11+ros2+autoware的自动驾驶第一弹

win11+ros2+autoware的自动驾驶第一弹