Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pythreejs import *
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
|
| 5 |
+
pose = {"x": 0, "y": 0, "z": 0}
|
| 6 |
+
trajectory = [(0, 0)] # Keep track of SLAM path
|
| 7 |
+
|
| 8 |
+
def move_robot(direction):
|
| 9 |
+
step = 1
|
| 10 |
+
if direction == "W":
|
| 11 |
+
pose["z"] -= step
|
| 12 |
+
elif direction == "S":
|
| 13 |
+
pose["z"] += step
|
| 14 |
+
elif direction == "A":
|
| 15 |
+
pose["x"] -= step
|
| 16 |
+
elif direction == "D":
|
| 17 |
+
pose["x"] += step
|
| 18 |
+
trajectory.append((pose["x"], pose["z"]))
|
| 19 |
+
return render_scene(), render_map()
|
| 20 |
+
|
| 21 |
+
def render_scene():
|
| 22 |
+
robot = Mesh(
|
| 23 |
+
geometry=BoxGeometry(1, 1, 1),
|
| 24 |
+
material=MeshStandardMaterial(color='red'),
|
| 25 |
+
position=[pose["x"], pose["y"], pose["z"]]
|
| 26 |
+
)
|
| 27 |
+
ambient_light = AmbientLight(color='#ffffff', intensity=0.5)
|
| 28 |
+
directional_light = DirectionalLight(color='white', position=[5, 5, 5], intensity=0.6)
|
| 29 |
+
scene = Scene(children=[robot, ambient_light, directional_light])
|
| 30 |
+
camera = PerspectiveCamera(position=[5, 5, 5])
|
| 31 |
+
camera.lookAt([0, 0, 0])
|
| 32 |
+
renderer = Renderer(scene=scene, camera=camera,
|
| 33 |
+
controls=[OrbitControls(controlling=camera)],
|
| 34 |
+
width=400, height=300)
|
| 35 |
+
return renderer
|
| 36 |
+
|
| 37 |
+
def render_map():
|
| 38 |
+
x_vals = [x for x, z in trajectory]
|
| 39 |
+
z_vals = [z for x, z in trajectory]
|
| 40 |
+
fig, ax = plt.subplots()
|
| 41 |
+
ax.plot(x_vals, z_vals, marker='o', color='blue')
|
| 42 |
+
ax.set_title("SLAM Path (Top-Down)")
|
| 43 |
+
ax.set_xlabel("X Position")
|
| 44 |
+
ax.set_ylabel("Z Position")
|
| 45 |
+
ax.grid(True)
|
| 46 |
+
return fig
|
| 47 |
+
|
| 48 |
+
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown("## 🤖 3D Robot + 🗺️ SLAM Path Visualization")
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
with gr.Column():
|
| 53 |
+
scene_display = gr.Component()
|
| 54 |
+
with gr.Column():
|
| 55 |
+
map_display = gr.Plot()
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
w_btn = gr.Button("⬆️ W")
|
| 59 |
+
s_btn = gr.Button("⬇️ S")
|
| 60 |
+
a_btn = gr.Button("⬅️ A")
|
| 61 |
+
d_btn = gr.Button("➡️ D")
|
| 62 |
+
|
| 63 |
+
w_btn.click(lambda: move_robot("W"), outputs=[scene_display, map_display])
|
| 64 |
+
s_btn.click(lambda: move_robot("S"), outputs=[scene_display, map_display])
|
| 65 |
+
a_btn.click(lambda: move_robot("A"), outputs=[scene_display, map_display])
|
| 66 |
+
d_btn.click(lambda: move_robot("D"), outputs=[scene_display, map_display])
|
| 67 |
+
|
| 68 |
+
scene_display.render(render_scene())
|
| 69 |
+
map_display.render(render_map())
|
| 70 |
+
|
| 71 |
+
demo.launch()
|