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
33 INSCRIBED_INFLATED_OBSTACLE = 253
34 MAX_NON_OBSTACLE = 252
40 FootprintCollisionChecker.
42 FootprintCollisionChecker Class for getting the cost
43 and checking the collisions of a Footprint
47 """Initialize the FootprintCollisionChecker Object."""
53 Iterate over all the points in a footprint and check for collision.
57 footprint (Polygon): The footprint to calculate the collision cost for
61 LETHAL_OBSTACLE (int): If collision was found, 254 will be returned
62 footprint_cost (float): The maximum cost found in the footprint points
69 x0, y0 = self.
worldToMapValidatedworldToMapValidated(footprint.points[0].x, footprint.points[0].y)
71 if x0
is None or y0
is None:
72 return LETHAL_OBSTACLE
77 for i
in range(len(footprint.points) - 1):
79 footprint.points[i + 1].x, footprint.points[i + 1].y
82 if x1
is None or y1
is None:
83 return LETHAL_OBSTACLE
85 footprint_cost = max(float(self.
lineCostlineCost(x0, x1, y0, y1)), footprint_cost)
89 if footprint_cost == LETHAL_OBSTACLE:
92 return max(float(self.
lineCostlineCost(xstart, x1, ystart, y1)), footprint_cost)
95 y0: float, y1: float, step_size: float = 0.5) -> float:
97 Iterate over all the points along a line and check for collision.
101 x0 (float): Abscissa of the initial point in map coordinates
102 y0 (float): Ordinate of the initial point in map coordinates
103 x1 (float): Abscissa of the final point in map coordinates
104 y1 (float): Ordinate of the final point in map coordinates
105 step_size (float): Optional, Increments' resolution, defaults to 0.5
109 LETHAL_OBSTACLE (int): If collision was found, 254 will be returned
110 line_cost (float): The maximum cost found in the line points
117 while line_iterator.isValid():
118 point_cost = float(self.
pointCostpointCost(
119 int(line_iterator.getX()), int(line_iterator.getY())
122 if point_cost == LETHAL_OBSTACLE:
125 if line_cost < point_cost:
126 line_cost = point_cost
128 line_iterator.advance()
134 Get the map coordinate XY using world coordinate XY.
138 wx (float): world coordinate X
139 wy (float): world coordinate Y
143 None: if coordinates are invalid
144 tuple of int: mx, my (if coordinates are valid)
145 mx (int): map coordinate X
146 my (int): map coordinate Y
151 'Costmap not specified, use setCostmap to specify the costmap first'
157 Get the cost of a point in the costmap using map coordinates XY.
161 mx (int): map coordinate X
162 my (int): map coordinate Y
166 np.uint8: cost of a point
171 'Costmap not specified, use setCostmap to specify the costmap first'
173 return self.
costmap_costmap_.getCostXY(x, y)
177 Specify which costmap to use.
181 costmap (PyCostmap2D): costmap to use in the object's methods
192 theta: float, footprint: Polygon) -> float:
194 Get the cost of a footprint at a specific Pose in map coordinates.
198 x (float): map coordinate X
199 y (float): map coordinate Y
200 theta (float): absolute rotation angle of the footprint
201 footprint (Polygon): the footprint to calculate its cost at the given Pose
205 LETHAL_OBSTACLE (int): If collision was found, 254 will be returned
206 footprint_cost (float): The maximum cost found in the footprint points
211 oriented_footprint = Polygon()
213 for i
in range(len(footprint.points)):
216 footprint.points[i].x * cos_th - footprint.points[i].y * sin_th
219 footprint.points[i].x * sin_th + footprint.points[i].y * cos_th
221 oriented_footprint.points.append(new_pt)