Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import gradio as gr
|
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
import numpy as np
|
| 4 |
import random
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
# Robot and environment variables
|
| 8 |
pose = [250, 250, 0]
|
|
@@ -20,12 +19,6 @@ def generate_obstacles(num_obstacles=5):
|
|
| 20 |
h = random.randint(20, 50)
|
| 21 |
obstacles.append((x, y, w, h))
|
| 22 |
|
| 23 |
-
def check_collision(x, y):
|
| 24 |
-
for (ox, oy, ow, oh) in obstacles:
|
| 25 |
-
if ox <= x <= ox + ow and oy <= y <= oy + oh:
|
| 26 |
-
return True
|
| 27 |
-
return False
|
| 28 |
-
|
| 29 |
def move_robot(direction):
|
| 30 |
global pose, trajectory
|
| 31 |
dx, dy = 0, 0
|
|
@@ -38,27 +31,21 @@ def move_robot(direction):
|
|
| 38 |
dx = -step_size
|
| 39 |
elif direction == "D":
|
| 40 |
dx = step_size
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
if check_collision(new_x, new_y):
|
| 46 |
-
return render_env(), render_slam_map(), "collision1.mp3", "π« Collision detected!"
|
| 47 |
-
|
| 48 |
-
pose[0] = new_x
|
| 49 |
-
pose[1] = new_y
|
| 50 |
-
trajectory.append((pose[0], pose[1]))
|
| 51 |
-
|
| 52 |
-
return render_env(), render_slam_map(), None, f"β
Moved {direction}"
|
| 53 |
|
| 54 |
def render_env():
|
| 55 |
fig, ax = plt.subplots(figsize=(5, 5))
|
| 56 |
ax.set_xlim(0, 500)
|
| 57 |
ax.set_ylim(0, 500)
|
| 58 |
ax.set_aspect('equal')
|
|
|
|
| 59 |
ax.plot(pose[0], pose[1], 'bo', label="Robot")
|
| 60 |
if trajectory:
|
| 61 |
ax.plot(*zip(*trajectory), 'b--', label="Trajectory")
|
|
|
|
| 62 |
for (x, y, w, h) in obstacles:
|
| 63 |
ax.add_patch(plt.Rectangle((x, y), w, h, color='red'))
|
| 64 |
ax.legend()
|
|
@@ -76,7 +63,7 @@ def render_slam_map():
|
|
| 76 |
return fig
|
| 77 |
|
| 78 |
def toggle_auto(auto_state):
|
| 79 |
-
return not auto_state, "
|
| 80 |
|
| 81 |
def auto_step(auto_state):
|
| 82 |
if auto_state:
|
|
@@ -86,10 +73,10 @@ def auto_step(auto_state):
|
|
| 86 |
|
| 87 |
# Build Gradio UI
|
| 88 |
with gr.Blocks() as demo:
|
| 89 |
-
gr.Markdown("##
|
| 90 |
env_plot = gr.Plot()
|
| 91 |
slam_plot = gr.Plot()
|
| 92 |
-
collision_audio = gr.Audio(
|
| 93 |
status_text = gr.Textbox(label="Status")
|
| 94 |
|
| 95 |
auto_state = gr.State(False)
|
|
@@ -101,13 +88,18 @@ with gr.Blocks() as demo:
|
|
| 101 |
gr.Button("β‘οΈ").click(fn=move_robot, inputs=gr.Textbox(value="D", visible=False), outputs=[env_plot, slam_plot, collision_audio, status_text])
|
| 102 |
auto_button = gr.Button("Toggle Auto Mode")
|
| 103 |
|
|
|
|
| 104 |
auto_button.click(fn=toggle_auto, inputs=auto_state, outputs=[auto_state, status_text])
|
| 105 |
|
| 106 |
-
|
|
|
|
|
|
|
| 107 |
|
|
|
|
| 108 |
demo.load(fn=lambda: (render_env(), render_slam_map(), None, "β
Ready"),
|
| 109 |
outputs=[env_plot, slam_plot, collision_audio, status_text])
|
| 110 |
|
|
|
|
| 111 |
generate_obstacles()
|
| 112 |
|
| 113 |
demo.launch()
|
|
|
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
import numpy as np
|
| 4 |
import random
|
|
|
|
| 5 |
|
| 6 |
# Robot and environment variables
|
| 7 |
pose = [250, 250, 0]
|
|
|
|
| 19 |
h = random.randint(20, 50)
|
| 20 |
obstacles.append((x, y, w, h))
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def move_robot(direction):
|
| 23 |
global pose, trajectory
|
| 24 |
dx, dy = 0, 0
|
|
|
|
| 31 |
dx = -step_size
|
| 32 |
elif direction == "D":
|
| 33 |
dx = step_size
|
| 34 |
+
pose[0] += dx
|
| 35 |
+
pose[1] += dy
|
| 36 |
+
trajectory.append(tuple(pose[:2]))
|
| 37 |
+
return render_env(), render_slam_map(), None, f"Moved {direction}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def render_env():
|
| 40 |
fig, ax = plt.subplots(figsize=(5, 5))
|
| 41 |
ax.set_xlim(0, 500)
|
| 42 |
ax.set_ylim(0, 500)
|
| 43 |
ax.set_aspect('equal')
|
| 44 |
+
# Draw robot
|
| 45 |
ax.plot(pose[0], pose[1], 'bo', label="Robot")
|
| 46 |
if trajectory:
|
| 47 |
ax.plot(*zip(*trajectory), 'b--', label="Trajectory")
|
| 48 |
+
# Draw obstacles
|
| 49 |
for (x, y, w, h) in obstacles:
|
| 50 |
ax.add_patch(plt.Rectangle((x, y), w, h, color='red'))
|
| 51 |
ax.legend()
|
|
|
|
| 63 |
return fig
|
| 64 |
|
| 65 |
def toggle_auto(auto_state):
|
| 66 |
+
return not auto_state, "Auto mode ON" if not auto_state else "Auto mode OFF"
|
| 67 |
|
| 68 |
def auto_step(auto_state):
|
| 69 |
if auto_state:
|
|
|
|
| 73 |
|
| 74 |
# Build Gradio UI
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
+
gr.Markdown("## SLAM Robot Demo with Auto Mode")
|
| 77 |
env_plot = gr.Plot()
|
| 78 |
slam_plot = gr.Plot()
|
| 79 |
+
collision_audio = gr.Audio(type="filepath", label="Collision")
|
| 80 |
status_text = gr.Textbox(label="Status")
|
| 81 |
|
| 82 |
auto_state = gr.State(False)
|
|
|
|
| 88 |
gr.Button("β‘οΈ").click(fn=move_robot, inputs=gr.Textbox(value="D", visible=False), outputs=[env_plot, slam_plot, collision_audio, status_text])
|
| 89 |
auto_button = gr.Button("Toggle Auto Mode")
|
| 90 |
|
| 91 |
+
# Auto mode toggle updates state
|
| 92 |
auto_button.click(fn=toggle_auto, inputs=auto_state, outputs=[auto_state, status_text])
|
| 93 |
|
| 94 |
+
# Manual auto-step button (simulates periodic move)
|
| 95 |
+
auto_step_btn = gr.Button("π Auto Step")
|
| 96 |
+
auto_step_btn.click(fn=auto_step, inputs=auto_state, outputs=[env_plot, slam_plot, collision_audio, status_text])
|
| 97 |
|
| 98 |
+
# Initial load state
|
| 99 |
demo.load(fn=lambda: (render_env(), render_slam_map(), None, "β
Ready"),
|
| 100 |
outputs=[env_plot, slam_plot, collision_audio, status_text])
|
| 101 |
|
| 102 |
+
# Initialize obstacles
|
| 103 |
generate_obstacles()
|
| 104 |
|
| 105 |
demo.launch()
|