Skip to content

Instantly share code, notes, and snippets.

@tik0
Created July 28, 2018 23:07
Show Gist options
  • Save tik0/6a1807799c34a0970cd0a6775af36177 to your computer and use it in GitHub Desktop.
Save tik0/6a1807799c34a0970cd0a6775af36177 to your computer and use it in GitHub Desktop.
Test for ROS numpy serialization
from nav_msgs.msg import OccupancyGrid
import numpy as np
import StringIO
# Check for the numpy serialization for ROS
# messages w/o publisher:
# http://wiki.ros.org/rospy_tutorials/Tutorials/numpy
# This is our buffer
buff = StringIO.StringIO()
# This is the messages to serialze
grid = OccupancyGrid()
# Just store some numpy array to data
np_array = np.minimum(-100, np.zeros((2,3,4)).flatten().astype(np.int8), dtype=np.int8)
# Test serialization: http://docs.ros.org/diamondback/api/nav_msgs/html/__OccupancyGrid_8py_source.html
# 1. np array serialize by numpy methods (should work)
grid.data = np_array
grid.serialize_numpy(buff, np)
# 2. np array serialze by common methods (shouldn't work: AttributeError: 'list' object has no attribute 'tostring')
grid.data = np_array
grid.serialize(buff)
# 3. list serialize by numpy methods (should work)
grid.data = np_array.tolist()
grid.serialize_numpy(buff, np)
# 4. list serialize by common methods (should work)
grid.serialize(buff)
# Print out the serialized data
grid.read()
grid.buf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment