(c++面向对象版)ROS2 发布者节点参考代码

#include 
#include 
#include 
#include 
#include 
#include "sys/time.h"
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using namespace std;
using namespace rclcpp;
class cnode;
cnode * pnode;
//C++
class cnode:public rclcpp::Node
{
    public:
    cnode(string str):Node(str)    //constructor
    {

    }
    
    uint32_t count1=0;
    stringstream ss;
    std_msgs::msg::String MESSAGE1;
    rclcpp::TimerBase::SharedPtr timer_;	
    std::shared_ptr> ppublisher=0;
    void timer_callback()
    {
        ss.str("");
        ss<<"hello nice to meet you"<create_publisher("topicName",10);
        ppublisher->publish(MESSAGE1);  //error occured
    }
};

int main(int argc,char ** argv)
{
    rclcpp::init(argc,argv);
    cnode mynode("talker"); 
    mynode.timer_=mynode.create_wall_timer(500ms,[&mynode]()->void {return mynode.cnode::timer_callback();});
    pnode=&mynode;
    std::shared_ptr p(pnode);
    spin(p);
    rclcpp::shutdown();
    return 0;
}

你可能感兴趣的:(c++,机器人,ros2,嵌入式系统,ubuntu)