Skip to main content

Convert ROS2 messages to and from numpy arrays

Project description

ros2_numpy

Note: This is the same as the original ros_numpy package by eric-wieser and ros2_numpy package by box-robotics just edited to be OS independent and installable using pip.

This project is a fork of ros2_numpy to work with ROS 2. It provides tools for converting ROS messages to and from numpy arrays. In the ROS 2 port, the module has been renamed to ros2_numpy. Users are encouraged to update their application code to import the module as shown below.

pip install transformations
import ros2_numpy as rnp

Prefacing your calls like rnp.numpify(...) or rnp.msgify(...) should help future proof your codebase while the ROS 2 ports are API compatible.

This module contains two core functions:

  • arr = numpify(msg, ...) - try to get a numpy object from a message
  • msg = msgify(MessageType, arr, ...) - try and convert a numpy object to a message

Currently supports:

  • sensor_msgs.msg.PointCloud2 ↔ structured np.array:

    data = np.zeros(100, dtype=[
      ('x', np.float32),
      ('y', np.float32),
      ('vectors', np.float32, (3,))
    ])
    data['x'] = np.arange(100)
    data['y'] = data['x']*2
    data['vectors'] = np.arange(100)[:,np.newaxis]
    
    msg = ros2_numpy.msgify(PointCloud2, data)
    
    data = ros2_numpy.numpify(msg)
    
  • sensor_msgs.msg.Image ↔ 2/3-D np.array, similar to the function of cv_bridge, but without the dependency on cv2

  • nav_msgs.msg.OccupancyGridnp.ma.array

  • geometry.msg.Vector3 ↔ 1-D np.array. hom=True gives [x, y, z, 0]

  • geometry.msg.Point ↔ 1-D np.array. hom=True gives [x, y, z, 1]

  • geometry.msg.Quaternion ↔ 1-D np.array, [x, y, z, w]

  • geometry.msg.Transform ↔ 4×4 np.array, the homogeneous transformation matrix

  • geometry.msg.Pose ↔ 4×4 np.array, the homogeneous transformation matrix from the origin

Support for more types can be added with:

@ros2_numpy.converts_to_numpy(SomeMessageClass)
def convert(my_msg):
    return np.array(...)

@ros2_numpy.converts_from_numpy(SomeMessageClass)
def convert(my_array):
    return SomeMessageClass(...)

Any extra args or kwargs to numpify or msgify will be forwarded to your conversion function

Future work

  • Add simple conversions for:

    • geometry_msgs.msg.Inertia

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ros2_numpy-0.0.2.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

ros2_numpy-0.0.2-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file ros2_numpy-0.0.2.tar.gz.

File metadata

  • Download URL: ros2_numpy-0.0.2.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for ros2_numpy-0.0.2.tar.gz
Algorithm Hash digest
SHA256 5dd03bae46e1f8443f616e7539121c73a3f36ca9df3461b43a03b0d044cd7b70
MD5 f2c985ae88ce64816d51077fa88036ba
BLAKE2b-256 1e8ce653f6dfb08a7ea13c5fd5e7f22560d7439e80b53570bdb32b46db7409b8

See more details on using hashes here.

File details

Details for the file ros2_numpy-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: ros2_numpy-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for ros2_numpy-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b168359cddb5e69b7fb9626ceb7d610b7f39eb26f494a7179e64ee866d77fbce
MD5 2b9919e0dd73667c7bc7049c92dfa71d
BLAKE2b-256 e5473de630b5fbec9cbd03e7d70e45ac6874d9898c23d2d51eda8765afa8b3c0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page