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

Append polygons in PolygonArray

asked 2022-10-06 14:02:12 -0500

updated 2022-10-11 02:15:14 -0500

ravijoshi gravatar image

Hey there, I am new to this forum. I just discovered the possibility to display multiple polygons at once (in my case 2D x/y bounding boxes for a radar in RViz). In this case globally I have defined in Python:

   from jsk_recognition_msgs.msg import PolygonArray
   BB = PolygonArray()

When entering in my callback function, I have a series of if conditions thet get executed sequentially (I am decoding a series of messages from CAN Bus using cantools). So I have something like this:

def can_callback(data):
         if data.id ==  ..A
         elif data.id == ..B
         elif data.id == ..C
         elif data.id == ..D

When program enters the last if (elif data.id, n times as the number of objects detected by the radar), it evaluates for each enter the four vertices of the polygon I would like to visualize. After obtaining the four Vertices namely V1, V2, V3, V4 (Point32 with z coord = 0.0 ), I append to each condition of the if the Point. (by looking at the structure of PolygonArray: PolygonArray

This is what I try to assign in the if:

elif data.id == ..D       + entering with i= 0 !
        / do things/
        / obtain V1, V2, V3, V4 / 

         BB.polygons[i].polygon.points.append(V1)
         BB.polygons[i].polygon.points.append(V2)
         BB.polygons[i].polygon.points.append(V3)
         BB.polygons[i].polygon.points.append(V4)

         i=+ 1

I am getting as an error:

BB.polygons[i].polygon.points.append(V1) | IndexError: list index out of range

I don't know where I am getting things wrong, I don't have a clue on how to solve this. Any help is appreciated

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-10-11 02:14:24 -0500

ravijoshi gravatar image

updated 2022-10-11 02:16:15 -0500

You are accessing an empty list unknowingly while writing the following statement:

BB.polygons[i].polygon.points.append(V1)

This is why the list index out of range error is displayed.

To get rid of this error, you should do the following:

# Create a PolygonArray
bb = PolygonArray() # We do not use a capital letter to name an instance as a common practice.

# Create a polygon
polygon = PolygonStamped()

# Add points to our polygon
polygon.points.append(v1)
polygon.points.append(v2)
polygon.points.append(v3)
polygon.points.append(v4)

# add polygon to the PolygonArray 
bb.polygons.append(polygon)

Notice that I am using the append function of list in python instead of using an index, such as i.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2022-10-06 14:02:12 -0500

Seen: 31 times

Last updated: Oct 11 '22