Nalyd1002

Arm robot

Feb 20th, 2023 (edited)
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.13 KB | None | 0 0
  1. import pybullet as p
  2. import pybullet_data
  3. import numpy as np
  4. import time
  5. import math
  6.  
  7. # Connect to the simulation environment
  8. physicsClient = p.connect(p.GUI)
  9. p.resetSimulation()
  10. p.setAdditionalSearchPath(pybullet_data.getDataPath())
  11. p.setGravity(0, 0, -9.81)
  12. p.setRealTimeSimulation(0)
  13.  
  14. # Load plane on which the objects will be placed
  15. planeId = p.loadURDF("plane.urdf")
  16.  
  17. # Load the Franka Panda robot arm and its end-effector
  18. robotStartPos = [0, 0, 0]
  19. robotStartOrientation = p.getQuaternionFromEuler([0, 0, 0])
  20. robotId = p.loadURDF("franka_panda/panda.urdf", robotStartPos, robotStartOrientation, useFixedBase=True,
  21.                      flags=p.URDF_USE_INERTIA_FROM_FILE)
  22. num_joints = p.getNumJoints(robotId)
  23.  
  24. pandaNumDofs = 7
  25.  
  26. jointPositions = [0.98, 0.458, 0.31, -2.24, -0.30, 2.66, 2.32, 0.02, 0.02]
  27.  
  28. orn = p.getQuaternionFromEuler([math.pi / 2., 0., 0.])
  29.  
  30. gripper_height = 0.2
  31.  
  32. # Load the objects to be picked up
  33. # It will improve loading performance for files with similar graphics assets
  34. flags = p.URDF_ENABLE_CACHED_GRAPHICS_SHAPES
  35.  
  36. object1 = p.loadURDF("lego/lego.urdf", [0, 0.4, 0.02], flags=flags)
  37. object2 = p.loadURDF("lego/lego.urdf", [0, 0.5, 0.02], flags=flags)  # object centered with the box
  38. object3 = p.loadURDF("sphere_small.urdf", [-0.2, 0.5, 0.02], flags=flags)
  39. object4 = p.loadURDF("sphere_small.urdf", [0.1, 0.5, 0.02], flags=flags)
  40. object5 = p.loadURDF("sphere_small.urdf", [0.1, 0.6, 0.02], flags=flags)
  41. object_list = [object1, object2, object3, object4, object5]
  42.  
  43. # Create constrains such that the 2 fingers of the gripper are always centered
  44. c = p.createConstraint(robotId, 9, robotId, 10, jointType=p.JOINT_GEAR,
  45.                        jointAxis=[1, 0, 0],
  46.                        parentFramePosition=[0, 0, 0],
  47.                        childFramePosition=[0, 0, 0])
  48. p.changeConstraint(c, gearRatio=-1, erp=0.1, maxForce=50)
  49.  
  50. # Get the joint indice for the gripper joint
  51. gripper_joint_index = num_joints - 1  # 'panda_grasptarget'
  52. # info = p.getNumJoints(robotId)
  53. # print(info)
  54. # for i in range(info):
  55. #    print(p.getJointInfo(robotId, i))
  56.  
  57. # Fix the camera in space
  58. # Set camera position and orientation
  59. camera_pos = [0, 1, 2]
  60. viewMatrix = p.computeViewMatrix(
  61.     cameraEyePosition=camera_pos,
  62.     cameraTargetPosition=[0, 1, 0],
  63.     cameraUpVector=[0, 1, 0])
  64.  
  65. projectionMatrix = p.computeProjectionMatrixFOV(
  66.     fov=45.0,
  67.     aspect=1.0,
  68.     nearVal=0.1,
  69.     farVal=3.1)
  70.  
  71. # set the starting configuration of the robot arm
  72. for i in range(len(jointPositions)):
  73.     p.resetJointState(robotId, i, jointPositions[i])
  74. for i in [9, 10]:
  75.     p.setJointMotorControl2(robotId, i, p.POSITION_CONTROL, 1, force=10)
  76.  
  77. count = 0
  78. while True:
  79.     width, height, rgbImg, depthImg, segImg = p.getCameraImage(
  80.         width=224,
  81.         height=224,
  82.         viewMatrix=viewMatrix,
  83.         projectionMatrix=projectionMatrix,
  84.         flags=p.ER_SEGMENTATION_MASK_OBJECT_AND_LINKINDEX)
  85.  
  86.     # print(rgbImg)
  87.     # print(depthImg)
  88.     # print(segImg)
  89.  
  90.     # define the target end-effector position
  91.     #target_position = [0.5, 0.3, 1]
  92.     if count < len(object_list):
  93.         orn = p.getQuaternionFromEuler([math.pi, 0., 0.])
  94.         print(count)
  95.         target_position = np.array(p.getBasePositionAndOrientation(object_list[count])[0]) + [0, 0, 0.2]
  96.         print(target_position)
  97.         # calculate the inverse kinematics for the target end-effector position
  98.         joint_angles = p.calculateInverseKinematics(robotId, gripper_joint_index, target_position, orn,
  99.                                                     maxNumIterations=20)
  100.  
  101.         # set the joint angles of the robot arm
  102.         for i in range(pandaNumDofs):
  103.             p.setJointMotorControl2(robotId, i, p.POSITION_CONTROL, joint_angles[i], force=5 * 240.)
  104.         print("Position has been updated")
  105.         p.stepSimulation()
  106.         time.sleep(3)
  107.     #    p.setJointMotorControlArray(robotId, list(range(0, pandaNumDofs + 2)), p.POSITION_CONTROL, joint_angles,
  108.     #                                forces=[5 * 240. for i in range(pandaNumDofs + 2)])
  109.  
  110.     # Set grip, here grip is closed -> should change targetPosition to close and open grip
  111. #        print(p.getLinkState(robotId, gripper_joint_index)[0])
  112. #        error_diff = 0.01
  113. #        print(p.getLinkState(robotId, gripper_joint_index)[0][0] - target_position[0])
  114. #        print(p.getLinkState(robotId, gripper_joint_index)[0][1] - target_position[1])
  115. #        print(p.getLinkState(robotId, gripper_joint_index)[0][2] - target_position[2])
  116. #        if ((p.getLinkState(robotId, gripper_joint_index)[0][0] - target_position[0])  # !!! does not work
  117. #            and (p.getLinkState(robotId, gripper_joint_index)[0][1] - target_position[1])
  118. #            and (p.getLinkState(robotId, gripper_joint_index)[0][2] - target_position[2])) < error_diff:
  119. #            for i in [9, 10]:
  120. #                p.setJointMotorControl2(robotId, i, p.POSITION_CONTROL, 0, force=10)
  121.         # p.setJointMotorControlArray(robotId, [9, 10], p.POSITION_CONTROL, [0 for i in range(2)],
  122.         #                            force=[10 for i in range(2)])
  123.     count += 1
  124.     p.stepSimulation()
  125.     time.sleep(1. / 240.)
  126.  
Tags: arm robot
Advertisement
Add Comment
Please, Sign In to add comment