11import numpy as np
22import matplotlib .pyplot as plt
3+ from matplotlib .backend_bases import KeyEvent
34from PathPlanning .TimeBasedPathPlanning .GridWithDynamicObstacles import (
45 Grid ,
56 Position ,
@@ -30,13 +31,18 @@ def PlotNodePath(grid: Grid, start: Position, goal: Position, path: NodePath):
3031
3132 # for stopping simulation with the esc key.
3233 plt .gcf ().canvas .mpl_connect (
33- "key_release_event" , lambda event : [exit (0 ) if event .key == "escape" else None ]
34+ "key_release_event" ,
35+ lambda event : [exit (0 ) if event .key == "escape" else None ]
36+ if isinstance (event , KeyEvent ) else None
3437 )
3538
3639 for i in range (0 , path .goal_reached_time ()):
3740 obs_positions = grid .get_obstacle_positions_at_time (i )
3841 obs_points .set_data (obs_positions [0 ], obs_positions [1 ])
3942 path_position = path .get_position (i )
43+ if not path_position :
44+ raise Exception (f"Path position not found for time { i } ." )
45+
4046 path_points .set_data ([path_position .x ], [path_position .y ])
4147 plt .pause (0.2 )
4248 plt .show ()
@@ -91,7 +97,9 @@ def PlotNodePaths(grid: Grid, start_and_goals: list[StartAndGoal], paths: list[N
9197
9298 # For stopping simulation with the esc key
9399 plt .gcf ().canvas .mpl_connect (
94- "key_release_event" , lambda event : [exit (0 ) if event .key == "escape" else None ]
100+ "key_release_event" ,
101+ lambda event : [exit (0 ) if event .key == "escape" else None ]
102+ if isinstance (event , KeyEvent ) else None
95103 )
96104
97105 # Find the maximum time across all paths
@@ -112,6 +120,8 @@ def PlotNodePaths(grid: Grid, start_and_goals: list[StartAndGoal], paths: list[N
112120 print (path )
113121 print (i )
114122 path_position = path .get_position (i )
123+ if not path_position :
124+ raise Exception (f"Path position not found for time { i } ." )
115125
116126 # Verify position is valid
117127 assert not path_position in obs_positions
0 commit comments