YashsharmaPhD commited on
Commit
cec7d8f
·
verified ·
1 Parent(s): d7c02d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -47
app.py CHANGED
@@ -6,16 +6,16 @@ import tempfile
6
  import gradio as gr
7
  import time
8
 
9
- # Setup PyBullet
10
- p.connect(p.DIRECT)
11
  p.setAdditionalSearchPath(pybullet_data.getDataPath())
12
  p.setGravity(0, 0, -9.8)
13
  p.loadURDF("plane.urdf")
14
  robot = p.loadURDF("franka_panda/panda.urdf", basePosition=[0, 0, 0], useFixedBase=True)
15
 
16
- # Add a cube to pick (black color)
17
  cube_id = p.loadURDF("cube_small.urdf", basePosition=[0.6, 0, 0.02])
18
- p.changeVisualShape(cube_id, -1, rgbaColor=[0, 0, 0, 1]) # black
19
 
20
  # Get joint indices
21
  def get_panda_joints(robot):
@@ -32,7 +32,7 @@ def get_panda_joints(robot):
32
 
33
  arm_joints, finger_joints = get_panda_joints(robot)
34
 
35
- # Add debug labels
36
  debug_labels = []
37
  def add_joint_labels():
38
  global debug_labels
@@ -46,7 +46,7 @@ def add_joint_labels():
46
  text_id = p.addUserDebugText(lbl, pos, textColorRGB=[1, 0, 0], textSize=1.2)
47
  debug_labels.append(text_id)
48
 
49
- # Render simulation image
50
  def render_sim(joint_values, gripper_val):
51
  for idx, tgt in zip(arm_joints, joint_values):
52
  p.setJointMotorControl2(robot, idx, p.POSITION_CONTROL, targetPosition=tgt)
@@ -89,61 +89,47 @@ def move_to_input_angles(joint_str):
89
  except Exception as e:
90
  return None, f"Error: {str(e)}"
91
 
92
- # Auto Pick and Place using IK
93
- def pick_and_place_auto(px, py, pz):
94
  try:
95
- obj_pos, _ = p.getBasePositionAndOrientation(cube_id)
96
- above_obj = list(obj_pos)
97
- above_obj[2] += 0.1
98
 
99
- # Move above object
100
- ik_approach = p.calculateInverseKinematics(robot, 11, above_obj)
101
- move_to_input_angles(",".join([f"{a:.4f}" for a in ik_approach[:7]]))
102
 
103
- # Move to grasp position
104
- ik_grasp = p.calculateInverseKinematics(robot, 11, obj_pos)
105
- move_to_input_angles(",".join([f"{a:.4f}" for a in ik_grasp[:7]]))
106
 
107
- # Close gripper
108
  for _ in range(30):
109
  for fj in finger_joints:
110
  p.setJointMotorControl2(robot, fj, p.POSITION_CONTROL, targetPosition=0.0)
111
  p.stepSimulation()
112
  time.sleep(0.01)
113
 
114
- # Lift
115
- lift_pos = list(obj_pos)
116
- lift_pos[2] += 0.15
117
- ik_lift = p.calculateInverseKinematics(robot, 11, lift_pos)
118
- move_to_input_angles(",".join([f"{a:.4f}" for a in ik_lift[:7]]))
 
 
119
 
120
- # Move to place
121
- place_pos = [px, py, pz]
122
- ik_place = p.calculateInverseKinematics(robot, 11, place_pos)
123
- move_to_input_angles(",".join([f"{a:.4f}" for a in ik_place[:7]]))
124
 
125
- # Open gripper
126
  for _ in range(30):
127
  for fj in finger_joints:
128
  p.setJointMotorControl2(robot, fj, p.POSITION_CONTROL, targetPosition=0.04)
129
  p.stepSimulation()
130
  time.sleep(0.01)
131
 
132
- return render_sim(ik_place[:7], 0.04)
133
  except Exception as e:
134
  return None, f"Error: {str(e)}"
135
 
136
- # Gripper selection stub
137
- def switch_gripper(gripper_type):
138
- return f"Switched to: {gripper_type}"
139
-
140
- # Gradio Interface
141
- with gr.Blocks(title="Franka Arm Auto Pick and Place") as demo:
142
- gr.Markdown("## 🤖 Franka 7-DOF Control")
143
-
144
- gripper_selector = gr.Dropdown(["Two-Finger", "Suction"], value="Two-Finger", label="Select Gripper")
145
- gripper_feedback = gr.Textbox(label="Gripper Status", interactive=False)
146
- gripper_selector.change(fn=switch_gripper, inputs=gripper_selector, outputs=gripper_feedback)
147
 
148
  joint_sliders = []
149
  with gr.Row():
@@ -171,14 +157,24 @@ with gr.Blocks(title="Franka Arm Auto Pick and Place") as demo:
171
  inputs=[], outputs=[img_output, text_output]
172
  )
173
 
174
- gr.Markdown("### 🎯 Auto Pick and Place (from object to custom location)")
175
- px = gr.Slider(0.3, 0.8, value=0.4, label="Place X")
176
- py = gr.Slider(-0.3, 0.3, value=0.0, label="Place Y")
177
- pz = gr.Slider(0.02, 0.4, value=0.05, label="Place Z")
 
 
 
 
 
 
178
 
