Skip to content

Commit

Permalink
simple command costmap api - update few functions (#3169)
Browse files Browse the repository at this point in the history
* * add aditional function to costmap_2d.py

Signed-off-by: Stevedan Omodolor <stevedan.o.omodolor@gmail.com>

Updated-by: Jaehun Kim <k9632441@gmail.com>

* finish task B

* Update nav2_simple_commander/nav2_simple_commander/costmap_2d.py

* Update method docs

* Remove underscores at parameters and split getCost into getCostXY and getCostIdx

* Update method docstrings

* lint code & update docstring, remove default value of getCostXY

* lint code with pep257 & flake8
  • Loading branch information
JacksonK9 authored and SteveMacenski committed Nov 8, 2022
1 parent 84c2683 commit f1c3390
Showing 1 changed file with 100 additions and 4 deletions.
104 changes: 100 additions & 4 deletions nav2_simple_commander/nav2_simple_commander/costmap_2d.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#! /usr/bin/env python3
# Copyright 2021 Samsung Research America
# Copyright 2022 Stevedan Ogochukwu Omodolor
# Copyright 2022 Jaehun Jackson Kim
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,6 +15,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""
This is a Python3 API for costmap 2d messages from the stack.
It provides the basic conversion, get/set,
and handling semantics found in the costmap 2d C++ API.
"""

import numpy as np


Expand All @@ -25,6 +33,15 @@ class PyCostmap2D:
"""

def __init__(self, occupancy_map):
"""
Initializer for costmap2D.
Initialize instance variables with parameter occupancy_map.
Args:
occupancy_map (OccupancyGrid): 2D OccupancyGrid Map
Returns:
None
"""
self.size_x = occupancy_map.info.width
self.size_y = occupancy_map.info.height
self.resolution = occupancy_map.info.resolution
Expand All @@ -33,8 +50,7 @@ def __init__(self, occupancy_map):
self.global_frame_id = occupancy_map.header.frame_id
self.costmap_timestamp = occupancy_map.header.stamp
# Extract costmap
self.costmap = np.array(occupancy_map.data, dtype=np.int8).reshape(
self.size_y, self.size_x)
self.costmap = np.array(occupancy_map.data, dtype=np.uint8)

def getSizeInCellsX(self):
"""Get map width in cells."""
Expand All @@ -46,11 +62,11 @@ def getSizeInCellsY(self):

def getSizeInMetersX(self):
"""Get x axis map size in meters."""
return (self.size_x - 1 + 0.5) * self.resolution_
return (self.size_x - 1 + 0.5) * self.resolution

def getSizeInMetersY(self):
"""Get y axis map size in meters."""
return (self.size_y - 1 + 0.5) * self.resolution_
return (self.size_y - 1 + 0.5) * self.resolution

def getOriginX(self):
"""Get the origin x axis of the map [m]."""
Expand All @@ -71,3 +87,83 @@ def getGlobalFrameID(self):
def getCostmapTimestamp(self):
"""Get costmap timestamp."""
return self.costmap_timestamp

def getCostXY(self, mx: int, my: int) -> np.uint8:
"""
Get the cost of a cell in the costmap using map coordinate XY.
Args:
mx (int): map coordinate X to get cost
my (int): map coordinate Y to get cost
Returns:
np.uint8: cost of a cell
"""
return self.costmap[self.getIndex(mx, my)]

def getCostIdx(self, index: int) -> np.uint8:
"""
Get the cost of a cell in the costmap using Index.
Args:
index (int): index of cell to get cost
Returns:
np.uint8: cost of a cell
"""
return self.costmap[index]

def setCost(self, mx: int, my: int, cost: np.uint8) -> None:
"""
Set the cost of a cell in the costmap using map coordinate XY.
Args:
mx (int): map coordinate X to get cost
my (int): map coordinate Y to get cost
cost (np.uint8): The cost to set the cell
Returns:
None
"""
self.costmap[self.getIndex(mx, my)] = cost

def mapToWorld(self, mx: int, my: int) -> tuple[float, float]:
"""
Get the world coordinate XY using map coordinate XY.
Args:
mx (int): map coordinate X to get world coordinate
my (int): map coordinate Y to get world coordinate
Returns:
tuple of float: wx, wy
wx (float) [m]: world coordinate X
wy (float) [m]: world coordinate Y
"""
wx = self.origin_x + (mx + 0.5) * self.resolution
wy = self.origin_y + (my + 0.5) * self.resolution
return (wx, wy)

def worldToMap(self, wx: float, wy: float) -> tuple[int, int]:
"""
Get the map coordinate XY using world coordinate XY.
Args:
wx (float) [m]: world coordinate X to get map coordinate
wy (float) [m]: world coordinate Y to get map coordinate
Returns:
tuple of int: mx, my
mx (int): map coordinate X
my (int): map coordinate Y
"""
mx = int((wx - self.origin_x) // self.resolution)
my = int((wy - self.origin_y) // self.resolution)
return (mx, my)

def getIndex(self, mx: int, my: int) -> int:
"""
Get the index of the cell using map coordinate XY.
Args:
mx (int): map coordinate X to get Index
my (int): map coordinate Y to get Index
Returns:
int: The index of the cell
"""
return my * self.size_x + mx

0 comments on commit f1c3390

Please sign in to comment.