Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Footprint Collision Checker (Simple Commander API) #3280

Merged
merged 38 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
a824708
Added Line Iterator
Afif-Swaidan Sep 17, 2022
5db702e
Merge branch 'ros-planning:main' into main
afifswaidan Sep 28, 2022
a1767f2
Updated Line Iterator to a new iteration method
afifswaidan Sep 28, 2022
a9c6d6d
Added the resolution as a parameter/ fixed linting
afifswaidan Oct 3, 2022
9b79757
Added the resolution as a parameter/ fixed linting
afifswaidan Oct 3, 2022
94934dd
Merge branch 'ros-planning:main' into main
afifswaidan Oct 3, 2022
9ca4db6
Merge branch 'ros-planning:main' into main
afifswaidan Oct 8, 2022
c050299
Added unittests for the line iterator
afifswaidan Oct 9, 2022
02f726d
Merge branch 'ros-planning:main' into main
afifswaidan Oct 15, 2022
9df1b62
Added unittests based on "unittest" package
afifswaidan Oct 15, 2022
6731c6b
Fixed __init__.py and rephrased some docstrings
afifswaidan Oct 15, 2022
d35d26d
Fixed linting errors
afifswaidan Oct 22, 2022
b8d0858
Fixed Linting Errors
afifswaidan Oct 23, 2022
dc438c4
Merge branch 'ros-planning:main' into main
afifswaidan Oct 29, 2022
9d3992b
Merge branch 'ros-planning:main' into main
afifswaidan Nov 6, 2022
f9d48a2
Added some unittests and removed some methods
afifswaidan Nov 6, 2022
a571cde
Dummy commit for CircleCI Issue
afifswaidan Nov 7, 2022
63ed3c9
Merge branch 'ros-planning:main' into main
afifswaidan Nov 12, 2022
f06c5eb
Initial commit for python collision checker
afifswaidan Nov 12, 2022
743144d
Initial commit for footprint_collision_checker
afifswaidan Nov 13, 2022
a8d252d
Added docstrings and renamed the validity function
afifswaidan Nov 19, 2022
8171237
Merge branch 'ros-planning:main' into main
afifswaidan Nov 19, 2022
23956e8
Merge branch 'ros-planning:main' into main
afifswaidan Dec 4, 2022
9360223
implemented worldToMapValidated and added tests
afifswaidan Dec 4, 2022
2fe5036
Merge branch 'main' of https://github.com/afifswaidan/navigation2
afifswaidan Dec 4, 2022
fdb20aa
Merge branch 'ros-planning:main' into main
afifswaidan Dec 6, 2022
3e2598f
fixed wrong imports
afifswaidan Dec 6, 2022
14ceb82
Merge branch 'ros-planning:main' into main
afifswaidan Dec 11, 2022
13d9205
Fixed test cases linting errors
afifswaidan Dec 11, 2022
66c4987
Fixed one line comment error
afifswaidan Dec 11, 2022
2aa3d33
Fixed docstring colon error
afifswaidan Dec 11, 2022
3e83f5c
Fixed docstring dashed line error
afifswaidan Dec 11, 2022
2007b28
Merge branch 'ros-planning:main' into main
afifswaidan Dec 13, 2022
bd93142
Added return None to keep consistent docstrings
afifswaidan Dec 13, 2022
35c67ad
Changed worldToMapValidated to 1 call only
afifswaidan Dec 14, 2022
41e30f4
Fixed linting error and added none-zero cost test case
afifswaidan Dec 14, 2022
6b7498f
Fixed linting errors
afifswaidan Dec 14, 2022
1010460
Fixed linting errors
afifswaidan Dec 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions nav2_simple_commander/nav2_simple_commander/costmap_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def mapToWorld(self, mx: int, my: int) -> tuple[float, float]:
wy = self.origin_y + (my + 0.5) * self.resolution
return (wx, wy)

