YashsharmaPhD commited on
Commit
b249855
Β·
verified Β·
1 Parent(s): 56b9dad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -6
app.py CHANGED
@@ -4,7 +4,8 @@ import matplotlib.patches as patches
4
  import matplotlib.image as mpimg
5
  import numpy as np
6
  import random
7
- import os
 
8
 
9
  # Global state
10
  pose = {"x": 0, "z": 0, "angle": 0}
@@ -14,6 +15,11 @@ color_index = 0
14
  rgb_colors = ['red', 'green', 'blue']
15
  noise_enabled = True
16
 
 
 
 
 
 
17
  # Generate random obstacles
18
  def generate_obstacles(count=10):
19
  obs = []
@@ -35,7 +41,8 @@ def toggle_noise():
35
 
36
  # Reset simulation
37
  def reset_sim(count):
38
- global pose, trajectory, obstacles, obstacle_hits, color_index
 
39
  pose = {"x": 0, "z": 0, "angle": 0}
40
  trajectory = [(0, 0)]
41
  obstacle_hits = []
@@ -139,13 +146,73 @@ def render_slam_map():
139
  plt.close(fig)
140
  return fig
141
 
142
- # Handle typed input
143
  def handle_text_input(direction):
144
  return move_robot(direction.strip().upper())
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  # Gradio UI
147
  with gr.Blocks() as demo:
148
- gr.Markdown("## πŸ€– SLAM Simulation: Real-Time Navigation & Obstacle Detection")
149
 
150
  obstacle_slider = gr.Slider(1, 20, value=10, step=1, label="Number of Obstacles")
151
 
@@ -167,14 +234,20 @@ with gr.Blocks() as demo:
167
  reset = gr.Button("πŸ”„ Reset")
168
  toggle = gr.Button("πŸ”€ Toggle Noise")
169
 
170
- # Direction buttons (with direct string inputs)
 
 
 
 
171
  w.click(fn=move_robot, inputs=gr.State("W"), outputs=[env_plot, slam_plot, collision_audio, status_text])
172
  a.click(fn=move_robot, inputs=gr.State("A"), outputs=[env_plot, slam_plot, collision_audio, status_text])
173
  s.click(fn=move_robot, inputs=gr.State("S"), outputs=[env_plot, slam_plot, collision_audio, status_text])
174
  d.click(fn=move_robot, inputs=gr.State("D"), outputs=[env_plot, slam_plot, collision_audio, status_text])
175
  reset.click(fn=reset_sim, inputs=[obstacle_slider], outputs=[env_plot, slam_plot, collision_audio, status_text])
176
  toggle.click(fn=lambda: (None, None, None, toggle_noise()), outputs=[env_plot, slam_plot, collision_audio, status_text])
177
-
178
  direction_input.submit(fn=handle_text_input, inputs=direction_input, outputs=[env_plot, slam_plot, collision_audio, status_text])
179
 
 
 
 
180
  demo.launch()
 
4
  import matplotlib.image as mpimg
5
  import numpy as np
6
  import random
7
+ import threading
8
+ import time
9
 
10
  # Global state
11
  pose = {"x": 0, "z": 0, "angle": 0}
 
15
  rgb_colors = ['red', 'green', 'blue']
16
  noise_enabled = True
17
 
18
+ # Mode state
19
+ mode = "Manual"
20
+ auto_running = False
21
+ auto_thread = None
22
+
23
  # Generate random obstacles
24
  def generate_obstacles(count=10):
25
  obs = []
 
41
 
42
  # Reset simulation
43
  def reset_sim(count):
44
+ global pose, trajectory, obstacles, obstacle_hits, color_index, auto_running
45
+ auto_running = False
46
  pose = {"x": 0, "z": 0, "angle": 0}
47
  trajectory = [(0, 0)]
48
  obstacle_hits = []
 
146
  plt.close(fig)
147
  return fig
148
 
149
+ # Text input handler
150
  def handle_text_input(direction):
151
  return move_robot(direction.strip().upper())
152
 
