YashsharmaPhD commited on
Commit
20e8f5b
·
verified ·
1 Parent(s): 209c586

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -14
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
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,6 +20,12 @@ def generate_obstacles(num_obstacles=5):
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,20 +38,27 @@ def move_robot(direction):
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
- ax.plot(*zip(*trajectory), 'b--', label="Trajectory")
47
- # Draw obstacles
48
  for (x, y, w, h) in obstacles:
49
  ax.add_patch(plt.Rectangle((x, y), w, h, color='red'))
50
  ax.legend()
@@ -62,7 +76,7 @@ def render_slam_map():
62
  return fig
63
 
64
  def toggle_auto(auto_state):
65
- return not auto_state, "Auto mode ON" if not auto_state else "Auto mode OFF"
66
 
67
  def auto_step(auto_state):
68
  if auto_state:
@@ -72,10 +86,10 @@ def auto_step(auto_state):
72
 
73
  # Build Gradio UI
74
  with gr.Blocks() as demo:
75
- gr.Markdown("## SLAM Robot Demo with Auto Mode")
76
  env_plot = gr.Plot()
77
  slam_plot = gr.Plot()
78
- collision_audio = gr.Audio(type="auto", label="Collision")
79
  status_text = gr.Textbox(label="Status")
80
 
81
  auto_state = gr.State(False)
@@ -87,16 +101,13 @@ with gr.Blocks() as demo:
87
  gr.Button("➡️").click(fn=move_robot, inputs=gr.Textbox(value="D", visible=False), outputs=[env_plot, slam_plot, collision_audio, status_text])
88
  auto_button = gr.Button("Toggle Auto Mode")
89
 
90
- # Auto mode toggle updates state
91
  auto_button.click(fn=toggle_auto, inputs=auto_state, outputs=[auto_state, status_text])
92
 
93
- # Periodic polling that runs every second to check auto state
94
  demo.load(fn=auto_step, inputs=auto_state, outputs=[env_plot, slam_plot, collision_audio, status_text], every=1)
95
 
96
- demo.load(fn=lambda: (render_env(), render_slam_map(), None, "Ready"),
97
  outputs=[env_plot, slam_plot, collision_audio, status_text])
98
 
99
- # Reset obstacles and robot pose
100
  generate_obstacles()
101
 
102
  demo.launch()
 
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
  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
  dx = -step_size
39
  elif direction == "D":
40
  dx = step_size
41
+
42
+ new_x = pose[0] + dx
43
+ new_y = pose[1] + dy
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
  return fig
77
 
78
  def toggle_auto(auto_state):
79
+ return not auto_state, "Auto mode ON" if not auto_state else "Auto mode OFF"
80
 
81
  def auto_step(auto_state):
82
  if auto_state:
 
86
 
87
  # Build Gradio UI
88
  with gr.Blocks() as demo:
89
+ gr.Markdown("## 🤖 SLAM Robot Demo with Auto Mode")
90
  env_plot = gr.Plot()
91
  slam_plot = gr.Plot()
92
+ collision_audio = gr.Audio(value=None, type="filepath", label="Collision", interactive=False, visible=False)
93
  status_text = gr.Textbox(label="Status")
94
 
95
  auto_state = gr.State(False)
 
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
  demo.load(fn=auto_step, inputs=auto_state, outputs=[env_plot, slam_plot, collision_audio, status_text], every=1)
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()