88
99#include "esp_log.h"
1010
11+ #define RCLCPY_SUPPORT_FAIL 1
12+ #define RCLCPY_OPTIONS_FAIL 2
1113
1214rclcpy_context_t rclcpy_default_context = {
1315 .initialized = false,
16+ .critical_fail = 0 ,
1417};
1518
1619static void * microros_allocate (size_t size , void * state ) {
@@ -40,16 +43,16 @@ static void *microros_zero_allocate(size_t number_of_elements, size_t size_of_el
4043
4144void rclcpy_reset (void ) {
4245 if (rclcpy_default_context .initialized ) {
43- // Clean up support context
46+ // Clean up micro-ROS objects
4447 rcl_ret_t ret = rclc_support_fini (& rclcpy_default_context .rcl_support );
4548 if (ret != RCL_RET_OK ) {
46- ESP_LOGW ("RCLCPY" , "Support cleanup error: %d" , ret );
49+ // ESP_LOGW("RCLCPY", "Support cleanup error: %d", ret);
50+ rclcpy_default_context .critical_fail = RCLCPY_SUPPORT_FAIL ;
4751 }
48-
49- // Clean up init options
5052 ret = rcl_init_options_fini (& rclcpy_default_context .init_options );
5153 if (ret != RCL_RET_OK ) {
52- ESP_LOGW ("RCLCPY" , "Init options cleanup error: %d" , ret );
54+ // ESP_LOGW("RCLCPY", "Init options cleanup error: %d", ret);
55+ rclcpy_default_context .critical_fail = RCLCPY_OPTIONS_FAIL ;
5356 }
5457
5558 // Reset context state
@@ -59,52 +62,51 @@ void rclcpy_reset(void) {
5962}
6063
6164void common_hal_rclcpy_init (const char * agent_ip , const char * agent_port , int16_t domain_id ) {
62- // Get empty allocator
63- rcl_allocator_t custom_allocator = rcutils_get_zero_initialized_allocator ();
65+ if (rclcpy_default_context .critical_fail != 0 ) {
66+ mp_raise_RuntimeError_varg (MP_ERROR_TEXT ("Critical ROS failure during soft reboot, reset required: %d" ), rclcpy_default_context .critical_fail );
67+ }
6468
65- // Set custom allocation methods
69+ // Set up circuitpython-friendly allocator
70+ rcl_allocator_t custom_allocator = rcutils_get_zero_initialized_allocator ();
6671 custom_allocator .allocate = microros_allocate ;
6772 custom_allocator .deallocate = microros_deallocate ;
6873 custom_allocator .reallocate = microros_reallocate ;
6974 custom_allocator .zero_allocate = microros_zero_allocate ;
70- // Set custom allocator as default
7175 if (!rcutils_set_default_allocator (& custom_allocator )) {
72- ESP_LOGW ("RCLCPY" , "allocator failure" );
7376 mp_raise_RuntimeError (MP_ERROR_TEXT ("ROS memory allocator failure" ));
7477 }
7578 rclcpy_default_context .rcl_allocator = custom_allocator ;
7679
7780 rcl_ret_t ret ;
7881
79- // Options Init
82+ // Micro-ROS options initialization
8083 rclcpy_default_context .init_options = rcl_get_zero_initialized_init_options ();
8184 ret = rcl_init_options_init (& rclcpy_default_context .init_options , rclcpy_default_context .rcl_allocator );
8285 if (ret != RCL_RET_OK ) {
83- ESP_LOGW ( "RCLCPY" , "Options init failure: %d" , ret );
86+ mp_raise_RuntimeError ( MP_ERROR_TEXT ( "ROS internal setup failure" ) );
8487 }
8588 if (domain_id < 0 ) {
8689 mp_raise_RuntimeError (MP_ERROR_TEXT ("Invalid ROS domain ID" ));
8790 }
8891 ret = rcl_init_options_set_domain_id (& rclcpy_default_context .init_options , domain_id );
8992 if (ret != RCL_RET_OK ) {
90- ESP_LOGW ( "RCLCPY" , "Options domain failure: %d" , ret );
93+ mp_raise_RuntimeError ( MP_ERROR_TEXT ( "ROS internal setup failure" ) );
9194 }
9295
93- // Set up Agent
96+ // Set up Micro-ROS Agent
9497 rclcpy_default_context .rmw_options = rcl_init_options_get_rmw_init_options (& rclcpy_default_context .init_options );
9598 ret = rmw_uros_options_set_udp_address (agent_ip , agent_port , rclcpy_default_context .rmw_options );
9699 if (ret != RCL_RET_OK ) {
97- ESP_LOGW ( "RCLCPY" , "Agent options failure: %d" , ret );
100+ mp_raise_RuntimeError ( MP_ERROR_TEXT ( "ROS internal setup failure" ) );
98101 }
99102
100- // Support Init
103+ // Final support object init requires a connected agent
101104 ret = rclc_support_init_with_options (& rclcpy_default_context .rcl_support ,
102105 0 ,
103106 NULL ,
104107 & rclcpy_default_context .init_options ,
105108 & rclcpy_default_context .rcl_allocator );
106109 if (ret != RCL_RET_OK ) {
107- ESP_LOGW ("RCLCPY" , "Initialization failure: %d" , ret );
108110 mp_raise_RuntimeError (MP_ERROR_TEXT ("ROS failed to initialize. Is agent connected?" ));
109111 } else {
110112 rclcpy_default_context .initialized = true;
0 commit comments