From 0c195f31a2c581d7f7273e72d8d036d2eaca327c Mon Sep 17 00:00:00 2001 From: Arjav Patel Date: Mon, 1 Jun 2026 23:11:04 +0530 Subject: [PATCH] examples/microros_sub: Add minimal Int32 subscriber example. Mirror of microros_pub for the receive direction. Creates a node, subscribes to /nuttx_sub with std_msgs/Int32, drives an rclc_executor spin loop, and prints each received value. Exercises the new rclc_executor + subscription callback path on top of the microros_transport library shipped in apps/system/microros. Validates the NuttX-native transport in the agent->client direction end-to-end against a micro-ROS agent + ros2 topic pub on the host. Signed-off-by: Arjav Patel --- examples/microros_sub/CMakeLists.txt | 35 ++++++ examples/microros_sub/Kconfig | 31 +++++ examples/microros_sub/Make.defs | 25 +++++ examples/microros_sub/Makefile | 32 ++++++ examples/microros_sub/microros_sub_main.c | 131 ++++++++++++++++++++++ 5 files changed, 254 insertions(+) create mode 100644 examples/microros_sub/CMakeLists.txt create mode 100644 examples/microros_sub/Kconfig create mode 100644 examples/microros_sub/Make.defs create mode 100644 examples/microros_sub/Makefile create mode 100644 examples/microros_sub/microros_sub_main.c diff --git a/examples/microros_sub/CMakeLists.txt b/examples/microros_sub/CMakeLists.txt new file mode 100644 index 00000000000..1c094949adb --- /dev/null +++ b/examples/microros_sub/CMakeLists.txt @@ -0,0 +1,35 @@ +# ############################################################################## +# apps/examples/microros_sub/CMakeLists.txt +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with this work for +# additional information regarding copyright ownership. The ASF licenses this +# file to you under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +# +# ############################################################################## + +if(CONFIG_EXAMPLES_MICROROS_SUB) + nuttx_add_application( + NAME + ${CONFIG_EXAMPLES_MICROROS_SUB_PROGNAME} + SRCS + microros_sub_main.c + STACKSIZE + ${CONFIG_EXAMPLES_MICROROS_SUB_STACKSIZE} + PRIORITY + ${CONFIG_EXAMPLES_MICROROS_SUB_PRIORITY} + DEPENDS + microros_transport) +endif() diff --git a/examples/microros_sub/Kconfig b/examples/microros_sub/Kconfig new file mode 100644 index 00000000000..3bbbf5efb27 --- /dev/null +++ b/examples/microros_sub/Kconfig @@ -0,0 +1,31 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_MICROROS_SUB + tristate "micro-ROS int32 subscriber example" + default n + depends on SYSTEM_MICROROS + ---help--- + Minimal micro-ROS subscriber that creates a node, subscribes to + the topic /nuttx_sub with std_msgs/Int32, and prints each + received value. Drives an rclc_executor spin loop and is used + together with microros_pub (or any external publisher) to + validate the NuttX-native transport in the receive direction. + +if EXAMPLES_MICROROS_SUB + +config EXAMPLES_MICROROS_SUB_PROGNAME + string "Program name" + default "microros_sub" + +config EXAMPLES_MICROROS_SUB_PRIORITY + int "micro-ROS sub task priority" + default 100 + +config EXAMPLES_MICROROS_SUB_STACKSIZE + int "micro-ROS sub stack size" + default 8192 + +endif diff --git a/examples/microros_sub/Make.defs b/examples/microros_sub/Make.defs new file mode 100644 index 00000000000..e9fe5aa1d46 --- /dev/null +++ b/examples/microros_sub/Make.defs @@ -0,0 +1,25 @@ +############################################################################ +# apps/examples/microros_sub/Make.defs +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_EXAMPLES_MICROROS_SUB),) +CONFIGURED_APPS += $(APPDIR)/examples/microros_sub +endif diff --git a/examples/microros_sub/Makefile b/examples/microros_sub/Makefile new file mode 100644 index 00000000000..a1144a70cc1 --- /dev/null +++ b/examples/microros_sub/Makefile @@ -0,0 +1,32 @@ +############################################################################ +# apps/examples/microros_sub/Makefile +# +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +PROGNAME = $(CONFIG_EXAMPLES_MICROROS_SUB_PROGNAME) +PRIORITY = $(CONFIG_EXAMPLES_MICROROS_SUB_PRIORITY) +STACKSIZE = $(CONFIG_EXAMPLES_MICROROS_SUB_STACKSIZE) +MODULE = $(CONFIG_EXAMPLES_MICROROS_SUB) + +MAINSRC = microros_sub_main.c + +include $(APPDIR)/Application.mk diff --git a/examples/microros_sub/microros_sub_main.c b/examples/microros_sub/microros_sub_main.c new file mode 100644 index 00000000000..1e1a6c550d1 --- /dev/null +++ b/examples/microros_sub/microros_sub_main.c @@ -0,0 +1,131 @@ +/**************************************************************************** + * apps/examples/microros_sub/microros_sub_main.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static std_msgs__msg__Int32 g_rx_msg; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void sub_callback(const void *msgin) +{ + const std_msgs__msg__Int32 *msg = (const std_msgs__msg__Int32 *)msgin; + + printf("microros_sub: got %d\n", (int)msg->data); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int main(int argc, FAR char *argv[]) +{ + rcl_subscription_t subscriber; + rcl_allocator_t allocator; + rclc_support_t support; + rcl_node_t node; + rclc_executor_t executor; + + printf("microros_sub: starting\n"); + + if (microros_transport_init() != 0) + { + printf("microros_sub: transport init failed\n"); + return 1; + } + + allocator = rcl_get_default_allocator(); + + if (rclc_support_init(&support, 0, NULL, &allocator) != RCL_RET_OK) + { + printf("microros_sub: rclc_support_init failed\n"); + return 1; + } + + if (rclc_node_init_default(&node, "nuttx_sub_node", "", &support) + != RCL_RET_OK) + { + printf("microros_sub: node init failed\n"); + return 1; + } + + if (rclc_subscription_init_default( + &subscriber, + &node, + ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), + "nuttx_sub") != RCL_RET_OK) + { + printf("microros_sub: subscription init failed\n"); + return 1; + } + + executor = rclc_executor_get_zero_initialized_executor(); + if (rclc_executor_init(&executor, &support.context, 1, &allocator) + != RCL_RET_OK) + { + printf("microros_sub: executor init failed\n"); + return 1; + } + + if (rclc_executor_add_subscription(&executor, + &subscriber, + &g_rx_msg, + &sub_callback, + ON_NEW_DATA) != RCL_RET_OK) + { + printf("microros_sub: add_subscription failed\n"); + return 1; + } + + printf("microros_sub: spinning on /nuttx_sub\n"); + + rclc_executor_spin(&executor); + + rclc_executor_fini(&executor); + rcl_subscription_fini(&subscriber, &node); + rcl_node_fini(&node); + rclc_support_fini(&support); + + printf("microros_sub: done\n"); + return 0; +}