码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • (C++版本)ros2发布者节点publisher示例源代码(改良版)


    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include "sys/time.h"
    7. #include "rclcpp/rclcpp.hpp"
    8. #include "std_msgs/msg/string.hpp"
    9. using namespace std;
    10. using namespace rclcpp;
    11. uint32_t count1=0;
    12. stringstream ss;
    13. std_msgs::msg::String message;
    14. rclcpp::TimerBase::SharedPtr timer_;
    15. //define a publisher
    16. //define a ros2 timer_process function
    17. class mynode:public Node
    18. {
    19. public:
    20. std::shared_ptr> ppublisher;
    21. mynode(string str ):Node(str)
    22. {
    23. std::cout<<"enter mynode structure"<
    24. ppublisher=this->create_publisher("topicName",10);
    25. timer_=this->create_wall_timer(100ms, bind(&mynode::timer_callback,this) );
    26. std::cout<<"leave mynode structure"<
    27. }
    28. void timer_callback()
    29. {
    30. std::cout<<"enter timer_callback"<
    31. ss.str()="";
    32. ss<<"hi im glad to see you"<
    33. message.data=ss.str();
    34. cout<
    35. //publish a topic
    36. // Publisher2.publish(message);
    37. ppublisher->publish(message);
    38. std::cout<<"leave timer_callback"<
    39. }
    40. };
    41. //define a node
    42. //define a publisher
    43. //utility publiser to publish a topic
    44. int main(int argc,char ** argv)
    45. {
    46. rclcpp::init(argc,argv);
    47. // Rate rate(1);
    48. std::cout<<"before spin"<
    49. spin(std::make_shared("nodeName")); //execute the timer_proc//
    50. std::cout<<"after spin"<
    51. rclcpp::shutdown();
    52. return 0;
    53. }

    说明:上面这个代码里面有个变量看来是没有用的timer_,但是一旦删除整个代码编译时候虽然不会报错,但是代码无法正常运行。

  • 相关阅读:
    14:00面试,14:06就出来了,问的问题有点变态。。。
    JUC学习笔记——进程与线程
    RabbitMQ(六)仲裁队列、流式队列、异地容灾(联邦队列Federation Queue)
    征文:中秋,你愿意和她一起打地(月)鼠(饼)吗?
    《痞子衡嵌入式半月刊》 第 49 期
    java计算机毕业设计水质监测数据采集系统源码+系统+数据库+lw文档+mybatis+运行部署
    求臻人故事 | 在求臻医学的沃土中,我像竹子般茁壮成长
    DETR精读笔记
    如何在 Nginx Proxy Manager(NPM)上部署静态网站
    IDEA设置方法分割线
  • 原文地址:https://blog.csdn.net/geniusChinaHN/article/details/133464176
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号