Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,24 +7,35 @@ import random
|
|
| 7 |
|
| 8 |
pose = {"x": 0, "z": 0, "angle": 0}
|
| 9 |
trajectory = [(0, 0)]
|
|
|
|
|
|
|
|
|
|
| 10 |
noise_enabled = True
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def toggle_noise(enabled):
|
| 19 |
global noise_enabled
|
| 20 |
noise_enabled = enabled
|
| 21 |
return "Noise: ON" if noise_enabled else "Noise: OFF"
|
| 22 |
|
| 23 |
-
def reset_sim():
|
| 24 |
-
global pose, trajectory
|
| 25 |
pose = {"x": 0, "z": 0, "angle": 0}
|
| 26 |
trajectory = [(0, 0)]
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
def check_collision(x, z):
|
| 30 |
for obs in obstacles:
|
|
@@ -61,9 +72,11 @@ def move_robot(direction):
|
|
| 61 |
trajectory.append((noisy_x, noisy_z))
|
| 62 |
else:
|
| 63 |
trajectory.append((pose["x"], pose["z"]))
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
def render_env():
|
|
|
|
| 67 |
fig, ax = plt.subplots()
|
| 68 |
ax.set_xlim(-10, 10)
|
| 69 |
ax.set_ylim(-10, 10)
|
|
@@ -88,33 +101,36 @@ def render_env():
|
|
| 88 |
scan_z = pose["z"] + r * np.sin(ang)
|
| 89 |
if check_collision(scan_x, scan_z):
|
| 90 |
ax.plot([pose["x"], scan_x], [pose["z"], scan_z], 'g-', linewidth=0.5)
|
|
|
|
| 91 |
break
|
| 92 |
plt.close(fig)
|
| 93 |
return fig
|
| 94 |
|
| 95 |
def render_slam_map():
|
|
|
|
| 96 |
fig, ax = plt.subplots()
|
| 97 |
ax.set_title("SLAM Trajectory Map")
|
| 98 |
x_vals = [x for x, z in trajectory]
|
| 99 |
z_vals = [z for x, z in trajectory]
|
| 100 |
ax.plot(x_vals, z_vals, 'bo-', markersize=3)
|
| 101 |
ax.grid(True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
plt.close(fig)
|
| 103 |
return fig
|
| 104 |
|
| 105 |
-
|
| 106 |
-
if not text:
|
| 107 |
-
return None, None, "Type W/A/S/D and press Enter"
|
| 108 |
-
key = text.strip().upper()
|
| 109 |
-
if key in ["W", "A", "S", "D"]:
|
| 110 |
-
return move_robot(key)
|
| 111 |
-
else:
|
| 112 |
-
return render_env(), render_slam_map(), "Invalid key. Use W/A/S/D"
|
| 113 |
-
|
| 114 |
with gr.Blocks() as demo:
|
| 115 |
-
gr.Markdown("## π€ SLAM
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
status_text = gr.Textbox(label="Status"
|
| 118 |
|
| 119 |
with gr.Row():
|
| 120 |
with gr.Column():
|
|
@@ -130,17 +146,11 @@ with gr.Blocks() as demo:
|
|
| 130 |
reset = gr.Button("π Reset")
|
| 131 |
toggle = gr.Button("π Toggle Noise")
|
| 132 |
|
| 133 |
-
input_key = gr.Textbox(label="Type W/A/S/D and press Enter to move", max_lines=1)
|
| 134 |
-
|
| 135 |
w.click(lambda: move_robot("W"), outputs=[env_plot, slam_plot, status_text])
|
| 136 |
s.click(lambda: move_robot("S"), outputs=[env_plot, slam_plot, status_text])
|
| 137 |
a.click(lambda: move_robot("A"), outputs=[env_plot, slam_plot, status_text])
|
| 138 |
d.click(lambda: move_robot("D"), outputs=[env_plot, slam_plot, status_text])
|
| 139 |
-
reset.click(reset_sim, outputs=[env_plot, slam_plot, status_text])
|
| 140 |
toggle.click(lambda: (None, None, toggle_noise(not noise_enabled)), outputs=[env_plot, slam_plot, status_text])
|
| 141 |
|
| 142 |
-
input_key.submit(on_keypress, inputs=input_key, outputs=[env_plot, slam_plot, status_text])
|
| 143 |
-
# Clear the textbox after submission so user can type next key
|
| 144 |
-
input_key.submit(lambda _: "", inputs=input_key, outputs=input_key)
|
| 145 |
-
|
| 146 |
demo.launch()
|
|
|
|
| 7 |
|
| 8 |
pose = {"x": 0, "z": 0, "angle": 0}
|
| 9 |
trajectory = [(0, 0)]
|
| 10 |
+
obstacle_hits = []
|
| 11 |
+
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):
|
| 18 |
+
obs.append({
|
| 19 |
+
"x": random.uniform(-8, 8),
|
| 20 |
+
"z": random.uniform(-8, 8),
|
| 21 |
+
"radius": random.uniform(0.5, 1.2)
|
| 22 |
+
})
|
| 23 |
+
return obs
|
| 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}
|
| 35 |
trajectory = [(0, 0)]
|
| 36 |
+
obstacle_hits = []
|
| 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:
|
|
|
|
| 72 |
trajectory.append((noisy_x, noisy_z))
|
| 73 |
else:
|
| 74 |
trajectory.append((pose["x"], pose["z"]))
|
| 75 |
+
|
| 76 |
+
return render_env(), render_slam_map(), "Moved " + direction
|
| 77 |
|
| 78 |
def render_env():
|
| 79 |
+
global obstacle_hits
|
| 80 |
fig, ax = plt.subplots()
|
| 81 |
ax.set_xlim(-10, 10)
|
| 82 |
ax.set_ylim(-10, 10)
|
|
|
|
| 101 |
scan_z = pose["z"] + r * np.sin(ang)
|
| 102 |
if check_collision(scan_x, scan_z):
|
| 103 |
ax.plot([pose["x"], scan_x], [pose["z"], scan_z], 'g-', linewidth=0.5)
|
| 104 |
+
obstacle_hits.append((scan_x, scan_z))
|
| 105 |
break
|
| 106 |
plt.close(fig)
|
| 107 |
return fig
|
| 108 |
|
| 109 |
def render_slam_map():
|
| 110 |
+
global color_index
|
| 111 |
fig, ax = plt.subplots()
|
| 112 |
ax.set_title("SLAM Trajectory Map")
|
| 113 |
x_vals = [x for x, z in trajectory]
|
| 114 |
z_vals = [z for x, z in trajectory]
|
| 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:]:
|
| 121 |
+
ax.plot(hit[0], hit[1], 'o', color=current_color, markersize=6)
|
| 122 |
+
color_index += 1
|
| 123 |
+
|
| 124 |
plt.close(fig)
|
| 125 |
return fig
|
| 126 |
|
| 127 |
+
# Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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():
|
| 136 |
with gr.Column():
|
|
|
|
| 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])
|
| 152 |
d.click(lambda: move_robot("D"), outputs=[env_plot, slam_plot, status_text])
|
| 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()
|