Robotics StackExchange | Archived questions

Reading pushlished data of custom msg types

I have a peculiar problem when I attempt to read data from a custom topic.

My msg type are Space.msg :

string region
uint8[] spaces

And, SpaceArray.msg :

Space[] spaces

I am successfully publishing data using the following function of one of my python scripts : def publish(self) : messagepub = rospy.Publisher("scansfreespace", SpaceArray, queue_size=10)

    spacesArray = SpaceArray()

    for region in self.regions :
        space = Space()
        space.region = region
        space.spaces = self.function_returning_array_of_numbers()
        spacesArray.spaces.append(space)

    message_pub.publish( spacesArray )

All the above work as expected. I get the expected output from *** as displayed below :

spaces: 
  - 
    region: "port_bow"
    spaces: []
  - 
    region: "port_abeam_aft"
    spaces: [97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130]
  - 
    region: "starboard_abeam_bow"
    spaces: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130]
  - 
    region: "port_abeam_bow"
    spaces: []
  - 
    region: "starboard_bow"
    spaces: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
  - 
    region: "port_aft"
    spaces: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130]
  - 
    region: "starboard_abeam_aft"
    spaces: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130]
  - 
    region: "starboard_aft"
    spaces: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130]
---

The problems begin when I subscribe to the topic in a different python script and I attempt to loop through the data, using :

for space in self.freespace.spaces :
    rospy.loginfo( space.region )
    rospy.loginfo( space.spaces )

I get the following, strange, output :

[INFO] [1629671292.468372, 5998.820000]: port_bow
[INFO] [1629671292.470293, 5998.822000]: 
[INFO] [1629671292.471589, 5998.823000]: port_abeam_aft
[INFO] [1629671292.474296, 5998.827000]: z{|}~���
[INFO] [1629671292.476524, 5998.828000]: starboard_abeam_bow
[INFO] [1629671292.478395, 5998.830000]: 


 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~���
[INFO] [1629671292.479938, 5998.830000]: port_abeam_bow
[INFO] [1629671292.481388, 5998.830000]: 
[INFO] [1629671292.483383, 5998.830000]: starboard_bow
[INFO] [1629671292.484975, 5998.832000]: 


 !"#$%&'()*+
[INFO] [1629671292.488132, 5998.837000]: port_aft
[INFO] [1629671292.490671, 5998.839000]: 


 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~���
[INFO] [1629671292.492211, 5998.840000]: starboard_abeam_aft
[INFO] [1629671292.494020, 5998.841000]: 


 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~���
[INFO] [1629671292.505584, 5998.849000]: starboard_aft
[INFO] [1629671292.507278, 5998.850000]: 


 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\bcdefghijklmnopqrstuvwxyz{|}~���
^Z

The array data is corrupted somehow and is unreadable. I am not sure how nor why.

Asked by sisko on 2021-08-22 18:11:16 UTC

Comments

Is the array size what you intended it to be? You may want to check the following.

rospy.loginfo(self.freespace.spaces.size())
rospy.loginfo(space.spaces.size())

Asked by miura on 2021-08-22 18:26:36 UTC

First line:

AttributeError: 'list' object has no attribute 'size'

Second line:

AttributeError: 'str' object has no attribute 'size'

I get those error in response to the code you suggested.

Asked by sisko on 2021-08-22 21:33:57 UTC

I believe this is a duplicate. See #q363356 fi.

Asked by gvdhoorn on 2021-08-23 01:19:22 UTC

I think it's definitely the same problem.

Asked by miura on 2021-08-23 18:33:31 UTC

@Miura: Maybe, but unless I missed something, there is no solution in that post

Asked by sisko on 2021-08-23 18:57:37 UTC

Answers

Using the uint8[] type will result in a str, so if you don't mind the larger data size, it would be easy to change the spaces to the uint16[] type.

Alternatively, you could do the following

spaces = [ord(c) for c in space.spaces]
rospy.loginfo(spaces)

ref: https://answers.ros.org/question/341940/custom-ros-message-with-unit8-in-python/

Asked by miura on 2021-08-24 18:10:44 UTC

Comments