YashsharmaPhD commited on
Commit
3660d05
·
verified ·
1 Parent(s): 773d554

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -36
app.py CHANGED
@@ -13,8 +13,9 @@ 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
17
  cube_id = p.loadURDF("cube_small.urdf", basePosition=[0.6, 0, 0.02])
 
18
 
19
  # Get joint indices
20
  def get_panda_joints(robot):
@@ -62,9 +63,7 @@ def render_sim(joint_values, gripper_val):
62
  add_joint_labels()
63
 
64
  # Camera
65
- width, height = 1280, 1280 # or even 1280x1280
66
-
67
- #width, height = 512, 512
68
  view_matrix = p.computeViewMatrix([1.5, 0, 1], [0, 0, 0.5], [0, 0, 1])
69
  proj_matrix = p.computeProjectionMatrixFOV(60, width / height, 0.1, 3.1)
70
  _, _, img, _, _ = p.getCameraImage(width, height, view_matrix, proj_matrix)
@@ -75,7 +74,7 @@ def render_sim(joint_values, gripper_val):
75
  ax.imshow(rgb.astype(np.uint8))
76
  ax.axis("off")
77
  tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
78
- plt.savefig(tmp.name, bbox_inches='tight')
79
  plt.close()
80
 
81
  # Text output
@@ -106,31 +105,37 @@ def move_to_input_angles(joint_str):
106
  except Exception as e:
107
  return None, f"Error: {str(e)}"
108
 
109
- # Pick and place motion
110
- def pick_and_place():
111
- pre_grasp = [0, -0.5, 0, -2.0, 0, 1.5, 0.8]
112
- grasp = [0, -0.5, 0, -2.1, 0, 1.7, 0.8]
113
- lift = [0, -0.3, 0, -1.5, 0, 1.3, 0.6]
114
- place = [0.5, -0.4, 0.2, -1.8, 0, 1.4, 0.6]
115
-
116
- sequence = [
117
- (pre_grasp, 0.04),
118
- (grasp, 0.0), # Close gripper
119
- (lift, 0.0),
120
- (place, 0.0),
121
- (place, 0.04) # Open gripper
122
- ]
123
-
124
- for joints, grip in sequence:
125
- for _ in range(50):
126
- for idx, val in zip(arm_joints, joints):
127
- p.setJointMotorControl2(robot, idx, p.POSITION_CONTROL, targetPosition=val)
128
- for fj in finger_joints:
129
- p.setJointMotorControl2(robot, fj, p.POSITION_CONTROL, targetPosition=grip)
130
- p.stepSimulation()
131
- time.sleep(0.01)
132
 
133
- return render_sim(sequence[-1][0], sequence[-1][1])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
  # Load gripper type (placeholder logic)
136
  def switch_gripper(gripper_type):
@@ -176,12 +181,11 @@ with gr.Blocks(title="Franka Arm Control with 7 DoF and Gripper Options") as dem
176
 
177
  gr.Button("🔄 Reset Robot").click(fn=reset, inputs=[], outputs=[img_output, text_output])
178
 
179
- # Pick and Place Button
180
- gr.Button("🤖 Pick and Place").click(fn=pick_and_place, inputs=[], outputs=[img_output, text_output])
181
-
182
- # Joint angle input box
183
- gr.Markdown("### 🧾 Enter Joint Angles (comma-separated)")
184
- 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")
185
- gr.Button("▶️ Move to Angles").click(fn=move_to_input_angles, inputs=joint_input, outputs=[img_output, text_output])
186
 
187
  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)
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
 
20
  # Get joint indices
21
  def get_panda_joints(robot):
 
63
  add_joint_labels()
64
 
65
  # Camera
66
+ width, height = 1280, 1280 # Increased resolution for clarity
 
 
67
  view_matrix = p.computeViewMatrix([1.5, 0, 1], [0, 0, 0.5], [0, 0, 1])
68
  proj_matrix = p.computeProjectionMatrixFOV(60, width / height, 0.1, 3.1)
69
  _, _, img, _, _ = p.getCameraImage(width, height, view_matrix, proj_matrix)
 
74
  ax.imshow(rgb.astype(np.uint8))
75
  ax.axis("off")
76
  tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
77
+ plt.savefig(tmp.name, bbox_inches='tight', dpi=200) # Increased DPI for sharper image
78
  plt.close()
79
 
80
  # Text output
 
105
  except Exception as e:
106
  return None, f"Error: {str(e)}"
107
 
108
+ # Pick and place motion with user input
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):
 
181
 
182
  gr.Button("🔄 Reset Robot").click(fn=reset, inputs=[], outputs=[img_output, text_output])
183
 
184
+ # Pick and Place Inputs
185
+ gr.Markdown("### 🧾 Enter Joint Angles for Pick and Place")
186
+ 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")
187
+ 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")
188
+ 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")
189
+ gr.Button("🤖 Perform Pick and Place").click(fn=pick_and_place, inputs=[position_input, approach_input, place_input], outputs=[img_output, text_output])
 
190
 
191
  demo.launch(debug=True)