You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2026/06/01 16:15:35 UTC
(nuttx-apps) 04/04: examples/microros_pub: Add minimal Int32 publisher example.
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit 1081225509857d4e265b28b242d59d314399380e
Author: Arjav Patel <ar...@gmail.com>
AuthorDate: Sun May 31 10:11:34 2026 +0530
examples/microros_pub: Add minimal Int32 publisher example.
End-to-end exercise of the micro-ROS stack on NuttX: calls
microros_transport_init() to register the configured backend,
brings up rclc_support / node / publisher, publishes 30
std_msgs/Int32 messages on /nuttx_pub at 1 Hz, then tears the
stack down cleanly.
The example's Make.defs adds $(APPDIR)/system/microros/transport
to its own CFLAGS rather than the system/microros Make.defs, so
the transport-glue header path is not pushed into the global
search path for unrelated apps.
Serves as the smoke test for the transport layer and as a template
for downstream publisher apps.
Signed-off-by: Arjav Patel <ar...@gmail.com>
---
examples/microros_pub/CMakeLists.txt | 35 ++++++++++
examples/microros_pub/Kconfig | 30 ++++++++
examples/microros_pub/Make.defs | 25 +++++++
examples/microros_pub/Makefile | 32 +++++++++
examples/microros_pub/microros_pub_main.c | 109 ++++++++++++++++++++++++++++++
5 files changed, 231 insertions(+)
diff --git a/examples/microros_pub/CMakeLists.txt b/examples/microros_pub/CMakeLists.txt
new file mode 100644
index 000000000..8bf83218e
--- /dev/null
+++ b/examples/microros_pub/CMakeLists.txt
@@ -0,0 +1,35 @@
+# ##############################################################################
+# apps/examples/microros_pub/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_PUB)
+ nuttx_add_application(
+ NAME
+ ${CONFIG_EXAMPLES_MICROROS_PUB_PROGNAME}
+ SRCS
+ microros_pub_main.c
+ STACKSIZE
+ ${CONFIG_EXAMPLES_MICROROS_PUB_STACKSIZE}
+ PRIORITY
+ ${CONFIG_EXAMPLES_MICROROS_PUB_PRIORITY}
+ DEPENDS
+ microros_transport)
+endif()
diff --git a/examples/microros_pub/Kconfig b/examples/microros_pub/Kconfig
new file mode 100644
index 000000000..a1d75478d
--- /dev/null
+++ b/examples/microros_pub/Kconfig
@@ -0,0 +1,30 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
+
+config EXAMPLES_MICROROS_PUB
+ tristate "micro-ROS int32 publisher example"
+ default n
+ depends on SYSTEM_MICROROS
+ ---help---
+ Minimal micro-ROS publisher that creates a node and publishes
+ an std_msgs/Int32 every second on the topic /nuttx_pub. Used
+ to validate the NuttX-native transport layer end-to-end with
+ a micro-ROS agent.
+
+if EXAMPLES_MICROROS_PUB
+
+config EXAMPLES_MICROROS_PUB_PROGNAME
+ string "Program name"
+ default "microros_pub"
+
+config EXAMPLES_MICROROS_PUB_PRIORITY
+ int "micro-ROS pub task priority"
+ default 100
+
+config EXAMPLES_MICROROS_PUB_STACKSIZE
+ int "micro-ROS pub stack size"
+ default 8192
+
+endif
diff --git a/examples/microros_pub/Make.defs b/examples/microros_pub/Make.defs
new file mode 100644
index 000000000..16128b96e
--- /dev/null
+++ b/examples/microros_pub/Make.defs
@@ -0,0 +1,25 @@
+############################################################################
+# apps/examples/microros_pub/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_PUB),)
+CONFIGURED_APPS += $(APPDIR)/examples/microros_pub
+endif
diff --git a/examples/microros_pub/Makefile b/examples/microros_pub/Makefile
new file mode 100644
index 000000000..1377e68b5
--- /dev/null
+++ b/examples/microros_pub/Makefile
@@ -0,0 +1,32 @@
+############################################################################
+# apps/examples/microros_pub/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_PUB_PROGNAME)
+PRIORITY = $(CONFIG_EXAMPLES_MICROROS_PUB_PRIORITY)
+STACKSIZE = $(CONFIG_EXAMPLES_MICROROS_PUB_STACKSIZE)
+MODULE = $(CONFIG_EXAMPLES_MICROROS_PUB)
+
+MAINSRC = microros_pub_main.c
+
+include $(APPDIR)/Application.mk
diff --git a/examples/microros_pub/microros_pub_main.c b/examples/microros_pub/microros_pub_main.c
new file mode 100644
index 000000000..d9211c36b
--- /dev/null
+++ b/examples/microros_pub/microros_pub_main.c
@@ -0,0 +1,109 @@
+/****************************************************************************
+ * apps/examples/microros_pub/microros_pub_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 <nuttx/config.h>
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include <rcl/rcl.h>
+#include <rcl/error_handling.h>
+#include <rclc/rclc.h>
+#include <rclc/executor.h>
+#include <std_msgs/msg/int32.h>
+
+#include <system/microros_transport.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+ rcl_publisher_t publisher;
+ std_msgs__msg__Int32 msg;
+ rcl_allocator_t allocator;
+ rclc_support_t support;
+ rcl_node_t node;
+ int i;
+
+ printf("microros_pub: starting\n");
+
+ if (microros_transport_init() != 0)
+ {
+ printf("microros_pub: transport init failed\n");
+ return 1;
+ }
+
+ allocator = rcl_get_default_allocator();
+
+ if (rclc_support_init(&support, 0, NULL, &allocator) != RCL_RET_OK)
+ {
+ printf("microros_pub: rclc_support_init failed\n");
+ return 1;
+ }
+
+ if (rclc_node_init_default(&node, "nuttx_node", "", &support)
+ != RCL_RET_OK)
+ {
+ printf("microros_pub: node init failed\n");
+ return 1;
+ }
+
+ if (rclc_publisher_init_default(
+ &publisher,
+ &node,
+ ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
+ "nuttx_pub") != RCL_RET_OK)
+ {
+ printf("microros_pub: publisher init failed\n");
+ return 1;
+ }
+
+ msg.data = 0;
+
+ for (i = 0; i < 30; i++)
+ {
+ if (rcl_publish(&publisher, &msg, NULL) == RCL_RET_OK)
+ {
+ printf("microros_pub: sent %d\n", (int)msg.data);
+ }
+ else
+ {
+ printf("microros_pub: publish failed\n");
+ }
+
+ msg.data++;
+ sleep(1);
+ }
+
+ rcl_publisher_fini(&publisher, &node);
+ rcl_node_fini(&node);
+ rclc_support_fini(&support);
+
+ printf("microros_pub: done\n");
+ return 0;
+}