Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import matplotlib.image as mpimg
|
|
| 5 |
import numpy as np
|
| 6 |
import random
|
| 7 |
|
|
|
|
| 8 |
pose = {"x": 0, "z": 0, "angle": 0}
|
| 9 |
trajectory = [(0, 0)]
|
| 10 |
obstacle_hits = []
|
|
@@ -12,6 +13,7 @@ color_index = 0
|
|
| 12 |
rgb_colors = ['red', 'green', 'blue']
|
| 13 |
noise_enabled = True
|
| 14 |
|
|
|
|
| 15 |
def generate_obstacles(count=10):
|
| 16 |
obs = []
|
| 17 |
for _ in range(count):
|
|
@@ -24,11 +26,13 @@ def generate_obstacles(count=10):
|
|
| 24 |
|
| 25 |
obstacles = generate_obstacles(10)
|
| 26 |
|
|
|
|
| 27 |
def toggle_noise(enabled):
|
| 28 |
global noise_enabled
|
| 29 |
noise_enabled = enabled
|
| 30 |
return "Noise: ON" if noise_enabled else "Noise: OFF"
|
| 31 |
|
|
|
|
| 32 |
def reset_sim(count):
|
| 33 |
global pose, trajectory, obstacles, obstacle_hits
|
| 34 |
pose = {"x": 0, "z": 0, "angle": 0}
|
|
@@ -37,6 +41,7 @@ def reset_sim(count):
|
|
| 37 |
obstacles = generate_obstacles(int(count))
|
| 38 |
return render_env(), render_slam_map(), f"Simulation Reset with {count} obstacles"
|
| 39 |
|
|
|
|
| 40 |
def check_collision(x, z):
|
| 41 |
for obs in obstacles:
|
| 42 |
dist = np.sqrt((obs["x"] - x)**2 + (obs["z"] - z)**2)
|
|
@@ -44,6 +49,7 @@ def check_collision(x, z):
|
|
| 44 |
return True
|
| 45 |
return False
|
| 46 |
|
|
|
|
| 47 |
def move_robot(direction):
|
| 48 |
global pose, trajectory
|
| 49 |
step = 1
|
|
@@ -75,6 +81,7 @@ def move_robot(direction):
|
|
| 75 |
|
| 76 |
return render_env(), render_slam_map(), "Moved " + direction
|
| 77 |
|
|
|
|
| 78 |
def render_env():
|
| 79 |
global obstacle_hits
|
| 80 |
fig, ax = plt.subplots()
|
|
@@ -106,6 +113,7 @@ def render_env():
|
|
| 106 |
plt.close(fig)
|
| 107 |
return fig
|
| 108 |
|
|
|
|
| 109 |
def render_slam_map():
|
| 110 |
global color_index
|
| 111 |
fig, ax = plt.subplots()
|
|
@@ -115,6 +123,7 @@ def render_slam_map():
|
|
| 115 |
ax.plot(x_vals, z_vals, 'bo-', markersize=3)
|
| 116 |
ax.grid(True)
|
| 117 |
|
|
|
|
| 118 |
if obstacle_hits:
|
| 119 |
current_color = rgb_colors[color_index % 3]
|
| 120 |
for hit in obstacle_hits[-20:]:
|
|
@@ -124,12 +133,21 @@ def render_slam_map():
|
|
| 124 |
plt.close(fig)
|
| 125 |
return fig
|
| 126 |
|
| 127 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
with gr.Blocks() as demo:
|
| 129 |
gr.Markdown("## π€ SLAM Simulation with Real-Time Obstacle Detection")
|
| 130 |
|
| 131 |
obstacle_slider = gr.Slider(1, 20, value=10, step=1, label="Number of Obstacles")
|
| 132 |
|
|
|
|
| 133 |
status_text = gr.Textbox(label="Status")
|
| 134 |
|
| 135 |
with gr.Row():
|
|
@@ -146,6 +164,7 @@ with gr.Blocks() as demo:
|
|
| 146 |
reset = gr.Button("π Reset")
|
| 147 |
toggle = gr.Button("π Toggle Noise")
|
| 148 |
|
|
|
|
| 149 |
w.click(lambda: move_robot("W"), outputs=[env_plot, slam_plot, status_text])
|
| 150 |
s.click(lambda: move_robot("S"), outputs=[env_plot, slam_plot, status_text])
|
| 151 |
a.click(lambda: move_robot("A"), outputs=[env_plot, slam_plot, status_text])
|
|
@@ -153,4 +172,7 @@ with gr.Blocks() as demo:
|
|
| 153 |
reset.click(fn=reset_sim, inputs=[obstacle_slider], outputs=[env_plot, slam_plot, status_text])
|
| 154 |
toggle.click(lambda: (None, None, toggle_noise(not noise_enabled)), outputs=[env_plot, slam_plot, status_text])
|
| 155 |
|
|
|
|
|
|
|
|
|
|
| 156 |
demo.launch()
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import random
|
| 7 |
|
| 8 |
+
# Global state
|
| 9 |
pose = {"x": 0, "z": 0, "angle": 0}
|
| 10 |
trajectory = [(0, 0)]
|
| 11 |
obstacle_hits = []
|
|
|
|
| 13 |
rgb_colors = ['red', 'green', 'blue']
|
| 14 |
noise_enabled = True
|
| 15 |
|
| 16 |
+
# Generate random obstacles
|
| 17 |
def generate_obstacles(count=10):
|
| 18 |
obs = []
|
| 19 |
for _ in range(count):
|
|
|
|
| 26 |
|
| 27 |
obstacles = generate_obstacles(10)
|
| 28 |
|
| 29 |
+
# Toggle sensor noise
|
| 30 |
def toggle_noise(enabled):
|
| 31 |
global noise_enabled
|
| 32 |
noise_enabled = enabled
|
| 33 |
return "Noise: ON" if noise_enabled else "Noise: OFF"
|
| 34 |
|
| 35 |
+
# Reset everything
|
| 36 |
def reset_sim(count):
|
| 37 |
global pose, trajectory, obstacles, obstacle_hits
|
| 38 |
pose = {"x": 0, "z": 0, "angle": 0}
|
|
|
|
| 41 |
obstacles = generate_obstacles(int(count))
|
| 42 |
return render_env(), render_slam_map(), f"Simulation Reset with {count} obstacles"
|
| 43 |
|
| 44 |
+
# Check for collision with obstacles
|
| 45 |
def check_collision(x, z):
|
| 46 |
for obs in obstacles:
|
| 47 |
dist = np.sqrt((obs["x"] - x)**2 + (obs["z"] - z)**2)
|
|
|
|
| 49 |
return True
|
| 50 |
return False
|
| 51 |
|
| 52 |
+
# Move robot based on direction
|
| 53 |
def move_robot(direction):
|
| 54 |
global pose, trajectory
|
| 55 |
step = 1
|
|
|
|
| 81 |
|
| 82 |
return render_env(), render_slam_map(), "Moved " + direction
|
| 83 |
|
| 84 |
+
# Render the robot environment
|
| 85 |
def render_env():
|
| 86 |
global obstacle_hits
|
| 87 |
fig, ax = plt.subplots()
|
|
|
|
| 113 |
plt.close(fig)
|
| 114 |
return fig
|
| 115 |
|
| 116 |
+
# Render SLAM map with trajectory + obstacle hit indicators
|
| 117 |
def render_slam_map():
|
| 118 |
global color_index
|
| 119 |
fig, ax = plt.subplots()
|
|
|
|
| 123 |
ax.plot(x_vals, z_vals, 'bo-', markersize=3)
|
| 124 |
ax.grid(True)
|
| 125 |
|
| 126 |
+
# Show blinking obstacle hit dots
|
| 127 |
if obstacle_hits:
|
| 128 |
current_color = rgb_colors[color_index % 3]
|
| 129 |
for hit in obstacle_hits[-20:]:
|
|
|
|
| 133 |
plt.close(fig)
|
| 134 |
return fig
|
| 135 |
|
| 136 |
+
# Handle typed input like "W", "A", "S", "D"
|
| 137 |
+
def handle_text_input(direction):
|
| 138 |
+
direction = direction.strip().upper()
|
| 139 |
+
if direction in ["W", "A", "S", "D"]:
|
| 140 |
+
return move_robot(direction)
|
| 141 |
+
else:
|
| 142 |
+
return render_env(), render_slam_map(), "β Invalid input. Use W / A / S / D."
|
| 143 |
+
|
| 144 |
+
# Gradio UI
|
| 145 |
with gr.Blocks() as demo:
|
| 146 |
gr.Markdown("## π€ SLAM Simulation with Real-Time Obstacle Detection")
|
| 147 |
|
| 148 |
obstacle_slider = gr.Slider(1, 20, value=10, step=1, label="Number of Obstacles")
|
| 149 |
|
| 150 |
+
direction_input = gr.Textbox(label="Type W / A / S / D and press Enter to move", placeholder="e.g., W")
|
| 151 |
status_text = gr.Textbox(label="Status")
|
| 152 |
|
| 153 |
with gr.Row():
|
|
|
|
| 164 |
reset = gr.Button("π Reset")
|
| 165 |
toggle = gr.Button("π Toggle Noise")
|
| 166 |
|
| 167 |
+
# Button callbacks
|
| 168 |
w.click(lambda: move_robot("W"), outputs=[env_plot, slam_plot, status_text])
|
| 169 |
s.click(lambda: move_robot("S"), outputs=[env_plot, slam_plot, status_text])
|
| 170 |
a.click(lambda: move_robot("A"), outputs=[env_plot, slam_plot, status_text])
|
|
|
|
| 172 |
reset.click(fn=reset_sim, inputs=[obstacle_slider], outputs=[env_plot, slam_plot, status_text])
|
| 173 |
toggle.click(lambda: (None, None, toggle_noise(not noise_enabled)), outputs=[env_plot, slam_plot, status_text])
|
| 174 |
|
| 175 |
+
# Textbox movement input
|
| 176 |
+
direction_input.submit(fn=handle_text_input, inputs=direction_input, outputs=[env_plot, slam_plot, status_text])
|
| 177 |
+
|
| 178 |
demo.launch()
|