https://github.com/geson-anko/numpy_ndarray_msgs
A ROS2 package for transmitting NumPy ndarrays between ROS2 nodes.
https://github.com/geson-anko/numpy_ndarray_msgs
messaging numpy python ros2
Last synced: 6 months ago
JSON representation
A ROS2 package for transmitting NumPy ndarrays between ROS2 nodes.
- Host: GitHub
- URL: https://github.com/geson-anko/numpy_ndarray_msgs
- Owner: Geson-anko
- License: mit
- Created: 2025-02-21T00:36:50.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-03-27T00:54:21.000Z (10 months ago)
- Last Synced: 2025-07-01T07:05:25.488Z (7 months ago)
- Topics: messaging, numpy, python, ros2
- Language: Python
- Homepage: https://qiita.com/GesonAnko/items/eca54b971685bd15a189
- Size: 107 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://docs.ros.org/en/humble/index.html)
[](https://docs.ros.org/en/jazzy/index.html)
[](https://www.python.org/downloads/)
[](https://opensource.org/licenses/MIT)
[](https://google.github.io/styleguide/pyguide.html#s3.8-comments-and-docstrings)
[](https://github.com/Geson-anko/numpy_ndarray_msgs/actions/workflows/main.yml)
# ROS2 NDArray Message
A ROS2 package for transmitting NumPy ndarrays between ROS2 nodes.
## Features
- Custom ROS2 message type for numpy.ndarray
- Bi-directional conversion between NumPy arrays and ROS2 messages
- Multi-dimensional array support with various data types
- Type-safe Python utilities with full type hints
## Requirements
- ROS2 Humble or higher
- Python 3.10+
## Installation
```bash
cd ~/ros2_ws/src
git clone https://github.com/Geson-anko/numpy_ndarray_msgs.git
cd ../
colcon build --packages-select numpy_ndarray_msgs
source install/setup.sh
```
### Install Python Utility Package
```bash
pip install ros2-ndarray-msg-utils
```
## Usage
### Python Utility Package
```python
import numpy as np
from ndarray_msg_utils import to_ros_msg, from_ros_msg, NDArray
from rclpy.clock import ROSClock
# Convert NumPy array to ROS2 message
array = np.array([[1, 2], [3, 4]], dtype=np.float32)
# Type Hint
msg: NDArray
msg = to_ros_msg(array)
# with Header
msg = to_ros_msg(array, timestamp=ROSClock().now(), frame_id="array_frame")
# Convert back to NumPy array
restored = from_ros_msg(msg)
```
### PyTorch Support
```bash
pip install "ros2-ndarray-msg-utils[torch]"
```
```py
import torch
from ndarray_msg_utils.torch import to_ros_msg, from_ros_msg
# Convert PyTorch tensor to ROS2 message
tensor = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32)
msg = to_ros_msg(tensor)
# Convert back to PyTorch tensor
restored = from_ros_msg(msg)
```
## License
MIT License