Update app.py
Browse files
app.py
CHANGED
|
@@ -6,20 +6,14 @@ import tempfile
|
|
| 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.connect(p.DIRECT)
|
| 12 |
-
|
| 13 |
p.setAdditionalSearchPath(pybullet_data.getDataPath())
|
| 14 |
p.setGravity(0, 0, -9.8)
|
| 15 |
p.loadURDF("plane.urdf")
|
| 16 |
robot = p.loadURDF("franka_panda/panda.urdf", basePosition=[0, 0, 0], useFixedBase=True)
|
| 17 |
-
|
| 18 |
-
# Add cube to pick
|
| 19 |
cube_id = p.loadURDF("cube_small.urdf", basePosition=[0.6, 0, 0.02])
|
| 20 |
-
p.changeVisualShape(cube_id, -1, rgbaColor=[0, 0, 0, 1])
|
| 21 |
|
| 22 |
-
# Get joint indices
|
| 23 |
def get_panda_joints(robot):
|
| 24 |
arm, fingers = [], []
|
| 25 |
for i in range(p.getNumJoints(robot)):
|
|
@@ -34,7 +28,6 @@ def get_panda_joints(robot):
|
|
| 34 |
|
| 35 |
arm_joints, finger_joints = get_panda_joints(robot)
|
| 36 |
|
| 37 |
-
# Add joint labels in sim
|
| 38 |
debug_labels = []
|
| 39 |
def add_joint_labels():
|
| 40 |
global debug_labels
|
|
@@ -48,8 +41,7 @@ def add_joint_labels():
|
|
| 48 |
text_id = p.addUserDebugText(lbl, pos, textColorRGB=[1, 0, 0], textSize=1.2)
|
| 49 |
debug_labels.append(text_id)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
def render_sim(joint_values, gripper_val):
|
| 53 |
for idx, tgt in zip(arm_joints, joint_values):
|
| 54 |
p.setJointMotorControl2(robot, idx, p.POSITION_CONTROL, targetPosition=tgt)
|
| 55 |
if len(finger_joints) == 2:
|
|
@@ -58,7 +50,7 @@ def render_sim(joint_values, gripper_val):
|
|
| 58 |
for _ in range(10): p.stepSimulation()
|
| 59 |
add_joint_labels()
|
| 60 |
width, height = 1280, 1280
|
| 61 |
-
view_matrix = p.computeViewMatrix(
|
| 62 |
proj_matrix = p.computeProjectionMatrixFOV(60, width / height, 0.1, 3.1)
|
| 63 |
_, _, img, _, _ = p.getCameraImage(width, height, view_matrix, proj_matrix)
|
| 64 |
rgb = np.reshape(img, (height, width, 4))[:, :, :3]
|
|
@@ -72,8 +64,7 @@ def render_sim(joint_values, gripper_val):
|
|
| 72 |
joint_text += f"\nGripper = {gripper_val:.3f} m"
|
| 73 |
return tmp.name, joint_text
|
| 74 |
|
| 75 |
-
|
| 76 |
-
def move_to_input_angles(joint_str):
|
| 77 |
try:
|
| 78 |
target_angles = [float(x.strip()) for x in joint_str.split(",")]
|
| 79 |
if len(target_angles) != 7:
|
|
@@ -87,12 +78,11 @@ def move_to_input_angles(joint_str):
|
|
| 87 |
p.stepSimulation()
|
| 88 |
time.sleep(0.01)
|
| 89 |
current_grip = p.getJointState(robot, finger_joints[0])[0]
|
| 90 |
-
return render_sim(target_angles, current_grip)
|
| 91 |
except Exception as e:
|
| 92 |
return None, f"Error: {str(e)}"
|
| 93 |
|
| 94 |
-
|
| 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(",")]
|
|
@@ -101,7 +91,7 @@ def pick_and_place(position_str, approach_str, place_str):
|
|
| 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_input_angles(approach_str)
|
| 105 |
|
| 106 |
for _ in range(30):
|
| 107 |
for fj in finger_joints:
|
|
@@ -117,7 +107,7 @@ def pick_and_place(position_str, approach_str, place_str):
|
|
| 117 |
p.stepSimulation()
|
| 118 |
time.sleep(0.01)
|
| 119 |
|
| 120 |
-
move_to_input_angles(place_str)
|
| 121 |
|
| 122 |
for _ in range(30):
|
| 123 |
for fj in finger_joints:
|
|
@@ -125,13 +115,12 @@ def pick_and_place(position_str, approach_str, place_str):
|
|
| 125 |
p.stepSimulation()
|
| 126 |
time.sleep(0.01)
|
| 127 |
|
| 128 |
-
return render_sim(place_angles, 0.04)
|
| 129 |
except Exception as e:
|
| 130 |
return None, f"Error: {str(e)}"
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
gr.Markdown("## 🤖 Franka Panda Robot Control")
|
| 135 |
|
| 136 |
joint_sliders = []
|
| 137 |
with gr.Row():
|
|
@@ -142,41 +131,49 @@ with gr.Blocks(title="Franka Arm Pick & Place") as demo:
|
|
| 142 |
joint_sliders.append(gr.Slider(-3.14, 3.14, value=0, label=f"Joint {i+1}"))
|
| 143 |
gripper = gr.Slider(0.0, 0.04, value=0.02, step=0.001, label="Gripper")
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
with gr.Row():
|
| 146 |
img_output = gr.Image(type="filepath", label="Simulation View")
|
| 147 |
text_output = gr.Textbox(label="Joint States")
|
| 148 |
|
| 149 |
def live_update(*vals):
|
| 150 |
-
joints = list(vals[
|
| 151 |
-
grip = vals[
|
| 152 |
-
|
|
|
|
|
|
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
|
|
|
| 156 |
|
| 157 |
gr.Button("🔄 Reset Robot").click(
|
| 158 |
-
fn=lambda: render_sim([0]*7, 0.02),
|
| 159 |
inputs=[], outputs=[img_output, text_output]
|
| 160 |
)
|
| 161 |
|
| 162 |
gr.Markdown("### ✍️ Move Robot to Custom Joint Angles")
|
| 163 |
-
joint_input_box = gr.Textbox(
|
| 164 |
-
label="Enter 7 Joint Angles (comma-separated)",
|
| 165 |
-
placeholder="e.g. 0.0, -0.5, 0.3, -1.2, 0.0, 1.5, 0.8"
|
| 166 |
-
)
|
| 167 |
gr.Button("▶️ Move to Angles").click(
|
| 168 |
-
fn=move_to_input_angles,
|
| 169 |
-
inputs=joint_input_box,
|
| 170 |
outputs=[img_output, text_output]
|
| 171 |
)
|
| 172 |
|
| 173 |
gr.Markdown("### 🧾 Pick and Place Input (3 sets of joint angles)")
|
| 174 |
-
position_input = gr.Textbox(label="Object Position Angles"
|
| 175 |
-
approach_input = gr.Textbox(label="Approach Angles"
|
| 176 |
-
place_input = gr.Textbox(label="Place Angles"
|
| 177 |
gr.Button("🤖 Perform Pick and Place").click(
|
| 178 |
-
fn=pick_and_place,
|
| 179 |
-
inputs=[position_input, approach_input, place_input],
|
| 180 |
outputs=[img_output, text_output]
|
| 181 |
)
|
| 182 |
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
import time
|
| 8 |
|
|
|
|
|
|
|
| 9 |
p.connect(p.DIRECT)
|
|
|
|
| 10 |
p.setAdditionalSearchPath(pybullet_data.getDataPath())
|
| 11 |
p.setGravity(0, 0, -9.8)
|
| 12 |
p.loadURDF("plane.urdf")
|
| 13 |
robot = p.loadURDF("franka_panda/panda.urdf", basePosition=[0, 0, 0], useFixedBase=True)
|
|
|
|
|
|
|
| 14 |
cube_id = p.loadURDF("cube_small.urdf", basePosition=[0.6, 0, 0.02])
|
| 15 |
+
p.changeVisualShape(cube_id, -1, rgbaColor=[0, 0, 0, 1])
|
| 16 |
|
|
|
|
| 17 |
def get_panda_joints(robot):
|
| 18 |
arm, fingers = [], []
|
| 19 |
for i in range(p.getNumJoints(robot)):
|
|
|
|
| 28 |
|
| 29 |
arm_joints, finger_joints = get_panda_joints(robot)
|
| 30 |
|
|
|
|
| 31 |
debug_labels = []
|
| 32 |
def add_joint_labels():
|
| 33 |
global debug_labels
|
|
|
|
| 41 |
text_id = p.addUserDebugText(lbl, pos, textColorRGB=[1, 0, 0], textSize=1.2)
|
| 42 |
debug_labels.append(text_id)
|
| 43 |
|
| 44 |
+
def render_sim(joint_values, gripper_val, cam_xyz, target_xyz):
|
|
|
|
| 45 |
for idx, tgt in zip(arm_joints, joint_values):
|
| 46 |
p.setJointMotorControl2(robot, idx, p.POSITION_CONTROL, targetPosition=tgt)
|
| 47 |
if len(finger_joints) == 2:
|
|
|
|
| 50 |
for _ in range(10): p.stepSimulation()
|
| 51 |
add_joint_labels()
|
| 52 |
width, height = 1280, 1280
|
| 53 |
+
view_matrix = p.computeViewMatrix(cam_xyz, target_xyz, [0, 0, 1])
|
| 54 |
proj_matrix = p.computeProjectionMatrixFOV(60, width / height, 0.1, 3.1)
|
| 55 |
_, _, img, _, _ = p.getCameraImage(width, height, view_matrix, proj_matrix)
|
| 56 |
rgb = np.reshape(img, (height, width, 4))[:, :, :3]
|
|
|
|
| 64 |
joint_text += f"\nGripper = {gripper_val:.3f} m"
|
| 65 |
return tmp.name, joint_text
|
| 66 |
|
| 67 |
+
def move_to_input_angles(joint_str, cam_xyz, target_xyz):
|
|
|
|
| 68 |
try:
|
| 69 |
target_angles = [float(x.strip()) for x in joint_str.split(",")]
|
| 70 |
if len(target_angles) != 7:
|
|
|
|
| 78 |
p.stepSimulation()
|
| 79 |
time.sleep(0.01)
|
| 80 |
current_grip = p.getJointState(robot, finger_joints[0])[0]
|
| 81 |
+
return render_sim(target_angles, current_grip, cam_xyz, target_xyz)
|
| 82 |
except Exception as e:
|
| 83 |
return None, f"Error: {str(e)}"
|
| 84 |
|
| 85 |
+
def pick_and_place(position_str, approach_str, place_str, cam_xyz, target_xyz):
|
|
|
|
| 86 |
try:
|
| 87 |
position_angles = [float(x.strip()) for x in position_str.split(",")]
|
| 88 |
approach_angles = [float(x.strip()) for x in approach_str.split(",")]
|
|
|
|
| 91 |
if len(position_angles) != 7 or len(approach_angles) != 7 or len(place_angles) != 7:
|
| 92 |
return None, "❌ All inputs must have 7 joint angles."
|
| 93 |
|
| 94 |
+
move_to_input_angles(approach_str, cam_xyz, target_xyz)
|
| 95 |
|
| 96 |
for _ in range(30):
|
| 97 |
for fj in finger_joints:
|
|
|
|
| 107 |
p.stepSimulation()
|
| 108 |
time.sleep(0.01)
|
| 109 |
|
| 110 |
+
move_to_input_angles(place_str, cam_xyz, target_xyz)
|
| 111 |
|
| 112 |
for _ in range(30):
|
| 113 |
for fj in finger_joints:
|
|
|
|
| 115 |
p.stepSimulation()
|
| 116 |
time.sleep(0.01)
|
| 117 |
|
| 118 |
+
return render_sim(place_angles, 0.04, cam_xyz, target_xyz)
|
| 119 |
except Exception as e:
|
| 120 |
return None, f"Error: {str(e)}"
|
| 121 |
|
| 122 |
+
with gr.Blocks(title="Franka Arm with 3D Camera Control") as demo:
|
| 123 |
+
gr.Markdown("## 🤖 Franka Robot with Camera Controls")
|
|
|
|
| 124 |
|
| 125 |
joint_sliders = []
|
| 126 |
with gr.Row():
|
|
|
|
| 131 |
joint_sliders.append(gr.Slider(-3.14, 3.14, value=0, label=f"Joint {i+1}"))
|
| 132 |
gripper = gr.Slider(0.0, 0.04, value=0.02, step=0.001, label="Gripper")
|
| 133 |
|
| 134 |
+
gr.Markdown("### 🎥 Camera Controls")
|
| 135 |
+
cam_x = gr.Slider(-3, 3, value=1.5, label="Camera X")
|
| 136 |
+
cam_y = gr.Slider(-3, 3, value=0.0, label="Camera Y")
|
| 137 |
+
cam_z = gr.Slider(-1, 3, value=1.0, label="Camera Z")
|
| 138 |
+
tgt_x = gr.Slider(-1, 1, value=0.0, label="Target X")
|
| 139 |
+
tgt_y = gr.Slider(-1, 1, value=0.0, label="Target Y")
|
| 140 |
+
tgt_z = gr.Slider(0, 2, value=0.5, label="Target Z")
|
| 141 |
+
|
| 142 |
with gr.Row():
|
| 143 |
img_output = gr.Image(type="filepath", label="Simulation View")
|
| 144 |
text_output = gr.Textbox(label="Joint States")
|
| 145 |
|
| 146 |
def live_update(*vals):
|
| 147 |
+
joints = list(vals[:7])
|
| 148 |
+
grip = vals[7]
|
| 149 |
+
cam = [vals[8], vals[9], vals[10]]
|
| 150 |
+
tgt = [vals[11], vals[12], vals[13]]
|
| 151 |
+
return render_sim(joints, grip, cam, tgt)
|
| 152 |
|
| 153 |
+
sliders = joint_sliders + [gripper, cam_x, cam_y, cam_z, tgt_x, tgt_y, tgt_z]
|
| 154 |
+
for s in sliders:
|
| 155 |
+
s.change(fn=live_update, inputs=sliders, outputs=[img_output, text_output])
|
| 156 |
|
| 157 |
gr.Button("🔄 Reset Robot").click(
|
| 158 |
+
fn=lambda: render_sim([0]*7, 0.02, [1.5, 0, 1.0], [0, 0, 0.5]),
|
| 159 |
inputs=[], outputs=[img_output, text_output]
|
| 160 |
)
|
| 161 |
|
| 162 |
gr.Markdown("### ✍️ Move Robot to Custom Joint Angles")
|
| 163 |
+
joint_input_box = gr.Textbox(label="Enter 7 Joint Angles (comma-separated)")
|
|
|
|
|
|
|
|
|
|
| 164 |
gr.Button("▶️ Move to Angles").click(
|
| 165 |
+
fn=lambda s, x, y, z, tx, ty, tz: move_to_input_angles(s, [x, y, z], [tx, ty, tz]),
|
| 166 |
+
inputs=[joint_input_box, cam_x, cam_y, cam_z, tgt_x, tgt_y, tgt_z],
|
| 167 |
outputs=[img_output, text_output]
|
| 168 |
)
|
| 169 |
|
| 170 |
gr.Markdown("### 🧾 Pick and Place Input (3 sets of joint angles)")
|
| 171 |
+
position_input = gr.Textbox(label="Object Position Angles")
|
| 172 |
+
approach_input = gr.Textbox(label="Approach Angles")
|
| 173 |
+
place_input = gr.Textbox(label="Place Angles")
|
| 174 |
gr.Button("🤖 Perform Pick and Place").click(
|
| 175 |
+
fn=lambda p, a, pl, x, y, z, tx, ty, tz: pick_and_place(p, a, pl, [x, y, z], [tx, ty, tz]),
|
| 176 |
+
inputs=[position_input, approach_input, place_input, cam_x, cam_y, cam_z, tgt_x, tgt_y, tgt_z],
|
| 177 |
outputs=[img_output, text_output]
|
| 178 |
)
|
| 179 |
|