micro-ROS之esp32与ros2资料(freertos)

举报
zhangrelay 发表于 2021/09/17 23:21:37 2021/09/17
【摘要】 重中之重:micro.ros.org/docs/tutorials/core/overview/ 所有案例都是流畅稳定运行的。 比arduino+esp32+ros2稳定性好很多哦。 eps32复位重联,会有节点重名现象。  不过,rqt图正常 源码都是开放的,稍微改一下也是非常方便的。 #inclu...

重中之重:micro.ros.org/docs/tutorials/core/overview/


所有案例都是流畅稳定运行的。

比arduino+esp32+ros2稳定性好很多哦。


eps32复位重联,会有节点重名现象。

 不过,rqt图正常


源码都是开放的,稍微改一下也是非常方便的。


  
  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. std_msgs__msg__Int32 msg;
  20. void timer_callback(rcl_timer_t * timer, int64_t last_call_time)
  21. {
  22. RCLC_UNUSED(last_call_time);
  23. if (timer != NULL) {
  24. RCSOFTCHECK(rcl_publish(&publisher, &msg, NULL));
  25. msg.data++;
  26. }
  27. }
  28. void micro_ros_task(void * arg)
  29. {
  30. rcl_allocator_t allocator = rcl_get_default_allocator();
  31. rclc_support_t support;
  32. rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
  33. RCCHECK(rcl_init_options_init(&init_options, allocator));
  34. rmw_init_options_t* rmw_options = rcl_init_options_get_rmw_init_options(&init_options);
  35. // Static Agent IP and port can be used instead of autodisvery.
  36. RCCHECK(rmw_uros_options_set_udp_address(CONFIG_MICRO_ROS_AGENT_IP, CONFIG_MICRO_ROS_AGENT_PORT, rmw_options));
  37. //RCCHECK(rmw_uros_discover_agent(rmw_options));
  38. // create init_options
  39. RCCHECK(rclc_support_init_with_options(&support, 0, NULL, &init_options, &allocator));
  40. // create node
  41. rcl_node_t node;
  42. RCCHECK(rclc_node_init_default(&node, "esp32_int32_publisher", "", &support));
  43. // create publisher
  44. RCCHECK(rclc_publisher_init_default(
  45. &publisher,
  46. &node,
  47. ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
  48. "freertos_int32_publisher"));
  49. // create timer,
  50. rcl_timer_t timer;
  51. const unsigned int timer_timeout = 1000;
  52. RCCHECK(rclc_timer_init_default(
  53. &timer,
  54. &support,
  55. RCL_MS_TO_NS(timer_timeout),
  56. timer_callback));
  57. // create executor
  58. rclc_executor_t executor;
  59. RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator));
  60. RCCHECK(rclc_executor_add_timer(&executor, &timer));
  61. msg.data = 0;
  62. while(1){
  63. rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100));
  64. usleep(10000);
  65. }
  66. // free resources
  67. RCCHECK(rcl_publisher_fini(&publisher, &node));
  68. RCCHECK(rcl_node_fini(&node));
  69. vTaskDelete(NULL);
  70. }
  71. void app_main(void)
  72. {
  73. #ifdef UCLIENT_PROFILE_UDP
  74. // Start the networking if required
  75. ESP_ERROR_CHECK(uros_network_interface_initialize());
  76. #endif // UCLIENT_PROFILE_UDP
  77. //pin micro-ros task in APP_CPU to make PRO_CPU to deal with wifi:
  78. xTaskCreate(micro_ros_task,
  79. "uros_task",
  80. CONFIG_MICRO_ROS_APP_STACK,
  81. NULL,
  82. CONFIG_MICRO_ROS_APP_TASK_PRIO,
  83. NULL);
  84. }

数值每次是加1:

msg.data++;
 

修改为+10;

节点名称:

RCCHECK(rclc_node_init_default(&node, "esp32_int32_publisher", "", &support));
 

修改一下:

RCCHECK(rclc_node_init_default(&node, "love_esp32_pub", "", &support));

主题名称:


  
  1.     RCCHECK(rclc_publisher_init_default(
  2.         &publisher,
  3.         &node,
  4.         ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
  5.         "freertos_int32_publisher"));

修改一下:

love_esp32_pub

(随意修改看看效果?)


 


超级稳定,测试半天,无任何丢包。


 

 

 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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