"""Panda Hand gripper."""importenumfromtypingimportDict,Sequenceimportnumpyasnpfromdm_controlimportmjcffromdm_control.composer.observationimportobservablefromdm_envimportspecsfromdm_robotics.momaimportsensorfromdm_robotics.moma.effectorsimportdefault_gripper_effectorfromdm_robotics.moma.modelsimporttypesfromdm_robotics.moma.modelsimportutilsasmodels_utilsfromdm_robotics.moma.models.end_effectors.robot_handsimportrobot_handfrom.importgripper_constantsasconstsfrom.importparametersasparams
[docs]classPandaHand(robot_hand.RobotHand):"""MoMa robot hand model of the Franka Hand."""_mjcf_root:mjcf.RootElement_state:consts.STATESdef_build(self,name:str='panda_hand'):self._mjcf_root=mjcf.from_path(consts.XML_PATH)self._mjcf_root.model=nameself._actuators=[self._mjcf_root.find('actuator',j)forjinconsts.ACTUATOR_NAMES]self._joints=[self._mjcf_root.find('joint',j)forjinconsts.JOINT_NAMES]self._tool_center_point=self._mjcf_root.find('site',consts.GRIPPER_SITE_NAME)self._state=consts.STATES.READY@propertydefjoints(self)->Sequence[types.MjcfElement]:"""List of joint elements belonging to the hand."""returnself._joints@propertydefactuators(self)->Sequence[types.MjcfElement]:"""List of actuator elements belonging to the hand."""returnself._actuators@propertydefmjcf_model(self)->types.MjcfElement:"""Returns the `mjcf.RootElement` object corresponding to the robot hand."""returnself._mjcf_root@propertydefname(self)->str:"""Name of the robot hand."""returnself.mjcf_model.model@propertydeftool_center_point(self)->types.MjcfElement:"""Tool center point site of the hand."""returnself._tool_center_point
[docs]definitialize_episode(self,physics:mjcf.Physics,random_state:np.random.RandomState):"""Function called at the beginning of every episode."""delrandom_state# Unused.self._state=consts.STATES.READY
[docs]defset_width(self,physics:mjcf.Physics,width:float):"""Set desired aperture of the gripper."""self.set_joint_positions(physics,[width*0.5]*2)
[docs]defset_joint_positions(self,physics:mjcf.Physics,joint_positions:np.ndarray)->None:"""Sets the joints of the gripper to a given configuration. Changes the joint state as well as the current control signal to the desired joint state. Args: physics: A `mujoco.Physics` instance. joint_positions: The desired joint state for the robot gripper. """physics_joints=models_utils.binding(physics,self._joints)physics_actuators=models_utils.binding(physics,self._actuators)physics_joints.qpos[:]=joint_positionsphysics_actuators.ctrl[:]=joint_positions[0]*2/0.08self._state=consts.STATES.READY
@enum.uniqueclass_PandaHandObservations(enum.Enum):WIDTH='{}_width'STATE='{}_state'defget_obs_key(self,name:str)->str:"""Returns the key to the observation in the observables dict."""returnself.value.format(name)
[docs]classPandaHandSensor(sensor.Sensor):"""Sensor for the Panda gripper. Provides two observations, namely, the current aperture or width between the fingers as well as gripper's state. """
[docs]classPandaHandEffector(default_gripper_effector.DefaultGripperEffector):"""Panda gripper effector. Action space is binary. The gripper's finger will move until blocked and then continuously apply a force either outwards or inwards, corresponding to actions 1 and 0 respectively. Actions between 0 and 1 get rounded."""