YashsharmaPhD commited on
Commit
03620db
Β·
verified Β·
1 Parent(s): 41e447a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -7,7 +7,6 @@ import random
7
  import os
8
  import threading
9
  import time
10
- from IPython.display import Audio, display
11
 
12
  # Global state
13
  pose = {"x": 0, "z": 0, "angle": 0}
@@ -71,9 +70,8 @@ def move_robot(direction):
71
  return render_env(), render_slam_map(), "❌ Invalid Key"
72
 
73
  if check_collision(new_x, new_z):
74
- audio_path = "collision.mp3"
75
- if os.path.exists(audio_path):
76
- display(Audio(audio_path, autoplay=True)) # 🎡 Direct audio playback
77
  return render_env(), render_slam_map(), "🚫 Collision detected!"
78
 
79
  pose["x"], pose["z"] = new_x, new_z
@@ -88,7 +86,7 @@ def move_robot(direction):
88
 
89
  def render_env():
90
  global obstacle_hits
91
- fig, ax = plt.subplots()
92
  ax.set_xlim(-10, 10)
93
  ax.set_ylim(-10, 10)
94
  ax.set_title("SLAM Environment View")
@@ -105,6 +103,9 @@ def render_env():
105
 
106
  ax.plot(pose["x"], pose["z"], 'ro', markersize=8)
107
 
 
 
 
108
  angles = np.linspace(0, 2*np.pi, 24)
109
  for ang in angles:
110
  for r in np.linspace(0, 3, 30):
@@ -120,7 +121,7 @@ def render_env():
120
 
121
  def render_slam_map():
122
  global color_index
123
- fig, ax = plt.subplots()
124
  ax.set_title("SLAM Trajectory Map")
125
  x_vals = [x for x, z in trajectory]
126
  z_vals = [z for x, z in trajectory]
@@ -128,7 +129,7 @@ def render_slam_map():
128
  ax.grid(True)
129
 
130
  if obstacle_hits:
131
- current_color = rgb_colors[color_index % 3]
132
  for hit in obstacle_hits[-20:]:
133
  ax.plot(hit[0], hit[1], 'o', color=current_color, markersize=6)
134
  color_index += 1
@@ -158,8 +159,7 @@ def toggle_auto_mode(env_plot, slam_plot, status_text):
158
  slam_plot.update(value=s)
159
  status_text.update(value=t)
160
 
161
- thread = threading.Thread(target=auto_movement, args=(update_ui,))
162
- thread.daemon = True
163
  thread.start()
164
  return "🟒 Auto Mode: ON"
165
  else:
@@ -167,7 +167,7 @@ def toggle_auto_mode(env_plot, slam_plot, status_text):
167
 
168
  # Gradio UI
169
  with gr.Blocks() as demo:
170
- gr.Markdown("## πŸ€– SLAM Simulation with Auto Mode + Collision Sound (No Play Button)")
171
 
172
  obstacle_slider = gr.Slider(1, 20, value=10, step=1, label="Number of Obstacles")
173
  direction_input = gr.Textbox(label="Type W / A / S / D and press Enter", placeholder="e.g., W")
@@ -188,10 +188,12 @@ with gr.Blocks() as demo:
188
  toggle = gr.Button("πŸ”€ Toggle Noise")
189
  auto = gr.Button("πŸ€– Toggle Auto")
190
 
191
- w.click(fn=move_robot, inputs=gr.State("W"), outputs=[env_plot, slam_plot, status_text])
192
- a.click(fn=move_robot, inputs=gr.State("A"), outputs=[env_plot, slam_plot, status_text])
193
- s.click(fn=move_robot, inputs=gr.State("S"), outputs=[env_plot, slam_plot, status_text])
194
- d.click(fn=move_robot, inputs=gr.State("D"), outputs=[env_plot, slam_plot, status_text])
 
 
195
  reset.click(fn=reset_sim, inputs=[obstacle_slider], outputs=[env_plot, slam_plot, status_text])
