Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _flag_reposition(self):
target_pos = self.robot.target_pos
self.flag = None
if self.gui and not self.config["display_ui"]:
self.visual_flagId = p.createVisualShape(p.GEOM_MESH,
fileName=os.path.join(
pybullet_data.getDataPath(), 'cube.obj'),
meshScale=[0.5, 0.5, 0.5],
rgbaColor=[1, 0, 0, 0.7])
self.last_flagId = p.createMultiBody(baseVisualShapeIndex=self.visual_flagId,
baseCollisionShapeIndex=-1,
basePosition=[target_pos[0], target_pos[1], 0.5])
else:
scaling = [1, 1, 1]
magnified = [2, 2, 2]
collisionId = p.createCollisionShape(p.GEOM_MESH, fileName=filename, meshScale=scaling, flags=p.GEOM_FORCE_CONCAVE_TRIMESH)
view_only_mesh = os.path.join(get_model_path(model_id), "mesh_view_only_z_up.obj")
if os.path.exists(view_only_mesh):
visualId = p.createVisualShape(p.GEOM_MESH,
fileName=view_only_mesh,
meshScale=scaling, flags=p.GEOM_FORCE_CONCAVE_TRIMESH)
else:
visualId = -1
boundaryUid = p.createMultiBody(baseCollisionShapeIndex = collisionId, baseVisualShapeIndex = visualId)
p.changeDynamics(boundaryUid, -1, lateralFriction=1)
#print(p.getDynamicsInfo(boundaryUid, -1))
self.scene_obj_list = [(boundaryUid, -1)] # baselink index -1
planeName = os.path.join(pybullet_data.getDataPath(), "mjcf/ground_plane.xml")
self.ground_plane_mjcf = p.loadMJCF(planeName)
if "z_offset" in self.env.config:
z_offset = self.env.config["z_offset"]
else:
z_offset = -10 #with hole filling, we don't need ground plane to be the same height as actual floors
p.resetBasePositionAndOrientation(self.ground_plane_mjcf[0], posObj = [0,0,z_offset], ornObj = [0,0,0,1])
p.changeVisualShape(boundaryUid, -1, rgbaColor=[168 / 255.0, 164 / 255.0, 92 / 255.0, 1.0],
specularColor=[0.5, 0.5, 0.5])
more_compact = 0.5 # set to 1.0 whole football field
# self.walk_target_x *= more_compact
# self.walk_target_y *= more_compact
startx, starty, _ = self.robot.body_xyz
self.flag = None
# self.flag = self.scene.cpp_world.debug_sphere(self.walk_target_x, self.walk_target_y, 0.2, 0.2, 0xFF8080)
self.flag_timeout = 3000 / self.scene.frame_skip
# print('targetxy', self.flagid, self.walk_target_x, self.walk_target_y, p.getBasePositionAndOrientation(self.flagid))
# p.resetBasePositionAndOrientation(self.flagid, posObj = [self.walk_target_x, self.walk_target_y, 0.5], ornObj = [0,0,0,0])
if self.lastid:
p.removeBody(self.lastid)
self.lastid = p.createMultiBody(baseMass=1,
baseVisualShapeIndex=self.visualid,
baseCollisionShapeIndex=self.colisionid,
basePosition=[startx, starty, 0.5])
p.applyExternalForce(self.lastid, -1, [force_x, force_y, 50], [0, 0, 0], p.LINK_FRAME)
ball_xyz, _ = p.getBasePositionAndOrientation(self.lastid)
self.robot.walk_target_x = ball_xyz[0]
self.robot.walk_target_y = ball_xyz[1]
self.robot.set_target_position([walk_target_x, walk_target_y, walk_target_z])
self.flag = None
if not self.gui:
return
if self.visual_flagId is None:
if self.config["display_ui"]:
self.visual_flagId = -1
else:
self.visual_flagId = p.createVisualShape(p.GEOM_MESH,
fileName=os.path.join(
pybullet_data.getDataPath(),
'cube.obj'),
meshScale=[0.5, 0.5, 0.5],
rgbaColor=[1, 0, 0, 0.7])
self.last_flagId = p.createMultiBody(baseVisualShapeIndex=self.visual_flagId,
baseCollisionShapeIndex=-1,
basePosition=[
walk_target_x / self.robot.mjcf_scaling,
walk_target_y / self.robot.mjcf_scaling,
walk_target_z / self.robot.mjcf_scaling
])
'''
for i in range(len(ANT_SENSOR_RESULT)):
walk_target_x, walk_target_y, walk_target_z = ANT_SENSOR_RESULT[i]
visual_flagId = p.createVisualShape(p.GEOM_MESH, fileName=os.path.join(pybullet_data.getDataPath(), 'cube.obj'), meshScale=[0.2, 0.2, 0.2], rgbaColor=[0.5, 0, 0, 0.7])
self.last_flagId = p.createMultiBody(baseVisualShapeIndex=visual_flagId, baseCollisionShapeIndex=-1, basePosition=[walk_target_x / self.robot.mjcf_scaling, walk_target_y / self.robot.mjcf_scaling, walk_target_z / self.robot.mjcf_scaling])
for i in range(len(ANT_DEPTH_RESULT)):
walk_target_x, walk_target_y, walk_target_z = ANT_DEPTH_RESULT[i]
visual_flagId = p.createVisualShape(p.GEOM_MESH, fileName=os.path.join(pybullet_data.getDataPath(), 'cube.obj'), meshScale=[0.2, 0.2, 0.2], rgbaColor=[0, 0.5, 0, 0.7])
self.last_flagId = p.createMultiBody(baseVisualShapeIndex=visual_flagId, baseCollisionShapeIndex=-1, basePosition=[walk_target_x / self.robot.mjcf_scaling, walk_target_y / self.robot.mjcf_scaling, walk_target_z / self.robot.mjcf_scaling])
shift = [0, -0.0, 0]
meshScale = [0.1, 0.1, 0.1]
visualShapeId = p.createVisualShape(shapeType=p.GEOM_BOX,
rgbaColor=[1, 1, 1, 1],
specularColor=[0.4, .4, 0],
halfExtents=[1,1,.5],
visualFramePosition=shift)
collisionShapeId = p.createCollisionShape(shapeType=p.GEOM_BOX,
collisionFramePosition=shift,
halfExtents=[1,1,.5])
atexUid = p.loadTexture("concrete2.png")
rangex = 5
rangey = 5
for i in range(rangex):
for j in range(rangey):
s=p.createMultiBody(baseMass=1000,
baseInertialFramePosition=[0, 0, 0],
baseCollisionShapeIndex=collisionShapeId,
baseVisualShapeIndex=visualShapeId,
basePosition=[((-rangex / 2) + i) * 5,
(-rangey / 2 + j) * 5, 1],
useMaximalCoordinates=True)
p.changeVisualShape(s, -1, textureUniqueId=atexUid)
# NOTE: Left leg
linkMasses.extend(m*np.array([0, 0, 0.105, 0.0475, 0, 0, 0.014]))
linkCollisionShapeIndices.extend([joint_c, joint_c, thigh_c, shin_c, joint_c, joint_c, foot_c])
linkVisualShapeIndices.extend([joint_v, joint_v, thigh_v, shin_v, joint_v, joint_v, foot_v])
linkPositions.extend([left_thigh_p, joint_p, joint_p, shin_p, foot_p, joint_p, joint_p])
linkOrientations.extend([joint_o]*7)
linkInertialFramePositions.extend([[0, 0, 0]]*7)
linkInertialFrameOrientations.extend([[0, 0, 0, 1]]*7)
linkParentIndices.extend([28, 36, 37, 38, 39, 40, 41])
linkJointTypes.extend([p.JOINT_REVOLUTE]*7)
linkJointAxis.extend([[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]])
linkLowerLimits.extend(np.array([np.deg2rad(-127), np.deg2rad(-45), np.deg2rad(-40), 0, np.deg2rad(-35), np.deg2rad(-24), np.deg2rad(-35)]))
linkUpperLimits.extend(np.array([np.deg2rad(30), np.deg2rad(40), np.deg2rad(45), np.deg2rad(130), np.deg2rad(38), np.deg2rad(23), np.deg2rad(43)]))
human = p.createMultiBody(baseMass=0 if static else m*0.1, baseCollisionShapeIndex=chest_c, baseVisualShapeIndex=chest_v, basePosition=chest_p, baseOrientation=[0, 0, 0, 1], linkMasses=linkMasses, linkCollisionShapeIndices=linkCollisionShapeIndices, linkVisualShapeIndices=linkVisualShapeIndices, linkPositions=linkPositions, linkOrientations=linkOrientations, linkInertialFramePositions=linkInertialFramePositions, linkInertialFrameOrientations=linkInertialFrameOrientations, linkParentIndices=linkParentIndices, linkJointTypes=linkJointTypes, linkJointAxis=linkJointAxis, linkLowerLimits=linkLowerLimits, linkUpperLimits=linkUpperLimits, useMaximalCoordinates=False, flags=p.URDF_USE_SELF_COLLISION, physicsClientId=self.id)
# Self collision has been enabled for the person
# For stability: Remove all collisions except between the arms/legs and the other body parts
num_joints = p.getNumJoints(human, physicsClientId=self.id)
for i in range(-1, num_joints):
for j in range(-1, num_joints):
p.setCollisionFilterPair(human, human, i, j, 0, physicsClientId=self.id)
for i in range(3, 10): # Right arm
for j in [-1] + list(range(10, num_joints)):
p.setCollisionFilterPair(human, human, i, j, 1, physicsClientId=self.id)
for i in range(13, 20): # Left arm
for j in list(range(-1, 10)) + list(range(20, num_joints)):
p.setCollisionFilterPair(human, human, i, j, 1, physicsClientId=self.id)
for i in range(28, 35): # Right leg
for j in list(range(-1, 24)) + list(range(35, num_joints)):
p.setCollisionFilterPair(human, human, i, j, 1, physicsClientId=self.id)
vhacd_resolution, (time2 - time1)
)
)
else:
obj_collision_id = p.createCollisionShape(
p.GEOM_MESH, vertices=obj_verts, physicsClientId=conn_id
)
obj_visual_id = p.createVisualShape(
p.GEOM_MESH,
fileName=obj_tmp_fname,
rgbaColor=[1, 0, 0, 1],
specularColor=[1, 0, 0],
physicsClientId=conn_id,
)
obj_body_id = p.createMultiBody(
baseMass=object_mass,
basePosition=obj_center_mass,
baseCollisionShapeIndex=obj_collision_id,
baseVisualShapeIndex=obj_visual_id,
physicsClientId=conn_id,
)
p.changeDynamics(
obj_body_id,
-1,
lateralFriction=object_friction,
restitution=object_restitution,
physicsClientId=conn_id,
)
# simulate for several steps
linkJointAxis=[[0,0,1]]
objects = [p.loadURDF("sphere_small.urdf", [-1.447238,0.040553,-1.375914],[0.744341,-0.356588,-0.508423,0.245575], useMaximalCoordinates=useMaximalCoordinatesEnvObjects)]
objects = [p.loadURDF("sphere_small.urdf", [-1.337466,0.789305,-1.381118],[-0.061649,0.766884,0.468601,-0.434168], useMaximalCoordinates=useMaximalCoordinatesEnvObjects)]
objects = [p.loadURDF("sphere_small.urdf", [4.705443,2.788506,-1.319134],[0.830352,-0.126438,-0.528531,0.123221], useMaximalCoordinates=useMaximalCoordinatesEnvObjects)]
objects = [p.loadURDF("sphere_small.urdf", [4.645979,2.714498,-1.320512],[0.837715,0.056015,-0.458430,-0.291440], useMaximalCoordinates=useMaximalCoordinatesEnvObjects)]
objects = [p.loadURDF("sphere_small.urdf", [4.661952,3.834003,-1.363581],[0.731792,-0.049574,-0.270830,0.623437], useMaximalCoordinates=useMaximalCoordinatesEnvObjects)]
#objects = p.loadSDF("stadium.sdf")
visualShapeId = p.createVisualShape(shapeType=p.GEOM_MESH,fileName="marble_cube.obj", rgbaColor=[1,1,1,1], specularColor=[1,1,1], visualFramePosition=shift, meshScale=meshScale)
collisionShapeId = p.createCollisionShape(shapeType=p.GEOM_MESH, fileName="marble_cube.obj", collisionFramePosition=shift,meshScale=meshScale)
#collisionShapeId = -1
uiCube = p.createMultiBody(baseMass=0,baseInertialFramePosition=[0,0,0],baseCollisionShapeIndex=collisionShapeId, baseVisualShapeIndex = visualShapeId, basePosition = [0,1,0], linkMasses=link_Masses,linkCollisionShapeIndices=linkCollisionShapeIndices,linkVisualShapeIndices=linkVisualShapeIndices,linkPositions=linkPositions,linkOrientations=linkOrientations,linkInertialFramePositions=linkInertialFramePositions, linkInertialFrameOrientations=linkInertialFrameOrientations,linkParentIndices=linkIndices,linkJointTypes=linkJointTypes,linkJointAxis=linkJointAxis, useMaximalCoordinates=False)
p.changeVisualShape(uiCube,0,rgbaColor=[0,0,0,1])
textOrn = p.getQuaternionFromEuler([0,0,-1.5707963])
numLines = 1
lines = [-1]*numLines
p.stepSimulation()
#disable rendering during loading makes it much faster
p.configureDebugVisualizer(p.COV_ENABLE_RENDERING, 0)
p.configureDebugVisualizer(p.COV_ENABLE_TINY_RENDERER, 0)
p.configureDebugVisualizer(p.COV_ENABLE_WIREFRAME, 0)
p.configureDebugVisualizer(p.COV_ENABLE_KEYBOARD_SHORTCUTS, 0)
#p.addUserDebugLine([0,0,0],[1,0,0],[1,0,0],lineWidth=2)
tool = p.loadURDF(os.path.join(self.directory, 'scratcher', 'tool_scratch.urdf'), basePosition=transform_pos, baseOrientation=transform_orient, physicsClientId=self.id)
elif self.task == 'bed_bathing':
tool = p.loadURDF(os.path.join(self.directory, 'bed_bathing', 'wiper.urdf'), basePosition=transform_pos, baseOrientation=transform_orient, physicsClientId=self.id)
elif self.task in ['drinking', 'scooping', 'feeding', 'arm_manipulation']:
if self.task == 'drinking':
visual_filename = os.path.join(self.directory, 'dinnerware', 'plastic_coffee_cup.obj')
collision_filename = os.path.join(self.directory, 'dinnerware', 'plastic_coffee_cup_vhacd.obj')
elif self.task in ['scooping', 'feeding']:
visual_filename = os.path.join(self.directory, 'dinnerware', 'spoon_reduced_compressed.obj')
collision_filename = os.path.join(self.directory, 'dinnerware', 'spoon_vhacd.obj')
elif self.task == 'arm_manipulation':
visual_filename = os.path.join(self.directory, 'arm_manipulation', 'arm_manipulation_scooper.obj')
collision_filename = os.path.join(self.directory, 'arm_manipulation', 'arm_manipulation_scooper_vhacd.obj')
tool_visual = p.createVisualShape(shapeType=p.GEOM_MESH, fileName=visual_filename, meshScale=mesh_scale, rgbaColor=[1, 1, 1, alpha], physicsClientId=self.id)
tool_collision = p.createCollisionShape(shapeType=p.GEOM_MESH, fileName=collision_filename, meshScale=mesh_scale, physicsClientId=self.id)
tool = p.createMultiBody(baseMass=0.01, baseCollisionShapeIndex=tool_collision, baseVisualShapeIndex=tool_visual, basePosition=transform_pos, baseOrientation=transform_orient, useMaximalCoordinates=maximal, physicsClientId=self.id)
if left:
# Disable collisions between the tool and robot
for j in (range(71, 86) if self.robot_type == 'pr2' else [18, 20, 21, 22, 23] if self.robot_type == 'sawyer' else [47, 49, 50, 51, 52] if self.robot_type == 'baxter' else [7, 8, 9, 10, 11, 12, 13, 14]):
for tj in list(range(p.getNumJoints(tool, physicsClientId=self.id))) + [-1]:
p.setCollisionFilterPair(robot, tool, j, tj, False, physicsClientId=self.id)
# Create constraint that keeps the tool in the gripper
constraint = p.createConstraint(robot, 76 if self.robot_type=='pr2' else 18 if self.robot_type=='sawyer' else 47 if self.robot_type=='baxter' else 8, tool, -1, p.JOINT_FIXED, [0, 0, 0], parentFramePosition=pos_offset, childFramePosition=[0, 0, 0], parentFrameOrientation=orient_offset, physicsClientId=self.id)
else:
# Disable collisions between the tool and robot
for j in (range(49, 64) if self.robot_type == 'pr2' else [18, 20, 21, 22, 23] if self.robot_type == 'sawyer' else [25, 27, 28, 29, 30] if self.robot_type == 'baxter' else [7, 8, 9, 10, 11, 12, 13, 14]):
for tj in list(range(p.getNumJoints(tool, physicsClientId=self.id))) + [-1]:
p.setCollisionFilterPair(robot, tool, j, tj, False, physicsClientId=self.id)
# Create constraint that keeps the tool in the gripper
constraint = p.createConstraint(robot, 54 if self.robot_type=='pr2' else 18 if self.robot_type=='sawyer' else 25 if self.robot_type=='baxter' else 8, tool, -1, p.JOINT_FIXED, [0, 0, 0], parentFramePosition=pos_offset, childFramePosition=[0, 0, 0], parentFrameOrientation=orient_offset, physicsClientId=self.id)
p.changeConstraint(constraint, maxForce=500, physicsClientId=self.id)
return tool