|
| 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/int32.h> |
| 14 | +#include <rclc/rclc.h> |
| 15 | +#include <rclc/executor.h> |
| 16 | +#include <rmw_microros/rmw_microros.h> |
| 17 | +#include "uxr/client/config.h" |
| 18 | + |
| 19 | +#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);}} |
| 20 | +#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);}} |
| 21 | + |
| 22 | +rcl_publisher_t publisher_1; |
| 23 | +rcl_publisher_t publisher_2; |
| 24 | + |
| 25 | +void thread_1(void * arg) |
| 26 | +{ |
| 27 | + std_msgs__msg__Int32 msg; |
| 28 | + msg.data = 0; |
| 29 | + while(1){ |
| 30 | + RCSOFTCHECK(rcl_publish(&publisher_1, &msg, NULL)); |
| 31 | + msg.data++; |
| 32 | + usleep(1000000); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +void thread_2(void * arg) |
| 37 | +{ |
| 38 | + std_msgs__msg__Int32 msg; |
| 39 | + msg.data = 0; |
| 40 | + while(1){ |
| 41 | + RCSOFTCHECK(rcl_publish(&publisher_2, &msg, NULL)); |
| 42 | + msg.data--; |
| 43 | + usleep(500000); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +void micro_ros_task(void * arg) |
| 48 | +{ |
| 49 | + rcl_allocator_t allocator = rcl_get_default_allocator(); |
| 50 | + rclc_support_t support; |
| 51 | + |
| 52 | + rcl_init_options_t init_options = rcl_get_zero_initialized_init_options(); |
| 53 | + RCCHECK(rcl_init_options_init(&init_options, allocator)); |
| 54 | + rmw_init_options_t* rmw_options = rcl_init_options_get_rmw_init_options(&init_options); |
| 55 | + |
| 56 | + // Static Agent IP and port can be used instead of autodisvery. |
| 57 | + RCCHECK(rmw_uros_options_set_udp_address(CONFIG_MICRO_ROS_AGENT_IP, CONFIG_MICRO_ROS_AGENT_PORT, rmw_options)); |
| 58 | + |
| 59 | + // create init_options |
| 60 | + RCCHECK(rclc_support_init_with_options(&support, 0, NULL, &init_options, &allocator)); |
| 61 | + |
| 62 | + // create node |
| 63 | + rcl_node_t node; |
| 64 | + RCCHECK(rclc_node_init_default(&node, "multithread_node", "", &support)); |
| 65 | + |
| 66 | + // create two publishers |
| 67 | + RCCHECK(rclc_publisher_init_default( |
| 68 | + &publisher_1, |
| 69 | + &node, |
| 70 | + ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), |
| 71 | + "multithread_publisher_1")); |
| 72 | + |
| 73 | + RCCHECK(rclc_publisher_init_default( |
| 74 | + &publisher_2, |
| 75 | + &node, |
| 76 | + ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), |
| 77 | + "multithread_publisher_2")); |
| 78 | + |
| 79 | + |
| 80 | + xTaskCreate(thread_1, |
| 81 | + "thread_1", |
| 82 | + CONFIG_MICRO_ROS_APP_STACK, |
| 83 | + NULL, |
| 84 | + CONFIG_MICRO_ROS_APP_TASK_PRIO, |
| 85 | + NULL); |
| 86 | + |
| 87 | + xTaskCreate(thread_2, |
| 88 | + "thread_2", |
| 89 | + CONFIG_MICRO_ROS_APP_STACK, |
| 90 | + NULL, |
| 91 | + CONFIG_MICRO_ROS_APP_TASK_PRIO + 1, |
| 92 | + NULL); |
| 93 | + |
| 94 | + while(1){ |
| 95 | + sleep(100); |
| 96 | + } |
| 97 | + |
| 98 | + // free resources |
| 99 | + RCCHECK(rcl_publisher_fini(&publisher_1, &node)); |
| 100 | + RCCHECK(rcl_publisher_fini(&publisher_2, &node)); |
| 101 | + RCCHECK(rcl_node_fini(&node)); |
| 102 | + |
| 103 | + vTaskDelete(NULL); |
| 104 | +} |
| 105 | + |
| 106 | +void app_main(void) |
| 107 | +{ |
| 108 | +#ifdef UCLIENT_PROFILE_UDP |
| 109 | + // Start the networking if required |
| 110 | + ESP_ERROR_CHECK(uros_network_interface_initialize()); |
| 111 | +#endif // UCLIENT_PROFILE_UDP |
| 112 | + |
| 113 | + //pin micro-ros task in APP_CPU to make PRO_CPU to deal with wifi: |
| 114 | + xTaskCreate(micro_ros_task, |
| 115 | + "uros_task", |
| 116 | + CONFIG_MICRO_ROS_APP_STACK, |
| 117 | + NULL, |
| 118 | + CONFIG_MICRO_ROS_APP_TASK_PRIO, |
| 119 | + NULL); |
| 120 | +} |
0 commit comments