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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -35
app.py CHANGED
@@ -1,5 +1,3 @@
1
- # Full adapted pick-and-place demo with consistent style
2
-
3
  import pybullet as p
4
  import pybullet_data
5
  import numpy as np
@@ -91,46 +89,47 @@ def move_to_input_angles(joint_str):
91
  except Exception as e:
92
  return None, f"Error: {str(e)}"
93
 
94
- # Pick-and-place sequence
95
- def pick_and_place(position_str, approach_str, place_str):
96
  try:
97
- position_angles = [float(x.strip()) for x in position_str.split(",")]
98
- approach_angles = [float(x.strip()) for x in approach_str.split(",")]
99
- place_angles = [float(x.strip()) for x in place_str.split(",")]
100
 
101
- if len(position_angles) != 7 or len(approach_angles) != 7 or len(place_angles) != 7:
102
- return None, "❌ All inputs must have 7 joint angles."
 
103
 
104
- # Move to approach
105
- move_to_input_angles(approach_str)
 
106
 
107
- # Grasp
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 slightly
115
- lifted = [a + 0.1 for a in approach_angles]
116
- for i in range(100):
117
- blended = [(1 - i/100) * approach_angles[j] + (i/100) * lifted[j] for j in range(7)]
118
- for idx, val in zip(arm_joints, blended):
119
- p.setJointMotorControl2(robot, idx, p.POSITION_CONTROL, targetPosition=val)
120
- p.stepSimulation()
121
- time.sleep(0.01)
122
 
123
  # Move to place
124
- move_to_input_angles(place_str)
 
 
125
 
126
- # Release
127
  for _ in range(30):
128
  for fj in finger_joints:
129
  p.setJointMotorControl2(robot, fj, p.POSITION_CONTROL, targetPosition=0.04)
130
  p.stepSimulation()
131
  time.sleep(0.01)
132
 
133
- return render_sim(place_angles, 0.04)
134
  except Exception as e:
135
  return None, f"Error: {str(e)}"
136
 
@@ -139,7 +138,7 @@ def switch_gripper(gripper_type):
139
  return f"Switched to: {gripper_type}"
140
 
141
  # Gradio Interface
142
- with gr.Blocks(title="Franka Arm Control with Pick and Place") as demo:
143
  gr.Markdown("## 🤖 Franka 7-DOF Control")
144
 
145
  gripper_selector = gr.Dropdown(["Two-Finger", "Suction"], value="Two-Finger", label="Select Gripper")
@@ -172,17 +171,14 @@ with gr.Blocks(title="Franka Arm Control with Pick and Place") as demo:
172
  inputs=[], outputs=[img_output, text_output]
173
  )
174
 
175
- gr.Markdown("### 🧾 Enter Joint Angles (comma-separated)")
176
- joint_input = gr.Textbox(label="Joint Angles (7 values)", placeholder="e.g. 0.0, -0.5, 0.3, -1.2, 0.0, 1.5, 0.8")
177
- gr.Button("▶️ Move to Angles").click(fn=move_to_input_angles, inputs=joint_input, outputs=[img_output, text_output])
178
-
179
- gr.Markdown("### 🧾 Pick and Place Input (3 sets of joint angles)")
180
- position_input = gr.Textbox(label="Object Position Angles", placeholder="e.g. 0.1, -0.3, 0.2, -1.0, 0.0, 1.2, 0.5")
181
- approach_input = gr.Textbox(label="Approach Angles", placeholder="e.g. 0.1, -0.5, 0.25, -1.2, 0.0, 1.3, 0.5")
182
- place_input = gr.Textbox(label="Place Angles", placeholder="e.g. 0.3, -0.4, 0.15, -1.4, 0.0, 1.4, 0.4")
183
- gr.Button("🤖 Perform Pick and Place").click(
184
- fn=pick_and_place,
185
- inputs=[position_input, approach_input, place_input],
186
  outputs=[img_output, text_output]
187
  )
188
 
 
 
 
1
  import pybullet as p
2
  import pybullet_data
3
  import numpy as np
 
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
 
 
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")
 
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