18 This is a Python3 API for a Footprint Collision Checker.
20 It provides the needed methods to manipulate the coordinates
21 and calculate the cost of a Footprint
24 from math
import cos, sin
26 from geometry_msgs.msg
import Point32, Polygon
32 INSCRIBED_INFLATED_OBSTACLE = 253
33 MAX_NON_OBSTACLE = 252
39 FootprintCollisionChecker.
41 FootprintCollisionChecker Class for getting the cost
42 and checking the collisions of a Footprint
46 """Initialize the FootprintCollisionChecker Object."""
52 Iterate over all the points in a footprint and check for collision.
56 footprint (Polygon): The footprint to calculate the collision cost for
60 LETHAL_OBSTACLE (int): If collision was found, 254 will be returned
61 footprint_cost (float): The maximum cost found in the footprint points
68 x0, y0 = self.
worldToMapValidatedworldToMapValidated(footprint.points[0].x, footprint.points[0].y)
70 if x0
is None or y0
is None:
71 return LETHAL_OBSTACLE
76 for i
in range(len(footprint.points) - 1):
78 footprint.points[i + 1].x, footprint.points[i + 1].y
81 if x1
is None or y1
is None:
82 return LETHAL_OBSTACLE
84 footprint_cost = max(float(self.
lineCostlineCost(x0, x1, y0, y1)), footprint_cost)
88 if footprint_cost == LETHAL_OBSTACLE:
91 return max(float(self.
lineCostlineCost(xstart, x1, ystart, y1)), footprint_cost)
93 def lineCost(self, x0, x1, y0, y1, step_size=0.5):
95 Iterate over all the points along a line and check for collision.
99 x0 (float): Abscissa of the initial point in map coordinates
100 y0 (float): Ordinate of the initial point in map coordinates
101 x1 (float): Abscissa of the final point in map coordinates
102 y1 (float): Ordinate of the final point in map coordinates
103 step_size (float): Optional, Increments' resolution, defaults to 0.5
107 LETHAL_OBSTACLE (int): If collision was found, 254 will be returned
108 line_cost (float): The maximum cost found in the line points
115 while line_iterator.isValid():
117 int(line_iterator.getX()), int(line_iterator.getY())
120 if point_cost == LETHAL_OBSTACLE:
123 if line_cost < point_cost:
124 line_cost = point_cost
126 line_iterator.advance()
132 Get the map coordinate XY using world coordinate XY.
136 wx (float): world coordinate X
137 wy (float): world coordinate Y
141 None: if coordinates are invalid
142 tuple of int: mx, my (if coordinates are valid)
143 mx (int): map coordinate X
144 my (int): map coordinate Y
149 'Costmap not specified, use setCostmap to specify the costmap first'
151 return self.
costmap_costmap_.worldToMap(wx, wy)
155 Get the cost of a point in the costmap using map coordinates XY.
159 mx (int): map coordinate X
160 my (int): map coordinate Y
164 np.uint8: cost of a point
169 'Costmap not specified, use setCostmap to specify the costmap first'
171 return self.
costmap_costmap_.getCostXY(x, y)
175 Specify which costmap to use.
179 costmap (PyCostmap2D): costmap to use in the object's methods
191 Get the cost of a footprint at a specific Pose in map coordinates.
195 x (float): map coordinate X
196 y (float): map coordinate Y
197 theta (float): absolute rotation angle of the footprint
198 footprint (Polygon): the footprint to calculate its cost at the given Pose
202 LETHAL_OBSTACLE (int): If collision was found, 254 will be returned
203 footprint_cost (float): The maximum cost found in the footprint points
208 oriented_footprint = Polygon()
210 for i
in range(len(footprint.points)):
213 footprint.points[i].x * cos_th - footprint.points[i].y * sin_th
216 footprint.points[i].x * sin_th + footprint.points[i].y * cos_th
218 oriented_footprint.points.append(new_pt)