def worldToMap(self, wx: float, wy: float) -> tuple[int, int]:
def worldToMapValidated(self, wx: float, wy: float):
"""
Get the map coordinate XY using world coordinate XY.

Expand All @@ -166,14 +166,19 @@ def worldToMap(self, wx: float, wy: float) -> tuple[int, int]:

Returns
-------
tuple of int: mx, my
(None, None): if coordinates are invalid
tuple of int: mx, my (if coordinates are valid)
mx (int): map coordinate X
my (int): map coordinate Y

"""
if wx < self.origin_x or wy < self.origin_y:
return (None, None)
mx = int((wx - self.origin_x) // self.resolution)
my = int((wy - self.origin_y) // self.resolution)
return (mx, my)
if mx < self.size_x and my < self.size_y:
return (mx, my)
return (None, None)

def getIndex(self, mx: int, my: int) -> int:
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
#! /usr/bin/env python3
# Copyright 2021 Samsung Research America
SteveMacenski marked this conversation as resolved.
Show resolved Hide resolved
# Copyright 2022 Afif Swaidan
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
This is a Python3 API for a Footprint Collision Checker.

It provides the needed methods to manipulate the coordinates
and calculate the cost of a Footprint
"""

from math import cos, sin
from nav2_simple_commander.line_iterator import LineIterator
from nav2_simple_commander.costmap_2d import PyCostmap2D
from geometry_msgs.msg import Polygon
from geometry_msgs.msg import Point32

NO_INFORMATION = 255
LETHAL_OBSTACLE = 254
INSCRIBED_INFLATED_OBSTACLE = 253
MAX_NON_OBSTACLE = 252
FREE_SPACE = 0


class FootprintCollisionChecker():
"""
FootprintCollisionChecker.

FootprintCollisionChecker Class for getting the cost
and checking the collisions of a Footprint
"""

def __init__(self):
"""Initialize the FootprintCollisionChecker Object."""
self.costmap_ = None
pass

def footprintCost(self, footprint: Polygon):
"""
Iterate over all the points in a footprint and check for collision.

Args
----
footprint (Polygon): The footprint to calculate the collision cost for

Returns
-------
LETHAL_OBSTACLE (int): If collision was found, 254 will be returned
footprint_cost (float): The maximum cost found in the footprint points

"""
footprint_cost = 0.0
x1 = 0.0
y1 = 0.0

x0, y0 = self.worldToMapValidated(
footprint.points[0].x, footprint.points[0].y)

if x0 is None or y0 is None:
return LETHAL_OBSTACLE

xstart = x0
ystart = y0

for i in range(len(footprint.points) - 1):
x1, y1 = self.worldToMapValidated(
footprint.points[i + 1].x, footprint.points[i + 1].y)

if x1 is None or y1 is None:
return LETHAL_OBSTACLE

footprint_cost = max(
float(self.lineCost(x0, x1, y0, y1)), footprint_cost)
x0 = x1
y0 = y1

if footprint_cost == LETHAL_OBSTACLE:
return footprint_cost

return max(float(self.lineCost(xstart, x1, ystart, y1)), footprint_cost)

def lineCost(self, x0, x1, y0, y1, step_size=0.1):
"""
Iterate over all the points along a line and check for collision.

Args
----
x0 (float): Abscissa of the initial point in map coordinates
y0 (float): Ordinate of the initial point in map coordinates
x1 (float): Abscissa of the final point in map coordinates
y1 (float): Ordinate of the final point in map coordinates
step_size (float): Optional, Increments' resolution, defaults to 0.1

Returns
-------
LETHAL_OBSTACLE (int): If collision was found, 254 will be returned
line_cost (float): The maximum cost found in the line points

"""
line_cost = 0.0
point_cost = -1.0
line_iterator = LineIterator(x0, y0, x1, y1, step_size)

while line_iterator.isValid():
point_cost = self.pointCost(
int(line_iterator.getX()), int(line_iterator.getY()))

if point_cost == LETHAL_OBSTACLE:
return point_cost

if line_cost < point_cost:
line_cost = point_cost

line_iterator.advance()

return line_cost

def worldToMapValidated(self, wx: float, wy: float):
"""
Get the map coordinate XY using world coordinate XY.

Args
----
wx (float): world coordinate X
wy (float): world coordinate Y

Returns
-------
None: if coordinates are invalid
tuple of int: mx, my (if coordinates are valid)
mx (int): map coordinate X
my (int): map coordinate Y

"""
if self.costmap_ is None:
raise ValueError(
"Costmap not specified, use setCostmap to specify the costmap first")
return self.costmap_.worldToMapValidated(wx, wy)

def pointCost(self, x: int, y: int):
"""
Get the cost of a point in the costmap using map coordinates XY.

Args
----
mx (int): map coordinate X
my (int): map coordinate Y

Returns
-------
np.uint8: cost of a point

"""
if self.costmap_ is None:
raise ValueError(
"Costmap not specified, use setCostmap to specify the costmap first")
return self.costmap_.getCostXY(x, y)

def setCostmap(self, costmap: PyCostmap2D):
"""
Specify which costmap to use.

Args
----
costmap (PyCostmap2D): costmap to use in the object's methods

Returns
-------
None

"""
self.costmap_ = costmap
return None

def footprintCostAtPose(self, x: float, y: float, theta: float, footprint: Polygon):
"""
Get the cost of a footprint at a specific Pose in map coordinates.

Args
----
x (float): map coordinate X
y (float): map coordinate Y
theta (float): absolute rotation angle of the footprint
footprint (Polygon): the footprint to calculate its cost at the given Pose

Returns
-------
LETHAL_OBSTACLE (int): If collision was found, 254 will be returned
footprint_cost (float): The maximum cost found in the footprint points

"""
cos_th = cos(theta)
sin_th = sin(theta)
oriented_footprint = Polygon()

for i in range(len(footprint.points)):
new_pt = Point32()
new_pt.x = x + \
(footprint.points[i].x * cos_th -
footprint.points[i].y * sin_th)
new_pt.y = y + \
(footprint.points[i].x * sin_th +
footprint.points[i].y * cos_th)
oriented_footprint.points.append(new_pt)

return self.footprintCost(oriented_footprint)
Loading