Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pybullet as p
|
| 2 |
+
import pybullet_data
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
def simulate_robot(joint1, joint2):
|
| 10 |
+
# Start PyBullet (headless)
|
| 11 |
+
physicsClient = p.connect(p.DIRECT)
|
| 12 |
+
p.setAdditionalSearchPath(pybullet_data.getDataPath())
|
| 13 |
+
p.setGravity(0, 0, -9.8)
|
| 14 |
+
|
| 15 |
+
# Load ground and robot
|
| 16 |
+
p.loadURDF("plane.urdf")
|
| 17 |
+
robot = p.loadURDF("franka_panda/panda.urdf", basePosition=[0, 0, 0])
|
| 18 |
+
|
| 19 |
+
# Apply joint controls (just controlling 2 for demo)
|
| 20 |
+
p.setJointMotorControl2(robot, 1, p.POSITION_CONTROL, targetPosition=joint1)
|
| 21 |
+
p.setJointMotorControl2(robot, 3, p.POSITION_CONTROL, targetPosition=joint2)
|
| 22 |
+
|
| 23 |
+
# Step simulation
|
| 24 |
+
for _ in range(100):
|
| 25 |
+
p.stepSimulation()
|
| 26 |
+
|
| 27 |
+
# Render image
|
| 28 |
+
view_matrix = p.computeViewMatrix([1.5, 0, 1], [0, 0, 0.5], [0, 0, 1])
|
| 29 |
+
proj_matrix = p.computeProjectionMatrixFOV(60, 1.0, 0.1, 3.1)
|
| 30 |
+
_, _, img, _, _ = p.getCameraImage(256, 256, view_matrix, proj_matrix)
|
| 31 |
+
rgb_array = np.reshape(img, (256, 256, 4))[:, :, :3]
|
| 32 |
+
|
| 33 |
+
# Save as temp image
|
| 34 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 35 |
+
plt.imsave(temp_file.name, rgb_array.astype(np.uint8))
|
| 36 |
+
p.disconnect()
|
| 37 |
+
return temp_file.name
|
| 38 |
+
|
| 39 |
+
# Gradio Interface
|
| 40 |
+
demo = gr.Interface(
|
| 41 |
+
fn=simulate_robot,
|
| 42 |
+
inputs=[
|
| 43 |
+
gr.Slider(-3.14, 3.14, value=0, label="Joint 1"),
|
| 44 |
+
gr.Slider(-3.14, 3.14, value=0, label="Joint 2"),
|
| 45 |
+
],
|
| 46 |
+
outputs=gr.Image(type="filepath", label="Simulated Robot View"),
|
| 47 |
+
title="PyBullet Panda Simulation",
|
| 48 |
+
description="Control joint angles and simulate the Panda robot using PyBullet headless mode.",
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
demo.launch()
|