forked from AtsushiSakai/PythonRobotics
-
Notifications
You must be signed in to change notification settings - Fork 1
DRAFT: time based path planners #2
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b6e3343
wip - sketch out obstacles
SchwartzCode b83912a
move to correct path
SchwartzCode 133baaa
better animation
SchwartzCode be24df9
clean up
SchwartzCode 0240106
use np to sample points
SchwartzCode f6ce576
implemented time-based A*
SchwartzCode 5f88530
cleaning up Grid + adding new obstacle arrangement
SchwartzCode 5cf40fc
added unit test
SchwartzCode 15087e6
formatting p1
SchwartzCode a69f382
format STA* file
SchwartzCode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import numpy as np | ||
import random | ||
import matplotlib.pyplot as plt | ||
|
||
class Grid(): | ||
|
||
# Set in constructor | ||
grid_size = None | ||
grid = None | ||
obstacle_paths = [] | ||
|
||
# Problem definition | ||
time_limit = 100 | ||
num_obstacles = 2 | ||
|
||
def __init__(self, grid_size: np.ndarray[int, int]): | ||
self.grid_size = grid_size | ||
self.grid = np.zeros((grid_size[0], grid_size[1], self.time_limit)) | ||
|
||
for i in range(1, self.num_obstacles+1): | ||
self.obstacle_paths.append(self.generate_dynamic_obstacle(i)) | ||
|
||
def generate_dynamic_obstacle(self, obs_idx: int) -> list[np.ndarray[int, int]]: | ||
# TODO: dont spawn on another obstacle | ||
initial_position = (np.random.randint(0, self.grid_size[0]), np.random.randint(0, self.grid_size[1])) | ||
positions = [initial_position] | ||
print("Initial position: ", initial_position) | ||
|
||
diffs = [np.array([0, 1]), np.array([0, -1]), np.array([1, 0]), np.array([-1, 0]), np.array([0, 0])] | ||
|
||
for t in range(1, self.time_limit-1): | ||
random.shuffle(diffs) | ||
for diff in diffs: | ||
new_position = positions[-1] + diff | ||
|
||
# Check if new position is in grid | ||
if new_position[0] < 0 or new_position[0] >= self.grid_size[0] or new_position[1] < 0 or new_position[1] >= self.grid_size[1]: | ||
continue | ||
|
||
# Check if new position occupied by another obstacle | ||
if self.grid[new_position[0], new_position[1], t] == 0: | ||
positions.append(new_position) | ||
self.grid[new_position[0], new_position[1], t] = obs_idx | ||
break | ||
|
||
# Impossible situation for obstacle - stay in place | ||
print("Impossible situation for obstacle!") | ||
positions.append(positions[-1]) | ||
|
||
return positions | ||
|
||
show_animation = True | ||
|
||
def main(): | ||
grid = Grid(np.array([10, 10])) | ||
|
||
plt.figure() | ||
|
||
for t in range(0, grid.time_limit): | ||
plt.clf() | ||
|
||
if show_animation: # pragma: no cover | ||
# TODO: iter is clunky. Should use np array | ||
ax = plt.axes() | ||
ax.set_xlim(0, grid.grid_size[0]) | ||
ax.set_ylim(0, grid.grid_size[1]) | ||
|
||
for (obs_idx, obs_path) in enumerate(grid.obstacle_paths): | ||
obs_pos = obs_path[t] | ||
# plt.plot(obs_pos[0], obs_pos[1], "xr") | ||
circle = plt.Circle((obs_pos[0], obs_pos[1]), 0.2) | ||
ax.add_patch(circle) | ||
plt.grid(True) | ||
plt.pause(0.3) | ||
|
||
# TODO: better animation closing | ||
# fig, ax = plt.subplots() | ||
# line, = ax.plot([], []) | ||
# ax.set_xlim(0, 10) | ||
# ax.set_ylim(-1, 1) | ||
|
||
# def init(): | ||
# line.set_data([], []) | ||
# return line, | ||
|
||
# def animate(i): | ||
# x = [0, 10] | ||
# y = [0, i % 2 * 2 - 1] | ||
# line.set_data(x, y) | ||
# return line, | ||
|
||
|
||
# ani = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=20, blit=True) | ||
|
||
# def close_event(evt): | ||
# ani.event_source.stop() | ||
# plt.close(fig) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.