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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -24
app.py CHANGED
@@ -5,7 +5,6 @@ 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 = []
@@ -19,47 +18,38 @@ def get_panda_joints(robot):
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
 
44
- # Render image
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"),
@@ -68,11 +58,12 @@ demo = gr.Interface(
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__":
 
5
  import tempfile
6
  import gradio as gr
7
 
 
8
  def get_panda_joints(robot):
9
  revolute = []
10
  finger = []
 
18
  revolute.append(i)
19
  return revolute, finger
20
 
21
+ # We will keep the simulation running globally
22
+ physicsClient = p.connect(p.DIRECT)
23
+ p.setAdditionalSearchPath(pybullet_data.getDataPath())
24
+ p.setGravity(0, 0, -9.8)
25
+ p.loadURDF("plane.urdf")
26
+ robot = p.loadURDF("franka_panda/panda.urdf", basePosition=[0, 0, 0])
27
+ arm_joints, finger_joints = get_panda_joints(robot)
28
 
29
+ def update_robot(j1, j2, j3, j4, gripper):
 
 
 
 
30
  targets = [j1, j2, j3, j4]
31
  for idx, tgt in zip(arm_joints[:4], targets):
32
  p.setJointMotorControl2(robot, idx, p.POSITION_CONTROL, targetPosition=tgt)
33
 
 
34
  for fj in finger_joints:
35
  p.setJointMotorControl2(robot, fj, p.POSITION_CONTROL, targetPosition=gripper)
36
 
37
+ for _ in range(10):
38
  p.stepSimulation()
39
 
 
40
  view_matrix = p.computeViewMatrix([1.5, 0, 1], [0, 0, 0.5], [0, 0, 1])
41
  proj_matrix = p.computeProjectionMatrixFOV(60, 1.0, 0.1, 3.1)
42
  _, _, img, _, _ = p.getCameraImage(256, 256, view_matrix, proj_matrix)
43
  rgb = np.reshape(img, (256, 256, 4))[:, :, :3]
44
 
 
45
  tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
46
  plt.imsave(tmp.name, rgb.astype(np.uint8))
47
 
 
 
 
48
  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"
49
  return tmp.name, joint_text
50
 
 
51
  demo = gr.Interface(
52
+ fn=update_robot,
53
  inputs=[
54
  gr.Slider(-3.14, 3.14, value=0, label="Joint 1"),
55
  gr.Slider(-3.14, 3.14, value=0, label="Joint 2"),
 
58
  gr.Slider(0.0, 0.04, value=0.02, step=0.001, label="Gripper Opening")
59
  ],
60
  outputs=[
61
+ gr.Image(type="filepath", label="Live Simulation View"),
62
+ gr.Textbox(label="Live Joint States")
63
  ],
64
+ live=True, # 💡 This enables real-time update!
65
+ title="Live Robot Control",
66
+ description="Move sliders to control robot joints and see live updates."
67
  )
68
 
69
  if __name__ == "__main__":