Skip to content

Commit d518dd3

Browse files
Mypy nav2 simple commander (#5059)
* Added most nav2_simple_commander files that can be trivially converted to be mypy compatible. Signed-off-by: Leander Stephen D'Souza <[email protected]> * Modified edge cases and types for robot_navigator to pass mypy checks. Signed-off-by: Leander Stephen D'Souza <[email protected]> * Added nav2_simple_commander to the linting workflow. Signed-off-by: Leander Stephen D'Souza <[email protected]> * Added logs and ignored type errors due to spins w/o timeout. Signed-off-by: Leander Stephen D'Souza <[email protected]> --------- Signed-off-by: Leander Stephen D'Souza <[email protected]>
1 parent 34a4b7c commit d518dd3

30 files changed

+248
-181
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ jobs:
4545
nav2_lifecycle_manager
4646
nav2_loopback_sim
4747
nav2_map_server
48+
nav2_simple_commander
4849
arguments: --config tools/pyproject.toml
4950

5051
pre-commit:

nav2_simple_commander/launch/assisted_teleop_example_launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from launch_ros.actions import Node
2929

3030

31-
def generate_launch_description():
31+
def generate_launch_description() -> LaunchDescription:
3232
nav2_bringup_dir = get_package_share_directory('nav2_bringup')
3333
sim_dir = get_package_share_directory('nav2_minimal_tb4_sim')
3434
desc_dir = get_package_share_directory('nav2_minimal_tb4_description')

nav2_simple_commander/launch/follow_path_example_launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from launch_ros.actions import Node
2828

2929

30-
def generate_launch_description():
30+
def generate_launch_description() -> LaunchDescription:
3131
nav2_bringup_dir = get_package_share_directory('nav2_bringup')
3232
sim_dir = get_package_share_directory('nav2_minimal_tb4_sim')
3333
desc_dir = get_package_share_directory('nav2_minimal_tb4_description')

nav2_simple_commander/launch/inspection_demo_launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from launch_ros.actions import Node
2828

2929

30-
def generate_launch_description():
30+
def generate_launch_description() -> LaunchDescription:
3131
nav2_bringup_dir = get_package_share_directory('nav2_bringup')
3232
sim_dir = get_package_share_directory('nav2_minimal_tb4_sim')
3333
desc_dir = get_package_share_directory('nav2_minimal_tb4_description')

nav2_simple_commander/launch/nav_through_poses_example_launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from launch_ros.actions import Node
2828

2929

30-
def generate_launch_description():
30+
def generate_launch_description() -> LaunchDescription:
3131
nav2_bringup_dir = get_package_share_directory('nav2_bringup')
3232
sim_dir = get_package_share_directory('nav2_minimal_tb4_sim')
3333
desc_dir = get_package_share_directory('nav2_minimal_tb4_description')

nav2_simple_commander/launch/nav_to_pose_example_launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from launch_ros.actions import Node
2828

2929

30-
def generate_launch_description():
30+
def generate_launch_description() -> LaunchDescription:
3131
nav2_bringup_dir = get_package_share_directory('nav2_bringup')
3232
sim_dir = get_package_share_directory('nav2_minimal_tb4_sim')
3333
desc_dir = get_package_share_directory('nav2_minimal_tb4_description')

nav2_simple_commander/launch/picking_demo_launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from launch_ros.actions import Node
2828

2929

30-
def generate_launch_description():
30+
def generate_launch_description() -> LaunchDescription:
3131
nav2_bringup_dir = get_package_share_directory('nav2_bringup')
3232
sim_dir = get_package_share_directory('nav2_minimal_tb4_sim')
3333
desc_dir = get_package_share_directory('nav2_minimal_tb4_description')

nav2_simple_commander/launch/recoveries_example_launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from launch_ros.actions import Node
2828

2929

30-
def generate_launch_description():
30+
def generate_launch_description() -> LaunchDescription:
3131
nav2_bringup_dir = get_package_share_directory('nav2_bringup')
3232
sim_dir = get_package_share_directory('nav2_minimal_tb4_sim')
3333
desc_dir = get_package_share_directory('nav2_minimal_tb4_description')

nav2_simple_commander/launch/security_demo_launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from launch_ros.actions import Node
2828

2929

30-
def generate_launch_description():
30+
def generate_launch_description() -> LaunchDescription:
3131
nav2_bringup_dir = get_package_share_directory('nav2_bringup')
3232
sim_dir = get_package_share_directory('nav2_minimal_tb4_sim')
3333
desc_dir = get_package_share_directory('nav2_minimal_tb4_description')

nav2_simple_commander/launch/waypoint_follower_example_launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from launch_ros.actions import Node
2828

2929

30-
def generate_launch_description():
30+
def generate_launch_description() -> LaunchDescription:
3131
nav2_bringup_dir = get_package_share_directory('nav2_bringup')
3232
sim_dir = get_package_share_directory('nav2_minimal_tb4_sim')
3333
desc_dir = get_package_share_directory('nav2_minimal_tb4_description')

nav2_simple_commander/nav2_simple_commander/costmap_2d.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@
2323
and handling semantics found in the costmap 2d C++ API.
2424
"""
2525

26+
from typing import Optional
27+
28+
from builtin_interfaces.msg import Time
29+
from nav_msgs.msg import OccupancyGrid
2630
import numpy as np
31+
from numpy.typing import NDArray
2732

2833

2934
class PyCostmap2D:
@@ -33,7 +38,7 @@ class PyCostmap2D:
3338
Costmap Python3 API for OccupancyGrids to populate from published messages
3439
"""
3540

36-
def __init__(self, occupancy_map):
41+
def __init__(self, occupancy_map: OccupancyGrid) -> None:
3742
"""
3843
Initialize costmap2D.
3944
@@ -46,49 +51,49 @@ def __init__(self, occupancy_map):
4651
None
4752
4853
"""
49-
self.size_x = occupancy_map.info.width
50-
self.size_y = occupancy_map.info.height
51-
self.resolution = occupancy_map.info.resolution
52-
self.origin_x = occupancy_map.info.origin.position.x
53-
self.origin_y = occupancy_map.info.origin.position.y
54-
self.global_frame_id = occupancy_map.header.frame_id
55-
self.costmap_timestamp = occupancy_map.header.stamp
54+
self.size_x: int = occupancy_map.info.width
55+
self.size_y: int = occupancy_map.info.height
56+
self.resolution: float = occupancy_map.info.resolution
57+
self.origin_x: float = occupancy_map.info.origin.position.x
58+
self.origin_y: float = occupancy_map.info.origin.position.y
59+
self.global_frame_id: str = occupancy_map.header.frame_id
60+
self.costmap_timestamp: Time = occupancy_map.header.stamp
5661
# Extract costmap
57-
self.costmap = np.array(occupancy_map.data, dtype=np.uint8)
62+
self.costmap: NDArray[np.uint8] = np.array(occupancy_map.data, dtype=np.uint8)
5863

59-
def getSizeInCellsX(self):
64+
def getSizeInCellsX(self) -> int:
6065
"""Get map width in cells."""
6166
return self.size_x
6267

63-
def getSizeInCellsY(self):
68+
def getSizeInCellsY(self) -> int:
6469
"""Get map height in cells."""
6570
return self.size_y
6671

67-
def getSizeInMetersX(self):
72+
def getSizeInMetersX(self) -> float:
6873
"""Get x axis map size in meters."""
6974
return (self.size_x - 1 + 0.5) * self.resolution
7075

71-
def getSizeInMetersY(self):
76+
def getSizeInMetersY(self) -> float:
7277
"""Get y axis map size in meters."""
7378
return (self.size_y - 1 + 0.5) * self.resolution
7479

75-
def getOriginX(self):
80+
def getOriginX(self) -> float:
7681
"""Get the origin x axis of the map [m]."""
7782
return self.origin_x
7883

79-
def getOriginY(self):
84+
def getOriginY(self) -> float:
8085
"""Get the origin y axis of the map [m]."""
8186
return self.origin_y
8287

83-
def getResolution(self):
88+
def getResolution(self) -> float:
8489
"""Get map resolution [m/cell]."""
8590
return self.resolution
8691

87-
def getGlobalFrameID(self):
92+
def getGlobalFrameID(self) -> str:
8893
"""Get global frame_id."""
8994
return self.global_frame_id
9095

91-
def getCostmapTimestamp(self):
96+
def getCostmapTimestamp(self) -> Time:
9297
"""Get costmap timestamp."""
9398
return self.costmap_timestamp
9499

@@ -106,7 +111,7 @@ def getCostXY(self, mx: int, my: int) -> np.uint8:
106111
np.uint8: cost of a cell
107112
108113
"""
109-
return self.costmap[self.getIndex(mx, my)]
114+
return np.uint8(self.costmap[self.getIndex(mx, my)])
110115

111116
def getCostIdx(self, index: int) -> np.uint8:
112117
"""
@@ -121,7 +126,7 @@ def getCostIdx(self, index: int) -> np.uint8:
121126
np.uint8: cost of a cell
122127
123128
"""
124-
return self.costmap[index]
129+
return np.uint8(self.costmap[index])
125130

126131
def setCost(self, mx: int, my: int, cost: np.uint8) -> None:
127132
"""
@@ -160,7 +165,7 @@ def mapToWorld(self, mx: int, my: int) -> tuple[float, float]:
160165
wy = self.origin_y + (my + 0.5) * self.resolution
161166
return (wx, wy)
162167

163-
def worldToMapValidated(self, wx: float, wy: float):
168+
def worldToMapValidated(self, wx: float, wy: float) -> tuple[Optional[int], Optional[int]]:
164169
"""
165170
Get the map coordinate XY using world coordinate XY.
166171

nav2_simple_commander/nav2_simple_commander/demo_inspection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"""
2727

2828

29-
def main():
29+
def main() -> None:
3030
rclpy.init()
3131

3232
navigator = BasicNavigator()

nav2_simple_commander/nav2_simple_commander/demo_picking.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
}
4242

