This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: struct.unpack_from return 2 values instead of one
Type: behavior Stage: resolved
Components: Versions:
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: sebaleme, steven.daprano
Priority: normal Keywords:

Created on 2020-11-28 21:19 by sebaleme, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
getIntensity.png sebaleme, 2020-11-28 21:19
Messages (3)
msg382017 - (view) Author: Sébastien Lemetter (sebaleme) Date: 2020-11-28 21:19
Hi,

I am using an old version, but maybe you can still help me. I am using the ROS operating system, and there, the unpack_from function is used for reading values from bags.
When I try to read uint16 with the function pointcloud2.readpoint(), it returns 2 values instead of 1, and I couldn t understand why. Since this readpoint() is from the official pointcloud2 package from ros, and unpack_from is from official struct package, I don t think I could do many error here. In the screenshot, you can see that the inputs parameters seems ok. I am reading a field from my bag which is 2 bytes, and the offset (12) make also sense (4th value after 3 floats).



def read_points(cloud, field_names=None):

    """

    Read points from a L{sensor_msgs.PointCloud2} message.

 

    @param cloud: The point cloud to read from.

    @type  cloud: L{sensor_msgs.PointCloud2}

    @param field_names: The names of fields to read. If None, read all fields. [default: None]

    @type  field_names: iterable

    @param skip_nans: If True, then don't return any point with a NaN value.

    @type  skip_nans: bool [default: False]

    @param uvs: If specified, then only return the points at the given coordinates. [default: empty list]

    @type  uvs: iterable

    @return: Generator which yields a list of values for each point.

    @rtype:  generator

    """

    assert isinstance(cloud, roslib.message.Message) and cloud._type == 'sensor_msgs/PointCloud2', 'cloud is not a sensor_msgs.msg.PointCloud2'

    fmt = _get_struct_fmt(cloud.is_bigendian, cloud.fields, field_names)

    print('fmt size ' , str(struct.calcsize(fmt)))

    print('cloud.height ' , str(cloud.height))

    print('cloud.row_step ' , str(cloud.row_step))

    width, height, point_step, row_step, data, isnan = cloud.width, cloud.height, cloud.point_step, cloud.row_step, cloud.data, math.isnan

    unpack_from = struct.Struct(fmt).unpack_from

 

    for v in range(height):

        offset = row_step * v

        for u in range(width):      

            yield unpack_from(data, offset)

            offset += point_step

def callback(data):

    print("pointcloud size is %d bytes" % len(data.data))

    pointCloudArray_a = [1,2,3,4,5]

    #print((data.data.size))

    for indoux in range(4):

        print("==========================")

        print(indoux)

        pointCloudArray_a[indoux] = np.array(list((read_points2(data, field_names=(fields_PI6[indoux])))))

Here my mail again if you need more infos:
sebaleme@gmail.com

thanks,
msg382018 - (view) Author: Sébastien Lemetter (sebaleme) Date: 2020-11-28 21:22
I forgot, here is my configuration:

Ubuntu 18.04
Ros Melodic
Python 2.7
msg382025 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-11-28 22:29
Hi Sébastien,

This is a bug tracker for reporting bugs, not a help desk to help with your code. There are many community resources to help you debug your code, but this is not one of them. You can try:

* Reddit's /r/learnpython
* the Python-list mailing list or news group https://mail.python.org/mailman/listinfo/python-list or comp.lang.python
* The Python Discuss forums https://discuss.python.org/c/users/7
* various IRC channels https://www.python.org/community/irc/
* other forums https://www.python.org/community/forums/

There may be others. However I guess you probably need ROS specific forums.

It may help to read these first:

https://stackoverflow.com/help/minimal-reproducible-example

http://www.sscce.org/

http://catb.org/~esr/faqs/smart-questions.html

If there is a problem with pointcloud2.readpoint returning two values when you expected one, you should check the pointcloud2 documentation, or the ROS project, and report the bug to them.

I'm going to close this as "Third Party", as any bug is most likely in the pointcloud2 library, or your own code.

Unfortunately, even if it turns out to be a bug in struct.unpack_from, although I don't understand why you think it might be, we aren't accepting any bug reports for Python 2, it is no longer maintained. Please upgrade to Python 3.9 and see if that fixes the problem.

Please note that your sample code gives a function "read_points" but the callback calls a different function "read_points2".
History
Date User Action Args
2022-04-11 14:59:38adminsetgithub: 86660
2020-11-28 22:29:42steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg382025

resolution: third party
stage: resolved
2020-11-28 21:22:13sebalemesetmessages: + msg382018
2020-11-28 21:19:50sebalemecreate