Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ 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]) # Change the cube color to black
|
| 19 |
|
|
@@ -46,8 +46,20 @@ 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 image and info
|
| 50 |
-
def render_sim(joint_values, gripper_val):
|
| 51 |
# Apply joint controls
|
| 52 |
for idx, tgt in zip(arm_joints, joint_values):
|
| 53 |
p.setJointMotorControl2(robot, idx, p.POSITION_CONTROL, targetPosition=tgt)
|
|
@@ -62,9 +74,9 @@ def render_sim(joint_values, gripper_val):
|
|
| 62 |
# Refresh joint labels
|
| 63 |
add_joint_labels()
|
| 64 |
|
| 65 |
-
# Camera
|
| 66 |
width, height = 1280, 1280 # Increased resolution for clarity
|
| 67 |
-
view_matrix = p.
|
| 68 |
proj_matrix = p.computeProjectionMatrixFOV(60, width / height, 0.1, 3.1)
|
| 69 |
_, _, img, _, _ = p.getCameraImage(width, height, view_matrix, proj_matrix)
|
| 70 |
rgb = np.reshape(img, (height, width, 4))[:, :, :3]
|
|
@@ -100,44 +112,12 @@ def move_to_input_angles(joint_str):
|
|
| 100 |
time.sleep(0.01)
|
| 101 |
|
| 102 |
current_grip = p.getJointState(robot, finger_joints[0])[0]
|
| 103 |
-
return render_sim(target_angles, current_grip)
|
| 104 |
|
| 105 |
except Exception as e:
|
| 106 |
return None, f"Error: {str(e)}"
|
| 107 |
|
| 108 |
-
#
|
| 109 |
-
def pick_and_place(position_angles, approach_angles, place_angles):
|
| 110 |
-
# Parse the angles from user input
|
| 111 |
-
position_angles = [float(x.strip()) for x in position_angles.split(",")]
|
| 112 |
-
approach_angles = [float(x.strip()) for x in approach_angles.split(",")]
|
| 113 |
-
place_angles = [float(x.strip()) for x in place_angles.split(",")]
|
| 114 |
-
|
| 115 |
-
# Move towards the position
|
| 116 |
-
move_to_input_angles(approach_angles)
|
| 117 |
-
|
| 118 |
-
# Close gripper to pick
|
| 119 |
-
for _ in range(50):
|
| 120 |
-
for fj in finger_joints:
|
| 121 |
-
p.setJointMotorControl2(robot, fj, p.POSITION_CONTROL, targetPosition=0.0)
|
| 122 |
-
p.stepSimulation()
|
| 123 |
-
time.sleep(0.01)
|
| 124 |
-
|
| 125 |
-
# Lift the object
|
| 126 |
-
move_to_input_angles([x + 0.1 for x in approach_angles]) # Example lift motion
|
| 127 |
-
|
| 128 |
-
# Move to the place position
|
| 129 |
-
move_to_input_angles(place_angles)
|
| 130 |
-
|
| 131 |
-
# Open gripper to place
|
| 132 |
-
for _ in range(50):
|
| 133 |
-
for fj in finger_joints:
|
| 134 |
-
p.setJointMotorControl2(robot, fj, p.POSITION_CONTROL, targetPosition=0.04) # Open gripper
|
| 135 |
-
p.stepSimulation()
|
| 136 |
-
time.sleep(0.01)
|
| 137 |
-
|
| 138 |
-
return render_sim(place_angles, 0.04) # Final joint angles after placement
|
| 139 |
-
|
| 140 |
-
# Load gripper type (placeholder logic)
|
| 141 |
def switch_gripper(gripper_type):
|
| 142 |
print(f"Switching to gripper: {gripper_type}")
|
| 143 |
return f"Switched to: {gripper_type}"
|
|
@@ -151,6 +131,10 @@ with gr.Blocks(title="Franka Arm Control with 7 DoF and Gripper Options") as dem
|
|
| 151 |
gripper_feedback = gr.Textbox(label="Gripper Status", interactive=False)
|
| 152 |
gripper_selector.change(fn=switch_gripper, inputs=gripper_selector, outputs=gripper_feedback)
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
# 7 DoF sliders arranged in two rows
|
| 155 |
joint_sliders = []
|
| 156 |
with gr.Row():
|
|
@@ -161,6 +145,12 @@ with gr.Blocks(title="Franka Arm Control with 7 DoF and Gripper Options") as dem
|
|
| 161 |
joint_sliders.append(gr.Slider(-3.14, 3.14, value=0, label=f"Joint {i+1}"))
|
| 162 |
gripper = gr.Slider(0.0, 0.04, value=0.02, step=0.001, label="Gripper Opening")
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
# Outputs
|
| 165 |
with gr.Row():
|
| 166 |
img_output = gr.Image(type="filepath", label="Simulation View")
|
|
@@ -170,14 +160,15 @@ with gr.Blocks(title="Franka Arm Control with 7 DoF and Gripper Options") as dem
|
|
| 170 |
def live_update(*vals):
|
| 171 |
joints = list(vals[:-1])
|
| 172 |
grip = vals[-1]
|
| 173 |
-
|
|
|
|
| 174 |
|
| 175 |
-
for s in joint_sliders + [gripper]:
|
| 176 |
-
s.change(fn=live_update, inputs=joint_sliders + [gripper], outputs=[img_output, text_output])
|
| 177 |
|
| 178 |
# Reset button
|
| 179 |
def reset():
|
| 180 |
-
return render_sim([0]*7, 0.02)
|
| 181 |
|
| 182 |
gr.Button("🔄 Reset Robot").click(fn=reset, inputs=[], outputs=[img_output, text_output])
|
| 183 |
|
|
@@ -186,11 +177,4 @@ with gr.Blocks(title="Franka Arm Control with 7 DoF and Gripper Options") as dem
|
|
| 186 |
joint_input = gr.Textbox(label="Joint Angles (7 values in radians)", placeholder="e.g. 0.0, -0.5, 0.3, -1.2, 0.0, 1.5, 0.8")
|
| 187 |
gr.Button("▶️ Move to Angles").click(fn=move_to_input_angles, inputs=joint_input, outputs=[img_output, text_output])
|
| 188 |
|
| 189 |
-
# Pick and Place Inputs
|
| 190 |
-
gr.Markdown("### 🧾 Enter Joint Angles for Pick and Place")
|
| 191 |
-
position_input = gr.Textbox(label="Position Angles (7 values in radians)", placeholder="e.g. 0.0, -0.5, 0.3, -1.2, 0.0, 1.5, 0.8")
|
| 192 |
-
approach_input = gr.Textbox(label="Approach Angles (7 values in radians)", placeholder="e.g. 0.0, -0.3, 0.3, -1.5, 0.0, 1.0, 0.6")
|
| 193 |
-
place_input = gr.Textbox(label="Place Angles (7 values in radians)", placeholder="e.g. 0.5, -0.4, 0.2, -1.8, 0.0, 1.4, 0.6")
|
| 194 |
-
gr.Button("🤖 Perform Pick and Place").click(fn=pick_and_place, inputs=[position_input, approach_input, place_input], outputs=[img_output, text_output])
|
| 195 |
-
|
| 196 |
demo.launch(debug=True)
|
|
|
|
| 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 by default)
|
| 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]) # Change the cube color to black
|
| 19 |
|
|
|
|
| 46 |
text_id = p.addUserDebugText(lbl, pos, textColorRGB=[1, 0, 0], textSize=1.2)
|
| 47 |
debug_labels.append(text_id)
|
| 48 |
|
| 49 |
+
# Change cube color dynamically
|
| 50 |
+
def change_cube_color(color):
|
| 51 |
+
color_map = {
|
| 52 |
+
"Black": [0, 0, 0, 1],
|
| 53 |
+
"Red": [1, 0, 0, 1],
|
| 54 |
+
"Green": [0, 1, 0, 1],
|
| 55 |
+
"Blue": [0, 0, 1, 1],
|
| 56 |
+
"Yellow": [1, 1, 0, 1],
|
| 57 |
+
"White": [1, 1, 1, 1]
|
| 58 |
+
}
|
| 59 |
+
p.changeVisualShape(cube_id, -1, rgbaColor=color_map[color])
|
| 60 |
+
|
| 61 |
# Render image and info
|
| 62 |
+
def render_sim(joint_values, gripper_val, camera_angles):
|
| 63 |
# Apply joint controls
|
| 64 |
for idx, tgt in zip(arm_joints, joint_values):
|
| 65 |
p.setJointMotorControl2(robot, idx, p.POSITION_CONTROL, targetPosition=tgt)
|
|
|
|
| 74 |
# Refresh joint labels
|
| 75 |
add_joint_labels()
|
| 76 |
|
| 77 |
+
# Camera view
|
| 78 |
width, height = 1280, 1280 # Increased resolution for clarity
|
| 79 |
+
view_matrix = p.computeViewMatrixFromYawPitchRoll(camera_angles[0], camera_angles[1], camera_angles[2], [1.5, 0, 1], [0, 0, 0.5], [0, 0, 1])
|
| 80 |
proj_matrix = p.computeProjectionMatrixFOV(60, width / height, 0.1, 3.1)
|
| 81 |
_, _, img, _, _ = p.getCameraImage(width, height, view_matrix, proj_matrix)
|
| 82 |
rgb = np.reshape(img, (height, width, 4))[:, :, :3]
|
|
|
|
| 112 |
time.sleep(0.01)
|
| 113 |
|
| 114 |
current_grip = p.getJointState(robot, finger_joints[0])[0]
|
| 115 |
+
return render_sim(target_angles, current_grip, [0, 0, 0])
|
| 116 |
|
| 117 |
except Exception as e:
|
| 118 |
return None, f"Error: {str(e)}"
|
| 119 |
|
| 120 |
+
# Gripper control
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
def switch_gripper(gripper_type):
|
| 122 |
print(f"Switching to gripper: {gripper_type}")
|
| 123 |
return f"Switched to: {gripper_type}"
|
|
|
|
| 131 |
gripper_feedback = gr.Textbox(label="Gripper Status", interactive=False)
|
| 132 |
gripper_selector.change(fn=switch_gripper, inputs=gripper_selector, outputs=gripper_feedback)
|
| 133 |
|
| 134 |
+
# Multi-color option for the cube
|
| 135 |
+
color_selector = gr.Dropdown(["Black", "Red", "Green", "Blue", "Yellow", "White"], value="Black", label="Select Cube Color")
|
| 136 |
+
color_selector.change(fn=change_cube_color, inputs=color_selector, outputs=[])
|
| 137 |
+
|
| 138 |
# 7 DoF sliders arranged in two rows
|
| 139 |
joint_sliders = []
|
| 140 |
with gr.Row():
|
|
|
|
| 145 |
joint_sliders.append(gr.Slider(-3.14, 3.14, value=0, label=f"Joint {i+1}"))
|
| 146 |
gripper = gr.Slider(0.0, 0.04, value=0.02, step=0.001, label="Gripper Opening")
|
| 147 |
|
| 148 |
+
# Camera rotation sliders (for 3D view movement)
|
| 149 |
+
with gr.Row():
|
| 150 |
+
camera_yaw = gr.Slider(-180, 180, value=0, label="Camera Yaw")
|
| 151 |
+
camera_pitch = gr.Slider(-90, 90, value=0, label="Camera Pitch")
|
| 152 |
+
camera_roll = gr.Slider(-180, 180, value=0, label="Camera Roll")
|
| 153 |
+
|
| 154 |
# Outputs
|
| 155 |
with gr.Row():
|
| 156 |
img_output = gr.Image(type="filepath", label="Simulation View")
|
|
|
|
| 160 |
def live_update(*vals):
|
| 161 |
joints = list(vals[:-1])
|
| 162 |
grip = vals[-1]
|
| 163 |
+
camera_angles = [camera_yaw.value, camera_pitch.value, camera_roll.value]
|
| 164 |
+
return render_sim(joints, grip, camera_angles)
|
| 165 |
|
| 166 |
+
for s in joint_sliders + [gripper, camera_yaw, camera_pitch, camera_roll]:
|
| 167 |
+
s.change(fn=live_update, inputs=joint_sliders + [gripper, camera_yaw, camera_pitch, camera_roll], outputs=[img_output, text_output])
|
| 168 |
|
| 169 |
# Reset button
|
| 170 |
def reset():
|
| 171 |
+
return render_sim([0]*7, 0.02, [0, 0, 0])
|
| 172 |
|
| 173 |
gr.Button("🔄 Reset Robot").click(fn=reset, inputs=[], outputs=[img_output, text_output])
|
| 174 |
|
|
|
|
| 177 |
joint_input = gr.Textbox(label="Joint Angles (7 values in radians)", placeholder="e.g. 0.0, -0.5, 0.3, -1.2, 0.0, 1.5, 0.8")
|
| 178 |
gr.Button("▶️ Move to Angles").click(fn=move_to_input_angles, inputs=joint_input, outputs=[img_output, text_output])
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
demo.launch(debug=True)
|