Skip to content

Commit 031ca37

Browse files
committed
Add agent information to init
Expands rclcpy init function to accept agent IP and port information, as well as a new domain_id value. Contains minor changes to prepare for reset support.
1 parent cdb2a0c commit 031ca37

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

ports/espressif/common-hal/rclcpy/__init__.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
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-
1511
rclcpy_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
}

ports/espressif/common-hal/rclcpy/__init__.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ typedef struct {
2424
rmw_init_options_t* rmw_options;
2525
} rclcpy_context_t;
2626

27-
extern rclcpy_context_t rclcpy_default_context;
27+
extern rclcpy_context_t rclcpy_default_context;
28+
void rclcpy_reset(void);

shared-bindings/rclcpy/__init__.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,24 @@
1313
#include "py/objstr.h"
1414
#include "py/runtime.h"
1515

16-
static mp_obj_t rclcpy_init(void) {
17-
common_hal_rclcpy_init();
16+
// static mp_obj_t rclcpy_init(void) {
17+
static mp_obj_t rclcpy_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
18+
enum { ARG_agent_ip, ARG_agent_port, ARG_domain_id};
19+
static const mp_arg_t allowed_args[] = {
20+
{ MP_QSTR_agent_ip, MP_ARG_REQUIRED | MP_ARG_OBJ },
21+
{ MP_QSTR_agent_port, MP_ARG_REQUIRED | MP_ARG_OBJ },
22+
{ MP_QSTR_domain_id, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
23+
};
24+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
25+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
26+
const char *agent_ip = mp_obj_str_get_str(args[ARG_agent_ip].u_obj);
27+
const char *agent_port = mp_obj_str_get_str(args[ARG_agent_port].u_obj);
28+
int16_t domain_id = args[ARG_domain_id].u_int;
29+
30+
common_hal_rclcpy_init(agent_ip, agent_port, domain_id);
1831
return mp_const_none;
1932
}
20-
static MP_DEFINE_CONST_FUN_OBJ_0(rclcpy_init_obj, rclcpy_init);
33+
static MP_DEFINE_CONST_FUN_OBJ_KW(rclcpy_init_obj, 2, rclcpy_init);
2134

2235

2336
// TODO: parallel implementation to Node constructor

shared-bindings/rclcpy/__init__.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
#pragma once
88
#include "common-hal/rclcpy/__init__.h"
99

10-
void common_hal_rclcpy_init(void);
10+
void common_hal_rclcpy_init(const char *agent_ip, const char *agent_port, int16_t domain_id);
1111
rclcpy_context_t * common_hal_rclcpy_get_default_context(void);
1212
bool common_hal_rclcpy_default_context_is_initialized(void);

0 commit comments

Comments
 (0)