ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Custom Message with uint8[] Reads as string dtype

asked 2020-10-13 16:23:47 -0500

cwillia109 gravatar image

updated 2022-07-24 11:06:32 -0500

lucasw gravatar image

I'm trying to pass a custom message through a service . I have a C++ node using this service just fine, however in Python I can't seem to pull information from the custom message. When I try to take the passed array, it reads it as a 'str' type. Is there anything I am doing wrong here?

Edit: in Visual Studio Code, when I create a variable field = DensityField() and type "field.data", hovering over "data" reveals that it thinks its a string. Is it possible to force catkin_make to regenerate the messages? I have no idea why it thinks its a string.

GetDensity.srv:

---
DensityField densityfield

DensityField.msg:

uint8 width
uint8 height
uint8[] data

Service Client:

def get_density():
    """
    :rtype: list[int]
    """
    rospy.wait_for_service("static_density")
    try:
        get_density = rospy.ServiceProxy("static_density", GetDensity)
        resp = get_density()
        return resp.densityfield
    except rospy.ServiceException as e:
        rospy.logerr("static_density server is not responding: %s" % (e))

...
    density = get_density()
    occupancy_grid = get_map()
    print(type(density)) # Prints <class 'voronoi_msgs.msg._DensityField.DensityField'>
    print(type(occupancy_grid)) # Prints <class 'nav_msgs.msg._OccupancyGrid.OccupancyGrid'>
    print(density.data[0] + density.data[1])
    np_density = np.array(density.data)

    np_density_filtered = np.multiply(np_density, np_og)
     # Gives error TypeError: ufunc 'multiply' did not contain a loop with signature matching types dtype('S62500') dtype('S62500') dtype('S62500')

The server:

data = []
width = 0
height = 0


def handle_density_request(msg):
    global data, width, height
    response = DensityField()
    response.data = data
    response.height = height
    response.width = width
    print(type(data)) # Prints <type 'list'>
    print(type(data[0])) # Prints <type 'int'>
    return GetDensityResponse(
        densityfield=response
    )


def main():
    global data, width, height
    rospy.init_node("density_server")
    rospy.Service("static_density", GetDensity, handle_density_request)
    rospack = rospkg.RosPack()
    path = rospack.get_path('voronoi_node_generator') + \
        '/data/' + rospy.get_param("~data_name")
    img = cv2.imread(path, 0)
    print(img)
    img = np.flip(img, 0)
    height, width = img.shape
    img = img.flatten()
    data = img.tolist()
    rospy.loginfo("Data length: %d | Width: %d | Height: %d" %
                  (len(data), width, height))
    rospy.loginfo("Data type: %s" % img.dtype)
    rospy.spin()
edit retag flag offensive close merge delete

Comments

Not sure, but #q59827 and #q341940 are probably duplicates.

gvdhoorn gravatar image gvdhoorn  ( 2020-10-14 05:15:16 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
3

answered 2020-10-14 02:57:33 -0500

gvdhoorn gravatar image

updated 2020-10-14 02:59:49 -0500

Custom Message with uint8[] Reads as string [..] in Python I can't seem to pull information from the custom message. When I try to take the passed array, it reads it as a 'str' type. Is there anything I am doing wrong here?

No, and this is actually as-designed and documented.

From wiki/msg: Message Description Specification, section Array handling (a screenshot of the page as recreating the table is not really possible here):

rospy_array_handling

Note the columns for Python 2 and Python 3, the row for uint8[] and note 2 about how in Python 2 that all gets mapped to str.

Note also this is a "Python 2 thing": there is a str<->bytes duality there almost. I can't find any authoritative sources right now, so I can't link to anything, but there is a myriad of pages which discuss this difficulty with Python 2.

Edit: in Visual Studio Code, when I create a variable field = DensityField() and type "field.data", hovering over "data" reveals that it thinks its a string. [..] I have no idea why it thinks its a string.

No, it doesn't think it, it is a str.

Is it possible to force catkin_make to regenerate the messages?

That would not change anything, as I hope is clear now.

edit flag offensive delete link more

Comments

I guess that's on me for assuming it works similar to C++. I see that numpy is needed in this situation.

cwillia109 gravatar image cwillia109  ( 2020-10-17 17:38:31 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2020-10-13 16:23:47 -0500

Seen: 1,385 times

Last updated: Oct 14 '20