Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
costmap_2d.py
1 #! /usr/bin/env python3
2 # Copyright 2021 Samsung Research America
3 # Copyright 2022 Stevedan Ogochukwu Omodolor
4 # Copyright 2022 Jaehun Jackson Kim
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 
18 """
19 This is a Python3 API for costmap 2d messages from the stack.
20 
21 It provides the basic conversion, get/set,
22 and handling semantics found in the costmap 2d C++ API.
23 """
24 
25 import numpy as np
26 
27 
29  """
30  PyCostmap2D.
31 
32  Costmap Python3 API for OccupancyGrids to populate from published messages
33  """
34 
35  def __init__(self, occupancy_map):
36  """
37  Initialize costmap2D.
38 
39  Args:
40  ----
41  occupancy_map (OccupancyGrid): 2D OccupancyGrid Map
42 
43  """
44  self.size_xsize_x = occupancy_map.info.width
45  self.size_ysize_y = occupancy_map.info.height
46  self.resolutionresolution = occupancy_map.info.resolution
47  self.origin_xorigin_x = occupancy_map.info.origin.position.x
48  self.origin_yorigin_y = occupancy_map.info.origin.position.y
49  self.global_frame_idglobal_frame_id = occupancy_map.header.frame_id
50  self.costmap_timestampcostmap_timestamp = occupancy_map.header.stamp
51  # Extract costmap
52  self.costmapcostmap = np.array(occupancy_map.data, dtype=np.uint8)
53 
54  def getSizeInCellsX(self):
55  """Get map width in cells."""
56  return self.size_xsize_x
57 
58  def getSizeInCellsY(self):
59  """Get map height in cells."""
60  return self.size_ysize_y
61 
62  def getSizeInMetersX(self):
63  """Get x axis map size in meters."""
64  return (self.size_xsize_x - 1 + 0.5) * self.resolutionresolution
65 
66  def getSizeInMetersY(self):
67  """Get y axis map size in meters."""
68  return (self.size_ysize_y - 1 + 0.5) * self.resolutionresolution
69 
70  def getOriginX(self):
71  """Get the origin x axis of the map [m]."""
72  return self.origin_xorigin_x
73 
74  def getOriginY(self):
75  """Get the origin y axis of the map [m]."""
76  return self.origin_yorigin_y
77 
78  def getResolution(self):
79  """Get map resolution [m/cell]."""
80  return self.resolutionresolution
81 
82  def getGlobalFrameID(self):
83  """Get global frame_id."""
84  return self.global_frame_idglobal_frame_id
85 
87  """Get costmap timestamp."""
88  return self.costmap_timestampcostmap_timestamp
89 
90  def getCostXY(self, mx: int, my: int) -> np.uint8:
91  """
92  Get the cost of a cell in the costmap using map coordinate XY.
93 
94  Args
95  ----
96  mx (int): map coordinate X to get cost
97  my (int): map coordinate Y to get cost
98 
99  Returns
100  -------
101  np.uint8: cost of a cell
102 
103  """
104  return self.costmapcostmap[self.getIndexgetIndex(mx, my)]
105 
106  def getCostIdx(self, index: int) -> np.uint8:
107  """
108  Get the cost of a cell in the costmap using Index.
109 
110  Args
111  ----
112  index (int): index of cell to get cost
113 
114  Returns
115  -------
116  np.uint8: cost of a cell
117 
118  """
119  return self.costmapcostmap[index]
120 
121  def setCost(self, mx: int, my: int, cost: np.uint8) -> None:
122  """
123  Set the cost of a cell in the costmap using map coordinate XY.
124 
125  Args
126  ----
127  mx (int): map coordinate X to get cost
128  my (int): map coordinate Y to get cost
129  cost (np.uint8): The cost to set the cell
130 
131  Returns
132  -------
133  None
134 
135  """
136  self.costmapcostmap[self.getIndexgetIndex(mx, my)] = cost
137 
138  def mapToWorld(self, mx: int, my: int) -> tuple[float, float]:
139  """
140  Get the world coordinate XY using map coordinate XY.
141 
142  Args
143  ----
144  mx (int): map coordinate X to get world coordinate
145  my (int): map coordinate Y to get world coordinate
146 
147  Returns
148  -------
149  tuple of float: wx, wy
150  wx (float) [m]: world coordinate X
151  wy (float) [m]: world coordinate Y
152 
153  """
154  wx = self.origin_xorigin_x + (mx + 0.5) * self.resolutionresolution
155  wy = self.origin_yorigin_y + (my + 0.5) * self.resolutionresolution
156  return (wx, wy)
157 
158  def worldToMap(self, wx: float, wy: float) -> tuple[int, int]:
159  """
160  Get the map coordinate XY using world coordinate XY.
161 
162  Args
163  ----
164  wx (float) [m]: world coordinate X to get map coordinate
165  wy (float) [m]: world coordinate Y to get map coordinate
166 
167  Returns
168  -------
169  tuple of int: mx, my
170  mx (int): map coordinate X
171  my (int): map coordinate Y
172 
173  """
174  mx = int((wx - self.origin_xorigin_x) // self.resolutionresolution)
175  my = int((wy - self.origin_yorigin_y) // self.resolutionresolution)
176  return (mx, my)
177 
178  def getIndex(self, mx: int, my: int) -> int:
179  """
180  Get the index of the cell using map coordinate XY.
181 
182  Args
183  ----
184  mx (int): map coordinate X to get Index
185  my (int): map coordinate Y to get Index
186 
187  Returns
188  -------
189  int: The index of the cell
190 
191  """
192  return my * self.size_xsize_x + mx
tuple[int, int] worldToMap(self, float wx, float wy)
Definition: costmap_2d.py:158
tuple[float, float] mapToWorld(self, int mx, int my)
Definition: costmap_2d.py:138
None setCost(self, int mx, int my, np.uint8 cost)
Definition: costmap_2d.py:121
np.uint8 getCostXY(self, int mx, int my)
Definition: costmap_2d.py:90
np.uint8 getCostIdx(self, int index)
Definition: costmap_2d.py:106