8
8
9
9
#include "esp_log.h"
10
10
11
+ #define RCLCPY_SUPPORT_FAIL 1
12
+ #define RCLCPY_OPTIONS_FAIL 2
11
13
12
14
rclcpy_context_t rclcpy_default_context = {
13
15
.initialized = false,
16
+ .critical_fail = 0 ,
14
17
};
15
18
16
19
static 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
40
43
41
44
void rclcpy_reset (void ) {
42
45
if (rclcpy_default_context .initialized ) {
43
- // Clean up support context
46
+ // Clean up micro-ROS objects
44
47
rcl_ret_t ret = rclc_support_fini (& rclcpy_default_context .rcl_support );
45
48
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 ;
47
51
}
48
-
49
- // Clean up init options
50
52
ret = rcl_init_options_fini (& rclcpy_default_context .init_options );
51
53
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 ;
53
56
}
54
57
55
58
// Reset context state
@@ -59,52 +62,51 @@ void rclcpy_reset(void) {
59
62
}
60
63
61
64
void 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
+ }
64
68
65
- // Set custom allocation methods
69
+ // Set up circuitpython-friendly allocator
70
+ rcl_allocator_t custom_allocator = rcutils_get_zero_initialized_allocator ();
66
71
custom_allocator .allocate = microros_allocate ;
67
72
custom_allocator .deallocate = microros_deallocate ;
68
73
custom_allocator .reallocate = microros_reallocate ;
69
74
custom_allocator .zero_allocate = microros_zero_allocate ;
70
- // Set custom allocator as default
71
75
if (!rcutils_set_default_allocator (& custom_allocator )) {
72
- ESP_LOGW ("RCLCPY" , "allocator failure" );
73
76
mp_raise_RuntimeError (MP_ERROR_TEXT ("ROS memory allocator failure" ));
74
77
}
75
78
rclcpy_default_context .rcl_allocator = custom_allocator ;
76
79
77
80
rcl_ret_t ret ;
78
81
79
- // Options Init
82
+ // Micro-ROS options initialization
80
83
rclcpy_default_context .init_options = rcl_get_zero_initialized_init_options ();
81
84
ret = rcl_init_options_init (& rclcpy_default_context .init_options , rclcpy_default_context .rcl_allocator );
82
85
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" ) );
84
87
}
85
88
if (domain_id < 0 ) {
86
89
mp_raise_RuntimeError (MP_ERROR_TEXT ("Invalid ROS domain ID" ));
87
90
}
88
91
ret = rcl_init_options_set_domain_id (& rclcpy_default_context .init_options , domain_id );
89
92
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" ) );
91
94
}
92
95
93
- // Set up Agent
96
+ // Set up Micro-ROS Agent
94
97
rclcpy_default_context .rmw_options = rcl_init_options_get_rmw_init_options (& rclcpy_default_context .init_options );
95
98
ret = rmw_uros_options_set_udp_address (agent_ip , agent_port , rclcpy_default_context .rmw_options );
96
99
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" ) );
98
101
}
99
102
100
- // Support Init
103
+ // Final support object init requires a connected agent
101
104
ret = rclc_support_init_with_options (& rclcpy_default_context .rcl_support ,
102
105
0 ,
103
106
NULL ,
104
107
& rclcpy_default_context .init_options ,
105
108
& rclcpy_default_context .rcl_allocator );
106
109
if (ret != RCL_RET_OK ) {
107
- ESP_LOGW ("RCLCPY" , "Initialization failure: %d" , ret );
108
110
mp_raise_RuntimeError (MP_ERROR_TEXT ("ROS failed to initialize. Is agent connected?" ));
109
111
} else {
110
112
rclcpy_default_context .initialized = true;
0 commit comments