179
- gr.Button("🤖 Auto Pick and Place").click(
180
- fn=pick_and_place_auto,
181
- inputs=[px, py, pz],
 
 
 
 
182
  outputs=[img_output, text_output]
183
  )
184
 
 
6
  import gradio as gr
7
  import time
8
 
9
+ # Connect in GUI mode for 3D interactive view
10
+ p.connect(p.GUI)
11
  p.setAdditionalSearchPath(pybullet_data.getDataPath())
12
  p.setGravity(0, 0, -9.8)
13
  p.loadURDF("plane.urdf")
14
  robot = p.loadURDF("franka_panda/panda.urdf", basePosition=[0, 0, 0], useFixedBase=True)
15
 
16
+ # Add cube to pick
17
  cube_id = p.loadURDF("cube_small.urdf", basePosition=[0.6, 0, 0.02])
18
+ p.changeVisualShape(cube_id, -1, rgbaColor=[0, 0, 0, 1]) # black cube
19
 
20
  # Get joint indices
21
  def get_panda_joints(robot):
 
32
 
33
  arm_joints, finger_joints = get_panda_joints(robot)
34
 
35
+ # Add joint labels in sim
36
  debug_labels = []
37
  def add_joint_labels():
38
  global debug_labels
 
46
  text_id = p.addUserDebugText(lbl, pos, textColorRGB=[1, 0, 0], textSize=1.2)
47
  debug_labels.append(text_id)
48
 
49
+ # Render PyBullet sim as image
50
  def render_sim(joint_values, gripper_val):
51
  for idx, tgt in zip(arm_joints, joint_values):
52
  p.setJointMotorControl2(robot, idx, p.POSITION_CONTROL, targetPosition=tgt)
 
89
  except Exception as e:
90
  return None, f"Error: {str(e)}"
91
 
92
+ # Pick and place
93
+ def pick_and_place(position_str, approach_str, place_str):
94
  try:
95
+ position_angles = [float(x.strip()) for x in position_str.split(",")]
96
+ approach_angles = [float(x.strip()) for x in approach_str.split(",")]
97
+ place_angles = [float(x.strip()) for x in place_str.split(",")]
98
 
99
+ if len(position_angles) != 7 or len(approach_angles) != 7 or len(place_angles) != 7:
100
+ return None, "❌ All inputs must have 7 joint angles."
 
101
 
102
+ move_to_input_angles(approach_str)
 
 
103
 
 
104
  for _ in range(30):
105
  for fj in finger_joints:
106
  p.setJointMotorControl2(robot, fj, p.POSITION_CONTROL, targetPosition=0.0)
107
  p.stepSimulation()
108
  time.sleep(0.01)
109
 
110
+ lifted = [a + 0.1 for a in approach_angles]
111
+ for i in range(100):
112
+ blended = [(1 - i/100) * approach_angles[j] + (i/100) * lifted[j] for j in range(7)]
113
+ for idx, val in zip(arm_joints, blended):
114
+ p.setJointMotorControl2(robot, idx, p.POSITION_CONTROL, targetPosition=val)
115
+ p.stepSimulation()
116
+ time.sleep(0.01)
117
 
118
+ move_to_input_angles(place_str)
 
 
 
119
 
 
120
  for _ in range(30):
121
  for fj in finger_joints:
122
  p.setJointMotorControl2(robot, fj, p.POSITION_CONTROL, targetPosition=0.04)
123
  p.stepSimulation()
124
  time.sleep(0.01)
125
 
126
+ return render_sim(place_angles, 0.04)
127
  except Exception as e:
128
  return None, f"Error: {str(e)}"
129
 
130
+ # Gradio UI
131
+ with gr.Blocks(title="Franka Arm Pick & Place") as demo:
132
+ gr.Markdown("## 🤖 Franka Panda Robot Control")
 
 
 
 
 
 
 
 
133
 
134
  joint_sliders = []
135
  with gr.Row():
 
157
  inputs=[], outputs=[img_output, text_output]
158
  )
159
 
160
+ gr.Markdown("### ✍️ Move Robot to Custom Joint Angles")
161
+ joint_input_box = gr.Textbox(
162
+ label="Enter 7 Joint Angles (comma-separated)",
163
+ placeholder="e.g. 0.0, -0.5, 0.3, -1.2, 0.0, 1.5, 0.8"
164
+ )
165
+ gr.Button("▶️ Move to Angles").click(
166
+ fn=move_to_input_angles,
167
+ inputs=joint_input_box,
168
+ outputs=[img_output, text_output]
169
+ )
170
 
171
+ gr.Markdown("### 🧾 Pick and Place Input (3 sets of joint angles)")
172
+ position_input = gr.Textbox(label="Object Position Angles", placeholder="7 angles...")
173
+ approach_input = gr.Textbox(label="Approach Angles", placeholder="7 angles...")
174
+ place_input = gr.Textbox(label="Place Angles", placeholder="7 angles...")
175
+ gr.Button("🤖 Perform Pick and Place").click(
176
+ fn=pick_and_place,
177
+ inputs=[position_input, approach_input, place_input],
178
  outputs=[img_output, text_output]
179
  )
180