Nav2 Navigation Stack - lyrical  lyrical
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 
27 from nav2_msgs.msg import Costmap
28 import numpy as np
29 
30 
32  """
33  PyCostmap2D.
34 
35  Costmap Python3 API for Costmaps to populate from published messages
36  """
37 
38  def __init__(self, occupancy_map: Costmap):
39  """
40  Initialize costmap2D.
41 
42  Args
43  ----
44  occupancy_map (Costmap): 2D OccupancyGrid Map
45 
46  Returns
47  -------
48  None
49 
50  """
51  self.size_xsize_x = occupancy_map.metadata.size_x
52  self.size_ysize_y = occupancy_map.metadata.size_y
53  self.resolutionresolution = occupancy_map.metadata.resolution
54  self.origin_xorigin_x = occupancy_map.metadata.origin.position.x
55  self.origin_yorigin_y = occupancy_map.metadata.origin.position.y
56  self.global_frame_idglobal_frame_id = occupancy_map.header.frame_id
57  self.costmap_timestampcostmap_timestamp = occupancy_map.header.stamp
58  self.costmapcostmap = np.array(occupancy_map.data, dtype=np.uint8)
59 
60  def getSizeInCellsX(self):
61  """Get map width in cells."""
62  return self.size_xsize_x
63 
64  def getSizeInCellsY(self):
65  """Get map height in cells."""
66  return self.size_ysize_y
67 
68  def getSizeInMetersX(self):
69  """Get x axis map size in meters."""
70  return self.size_xsize_x * self.resolutionresolution
71 
72  def getSizeInMetersY(self):
73  """Get y axis map size in meters."""
74  return self.size_ysize_y * self.resolutionresolution
75 
76  def getOriginX(self):
77  """Get the origin x axis of the map [m]."""
78  return self.origin_xorigin_x
79 
80  def getOriginY(self):
81  """Get the origin y axis of the map [m]."""
82  return self.origin_yorigin_y
83 
84  def getResolution(self):
85  """Get map resolution [m/cell]."""
86  return self.resolutionresolution
87 
88  def getGlobalFrameID(self):
89  """Get global frame_id."""
90  return self.global_frame_idglobal_frame_id
91 
93  """Get costmap timestamp."""
94  return self.costmap_timestampcostmap_timestamp
95 
96  def getCostXY(self, mx: int, my: int):
97  """
98  Get the cost of a cell in the costmap using map coordinate XY.
99 
100  Args
101  ----
102  mx (int): map coordinate X to get cost
103  my (int): map coordinate Y to get cost
104 
105  Returns
106  -------
107  np.uint8: cost of a cell
108 
109  """
110  return np.uint8(self.costmapcostmap[self.getIndexgetIndex(mx, my)])
111 
112  def getCostIdx(self, index: int):
113  """
114  Get the cost of a cell in the costmap using Index.
115 
116  Args
117  ----
118  index (int): index of cell to get cost
119 
120  Returns
121  -------
122  np.uint8: cost of a cell
123 
124  """
125  return np.uint8(self.costmapcostmap[index])
126 
127  def setCost(self, mx: int, my: int, cost: np.uint8):
128  """
129  Set the cost of a cell in the costmap using map coordinate XY.
130 
131  Args
132  ----
133  mx (int): map coordinate X to get cost
134  my (int): map coordinate Y to get cost
135  cost (np.uint8): The cost to set the cell
136 
137  Returns
138  -------
139  None
140 
141  """
142  self.costmapcostmap[self.getIndexgetIndex(mx, my)] = cost
143 
144  def mapToWorld(self, mx: int, my: int):
145  """
146  Get the world coordinate XY using map coordinate XY.
147 
148  Args
149  ----
150  mx (int): map coordinate X to get world coordinate
151  my (int): map coordinate Y to get world coordinate
152 
153  Returns
154  -------
155  tuple of float: wx, wy
156  wx (float) [m]: world coordinate X
157  wy (float) [m]: world coordinate Y
158 
159  """
160  wx = self.origin_xorigin_x + (mx + 0.5) * self.resolutionresolution
161  wy = self.origin_yorigin_y + (my + 0.5) * self.resolutionresolution
162  return (wx, wy)
163 
164  def worldToMapValidated(self, wx: float, wy: float):
165  """
166  Get the map coordinate XY using world coordinate XY.
167 
168  Args
169  ----
170  wx (float) [m]: world coordinate X to get map coordinate
171  wy (float) [m]: world coordinate Y to get map coordinate
172 
173  Returns
174  -------
175  (None, None): if coordinates are invalid
176  tuple of int: mx, my (if coordinates are valid)
177  mx (int): map coordinate X
178  my (int): map coordinate Y
179 
180  """
181  if wx < self.origin_xorigin_x or wy < self.origin_yorigin_y:
182  return (None, None)
183  mx = int((wx - self.origin_xorigin_x) // self.resolutionresolution)
184  my = int((wy - self.origin_yorigin_y) // self.resolutionresolution)
185  if mx < self.size_xsize_x and my < self.size_ysize_y:
186  return (mx, my)
187  return (None, None)
188 
189  def getIndex(self, mx: int, my: int):
190  """
191  Get the index of the cell using map coordinate XY.
192 
193  Args
194  ----
195  mx (int): map coordinate X to get Index
196  my (int): map coordinate Y to get Index
197 
198  Returns
199  -------
200  int: The index of the cell
201 
202  """
203  return my * self.size_xsize_x + mx
def setCost(self, int mx, int my, np.uint8 cost)
Definition: costmap_2d.py:127
def getCostXY(self, int mx, int my)
Definition: costmap_2d.py:96
def worldToMapValidated(self, float wx, float wy)
Definition: costmap_2d.py:164
def mapToWorld(self, int mx, int my)
Definition: costmap_2d.py:144
def __init__(self, Costmap occupancy_map)
Definition: costmap_2d.py:38