Update app.py
Browse files
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
|
| 95 |
-
def
|
| 96 |
try:
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
|
| 101 |
-
|
| 102 |
-
|
|
|
|
| 103 |
|
| 104 |
-
# Move to
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
-
#
|
| 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 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
p.setJointMotorControl2(robot, idx, p.POSITION_CONTROL, targetPosition=val)
|
| 120 |
-
p.stepSimulation()
|
| 121 |
-
time.sleep(0.01)
|
| 122 |
|
| 123 |
# Move to place
|
| 124 |
-
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
#
|
| 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(
|
| 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
|
| 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("###
|
| 176 |
-
|
| 177 |
-
gr.
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 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 |
|