suraj0152

chatter-sub-pub

Mar 19th, 2024 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.85 KB | Software | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4.  
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "esp_log.h"
  8. #include "esp_system.h"
  9.  
  10. #include <uros_network_interfaces.h>
  11. #include <rcl/rcl.h>
  12. #include <rcl/error_handling.h>
  13. #include <std_msgs/msg/string.h> // Assuming the message type is String
  14. #include <rclc/rclc.h>
  15. #include <rclc/executor.h>
  16.  
  17. #ifdef CONFIG_MICRO_ROS_ESP_XRCE_DDS_MIDDLEWARE
  18. #include <rmw_microros/rmw_microros.h>
  19. #endif
  20.  
  21. #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);}}
  22. #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);}}
  23.  
  24. rcl_subscription_t subscriber;
  25. std_msgs__msg__String recv_msg;
  26. rcl_publisher_t publisher;
  27. std_msgs__msg__String send_msg;
  28.  
  29. void subscription_callback(const void * msgin)
  30. {
  31.     const std_msgs__msg__String * msg = (const std_msgs__msg__String *)msgin;
  32.     printf("Received: %s\n", msg->data.data);
  33.  
  34.     // Publish the received msg from subs on another topic
  35.     RCSOFTCHECK(rcl_publish(&publisher, msg, NULL));
  36.     printf("Published: %s\n", msg->data.data);
  37. }
  38.  
  39. void micro_ros_task(void * arg)
  40. {
  41.     rcl_allocator_t allocator = rcl_get_default_allocator();
  42.     rclc_support_t support;
  43.  
  44.     // Create init_options.
  45.     rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
  46.     RCCHECK(rcl_init_options_init(&init_options, allocator));
  47.  
  48. #ifdef CONFIG_MICRO_ROS_ESP_XRCE_DDS_MIDDLEWARE
  49.     rmw_init_options_t* rmw_options = rcl_init_options_get_rmw_init_options(&init_options);
  50.  
  51.     // Static Agent IP and port can be used instead of autodisvery.
  52.     RCCHECK(rmw_uros_options_set_udp_address(CONFIG_MICRO_ROS_AGENT_IP, CONFIG_MICRO_ROS_AGENT_PORT, rmw_options));
  53.     //RCCHECK(rmw_uros_discover_agent(rmw_options));
  54. #endif
  55.     // Setup support structure.
  56.     RCCHECK(rclc_support_init_with_options(&support, 0, NULL, &init_options, &allocator));
  57.  
  58.     // Create node.
  59.     rcl_node_t node = rcl_get_zero_initialized_node();
  60.     RCCHECK(rclc_node_init_default(&node, "string_subscriber_publisher_rclc", "", &support));
  61.  
  62.     // Create subscriber.
  63.     RCCHECK(rclc_subscription_init_default(
  64.         &subscriber,
  65.         &node,
  66.         ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, String),
  67.         "chatter"));
  68.  
  69.     // Create publisher.
  70.     RCCHECK(rclc_publisher_init_default(
  71.         &publisher,
  72.         &node,
  73.         ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, String),
  74.         "published_chatter"));
  75.  
  76.     // Create executor.
  77.     rclc_executor_t executor = rclc_executor_get_zero_initialized_executor();
  78.     RCCHECK(rclc_executor_init(&executor, &support.context, 1, &allocator));
  79.     unsigned int rcl_wait_timeout = 1000;   // in ms
  80.     RCCHECK(rclc_executor_set_timeout(&executor, RCL_MS_TO_NS(rcl_wait_timeout)));
  81.  
  82.     // Add subscriber to executor.
  83.     RCCHECK(rclc_executor_add_subscription(&executor, &subscriber, &recv_msg, &subscription_callback, ON_NEW_DATA));
  84.  
  85.     // Spin forever.
  86.     while(1){
  87.         rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100));
  88.         usleep(100000);
  89.     }
  90.  
  91.     // Free resources.
  92.     RCCHECK(rcl_subscription_fini(&subscriber, &node));
  93.     RCCHECK(rcl_publisher_fini(&publisher, &node));
  94.     RCCHECK(rcl_node_fini(&node));
  95.  
  96.     vTaskDelete(NULL);
  97. }
  98.  
  99. void app_main(void)
  100. {
  101. #if defined(CONFIG_MICRO_ROS_ESP_NETIF_WLAN) || defined(CONFIG_MICRO_ROS_ESP_NETIF_ENET)
  102.     ESP_ERROR_CHECK(uros_network_interface_initialize());
  103. #endif
  104.  
  105.     //pin micro-ros task in APP_CPU to make PRO_CPU to deal with wifi:
  106.     xTaskCreate(micro_ros_task,
  107.             "uros_task",
  108.             CONFIG_MICRO_ROS_APP_STACK,
  109.             NULL,
  110.             CONFIG_MICRO_ROS_APP_TASK_PRIO,
  111.             NULL);
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment