YashsharmaPhD commited on
Commit
f6dacca
·
verified ·
1 Parent(s): f3db073

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -18
app.py CHANGED
@@ -3,24 +3,41 @@ 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
 
@@ -28,24 +45,34 @@ def simulate_robot(joint1, joint2):
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__":
 
3
  import numpy as np
4
  import matplotlib.pyplot as plt
5
  import tempfile
 
6
  import gradio as gr
7
 
8
+ # Helper to get Panda arm and gripper joints
9
+ def get_panda_joints(robot):
10
+ revolute = []
11
+ finger = []
12
+ for i in range(p.getNumJoints(robot)):
13
+ name = p.getJointInfo(robot, i)[1].decode()
14
+ joint_type = p.getJointInfo(robot, i)[2]
15
+ if joint_type == p.JOINT_REVOLUTE:
16
+ if "finger" in name:
17
+ finger.append(i)
18
+ else:
19
+ revolute.append(i)
20
+ return revolute, finger
21
+
22
+ # Simulation and rendering function
23
+ def simulate_robot(j1, j2, j3, j4, gripper):
24
  physicsClient = p.connect(p.DIRECT)
25
  p.setAdditionalSearchPath(pybullet_data.getDataPath())
26
  p.setGravity(0, 0, -9.8)
27
 
 
28
  p.loadURDF("plane.urdf")
29
+ robot = p.loadURDF("franka_panda/panda.urdf", basePosition=[0,0,0])
30
+ arm_joints, finger_joints = get_panda_joints(robot)
31
+
32
+ # Set arm joint positions
33
+ targets = [j1, j2, j3, j4]
34
+ for idx, tgt in zip(arm_joints[:4], targets):
35
+ p.setJointMotorControl2(robot, idx, p.POSITION_CONTROL, targetPosition=tgt)
36
 
37
+ # Gripper control
38
+ for fj in finger_joints:
39
+ p.setJointMotorControl2(robot, fj, p.POSITION_CONTROL, targetPosition=gripper)
40
 
 
41
  for _ in range(100):
42
  p.stepSimulation()
43
 
 
45
  view_matrix = p.computeViewMatrix([1.5, 0, 1], [0, 0, 0.5], [0, 0, 1])
46
  proj_matrix = p.computeProjectionMatrixFOV(60, 1.0, 0.1, 3.1)
47
  _, _, img, _, _ = p.getCameraImage(256, 256, view_matrix, proj_matrix)
48
+ rgb = np.reshape(img, (256, 256, 4))[:, :, :3]
49
+
50
+ # Save image
51
+ tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
52
+ plt.imsave(tmp.name, rgb.astype(np.uint8))
53
 
 
 
 
54
  p.disconnect()
 
55
 
56
+ # Create joint info text
57
+ joint_text = f"Joint Angles:\nJ1 = {j1:.2f} rad\nJ2 = {j2:.2f} rad\nJ3 = {j3:.2f} rad\nJ4 = {j4:.2f} rad\nGripper = {gripper:.3f} m"
58
+ return tmp.name, joint_text
59
+
60
+ # Gradio interface
61
  demo = gr.Interface(
62
  fn=simulate_robot,
63
  inputs=[
64
  gr.Slider(-3.14, 3.14, value=0, label="Joint 1"),
65
  gr.Slider(-3.14, 3.14, value=0, label="Joint 2"),
66
+ gr.Slider(-3.14, 3.14, value=0, label="Joint 3"),
67
+ gr.Slider(-3.14, 3.14, value=0, label="Joint 4"),
68
+ gr.Slider(0.0, 0.04, value=0.02, step=0.001, label="Gripper Opening")
69
+ ],
70
+ outputs=[
71
+ gr.Image(type="filepath", label="Simulation View"),
72
+ gr.Textbox(label="Joint States")
73
  ],
74
+ title="Panda Arm Simulation with Joint Display",
75
+ description="Control 4 Panda arm joints + gripper. The updated simulation view and joint states are shown below."
 
76
  )
77
 
78
  if __name__ == "__main__":