Skip to content

Commit

Permalink
feat: aimrt_py support ros2 protocol interface (#95)
Browse files Browse the repository at this point in the history
* feat: add checks for ROS 2 message type support

Implement validation functions to ensure message or service types are compliant with ROS 2 standards, enhancing compatibility and error handling when integrating ROS 1 and ROS 2 environments.

* build: update file globbing to include configure dependencies

Ensure source files are automatically detected during configuration by adding CONFIGURE_DEPENDS to the file globbing commands for C++ and Python files. This improves build reliability as it accounts for changes in the file structure.

* feat: add ROS2 message support and enhance publishing functionality

Implement functions to register, publish, and subscribe to ROS2 messages, improving interoperability with ROS2 systems. Adjust type support to handle both protobuf and ROS2 message types, streamlining the process of message serialization and ensuring robustness in message handling.

* feat: add HTTP and ROS2 example configurations and applications

Introduce example configurations and applications for both HTTP and ROS2 channels, enabling greater flexibility and showcasing the capabilities of the system for publishing and subscribing to messages.

* chore: disable unimplemented copy and move functions

Throw runtime errors for the unimplemented copy and move methods to ensure clarity on their current status and prevent accidental usage.

* feat: enhance message publishing with iteration and logging

Implement continuous publishing of messages with incremental identifiers in the publisher application. Update the subscriber to log message count, improving visibility into received messages.

* refactor: reorganize and encapsulate ROS2 message handling functionality

Streamline the handling of ROS2 messages by encapsulating related functionality into a new utility header file. This enhances code clarity and maintainability while adhering to best practices in modular design.

* refactor: streamline protobuf and ROS2 message publishing

Enhance context handling and unify publish type registration for better clarity and usability. Simplify serialization type management and improve the architecture for future extensions, particularly for messaging features.

* refactor: streamline messaging functions

Enhance message publishing by consolidating function calls and improving clarity. Update context handling to better support message serialization types, allowing for a more flexible interaction with both protobuf and ROS2 messages.

* refactor: simplify message subscription handling and reduce publish frequency

Adjust the message subscription method to handle both Protobuf and ROS2 message types more efficiently and eliminate redundant subscription functions. Decrease the publishing frequency in the example app for improved performance and readability.

* fix: remove unnecessary error message for ROS 1 type detection

Eliminate confusion by removing the printed warning about ROS 1 message types, as it was not providing significant value in handling unsupported types.

* docs: update README and add ros2 channel examples

Clarify publisher behavior in examples and provide comprehensive documentation for ros2 channel implementations.

* feat: support ros2 message types in aimrt_py channel

Add support for ros2 message types in the aimrt_py channel to enhance interoperability with ROS2-based applications.

* fix: improve error handling during ROS2 message serialization and deserialization

Enhance robustness by adding exception handling to ensure the process correctly handles serialization and deserialization failures, improving overall stability in handling ROS2 messages.

* fix: correct argument validation and formatting

Remove unnecessary whitespace and enhance error handling for argument types in the publishing function, ensuring clear type checking and preventing potential runtime errors.

* refactor: streamline message conversion logic

Simplify the conversion of ROS2 messages to Python objects by moving the conversion logic inline. This eliminates unnecessary function calls and improves performance while maintaining clarity in the subscription method.

* feat: add ROS2 support to Python runtime

Enable linking and compilation of ROS2 interfaces based on the build configuration. This improves flexibility by allowing the Python runtime to interface with ROS2 when needed, enhancing interoperability and feature set.

* feat: enhance message type handling for publishers and subscribers

Add utility functions to generate names for ROS2 and protobuf message types, centralizing logic for type name creation. Update the publisher and subscriber to utilize these functions, improving code readability and maintainability while ensuring accurate message type management.

* feat: update message types to use RosTestMsg for better compatibility

Register new message type RosTestMsg in both publisher and subscriber applications. Update data format for publishing events to improve clarity and consistency in message handling. Ensure proper setup in the shell scripts for localization configuration.

* chore: consolidate pybind11 includes

Simplify header files by removing redundant pybind11 includes, reducing clutter and improving maintainability.

* docs: add ROS2 message support details and example links

Enhance the documentation by including information on ROS2 message formats and how to generate corresponding Python code. This provides users with clearer guidance on integrating ROS2 functionality within the project.

* chore: update copyright information to reflect new authorship

Update copyright year and licensing information across multiple configuration files and application scripts to maintain proper attribution for the AimRT project.

* feat: add ROS2 RPC client and server examples with configurations

Introduce example applications for ROS2 RPC communication, including client and server scripts, along with necessary configuration files. Implement a build script to set up the protocol buffers correctly, enhancing usability by supporting both HTTP and gRPC backends.

* feat: update RPC request and response handling

Refactor the RPC client and server implementations to utilize the updated request and response types from the example ROS2 service, improving code clarity and ensuring compatibility with the new service structure.

* refactor: simplify RPC response handling

Remove unnecessary response object instantiation and streamline the response creation process for improved clarity and efficiency in the RPC invocation logic.

* refactor: streamline publisher and subscriber method names

Simplify method names related to publishing and subscribing to enhance readability and maintainability. Transition from a prefix-based naming convention to a more concise approach, ensuring consistency across the codebase.

* chore: update configuration file paths for gRPC client and server examples

Align the configuration file paths in the example scripts to use the gRPC-specific settings, ensuring the correct configuration is utilized for both the client and server.

* refactor: rename internal methods for clarity

Improve naming conventions for protobuf message type functions, enhancing code readability and maintainability. Update related usages to keep consistent across the runtime and RPC generator.

* feat: add service type validation and improve bash scripts

Introduce a new bash script for debugging the ROS2 client and enhance existing scripts with proper sourcing. Implement service type validation to ensure the reliability of service registration. This improves the robustness of RPC interactions and simplifies the setup process for developers.

* feat: extend RPC response data structure and improve logging

Add additional data types to the RosTestRpc response for enhanced functionality. Set logging level to trace for more detailed output during execution.

* feat: add support for static and dynamic arrays in RPC service

Enhance the RPC service by introducing static and dynamic array types for various data types, enabling more complex data handling in requests and responses. This improves flexibility and usability for clients interacting with the service.

* feat: add support for new ROS message types

Enhance the RPC server to handle additional ROS message types, including dynamic and static arrays of custom data structures. This improves the server's data handling capabilities and aligns with the updated service specifications.

* refactor: improve type mapping and message copy functions

Update type mappings for better consistency and clarity. Enhance the readability of message copying functions by formatting multi-line expressions.

* refactor: improve message copying and moving functions

Enhance the handling of ROS message copying and moving by updating the parameter order and adding tailored move functions for better memory management. This optimizes the performance of message manipulations and ensures cleaner, more maintainable code.

* refactor: simplify message type support functions

Remove inline implementations and enhance readability of message type support functions, while maintaining functionality and improving code organization.

* refactor: streamline ROS message handling and improve introspection support

Enhance readability and maintainability by cleaning up function definitions related to ROS message type support. Implement a more robust method for obtaining message members information, addressing potential failure scenarios. Adjust example service implementation for consistency in dynamic data handling.

* fix: ensure pointers for ROS message creation and destruction are valid

Throw runtime exceptions if the function pointers for creating or destroying ROS messages are null, enhancing error handling and stability during object initialization.

* feat: enhance RosTestRpc method signature for better type safety

Improve the RosTestRpc method by adding overloads for different argument configurations, ensuring clearer type expectations and reducing potential runtime errors. This change aids in maintaining robust interactions with the ROS2 service framework.

* refactor: simplify parameter naming in RPC proxy function

Rename the `ctx_ref` parameter to `ctx` in the RPC function signature for cleaner and more concise code. This enhances readability and maintains consistency across the codebase.

* fix: restrict static array sizes in RosTestRpc service

Limit the size of static array fields to three elements each for better resource management and to prevent potential overflow issues.

* feat: enhance python runtime generation for ROS2 services

Add support for generating code from ROS2 service definitions, improving interoperability with the aimrt framework. Update build scripts to include new generation scripts and ensure file dependencies are configured correctly.

* refactor: update RPC service and client implementations to use ROS2 naming convention

Modify scripts to utilize the new aimrt_py-gen-ros2-rpc tool and update service and client references accordingly for consistency and clarity. Remove deprecated code to streamline functionality and improve maintainability.

* chore: update .gitignore to exclude generated files

Add pattern to ignore *_aimrt_rpc_ros2.py files to keep the repository clean from auto-generated files.

* refactor: streamline response structure for RosTestRpc

Remove unused fields from RosTestRpc response to simplify the data structure and enhance clarity. Adjust logging level for better control over output verbosity.

* chore: update log level to INFO for consistency

Adjust the core log level to INFO to ensure a more standard logging output and reduce unnecessary verbosity in the logs.

* docs: add README for ros2 rpc examples

Provide detailed instructions and explanations for setting up and running Python RPC examples using ros2 protocols with HTTP, gRPC, and native ros2 backends.

* feat: enhance aimrt_py channel with rpc support for ros2 message types

Add support for ros2 message types in both aimrt_py channel and rpc to improve compatibility and functionality.

* docs: add ros2_rpc example to Python interface section

Include a new example link to enhance the documentation and provide more resources for users exploring the Python interfaces.

* chore: add spacing for improved readability in service proxy class

Improve code readability by adding blank lines in the service proxy class definition.

* chore: standardize argument names for RPC generator

Update argument names in the RPC generation script and implementation for consistency, enhancing readability and usability.

* docs: update RPC documentation to include ROS2 Srv support

Add examples and instructions for using ROS2 Srv in RPC, enhancing integration options for developers. Adjust existing protobuf references for clarity and consistency.

* style: standardize whitespace in RosTestRpc.srv

Improve readability by ensuring consistent spacing in the service definition.

* build: exclude ROS2 specific files from the build if not using ROS2

Remove ROS2-related source files from the compilation when the project is configured without ROS2 support, helping to streamline the build process and avoid unnecessary dependencies.

* style: add missing newline at end of file for consistency

* refactor: rename copy function to improve clarity

Refactor the function used for copying messages within dynamic arrays to better convey its functionality. This change enhances code readability and maintainability.

---------

Co-authored-by: zhangyi <zhangyi@agibot.com>
  • Loading branch information
zhangyi1357 and zhangyi authored Nov 21, 2024
1 parent 1b88aa7 commit df5d21e
Show file tree
Hide file tree
Showing 59 changed files with 2,526 additions and 272 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dist/
__pycache__/
*_pb2.py
*_pb2_grpc.py
*_aimrt_rpc_ros2.py
log/
tmp/

Expand Down
1 change: 1 addition & 0 deletions document/sphinx-cn/release_notes/v0_9_0.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- 新增 aimrt_cli trans 命令,用于将 使用 aimrt record_playback 插件录制的 bag 文件转换为 ros2 的 bag 文件;
- 新增 Echo 插件,用于回显消息;
- 新增了基于执行器的定时器,方便执行定时任务;
- aimrt_py channel 和 rpc 支持 ros2 消息类型;

**次要修改**
- 缩短了一些 examples 的文件路径长度;
Expand Down
2 changes: 2 additions & 0 deletions document/sphinx-cn/tutorials/examples/examples_py.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ AimRT 提供了以下 Python 接口使用示例:
- {{ '[pb_rpc]({}/src/examples/py/pb_rpc)'.format(code_site_root_path_url) }}
- {{ '[pb_chn_bench]({}/src/examples/py/pb_chn_bench)'.format(code_site_root_path_url) }}
- {{ '[pb_rpc_bench]({}/src/examples/py/pb_rpc_bench)'.format(code_site_root_path_url) }}
- {{ '[ros2_chn]({}/src/examples/py/ros2_chn)'.format(code_site_root_path_url) }}
- {{ '[ros2_rpc]({}/src/examples/py/ros2_rpc)'.format(code_site_root_path_url) }}

关于这些示例的说明:
- 每个示例都有自己独立的 readme 文档,详情请点击示例链接进入后查看;
Expand Down
31 changes: 29 additions & 2 deletions document/sphinx-cn/tutorials/interface_py/channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
- {{ '[examples_py_pb_chn_publisher_app.py]({}/src/examples/py/pb_chn/examples_py_pb_chn_publisher_app.py)'.format(code_site_root_path_url) }}
- {{ '[examples_py_pb_chn_subscriber_app.py]({}/src/examples/py/pb_chn/examples_py_pb_chn_subscriber_app.py)'.format(code_site_root_path_url) }}


## protobuf 协议
## 协议

协议用于确定通信各端的消息格式。一般来说,协议都是使用一种与具体的编程语言无关的 IDL ( Interface description language )描述,然后由某种工具转换为各个语言的代码。

### Protobuf

[Protobuf](https://protobuf.dev/)是一种由 Google 开发的、用于序列化结构化数据的轻量级、高效的数据交换格式,是一种广泛使用的 IDL。

当前版本 AimRT Python 只支持 protobuf 协议。在使用 AimRT Python 发送/订阅消息之前,使用者需要先基于 protobuf 协议生成一些 python 的桩代码。
Expand All @@ -36,6 +37,32 @@ protoc --python_out=. example.proto
这将生成`example_pb2.py`文件,包含了根据定义的消息类型生成的 Python 接口,我们的业务代码中需要 import 此文件。


### ROS2 Message

ROS2 Message 是一种用于在 ROS2 中进行通信和数据交换的结构化数据格式。在使用时,开发者需要先定义一个 ROS2 Package,在其中定义一个`.msg`文件,比如`example.msg`

```
int32 num
float32 num2
char data
```

然后直接通过 ROS2 提供的 CMake 方法`rosidl_generate_interfaces`,为消息生成 C++ 代码和 CMake Target,例如:
```cmake
rosidl_generate_interfaces(
example_ros2_msg_gencode
"msg/example.msg"
)
```

构建之后,还需要设置相应的环境变量,才能在 python 中使用生成的消息类型,在 aimrt 的 build 目录中,执行:

```bash
source install/share/example_ros2/setup.bash
```

关于更多生成自定义 ROS2 Message 的详细信息,请参考[ROS2 官方文档](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Custom-ROS2-Interfaces.html)

## ChannelHandle

模块可以通过调用`CoreRef`句柄的`GetChannelHandle()`接口,获取`ChannelHandleRef`句柄,来使用 Channel 功能。其提供的核心接口如下:
Expand Down
42 changes: 36 additions & 6 deletions document/sphinx-cn/tutorials/interface_py/rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
参考示例:
- {{ '[examples_py_pb_rpc_client_app.py]({}/src/examples/py/pb_rpc/examples_py_pb_rpc_client_app.py)'.format(code_site_root_path_url) }}
- {{ '[examples_py_pb_rpc_server_app.py]({}/src/examples/py/pb_rpc/examples_py_pb_rpc_server_app.py)'.format(code_site_root_path_url) }}
- {{ '[examples_py_ros2_rpc_client_app.py]({}/src/examples/py/ros2_rpc/examples_py_ros2_rpc_client_app.py)'.format(code_site_root_path_url) }}
- {{ '[examples_py_ros2_rpc_server_app.py]({}/src/examples/py/ros2_rpc/examples_py_ros2_rpc_server_app.py)'.format(code_site_root_path_url) }}

## protobuf 协议
## 协议


协议用于确定 RPC 中客户端和服务端的消息格式。一般来说,协议都是使用一种与具体的编程语言无关的 IDL ( Interface description language )描述,然后由某种工具转换为各个语言的代码。对于 RPC 来说,这里需要两个步骤:
- 参考[Channel](./channel.md)章节中的介绍,开发者需要先利用一些官方的工具为协议文件中的**消息类型**生成指定编程语言中的代码;
- 开发者需要使用 AimRT 提供的工具,为协议文件中**服务定义**生成指定编程语言中的代码;


[Protobuf](https://protobuf.dev/)是一种由 Google 开发的、用于序列化结构化数据的轻量级、高效的数据交换格式,是一种广泛使用的 IDL。它不仅能够描述消息结构,还提供了`service`语句来定义 RPC 服务。

当前版本 AimRT Python 只支持 protobuf 协议。在使用 AimRT Python 发起/处理 RPC 请求之前,使用者需要先基于 protobuf 协议生成一些 python 的桩代码。
### Protobuf

[Protobuf](https://protobuf.dev/)是一种由 Google 开发的、用于序列化结构化数据的轻量级、高效的数据交换格式,是一种广泛使用的 IDL。它不仅能够描述消息结构,还提供了`service`语句来定义 RPC 服务。

在使用时,开发者需要先定义一个`.proto`文件,在其中定义消息结构和 RPC 服务。例如`rpc.proto`

Expand Down Expand Up @@ -57,6 +58,29 @@ protoc --aimrt_rpc_out=. --plugin=protoc-gen-aimrt_rpc=./protoc_plugin_py_gen_ai

这将生成`rpc_aimrt_rpc_pb2.py`文件,包含了根据定义的服务生成的 Python 接口,我们的业务代码中需要 import 此文件。

### ROS2 Srv

ROS2 Srv 是一种用于在 ROS2 中进行 RPC 定义的格式。在使用时,开发者需要先定义一个 ROS2 Package,在其中定义一个`.srv`文件,比如`example.srv`

```
byte[] data
---
int64 code
```

其中,以`---`来分割 Req 和 Rsp 的定义。

aimrt_py 安装时会自动安装一个命令行工具 `aimrt_py-gen-ros2-rpc`,用于根据 ROS2 Srv 文件生成 AimRT Python 的 RPC 桩代码。

```shell
aimrt_py-gen-ros2-rpc --pkg_name=example_pkg --srv_file=./example.srv --output_path=./
```

其中 pkg_name 是 ROS2 Package 的名称,srv_file 是 ROS2 Srv 文件的路径,output_path 是生成的桩代码的输出路径。这将生成一个`example_aimrt_rpc_ros2.py`文件,包含了根据定义的服务生成的 Python 接口,我们的业务代码中需要 import 此文件。

需要注意的是,`aimrt_py-gen-ros2-rpc` 只会生成 Req 和 Rsp 的定义,不会生成消息结构部分的代码,消息结构部分代码仍然需要使用者自行生成(即构建 ROS2 Package 并生成消息结构代码, 详情可以参考 [ROS2 官方文档](https://docs.ros.org/en/humble/Tutorials/Beginner-Client-Libraries/Custom-ROS2-Interfaces.html))。


## RpcHandle

模块可以通过调用`CoreRef`句柄的`GetRpcHandle()`接口,获取`RpcHandleRef`句柄。一般情况下,开发者不会直接使用`RpcHandleRef`直接提供的接口,而是根据 RPC IDL 文件生成一些桩代码,对`RpcHandleRef`句柄做一些封装,然后在业务代码中使用这些经过封装的接口。
Expand Down Expand Up @@ -126,7 +150,7 @@ RpcContext 是 RPC 调用时上下文信息,开发者可以在 RPC 调用时



以下是一个使用 AimRT Python 进行 RPC Client 调用的示例,通过 Create Module 方式拿到`CoreRef`句柄。如果是基于`Module`模式在`Initialize`方法中拿到`CoreRef`句柄,使用方式也类似:
以下是一个使用 AimRT Python 基于 protobuf 协议进行 RPC Client 调用的示例,通过 Create Module 方式拿到`CoreRef`句柄。如果是基于`Module`模式在`Initialize`方法中拿到`CoreRef`句柄,使用方式也类似:

```python
import aimrt_py
Expand Down Expand Up @@ -200,7 +224,7 @@ if __name__ == '__main__':
- **Step 2**:在`Initialize`阶段调用`RpcHandleRef``RegisterService`方法注册 RPC Service;


以下是一个使用 AimRT Python 进行 RPC Service 处理的示例,通过 Create Module 方式拿到`CoreRef`句柄。如果是基于`Module`模式在`Initialize`方法中拿到`CoreRef`句柄,使用方式也类似:
以下是一个使用 AimRT Python 基于 protobuf 协议进行 RPC Service 处理的示例,通过 Create Module 方式拿到`CoreRef`句柄。如果是基于`Module`模式在`Initialize`方法中拿到`CoreRef`句柄,使用方式也类似:

```python
import aimrt_py
Expand Down Expand Up @@ -281,3 +305,9 @@ def main():
if __name__ == '__main__':
main()
```

基于 ROS2 Srv 协议的 RPC 调用和处理,除数据类型不同外,其他使用方式与基于 protobuf 协议的 RPC 调用和处理基本一致。

完整示例请参考:
- {{ '[examples/py/ros2_rpc]({}/src/examples/py/ros2_rpc)'.format(code_site_root_path_url) }}
- {{ '[examples/py/pb_rpc]({}/src/examples/py/pb_rpc)'.format(code_site_root_path_url) }}
4 changes: 2 additions & 2 deletions src/examples/py/pb_chn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

说明:
- 此示例创建了以下两个模块:
- `NormalPublisherPyModule`:会在启动后向配置的 topic 中发布一条 `ExampleEventMsg` 类型的消息,然后结束进程;
- `NormalPublisherPyModule`:会在启动后向配置的 topic 中发布若干条 `ExampleEventMsg` 类型的消息,然后结束进程;
- `NormalSubscriberPyModule`:会订阅配置的 topic 下的 `ExampleEventMsg` 类型的消息;
- 此示例使用 http 类型的 channel 后端进行通信,请确保相关端口未被占用;

Expand Down Expand Up @@ -70,7 +70,7 @@

说明:
- 此示例创建了以下两个模块:
- `NormalPublisherPyModule`:会在启动后向配置的 topic 中发布一条 `ExampleEventMsg` 类型的消息,然后结束进程;
- `NormalPublisherPyModule`:会在启动后向配置的 topic 中发布若干条 `ExampleEventMsg` 类型的消息,然后结束进程;
- `NormalSubscriberPyModule`:会订阅配置的 topic 下的 `ExampleEventMsg` 类型的消息;
- 此示例使用 ros2 类型的 channel 后端进行通信,请确保本地安装有 ROS2 Humble;

Expand Down
3 changes: 2 additions & 1 deletion src/examples/py/pb_chn/examples_py_pb_chn_publisher_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ def main():
# Sleep for seconds
time.sleep(1)

# Publish event
event_msg = event_pb2.ExampleEventMsg()

# Publish event
event_msg.msg = "Publish without ctx or serialization_type"
event_msg.num = 1
aimrt_py.Publish(publisher, event_msg)
Expand Down
71 changes: 71 additions & 0 deletions src/examples/py/ros2_chn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# ros2 channel examples


## ros2 channel http


一个基于 ros2 协议与 http 后端的 python channel 示例,演示内容包括:
- 如何在 python 中使用 ros2 协议作为 channel 传输协议;
- 如何基于 aimrt_py 创建模块的方式使用 Channel publish 和 subscribe 功能;
- 如何使用 http 类型的 channel 后端;


核心代码:
- [examples_py_ros2_chn_publisher_app.py](./examples_py_ros2_chn_publisher_app.py)
- [examples_py_ros2_chn_subscriber_app.py](./examples_py_ros2_chn_subscriber_app.py)


配置文件:
- [examples_py_ros2_chn_http_pub_cfg.yaml](./cfg/examples_py_ros2_chn_http_pub_cfg.yaml)
- [examples_py_ros2_chn_http_sub_cfg.yaml](./cfg/examples_py_ros2_chn_http_sub_cfg.yaml)



运行方式(linux):
- [安装 `aimrt_py`](../../../../document/sphinx-cn/tutorials/quick_start/installation_py.md)
- 运行本目录下的[start_examples_py_ros2_chn_http_sub.sh](./start_examples_py_ros2_chn_http_sub.sh)脚本,启动 subscriber;
- 在新终端里运行本目录下的[start_examples_py_ros2_chn_http_pub.sh](./start_examples_py_ros2_chn_http_pub.sh)脚本,启动 publisher 发布一条消息;
- 向 subscriber 进程所在终端里键入`ctrl-c`以停止进程;


说明:
- 此示例创建了以下两个模块:
- `NormalPublisherPyModule`:会在启动后向配置的 topic 中发布若干条 `ExampleEventMsg` 类型的消息,然后结束进程;
- `NormalSubscriberPyModule`:会订阅配置的 topic 下的 `ExampleEventMsg` 类型的消息;
- 此示例使用 http 类型的 channel 后端进行通信,请确保相关端口未被占用;



## ros2 channel ros2

一个基于 ros2 协议与 ros2 后端的 python channel 示例,演示内容包括:
- 如何在 python 中使用 ros2 协议作为 channel 传输协议;
- 如何基于 aimrt_py 创建模块的方式使用 Channel publish 和 subscribe 功能;
- 如何使用 ros2 类型的 channel 后端;


核心代码:
- [examples_py_ros2_chn_publisher_app.py](./examples_py_ros2_chn_publisher_app.py)
- [examples_py_ros2_chn_subscriber_app.py](./examples_py_ros2_chn_subscriber_app.py)


配置文件:
- [examples_py_ros2_chn_ros2_pub_cfg.yaml](./cfg/examples_py_ros2_chn_ros2_pub_cfg.yaml)
- [examples_py_ros2_chn_ros2_sub_cfg.yaml](./cfg/examples_py_ros2_chn_ros2_sub_cfg.yaml)



运行方式(linux):
- [安装 `aimrt_py`](../../../../document/sphinx-cn/tutorials/quick_start/installation_py.md)
- 运行本目录下的[start_examples_py_ros2_chn_ros2_sub.sh](./start_examples_py_ros2_chn_ros2_sub.sh)脚本,启动 subscriber;
- 在新终端里运行本目录下的[start_examples_py_pb_chn_ros2_pub.sh](./start_examples_py_pb_chn_ros2_pub.sh)脚本,启动 publisher 发布一条消息;
- 向 subscriber 进程所在终端里键入`ctrl-c`以停止进程;


说明:
- 此示例创建了以下两个模块:
- `NormalPublisherPyModule`:会在启动后向配置的 topic 中发布若干条 `ExampleEventMsg` 类型的消息,然后结束进程;
- `NormalSubscriberPyModule`:会订阅配置的 topic 下的 `ExampleEventMsg` 类型的消息;
- 此示例使用 ros2 类型的 channel 后端进行通信,请确保本地安装有 ROS2 Humble;


Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (c) 2024 The AimRT Authors.
# AimRT is licensed under Mulan PSL v2.

aimrt:
plugin:
plugins:
- name: net_plugin
path: ${AIMRT_PLUGIN_DIR}/libaimrt_net_plugin.so
options:
thread_num: 4
http_options:
listen_ip: 127.0.0.1
listen_port: 50081
log:
core_lvl: INFO # Trace/Debug/Info/Warn/Error/Fatal/Off
default_module_lvl: INFO
backends:
- type: console
channel:
backends:
- type: http
options:
pub_topics_options:
- topic_name: "(.*)"
server_url_list: ["http://127.0.0.1:50080"]
pub_topics_options:
- topic_name: "(.*)"
enable_backends: [http]
module:
modules:
- name: NormalPublisherPyModule
log_lvl: INFO

NormalPublisherPyModule:
topic_name: test_topic
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) 2024 The AimRT Authors.
# AimRT is licensed under Mulan PSL v2.

aimrt:
plugin:
plugins:
- name: net_plugin
path: ${AIMRT_PLUGIN_DIR}/libaimrt_net_plugin.so
options:
thread_num: 4
http_options:
listen_ip: 127.0.0.1
listen_port: 50080
log:
core_lvl: INFO # Trace/Debug/Info/Warn/Error/Fatal/Off
default_module_lvl: INFO
backends:
- type: console
channel:
backends:
- type: http
sub_topics_options:
- topic_name: "(.*)"
enable_backends: [http]
module:
modules:
- name: NormalSubscriberPyModule
log_lvl: INFO

NormalSubscriberPyModule:
topic_name: test_topic
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) 2024 The AimRT Authors.
# AimRT is licensed under Mulan PSL v2.

aimrt:
plugin:
plugins:
- name: ros2_plugin
path: ${AIMRT_PLUGIN_DIR}/libaimrt_ros2_plugin.so
options:
node_name: example_ros2_chn_py_publisher_node
executor_type: MultiThreaded # SingleThreaded/StaticSingleThreaded/MultiThreaded
executor_thread_num: 2
log:
core_lvl: INFO # Trace/Debug/Info/Warn/Error/Fatal/Off
default_module_lvl: INFO
backends:
- type: console
channel:
backends:
- type: ros2
pub_topics_options:
- topic_name: "(.*)"
enable_backends: [ros2]
module:
modules:
- name: NormalPublisherPyModule
log_lvl: INFO

NormalPublisherPyModule:
topic_name: test_topic
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright (c) 2024 The AimRT Authors.
# AimRT is licensed under Mulan PSL v2.

aimrt:
plugin:
plugins:
- name: ros2_plugin
path: ${AIMRT_PLUGIN_DIR}/libaimrt_ros2_plugin.so
options:
node_name: example_ros2_chn_py_subscriber_node
executor_type: MultiThreaded # SingleThreaded/StaticSingleThreaded/MultiThreaded
executor_thread_num: 2
log:
core_lvl: INFO # Trace/Debug/Info/Warn/Error/Fatal/Off
default_module_lvl: INFO
backends:
- type: console
channel:
backends:
- type: ros2
sub_topics_options:
- topic_name: "(.*)"
enable_backends: [ros2]
module:
modules:
- name: NormalSubscriberPyModule
log_lvl: INFO

NormalSubscriberPyModule:
topic_name: test_topic
Loading

0 comments on commit df5d21e

Please sign in to comment.