4343

44-
def main():
44+
def main() -> None:
4545
# Received virtual request for picking item at Shelf A and bring to
4646
# worker at the pallet jack 7 for shipping. This request would
4747
# contain the shelf ID ('shelf_A') and shipping destination ('frieght_bay_3')
@@ -94,7 +94,7 @@ def main():
9494
'Estimated time of arrival at '
9595
+ request_item_location
9696
+ ' for worker: '
97-
+ '{0:.0f}'.format(
97+
+ '{:.0f}'.format(
9898
Duration.from_msg(feedback.estimated_time_remaining).nanoseconds
9999
/ 1e9
100100
)

nav2_simple_commander/nav2_simple_commander/demo_recoveries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"""
2626

2727

28-
def main():
28+
def main() -> None:
2929
rclpy.init()
3030

3131
navigator = BasicNavigator()

nav2_simple_commander/nav2_simple_commander/demo_security.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"""
2828

2929

30-
def main():
30+
def main() -> None:
3131
rclpy.init()
3232

3333
navigator = BasicNavigator()
@@ -56,7 +56,7 @@ def main():
5656
navigator.waitUntilNav2Active()
5757

5858
# Do security route until dead
59-
while rclpy.ok():
59+
while rclpy.ok(): # type: ignore[attr-defined]
6060
# Send our route
6161
route_poses = []
6262
pose = PoseStamped()
@@ -78,7 +78,7 @@ def main():
7878
if feedback and i % 5 == 0:
7979
print(
8080
'Estimated time to complete current route: '
81-
+ '{0:.0f}'.format(
81+
+ '{:.0f}'.format(
8282
Duration.from_msg(feedback.estimated_time_remaining).nanoseconds
8383
/ 1e9
8484
)

nav2_simple_commander/nav2_simple_commander/example_assisted_teleop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"""
2525

2626

27-
def main():
27+
def main() -> None:
2828
rclpy.init()
2929

3030
navigator = BasicNavigator()

nav2_simple_commander/nav2_simple_commander/example_follow_path.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"""
2323

2424

25-
def main():
25+
def main() -> None:
2626
rclpy.init()
2727

2828
navigator = BasicNavigator()
@@ -69,9 +69,9 @@ def main():
6969
if feedback and i % 5 == 0:
7070
print(
7171
'Estimated distance remaining to goal position: '
72-
+ '{0:.3f}'.format(feedback.distance_to_goal)
72+
+ f'{feedback.distance_to_goal:.3f}'
7373
+ '\nCurrent speed of the robot: '
74-
+ '{0:.3f}'.format(feedback.speed)
74+
+ f'{feedback.speed:.3f}'
7575
)
7676

7777
# Do something depending on the return code

nav2_simple_commander/nav2_simple_commander/example_nav_through_poses.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"""
2424

2525

26-
def main():
26+
def main() -> None:
2727
rclpy.init()
2828

2929
navigator = BasicNavigator()
@@ -102,7 +102,7 @@ def main():
102102
if feedback and i % 5 == 0:
103103
print(
104104
'Estimated time of arrival: '
105-
+ '{0:.0f}'.format(
105+
+ '{:.0f}'.format(
106106
Duration.from_msg(feedback.estimated_time_remaining).nanoseconds
107107
/ 1e9
108108
)

nav2_simple_commander/nav2_simple_commander/example_nav_to_pose.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"""
2424

2525

26-
def main():
26+
def main() -> None:
2727
rclpy.init()
2828

2929
navigator = BasicNavigator()
@@ -82,7 +82,7 @@ def main():
8282
if feedback and i % 5 == 0:
8383
print(
8484
'Estimated time of arrival: '
85-
+ '{0:.0f}'.format(
85+
+ '{:.0f}'.format(
8686
Duration.from_msg(feedback.estimated_time_remaining).nanoseconds
8787
/ 1e9
8888
)

nav2_simple_commander/nav2_simple_commander/example_waypoint_follower.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"""
2424

2525

26-
def main():
26+
def main() -> None:
2727
rclpy.init()
2828

2929
navigator = BasicNavigator()

0 commit comments

Comments
 (0)