20 This is a Python3 API for costmap 2d messages from the stack.
22 It provides the basic conversion, get/set,
23 and handling semantics found in the costmap 2d C++ API.
26 from typing
import Optional
31 from numpy.typing
import NDArray
38 Costmap Python3 API for OccupancyGrids to populate from published messages
41 def __init__(self, occupancy_map: OccupancyGrid) ->
None:
47 occupancy_map (OccupancyGrid): 2D OccupancyGrid Map
54 self.size_x: int = occupancy_map.info.width
55 self.size_y: int = occupancy_map.info.height
56 self.resolution: float = occupancy_map.info.resolution
57 self.origin_x: float = occupancy_map.info.origin.position.x
58 self.origin_y: float = occupancy_map.info.origin.position.y
59 self.global_frame_id: str = occupancy_map.header.frame_id
60 self.costmap_timestamp: Time = occupancy_map.header.stamp
62 self.costmap: NDArray[np.uint8] = np.array(occupancy_map.data, dtype=np.uint8)
65 """Get map width in cells."""
69 """Get map height in cells."""
73 """Get x axis map size in meters."""
74 return (self.size_x - 1 + 0.5) * self.resolution
77 """Get y axis map size in meters."""
78 return (self.size_y - 1 + 0.5) * self.resolution
81 """Get the origin x axis of the map [m]."""
85 """Get the origin y axis of the map [m]."""
89 """Get map resolution [m/cell]."""
90 return self.resolution
93 """Get global frame_id."""
94 return self.global_frame_id
97 """Get costmap timestamp."""
98 return self.costmap_timestamp
102 Get the cost of a cell in the costmap using map coordinate XY.
106 mx (int): map coordinate X to get cost
107 my (int): map coordinate Y to get cost
111 np.uint8: cost of a cell
114 return np.uint8(self.costmap[self.
getIndexgetIndex(mx, my)])
118 Get the cost of a cell in the costmap using Index.
122 index (int): index of cell to get cost
126 np.uint8: cost of a cell
129 return np.uint8(self.costmap[index])
131 def setCost(self, mx: int, my: int, cost: np.uint8) ->
None:
133 Set the cost of a cell in the costmap using map coordinate XY.
137 mx (int): map coordinate X to get cost
138 my (int): map coordinate Y to get cost
139 cost (np.uint8): The cost to set the cell
146 self.costmap[self.
getIndexgetIndex(mx, my)] = cost
148 def mapToWorld(self, mx: int, my: int) -> tuple[float, float]:
150 Get the world coordinate XY using map coordinate XY.
154 mx (int): map coordinate X to get world coordinate
155 my (int): map coordinate Y to get world coordinate
159 tuple of float: wx, wy
160 wx (float) [m]: world coordinate X
161 wy (float) [m]: world coordinate Y
164 wx = self.origin_x + (mx + 0.5) * self.resolution
165 wy = self.origin_y + (my + 0.5) * self.resolution
170 Get the map coordinate XY using world coordinate XY.
174 wx (float) [m]: world coordinate X to get map coordinate
175 wy (float) [m]: world coordinate Y to get map coordinate
179 (None, None): if coordinates are invalid
180 tuple of int: mx, my (if coordinates are valid)
181 mx (int): map coordinate X
182 my (int): map coordinate Y
185 if wx < self.origin_x
or wy < self.origin_y:
187 mx = int((wx - self.origin_x) // self.resolution)
188 my = int((wy - self.origin_y) // self.resolution)
189 if mx < self.size_x
and my < self.size_y:
195 Get the index of the cell using map coordinate XY.
199 mx (int): map coordinate X to get Index
200 my (int): map coordinate Y to get Index
204 int: The index of the cell
207 return my * self.size_x + mx
int getSizeInCellsY(self)
tuple[float, float] mapToWorld(self, int mx, int my)
None setCost(self, int mx, int my, np.uint8 cost)
None __init__(self, OccupancyGrid occupancy_map)
int getIndex(self, int mx, int my)
int getSizeInCellsX(self)
str getGlobalFrameID(self)
Time getCostmapTimestamp(self)
float getSizeInMetersX(self)
tuple[Optional[int], Optional[int]] worldToMapValidated(self, float wx, float wy)
float getResolution(self)
float getSizeInMetersY(self)
np.uint8 getCostXY(self, int mx, int my)
np.uint8 getCostIdx(self, int index)