YashsharmaPhD commited on
Commit
1dbee15
Β·
verified Β·
1 Parent(s): 574bea2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -15
app.py CHANGED
@@ -4,7 +4,6 @@ import matplotlib.patches as patches
4
  import matplotlib.image as mpimg
5
  import numpy as np
6
  import random
7
- import os
8
  import threading
9
  import time
10
 
@@ -18,7 +17,6 @@ noise_enabled = True
18
  obstacles = []
19
  auto_mode = False
20
 
21
- # Generate obstacles
22
  def generate_obstacles(count=10):
23
  return [{
24
  "x": random.uniform(-8, 8),
@@ -37,9 +35,9 @@ def reset_sim(count):
37
  global pose, trajectory, obstacles, obstacle_hits, color_index
38
  pose = {"x": 0, "z": 0, "angle": 0}
39
  trajectory = [(0, 0)]
40
- obstacle_hits = []
41
  color_index = 0
42
- obstacles = generate_obstacles(int(count))
43
  return render_env(), render_slam_map(), f"Simulation Reset with {count} obstacles"
44
 
45
  def check_collision(x, z):
@@ -70,11 +68,10 @@ def move_robot(direction):
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
 
78
  if noise_enabled:
79
  noisy_x = pose["x"] + random.uniform(-0.1, 0.1)
80
  noisy_z = pose["z"] + random.uniform(-0.1, 0.1)
@@ -82,7 +79,7 @@ def move_robot(direction):
82
  else:
83
  trajectory.append((pose["x"], pose["z"]))
84
 
85
- return render_env(), render_slam_map(), "Moved " + direction
86
 
87
  def render_env():
88
  global obstacle_hits
@@ -94,7 +91,7 @@ def render_env():
94
  try:
95
  bg = mpimg.imread("map.png")
96
  ax.imshow(bg, extent=(-10, 10, -10, 10), alpha=0.2)
97
- except:
98
  pass
99
 
100
  for obs in obstacles:
@@ -171,7 +168,7 @@ with gr.Blocks() as demo:
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")
174
- status_text = gr.Textbox(label="Status")
175
 
176
  with gr.Row():
177
  with gr.Column():
@@ -188,17 +185,11 @@ with gr.Blocks() as demo:
188
  toggle = gr.Button("πŸ”€ Toggle Noise")
189
  auto = gr.Button("πŸ€– Toggle Auto")
190
 
191
- # Corrected button input passing
192
  w.click(fn=lambda: move_robot("W"), outputs=[env_plot, slam_plot, status_text])
193
  a.click(fn=lambda: move_robot("A"), outputs=[env_plot, slam_plot, status_text])
194
  s.click(fn=lambda: move_robot("S"), outputs=[env_plot, slam_plot, status_text])
195
  d.click(fn=lambda: move_robot("D"), outputs=[env_plot, slam_plot, status_text])
196
 
197
- #w.click(fn=move_robot, inputs=gr.Text(value="W"), outputs=[env_plot, slam_plot, status_text])
198
- #a.click(fn=move_robot, inputs=gr.Text(value="A"), outputs=[env_plot, slam_plot, status_text])
199
- #s.click(fn=move_robot, inputs=gr.Text(value="S"), outputs=[env_plot, slam_plot, status_text])
200
- #d.click(fn=move_robot, inputs=gr.Text(value="D"), outputs=[env_plot, slam_plot, status_text])
201
-
202
  reset.click(fn=reset_sim, inputs=[obstacle_slider], outputs=[env_plot, slam_plot, status_text])
203
  toggle.click(fn=lambda: (None, None, toggle_noise()), outputs=[env_plot, slam_plot, status_text])
204
  auto.click(fn=toggle_auto_mode, inputs=[env_plot, slam_plot, status_text], outputs=auto)
 
4
  import matplotlib.image as mpimg
5
  import numpy as np
6
  import random
 
7
  import threading
8
  import time
9
 
 
17
  obstacles = []
18
  auto_mode = False
19
 
 
20
  def generate_obstacles(count=10):
21
  return [{
22
  "x": random.uniform(-8, 8),
 
35
  global pose, trajectory, obstacles, obstacle_hits, color_index
36
  pose = {"x": 0, "z": 0, "angle": 0}
37
  trajectory = [(0, 0)]
38
+ obstacle_hits.clear()
39
  color_index = 0
40
+ obstacles[:] = generate_obstacles(int(count))
41
  return render_env(), render_slam_map(), f"Simulation Reset with {count} obstacles"
42
 
43
  def check_collision(x, z):
 
68
  return render_env(), render_slam_map(), "❌ Invalid Key"
69
 
70
  if check_collision(new_x, new_z):
 
 
71
  return render_env(), render_slam_map(), "🚫 Collision detected!"
72
 
73
  pose["x"], pose["z"] = new_x, new_z
74
+
75
  if noise_enabled:
76
  noisy_x = pose["x"] + random.uniform(-0.1, 0.1)
77
  noisy_z = pose["z"] + random.uniform(-0.1, 0.1)
 
79
  else:
80
  trajectory.append((pose["x"], pose["z"]))
81
 
82
+ return render_env(), render_slam_map(), f"Moved {direction}"
83
 
84
  def render_env():
85
  global obstacle_hits
 
91
  try:
92
  bg = mpimg.imread("map.png")
93
  ax.imshow(bg, extent=(-10, 10, -10, 10), alpha=0.2)
94
+ except FileNotFoundError:
95
  pass
96
 
97
  for obs in obstacles:
 
168
 
169
  obstacle_slider = gr.Slider(1, 20, value=10, step=1, label="Number of Obstacles")
170
  direction_input = gr.Textbox(label="Type W / A / S / D and press Enter", placeholder="e.g., W")
171
+ status_text = gr.Textbox(label="Status", interactive=False)
172
 
173
  with gr.Row():
174
  with gr.Column():
 
185
  toggle = gr.Button("πŸ”€ Toggle Noise")
186
  auto = gr.Button("πŸ€– Toggle Auto")
187
 
 
188
  w.click(fn=lambda: move_robot("W"), outputs=[env_plot, slam_plot, status_text])
189
  a.click(fn=lambda: move_robot("A"), outputs=[env_plot, slam_plot, status_text])
190
  s.click(fn=lambda: move_robot("S"), outputs=[env_plot, slam_plot, status_text])
191
  d.click(fn=lambda: move_robot("D"), outputs=[env_plot, slam_plot, status_text])
192
 
 
 
 
 
 
193
  reset.click(fn=reset_sim, inputs=[obstacle_slider], outputs=[env_plot, slam_plot, status_text])
194
  toggle.click(fn=lambda: (None, None, toggle_noise()), outputs=[env_plot, slam_plot, status_text])
195
  auto.click(fn=toggle_auto_mode, inputs=[env_plot, slam_plot, status_text], outputs=auto)