ROS2学习笔记-订阅发布

        1.订阅发布实现

        ros2节点之间有4种通讯方式,本章节记录订阅发布这种通讯方式,ros2的功能拆分比较独立,订阅者和发布者分别用不同的模板类表示,定义订阅发布模板类的时候需要指定消息类型,订阅发布具体的类的构造由节点成员函数进行构造。具体实现如下:

#ifndef TSUBER_HPP
#define TSUBER_HPP

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "rclcpp/publisher.hpp"

class tsuber:public rclcpp::Node
{
public:
    tsuber(std::string msg);
    void timer_callback();
    void sub_callback(std_msgs::msg::String msg);
private:
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr puber;
    rclcpp::Subscription<std_msgs::msg::String>::SharedPtr suber;
    rclcpp::TimerBase::SharedPtr timer_;
};

#endif // TSUBER_HPP
#include "tsuber.hpp"
using ::std::placeholders::_1;
tsuber::tsuber(std::string msg):rclcpp::Node(msg)
{
    RCLCPP_INFO(this->get_logger(),"%s",msg.c_str());
    puber = this->create_publisher<std_msgs::msg::String>("hello12345",10);
    timer_ = this->create_wall_timer(std::chrono::milliseconds(100),std::bind(&tsuber::timer_callback,this));
    suber = this->create_subscription<std_msgs::msg::String>("hello12345",10,std::bind(&tsuber::sub_callback,this,_1));
}

void tsuber::timer_callback()
{
    std_msgs::msg::String msg;
    
    msg.data = "hello";
    puber->publish(msg);
}

void tsuber::sub_callback(std_msgs::msg::String msg)
{
    RCLCPP_INFO(this->get_logger(),"%s",msg.data.c_str());
}

        注意: 使用成员函数当做回调函数的时候会使用

std::bind(&tsuber::sub_callback,this,_1)

std::bind可以看作一个通用的函数适配器,它接受一个可调用对象,生成一个新的可调用对象来适应原对象的参数列表

  • 将可调用对象和其参数绑定成一个仿函数
  • 只绑定部分参数,减少可调用对象传入的参数

 使用方式:

        auto newCallable = bind(callable, arg_list);
该形式表达的意思是:当调用newCallable时,会调用callable,并传给它arg_list中的参数。arg_list中的参数可能包含形如_n的名字。其中n是一个整数,这些参数是占位符,表示newCallable的参数,它们占据了传递给newCallable的参数的位置。数值n表示生成的可调用对象中参数的位置:_1为newCallable的第一个参数,_2为第二个参数,以此类推。_n使用的时候需要声明或者直接使用std域直接定义。


预绑定的参数是以值传递的形式,不预绑定的参数要用std::placeholders(占位符)的形式占位,从_1开始,依次递增,是以引用传递的形式;


std::placeholders表示新的可调用对象的第几个参数,而且与原函数的该占位符所在位置的进行匹配;


bind绑定类成员函数时,第一个参数表示对象的成员函数的指针,第二个参数表示对象的地址,这是因为对象的成员函数需要有this指针。并且编译器不会将对象的成员函数隐式转换成函数指针,需要通过&手动转换;


std::bind的返回值是可调用实体,可以直接赋给std::function。
 

using ::std::placeholders::_1;//直接在bind里面使用_1即可。
std::placeholders::_1 //或者把这个填第一个占位符。

主程序如下:

#include <iostream>
#include "tsuber.hpp"
using namespace std;

int main(int argc,char **argv)
{
    rclcpp::init(argc,argv);
    auto p = std::make_shared<tsuber>("hello");
    rclcpp::spin(p);
    rclcpp::shutdown();
    return 0;
}

 2.编译程序

编译程序需要在CMakeLists.txt和package.xml里面添加执行程序:
CMakeLists.txt添加:

find_package(std_msgs REQUIRED)

add_executable(sub ./src/main.cpp ./src/tsuber.cpp)
ament_target_dependencies(sub rclcpp std_msgs)

install(TARGETS
  sub
  DESTINATION lib/${PROJECT_NAME}
)

package.xml添加: 

<depend>std_msgs</depend>

 3.编译程序

colcon build

4.测试程序

source ./install/setup.bash
ros2 run sub sub

打开cli输入以上命令执行程序:

也可以另开窗口打开rqt 查看订阅发布:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值