88
99#include "esp_log.h"
1010
11- #define RCLCPY_DOMAIN_ID 3
12- #define RCLCPY_AGENT_IP "192.168.10.111"
13- #define RCLCPY_AGENT_PORT "8888"
14-
1511rclcpy_context_t rclcpy_default_context = {
1612 .initialized = false,
1713};
@@ -41,7 +37,10 @@ static void* microros_zero_allocate(size_t number_of_elements, size_t size_of_el
4137 return ptr ;
4238}
4339
44- void common_hal_rclcpy_init (void ) {
40+ void rclcpy_reset (void ) {
41+ }
42+
43+ void common_hal_rclcpy_init (const char * agent_ip , const char * agent_port , int16_t domain_id ) {
4544 // Get empty allocator
4645 rcl_allocator_t custom_allocator = rcutils_get_zero_initialized_allocator ();
4746
@@ -65,29 +64,30 @@ void common_hal_rclcpy_init(void) {
6564 if (ret != RCL_RET_OK ) {
6665 ESP_LOGW ("RCLCPY" , "Options init failure: %d" , ret );
6766 }
68- ret = rcl_init_options_set_domain_id (& rclcpy_default_context .init_options , RCLCPY_DOMAIN_ID );
67+ if (domain_id < 0 ) {
68+ mp_raise_RuntimeError (MP_ERROR_TEXT ("Invalid ROS domain ID" ));
69+ }
70+ ret = rcl_init_options_set_domain_id (& rclcpy_default_context .init_options , domain_id );
6971 if (ret != RCL_RET_OK ) {
7072 ESP_LOGW ("RCLCPY" , "Options domain failure: %d" , ret );
7173 }
7274
7375 // Set up Agent
7476 rclcpy_default_context .rmw_options = rcl_init_options_get_rmw_init_options (& rclcpy_default_context .init_options );
75- ret = rmw_uros_options_set_udp_address (RCLCPY_AGENT_IP , RCLCPY_AGENT_PORT , rclcpy_default_context .rmw_options );
77+ ret = rmw_uros_options_set_udp_address (agent_ip , agent_port , rclcpy_default_context .rmw_options );
7678 if (ret != RCL_RET_OK ) {
7779 ESP_LOGW ("RCLCPY" , "Agent options failure: %d" , ret );
7880 }
79- //RCCHECK(rmw_uros_discover_agent(rmw_options));
8081
8182 // Support Init
82- // ret = rclc_support_init(&rclcpy_default_context.rcl_support, 0, NULL, &rclcpy_default_context.rcl_allocator);
8383 ret = rclc_support_init_with_options (& rclcpy_default_context .rcl_support ,
8484 0 ,
8585 NULL ,
8686 & rclcpy_default_context .init_options ,
8787 & rclcpy_default_context .rcl_allocator );
8888 if (ret != RCL_RET_OK ) {
8989 ESP_LOGW ("RCLCPY" , "Initialization failure: %d" , ret );
90- mp_raise_RuntimeError (MP_ERROR_TEXT ("ROS failed to initialize" ));
90+ mp_raise_RuntimeError (MP_ERROR_TEXT ("ROS failed to initialize. Is agent connected? " ));
9191 } else {
9292 rclcpy_default_context .initialized = true;
9393 }
0 commit comments