YashsharmaPhD commited on
Commit
0cd2dc0
·
verified ·
1 Parent(s): f887594

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -4
app.py CHANGED
@@ -1,19 +1,25 @@
1
  import gradio as gr
2
  import matplotlib.pyplot as plt
 
 
3
 
4
- pose = {"x": 0, "z": 0}
5
  trajectory = [(0, 0)]
6
 
7
  def move_robot(direction):
8
  step = 1
9
  if direction == "W":
10
  pose["z"] -= step
 
11
  elif direction == "S":
12
  pose["z"] += step
 
13
  elif direction == "A":
14
  pose["x"] -= step
 
15
  elif direction == "D":
16
  pose["x"] += step
 
17
  trajectory.append((pose["x"], pose["z"]))
18
  return render_robot_view(), render_slam_map()
19
 
@@ -22,8 +28,24 @@ def render_robot_view():
22
  ax.set_title("Robot View (Top Down)")
23
  ax.set_xlim(-10, 10)
24
  ax.set_ylim(-10, 10)
25
- ax.plot(pose["x"], pose["z"], "ro", label="Robot")
26
- ax.legend()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  ax.grid(True)
28
  return fig
29
 
@@ -39,7 +61,7 @@ def render_slam_map():
39
  return fig
40
 
41
  with gr.Blocks() as demo:
42
- gr.Markdown("## 🤖 Robot Movement + 🗺️ SLAM Path")
43
 
44
  with gr.Row():
45
  with gr.Column():
 
1
  import gradio as gr
2
  import matplotlib.pyplot as plt
3
+ import matplotlib.patches as patches
4
+ import numpy as np
5
 
6
+ pose = {"x": 0, "z": 0, "angle": 0}
7
  trajectory = [(0, 0)]
8
 
9
  def move_robot(direction):
10
  step = 1
11
  if direction == "W":
12
  pose["z"] -= step
13
+ pose["angle"] = 90
14
  elif direction == "S":
15
  pose["z"] += step
16
+ pose["angle"] = -90
17
  elif direction == "A":
18
  pose["x"] -= step
19
+ pose["angle"] = 180
20
  elif direction == "D":
21
  pose["x"] += step
22
+ pose["angle"] = 0
23
  trajectory.append((pose["x"], pose["z"]))
24
  return render_robot_view(), render_slam_map()
25
 
 
28
  ax.set_title("Robot View (Top Down)")
29
  ax.set_xlim(-10, 10)
30
  ax.set_ylim(-10, 10)
31
+
32
+ # Draw the robot as a triangle
33
+ robot_size = 0.5
34
+ angle_rad = np.deg2rad(pose["angle"])
35
+ x, z = pose["x"], pose["z"]
36
+ triangle = np.array([
37
+ [0, robot_size],
38
+ [-robot_size/2, -robot_size/2],
39
+ [robot_size/2, -robot_size/2]
40
+ ])
41
+ rotation_matrix = np.array([
42
+ [np.cos(angle_rad), -np.sin(angle_rad)],
43
+ [np.sin(angle_rad), np.cos(angle_rad)]
44
+ ])
45
+ rotated_triangle = triangle @ rotation_matrix.T + np.array([x, z])
46
+ robot_patch = patches.Polygon(rotated_triangle, closed=True, color='red')
47
+ ax.add_patch(robot_patch)
48
+
49
  ax.grid(True)
50
  return fig
51
 
 
61
  return fig
62
 
63
  with gr.Blocks() as demo:
64
+ gr.Markdown("## 🤖 Robot Simulation (Triangle) + 🗺️ SLAM Path")
65
 
66
  with gr.Row():
67
  with gr.Column():