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