196
  toggle.click(fn=lambda: (None, None, toggle_noise()), outputs=[env_plot, slam_plot, status_text])
197
  auto.click(fn=toggle_auto_mode, inputs=[env_plot, slam_plot, status_text], outputs=auto)
 
7
  import os
8
  import threading
9
  import time
 
10
 
11
  # Global state
12
  pose = {"x": 0, "z": 0, "angle": 0}
 
70
  return render_env(), render_slam_map(), "❌ Invalid Key"
71
 
72
  if check_collision(new_x, new_z):
73
+ # Gradio does not support IPython Audio playback in the browser.
74
+ # Instead, we can display a status message or play a sound via HTML audio.
 
75
  return render_env(), render_slam_map(), "🚫 Collision detected!"
76
 
77
  pose["x"], pose["z"] = new_x, new_z
 
86
 
87
  def render_env():
88
  global obstacle_hits
89
+ fig, ax = plt.subplots(figsize=(5,5))
90
  ax.set_xlim(-10, 10)
91
  ax.set_ylim(-10, 10)
92
  ax.set_title("SLAM Environment View")
 
103
 
104
  ax.plot(pose["x"], pose["z"], 'ro', markersize=8)
105
 
106
+ # Clear previous hits to avoid infinite growth
107
+ obstacle_hits.clear()
108
+
109
  angles = np.linspace(0, 2*np.pi, 24)
110
  for ang in angles:
111
  for r in np.linspace(0, 3, 30):
 
121
 
122
  def render_slam_map():
123
  global color_index
124
+ fig, ax = plt.subplots(figsize=(5,5))
125
  ax.set_title("SLAM Trajectory Map")
126
  x_vals = [x for x, z in trajectory]
127
  z_vals = [z for x, z in trajectory]
 
129
  ax.grid(True)
130
 
131
  if obstacle_hits:
132
+ current_color = rgb_colors[color_index % len(rgb_colors)]
133
  for hit in obstacle_hits[-20:]:
134
  ax.plot(hit[0], hit[1], 'o', color=current_color, markersize=6)
135
  color_index += 1
 
159
  slam_plot.update(value=s)
160
  status_text.update(value=t)
161
 
162
+ thread = threading.Thread(target=auto_movement, args=(update_ui,), daemon=True)
 
163
  thread.start()
164
  return "🟒 Auto Mode: ON"
165
  else:
 
167
 
168
  # Gradio UI
169
  with gr.Blocks() as demo:
170
+ gr.Markdown("## πŸ€– SLAM Simulation with Auto Mode + Collision Status")
171
 
172
  obstacle_slider = gr.Slider(1, 20, value=10, step=1, label="Number of Obstacles")
173
  direction_input = gr.Textbox(label="Type W / A / S / D and press Enter", placeholder="e.g., W")
 
188
  toggle = gr.Button("πŸ”€ Toggle Noise")
189
  auto = gr.Button("πŸ€– Toggle Auto")
190
 
191
+ # Corrected button input passing
192
+ w.click(fn=move_robot, inputs=gr.Text(value="W"), outputs=[env_plot, slam_plot, status_text])
193
+ a.click(fn=move_robot, inputs=gr.Text(value="A"), outputs=[env_plot, slam_plot, status_text])
194
+ s.click(fn=move_robot, inputs=gr.Text(value="S"), outputs=[env_plot, slam_plot, status_text])
195
+ d.click(fn=move_robot, inputs=gr.Text(value="D"), outputs=[env_plot, slam_plot, status_text])
196
+
197
  reset.click(fn=reset_sim, inputs=[obstacle_slider], outputs=[env_plot, slam_plot, status_text])
198
  toggle.click(fn=lambda: (None, None, toggle_noise()), outputs=[env_plot, slam_plot, status_text])
199
  auto.click(fn=toggle_auto_mode, inputs=[env_plot, slam_plot, status_text], outputs=auto)