153
+ # Set mode (manual/automatic)
154
+ def set_mode(new_mode):
155
+ global mode
156
+ mode = new_mode
157
+ return f"Mode set to: {mode}"
158
+
159
+ # Auto move thread logic
160
+ def auto_move_loop(update_outputs):
161
+ global auto_running
162
+ directions = ["W", "A", "S", "D"]
163
+ while auto_running:
164
+ valid_dirs = directions.copy()
165
+ random.shuffle(valid_dirs)
166
+ moved = False
167
+
168
+ for dir in valid_dirs:
169
+ step = 1
170
+ if dir == "W":
171
+ new_x, new_z = pose["x"], pose["z"] + step
172
+ elif dir == "S":
173
+ new_x, new_z = pose["x"], pose["z"] - step
174
+ elif dir == "A":
175
+ new_x, new_z = pose["x"] - step, pose["z"]
176
+ elif dir == "D":
177
+ new_x, new_z = pose["x"] + step, pose["z"]
178
+
179
+ if not check_collision(new_x, new_z):
180
+ env, slam, audio, msg = move_robot(dir)
181
+ update_outputs(env, slam, audio, f"[AUTO] {msg}")
182
+ moved = True
183
+ break
184
+
185
+ if not moved:
186
+ update_outputs(render_env(), render_slam_map(), "collision.mp3", "[AUTO] No path available!")
187
+ auto_running = False
188
+ break
189
+
190
+ time.sleep(1)
191
+
192
+ # Start auto mode
193
+ def start_auto(env, slam, audio, status):
194
+ global auto_running, auto_thread
195
+ if mode != "Automatic":
196
+ return env, slam, audio, "Switch to 'Automatic' mode to start."
197
+
198
+ if auto_running:
199
+ return env, slam, audio, "Auto mode already running."
200
+
201
+ auto_running = True
202
+
203
+ def update(env_, slam_, audio_, status_):
204
+ env.update(env_)
205
+ slam.update(slam_)
206
+ audio.update(audio_)
207
+ status.update(status_)
208
+
209
+ auto_thread = threading.Thread(target=auto_move_loop, args=(lambda e, s, a, t: update(e, s, a, t),))
210
+ auto_thread.start()
211
+ return env, slam, audio, "Auto movement started."
212
+
213
  # Gradio UI
214
  with gr.Blocks() as demo:
215
+ gr.Markdown("## πŸ€– SLAM Simulation: Manual & Automatic Robot Navigation")
216
 
217
  obstacle_slider = gr.Slider(1, 20, value=10, step=1, label="Number of Obstacles")
218
 
 
234
  reset = gr.Button("πŸ”„ Reset")
235
  toggle = gr.Button("πŸ”€ Toggle Noise")
236
 
237
+ with gr.Row():
238
+ mode_selector = gr.Dropdown(choices=["Manual", "Automatic"], value="Manual", label="Select Mode")
239
+ start_auto_btn = gr.Button("▢️ Start Auto Mode")
240
+
241
+ # Button wiring
242
  w.click(fn=move_robot, inputs=gr.State("W"), outputs=[env_plot, slam_plot, collision_audio, status_text])
243
  a.click(fn=move_robot, inputs=gr.State("A"), outputs=[env_plot, slam_plot, collision_audio, status_text])
244
  s.click(fn=move_robot, inputs=gr.State("S"), outputs=[env_plot, slam_plot, collision_audio, status_text])
245
  d.click(fn=move_robot, inputs=gr.State("D"), outputs=[env_plot, slam_plot, collision_audio, status_text])
246
  reset.click(fn=reset_sim, inputs=[obstacle_slider], outputs=[env_plot, slam_plot, collision_audio, status_text])
247
  toggle.click(fn=lambda: (None, None, None, toggle_noise()), outputs=[env_plot, slam_plot, collision_audio, status_text])
 
248
  direction_input.submit(fn=handle_text_input, inputs=direction_input, outputs=[env_plot, slam_plot, collision_audio, status_text])
249
 
250
+ mode_selector.change(fn=set_mode, inputs=mode_selector, outputs=status_text)
251
+ start_auto_btn.click(fn=start_auto, inputs=[env_plot, slam_plot, collision_audio, status_text], outputs=[env_plot, slam_plot, collision_audio, status_text])
252
+
253
  demo.launch()