micro-ROS之esp32与ros2发布pub与接收sub

举报
zhangrelay 发表于 2021/09/17 22:38:15 2021/09/17
【摘要】 先看源码 #include <string.h>#include <stdio.h>#include <unistd.h> #include "freertos/FreeRTOS.h"#include "freertos/task.h"#include "esp_log.h"#include "e...

先看源码



  
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include "freertos/FreeRTOS.h"
  5. #include "freertos/task.h"
  6. #include "esp_log.h"
  7. #include "esp_system.h"
  8. #include <uros_network_interfaces.h>
  9. #include <rcl/rcl.h>
  10. #include <rcl/error_handling.h>
  11. #include <std_msgs/msg/int32.h>
  12. #include <rclc/rclc.h>
  13. #include <rclc/executor.h>
  14. #include <rmw_microros/rmw_microros.h>
  15. #include "uxr/client/config.h"
  16. #define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Aborting.\n",__LINE__,(int)temp_rc);vTaskDelete(NULL);}}
  17. #define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Continuing.\n",__LINE__,(int)temp_rc);}}
  18. rcl_publisher_t publisher;
  19. rcl_subscription_t subscriber;
  20. std_msgs__msg__Int32 send_msg;
  21. std_msgs__msg__Int32 recv_msg;
  22. void timer_callback(rcl_timer_t * timer, int64_t last_call_time)
  23. {
  24. (void) last_call_time;
  25. if (timer != NULL) {
  26. RCSOFTCHECK(rcl_publish(&publisher, &send_msg, NULL));
  27. printf("Sent: %d\n", send_msg.data);
  28. send_msg.data++;
  29. }
  30. }
  31. void subscription_callback(const void * msgin)
  32. {
  33. const std_msgs__msg__Int32 * msg = (const std_msgs__msg__Int32 *)msgin;
  34. printf("Received: %d\n", msg->data);
  35. }
  36. void micro_ros_task(void * arg)
  37. {
  38. rcl_allocator_t allocator = rcl_get_default_allocator();
  39. rclc_support_t support;
  40. // Create init_options.
  41. rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
  42. RCCHECK(rcl_init_options_init(&init_options, allocator));
  43. rmw_init_options_t* rmw_options = rcl_init_options_get_rmw_init_options(&init_options);
  44. // Use static agent IP and port.
  45. RCCHECK(rmw_uros_options_set_udp_address(CONFIG_MICRO_ROS_AGENT_IP, CONFIG_MICRO_ROS_AGENT_PORT, rmw_options));
  46. // Setup support structure.
  47. RCCHECK(rclc_support_init_with_options(&support, 0, NULL, &init_options, &allocator));
  48. // Create node.
  49. rcl_node_t node = rcl_get_zero_initialized_node();
  50. RCCHECK(rclc_node_init_default(&node, "esp32_int32_publisher_subscriber_rclc", "", &support));
  51. // Create publisher.
  52. RCCHECK(rclc_publisher_init_default(
  53. &publisher,
  54. &node,
  55. ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
  56. "esp32_int32_publisher"));
  57. // Create subscriber.
  58. RCCHECK(rclc_subscription_init_default(
  59. &subscriber,
  60. &node,
  61. ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
  62. "esp32_int32_subscriber"));
  63. // Create timer.
  64. rcl_timer_t timer = rcl_get_zero_initialized_timer();
  65. const unsigned int timer_timeout = 1000;
  66. RCCHECK(rclc_timer_init_default(
  67. &timer,
  68. &support,
  69. RCL_MS_TO_NS(timer_timeout),
  70. timer_callback));
  71. // Create executor.
  72. rclc_executor_t executor = rclc_executor_get_zero_initialized_executor();
  73. RCCHECK(rclc_executor_init(&executor, &support.context, 2, &allocator));
  74. unsigned int rcl_wait_timeout = 1000; // in ms
  75. RCCHECK(rclc_executor_set_timeout(&executor, RCL_MS_TO_NS(rcl_wait_timeout)));
  76. // Add timer and subscriber to executor.
  77. RCCHECK(rclc_executor_add_timer(&executor, &timer));
  78. RCCHECK(rclc_executor_add_subscription(&executor, &subscriber, &recv_msg, &subscription_callback, ON_NEW_DATA));
  79. // Spin forever.
  80. send_msg.data = 0;
  81. while(1){
  82. rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100));
  83. usleep(100000);
  84. }
  85. // Free resources.
  86. RCCHECK(rcl_subscription_fini(&subscriber, &node));
  87. RCCHECK(rcl_publisher_fini(&publisher, &node));
  88. RCCHECK(rcl_node_fini(&node));
  89. vTaskDelete(NULL);
  90. }
  91. void app_main(void)
  92. {
  93. #ifdef UCLIENT_PROFILE_UDP
  94. // Start the networking if required
  95. ESP_ERROR_CHECK(uros_network_interface_initialize());
  96. #endif // UCLIENT_PROFILE_UDP
  97. //pin micro-ros task in APP_CPU to make PRO_CPU to deal with wifi:
  98. xTaskCreate(micro_ros_task,
  99. "uros_task",
  100. CONFIG_MICRO_ROS_APP_STACK,
  101. NULL,
  102. CONFIG_MICRO_ROS_APP_TASK_PRIO,
  103. NULL);
  104. }

代码做稳定性测试,目前就是觉得节点好像不能自动注销呢。


 看这个红红!

这是一个节点,既有收又能发。


 

 


 

 

 

 

 

文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/120351704

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。