
    ni                     d   S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKrSSK	r	SSK
rSSKrSSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  / SQr/ SQr/ SQr/ SQr/ SQrSrSr/ SQr/ SQr\	R@                  " S5      r!\	R@                  " S5      r"\	R@                  " S5      r#\	R@                  " S5      r$\	R@                  " S5      r%\	R@                  " S5      r&\	R@                  " S5      r'Sr(Sr)Sr*S r+S!\RX                  -  r-S!r.S"r/S# r0\Rb                   " S$ S%\25      5       r3g)&zFThis file implements the functionalities of a minitaur using pybullet.    )absolute_import)division)print_functionNminitaur_constants)minitaur_motor)robot_config)safety_checker)safety_error)action_filter)
kinematics)r   r   皙?)r   r      )r   r   r   r   )r   g{Gzt?r   )r   {Gz?r   g@      ?)
front_left	back_leftfront_right
back_right)motor_front_leftL_jointmotor_front_leftR_jointmotor_back_leftL_jointmotor_back_leftR_jointmotor_front_rightL_jointmotor_front_rightR_jointmotor_back_rightL_jointmotor_back_rightR_jointzchassis\D*centerzmotor\D*jointzknee\D*zmotor\D*_bracket_jointzhip\D*jointz
hip\D*linkzmotor\D*link)        r   r   r   r   )r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r         z:robotics/reinforcement_learning/minitaur/robots/data/urdf/c                 H   [         R                  " U 5      n[        [        U 5      5       Hs  n[        R
                  " X   [        5      X'   X   [        R                  :  a  X==   [        -  ss'   MJ  X   [        R                  * :  d  Mc  X==   [        -  ss'   Mu     U$ )zMaps a list of angles to [-pi, pi].

Args:
  angles: A list of angles in rad.

Returns:
  A list of angle mapped to [-pi, pi].
)copydeepcopyrangelenmathfmodTWO_PIpi)anglesmapped_anglesis      `/home/james-whalen/.local/lib/python3.13/site-packages/pybullet_envs/minitaur/robots/minitaur.pyMapToMinusPiToPir/   4   s~     --'-VayyF3M477"& 		TWWH	$&   
    c                      \ rS rSrSr\\\SSS\R                  R                  \R                  SSSS	S	\S\\SSSSSS4S
 jrS rSmS jrS rS rS rS rS rS rS rS rS rS rS rS rSnS jrS r S r!S r"S r#S r$S r%S  r&S! r'S" r(S# r)S$ r*S% r+S& r,S' r-S( r.S) r/S* r0S+ r1S, r2S- r3S. r4S/ r5S0 r6S1 r7S2 r8S3 r9S4 r:S5 r;S6 r<S7 r=S8 r>S9 r?S: r@S; rAS< rBSmS= jrCS> rDS? rES@ rFSA rGSB rHSC rISD rJSE rKSF rLSG rMSH rNSI rOSJ rPSK rQSL rRSM rSSN rTSO rUSP rVSQ rWSR rXSS rYST rZSU r[SV r\SW r]SX r^SY r_SZ r`S[ raS\ rbS] rcS^ rd\eS_ 5       rfS` rgSa rhSb ri\eSc 5       rj\eSd 5       rkSe rlSf rmSg rnSh ro\eSi 5       rp\eSj 5       rq\rSk 5       rsSlrtg)oMinitaurG   zHThe minitaur class that simulates a quadruped robot from Ghost Robotics.r   r   Fr   g{Gz?Nr   c                    X l         U R                   U-  U l        Xl        X`l        X@l        Xpl        UU l        UU l        [        R                  " U R                   5      U l
        [        R                  " U R                   5      U l        SU l        Xl        Xl        Xl        [         R"                  " SS9U l        / U l        S/U l        / U l        / U l        / U l        UU l        UU l        UU l        U R7                  Ub  UO	[9        5       5        UU l        SU l        SU l        UU l         UU l!        SU l"        U	(       d  [G        S5      eU R2                  (       a  U R4                  (       a  [G        S5      e[I        U
[         RJ                  [        RL                  45      (       a  [        RN                  " U
5      U l(        O[        RR                  " X*5      U l(        [I        U[         RJ                  [        RL                  45      (       a  [        RN                  " U5      U l*        O[        RR                  " X+5      U l*        [I        U[         RJ                  [        RL                  45      (       a  [        RN                  " U5      U l+        OUc  SU l+        OXl+        Xl,        U	" U
UU RV                  US	9U l-        XPl.        S
U l/        S
U l0        U R                  Rc                  / SQU Re                  5       S9u  nU l3        U RB                  (       a  U Ri                  5       U l5        U Rm                  SS9  U Ro                  5         g)a  Constructs a minitaur and reset it to the initial states.

Args:
  pybullet_client: The instance of BulletClient to manage different
    simulations.
  num_motors: The number of the motors on the robot.
  dofs_per_leg: The number of degrees of freedom for each leg.
  urdf_root: The path to the urdf folder.
  time_step: The time step of the simulation.
  action_repeat: The number of ApplyAction() for each control step.
  self_collision_enabled: Whether to enable self collision.
  motor_control_mode: Enum. Can either be POSITION, TORQUE, or HYBRID.
  motor_model_class: We can choose from simple pd model to more accureate DC
    motor models.
  motor_kp: proportional gain for the motors.
  motor_kd: derivative gain for the motors.
  motor_torque_limits: Torque limits for the motors. Can be a single float
    or a list of floats specifying different limits for different robots. If
    not provided, the default limit of the robot is used.
  pd_latency: The latency of the observations (in seconds) used to calculate
    PD control. On the real hardware, it is the latency between the
    microcontroller and the motor controller.
  control_latency: The latency of the observations (in second) used to
    calculate action. On the real hardware, it is the latency from the motor
    controller, the microcontroller to the host (Nvidia TX2).
  observation_noise_stdev: The standard deviation of a Gaussian noise model
    for the sensor. It should be an array for separate sensors in the
    following order [motor_angle, motor_velocity, motor_torque,
    base_roll_pitch_yaw, base_angular_velocity]
  motor_overheat_protection: Whether to shutdown the motor that has exerted
    large torque (OVERHEAT_SHUTDOWN_TORQUE) for an extended amount of time
    (OVERHEAT_SHUTDOWN_TIME). See ApplyAction() in minitaur.py for more
    details.
  motor_direction: A list of direction values, either 1 or -1, to compensate
    the axis difference of motors between the simulation and the real robot.
  motor_offset: A list of offset value for the motor angles. This is used to
    compensate the angle difference between the simulation and the real
    robot.
  on_rack: Whether to place the minitaur on rack. This is only used to debug
    the walking gait. In this mode, the minitaur's base is hanged midair so
    that its walking gait is clearer to visualize.
  reset_at_current_position: Whether to reset the minitaur at the current
    position and orientation. This is for simulating the reset behavior in
    the real world.
  sensors: a list of sensors that are attached to the robot.
  safety_config: A SafetyConfig class to configure the safety layer. If
    None, the safety layer will be disabled.
  enable_action_interpolation: Whether to interpolate the current action
    with the previous action in order to produce smoother motions
  enable_action_filter: Boolean specifying if a lowpass filter should be
    used to smooth actions.
g      @d   )maxlenr   NTz!Must provide a motor model class!z@on_rack and reset_at_current_position cannot be enabled together)kpkdtorque_limitsmotor_control_moder   r   r   r   )positionorientationg      )
reset_time)8
num_motorsnum_legs_pybullet_client_action_repeat
_urdf_root_self_collision_enabled_motor_direction_motor_offsetnpzeros_observed_motor_torques_applied_motor_torques
_max_force_pd_latency_control_latency_observation_noise_stdevcollectionsdeque_observation_history_control_observation_chassis_link_ids_leg_link_ids_motor_link_ids_foot_link_ids_motor_overheat_protection_on_rack_reset_at_current_positionSetAllSensorslistsafety_config_is_safe_safety_checker_enable_action_interpolation_enable_action_filter_last_action
ValueError
isinstanceSequencendarrayasarray
_motor_kpsfull
_motor_kds_motor_torque_limits_motor_control_mode_motor_model	time_step_step_counter_state_action_counterinvertTransform_GetDefaultInitOrientation_init_orientation_inv_BuildActionFilter_action_filterResetReceiveObservation)selfpybullet_clientr?   dofs_per_leg	urdf_rootrm   action_repeatself_collision_enabledr:   motor_model_classmotor_kpmotor_kdmotor_torque_limits
pd_latencycontrol_latencyobservation_noise_stdevmotor_overheat_protectionmotor_directionmotor_offseton_rackreset_at_current_positionsensorsr\   enable_action_interpolationenable_action_filter_s                             r.   __init__Minitaur.__init__K   s   Z !OOO|3DM+'O#9 +D%D#%88DOO#<D "$((4??";DDO!+$;! + 1 1 =D "D TDDDD&?D#DM&?D#'"5w46B&DDMD(CD%!5DD:;;}}88 4 5 5 ([112::>??

8,do
5do([112::>??

8,do
5do%(<(<bjj'IJJ"$**-@"Ad		$"&d"51)//-	/D ND "#D$($9$9$I$I(G(G(I %J %K!At! !! 335d 	JJ$J
r0   c                 4    U R                   U R                  -  $ N)rn   rm   rw   s    r.   GetTimeSinceResetMinitaur.GetTimeSinceReset   s    ..r0   c                     U R                  X5        U R                  R                  5         U R                  5         U =R                  S-  sl        g )Nr   )ApplyActionrA   stepSimulationrv   ro   )rw   actionr:   s      r.   _StepInternalMinitaur._StepInternal   sB    V0((*!#
r0   c                     U R                   (       a  U R                  U5      n[        U R                  5       H:  nU R	                  X5      nU R                  U5        U =R                  S-  sl        M<     Xl        g)zSteps simulation.r   N)r`   _FilterActionr%   rB   ProcessActionr   rn   ra   )rw   r   r-   proc_actions       r.   StepMinitaur.Step   sj    !!!!&)f4&&'&&v1k
%
A (
 
r0   c                     g r    r   s    r.   	TerminateMinitaur.Terminate   s    r0   c                     U R                   $ )z#Get list of IDs for all knee links.)_knee_link_idsr   s    r.   GetKneeLinkIDsMinitaur.GetKneeLinkIDs      r0   c                     U R                   $ )z#Get list of IDs for all foot links.)rV   r   s    r.   GetFootLinkIDsMinitaur.GetFootLinkIDs  r   r0   c                    / U l         U R                   HE  nU R                   R                  U R                  R	                  U R
                  U5      S   5        MG     / U l        U R                   HE  nU R                  R                  U R                  R	                  U R
                  U5      S   5        MG     U R                   HE  nU R                  R                  U R                  R	                  U R
                  U5      S   5        MG     g)z0Records the mass information from the URDF file.r   N)	_base_mass_urdfrS   appendrA   getDynamicsInfo	quadruped_leg_masses_urdfrT   rU   )rw   
chassis_idleg_idmotor_ids       r.   _RecordMassInfoFromURDF Minitaur._RecordMassInfoFromURDF  s    D,,

!!



/
/

KA
NP - D$$
""



/
/
G
JL % ((
""



/
/
I!
LN )r0   c                 v   / U l         U R                  R                  U R                  5      n[	        SU5       HG  nU R                  R                  U R                  U5      S   nU R                   R                  U5        MI     U R                   Vs/ s H  o@R                   US-      PM     snU l        U R                   Vs/ s H  oPR                   US-      PM     snU l
        U R                  R                  U R                   Vs/ s H  o`R                   US-      PM     sn5        gs  snf s  snf s  snf )z/Record the inertia of each body from URDF file.r   r!   r   N)
_link_urdfrA   getNumJointsr   r%   r   r   rS   _base_inertia_urdfrT   _leg_inertia_urdfextendrU   )rw   
num_bodiesbody_idinertiar   r   r   s          r.   _RecordInertiaInfoFromURDF#Minitaur._RecordInertiaInfoFromURDF  s   DO&&33DNNCJZ(%%55dnn6=??@Bg
ooW% ) ;?:P:P:PJ
Q':PD 372D2D2D
#2DD 	!!7;7K7KL7K8A	&7KLN 	Ms   D,D1	D6c                    U R                   R                  U R                  5      n0 U l        [	        U5       HM  nU R                   R                  U R                  U5      nUS   U R                  US   R                  S5      '   MO     g )Nr   r   UTF-8)rA   r   r   _joint_name_to_idr%   getJointInfodecoderw   
num_jointsr-   
joint_infos       r.   _BuildJointNameToIdDict Minitaur._BuildJointNameToIdDict,  sr    &&33DNNCJD:((55dnnaHj>HmdZ]11':; r0   c                 F   U R                   R                  U R                  5      nS/U l        / U l        / U l        / U l        / U l        [        U5       GH  nU R                   R                  U R                  U5      nUS   R                  S5      nU R                  U   n[        R                  U5      (       a  U R                  R                  U5        M  [        R                  U5      (       a  U R                  R                  U5        M  [         R                  U5      (       a  U R
                  R                  U5        M  ["        R                  U5      (       a  U R                  R                  U5        GM*  [$        R                  U5      (       d4  [&        R                  U5      (       d  [(        R                  U5      (       a  U R                  R                  U5        GM  [+        SU-  5      e   U R                  R-                  U R                  5        U R                  R/                  5         U R
                  R/                  5         U R                  R/                  5         U R                  R/                  5         U R                  R/                  5         g)znBuild the link Ids from its name in the URDF file.

Raises:
  ValueError: Unknown category of the joint name.
r   r   r   zUnknown category of joint %sN)rA   r   r   rS   rT   rU   rV   _bracket_link_idsr%   r   r   r   _CHASSIS_NAME_PATTERNmatchr   _BRACKET_NAME_PATTERN_MOTOR_NAME_PATTERN_KNEE_NAME_PATTERN_LEG_NAME_PATTERN1_LEG_NAME_PATTERN2_LEG_NAME_PATTERN3rb   r   sort)rw   r   r-   r   
joint_namejoint_ids         r.   _BuildUrdfIdsMinitaur._BuildUrdfIds3  s    &&33DNNCJ TDDDDD:((55dnnaHja=''0j''
3h		$	$Z	0	0%%h/ &&z22%%h/$$Z00##H-##J//""8,$$Z00$$Z00$$Z00!!(+7*DEE# & 	d112!!r0   c                     U R                   R                  U R                  5      n[        U5       HH  nU R                   R	                  U R                  U5      nU R                   R                  US   SSSS9  MJ     g )Nr   r   )linearDampingangularDamping)rA   r   r   r%   r   changeDynamicsr   s       r.   _RemoveDefaultJointDamping#Minitaur._RemoveDefaultJointDampingZ  sn    &&33DNNCJ:((55dnnaHj
**
Q-1Q + @ r0   c                 t    U R                  5        Vs/ s H  nU R                  U   PM     snU l        g s  snf r   )_GetMotorNamesr   _motor_id_list)rw   
motor_names     r.   _BuildMotorIdListMinitaur._BuildMotorIdLista  s=     --//J 	z*/D s   5c                     U R                   R                  U R                  SSSU R                   R                  / SQ/ SQUUS9	nU$ )a=  Create a constraint that keeps the chassis at a fixed frame.

This frame is defined by init_position and init_orientation.

Args:
  init_position: initial position of the fixed frame.
  init_orientation: initial orientation of the fixed frame in quaternion
    format [x,y,z,w].

Returns:
  Return the constraint id.
r   r;   )	parentBodyUniqueIdparentLinkIndexchildBodyUniqueIdchildLinkIndex	jointType	jointAxisparentFramePositionchildFramePositionchildFrameOrientation)rA   createConstraintr   JOINT_FIXED)rw   init_positioninit_orientationfixed_constraints       r.   _CreateRackConstraintMinitaur._CreateRackConstraintg  sS     ,,==>>''33%(. > 	0 r0   c                     g)a<  Whether the observation is valid for the current time step.

In simulation, observations are always valid. In real hardware, it may not
be valid from time to time when communication error happens between the
Nvidia TX2 and the microcontroller.

Returns:
  Whether the observation is valid for the current time step.
Tr   r   s    r.   IsObservationValidMinitaur.IsObservationValid  s     r0   c                 0   U(       a  U R                  5         U R                  (       a3  U R                  U R                  5       U R	                  5       5      U l        U R                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R                  SS9  O}U R                  R                  U R                  U R                  5       U R	                  5       5        U R                  R!                  U R                  / SQ/ SQ5        U R                  SS9  ["        R$                  " U R&                  5      U l        S/U R&                  -  U l        U R,                  R/                  5         SU l        SU l        SU l        SU l        U R8                  (       a  [:        R<                  " U 5      U l        U RA                  X#5        U RB                  (       a  U RE                  5         g)a<  Reset the minitaur to its initial states.

Args:
  reload_urdf: Whether to reload the urdf file. If not, Reset() just place
    the minitaur back to its starting position.
  default_motor_angles: The default motor angles. If it is None, minitaur
    will hold a default pose (motor angle math.pi / 2) for 100 steps. In
    torque control mode, the phase of holding the default pose is skipped.
  reset_time: The duration (in seconds) to hold the default motor angles. If
    reset_time <= 0 or in torque control mode, the phase of holding the
    default pose is skipped.
T)add_constraintr;   Fr   N)#_LoadRobotURDFrX   r   _GetDefaultInitPositionrq   rack_constraintr   r   r   r   r   r   	ResetPoserA   resetBasePositionAndOrientationr   resetBaseVelocityrG   rH   r?   _overheat_counter_motor_enabled_listrQ   clearrn   ro   r]   ra   r\   r
   SafetyCheckerr^   _SettleDownForResetr`   _ResetActionFilter)rw   reload_urdfdefault_motor_anglesr>   s       r.   ru   Minitaur.Reset  s    
	&&t'C'C'E'+'F'F'HJ 	 ""$

%%'

""$
%%'
nnDn)
;;
..$668

)
)
+- --dnni.79
nnEn*XXdoo6D $v7D##%D!"DDMD +99$?d1>!!

r0   c                 d   U R                  5       nU R                  (       aR  U R                  R                  UU R	                  5       U R                  5       U R                  R                  S9U l        gU R                  R                  XR	                  5       U R                  5       5      U l        g)z"Loads the URDF file for the robot.)flagsN)GetURDFFilerD   rA   loadURDFr   rq   URDF_USE_SELF_COLLISIONr   )rw   	urdf_files     r.   r   Minitaur._LoadRobotURDF  s      "I##,,55


&
&
(

)
)
+%%==	 6 ?dn ,,55
113

)
)
+-dnr0   c                    US::  a  gU R                  5         [        S5       H]  nU R                  [        R                  S-  /U R
                  -  [        R                  R                  S9  U R                  (       a  M]    g   Uc  g[        X R                  -  5      n[        U5       H>  nU R                  U[        R                  R                  S9  U R                  (       a  M>    g   g)a(  Sets the default motor angles and waits for the robot to settle down.

The reset is skipped is reset_time is less than zereo.

Args:
  default_motor_angles: A list of motor angles that the robot will achieve
    at the end of the reset phase.
  reset_time: The time duration for the reset phase.
r   Nr5   r!   )r:   )rv   r%   r   r'   r*   r?   r	   MotorControlModePOSITIONr]   intrm   )rw   r
  r>   r   num_steps_to_resets        r.   r  Minitaur._SettleDownForReset  s     Q 	3Z
77Q;-$//
))::CC  E ]]]  #Z..89%&

)::CC  E ]]] 'r0   c                 x    U R                   R                  U R                  UU R                   R                  US9  g )N)	bodyIndex
jointIndexcontrolModeforce)rA   setJointMotorControl2r   TORQUE_CONTROL)rw   r   torques      r.   _SetMotorTorqueByIdMinitaur._SetMotorTorqueById  s7    //..))88	 0 r0   c                 x    U R                   R                  U R                  UU R                   R                  US9  g )N)r  jointIndicesr  forces)rA   setJointMotorControlArrayr   r  )rw   	motor_idstorquess      r.   _SetMotorTorqueByIdsMinitaur._SetMotorTorqueByIds  s7    33..))88	 4 r0   c                 B    U R                  U R                  U   U5        g r   )_SetDesiredMotorAngleByIdr   )rw   r   desired_angles      r.   _SetDesiredMotorAngleByName$Minitaur._SetDesiredMotorAngleByName  s     ""4#9#9*#E#02r0   c                      SU R                   -  $ )Nz%s/quadruped/minitaur.urdf)rC   r   s    r.   r  Minitaur.GetURDFFile  s    '$//99r0   c                 ^    [        U R                  5       H  nU R                  X!5        M     g)zpReset the pose of the minitaur.

Args:
  add_constraint: Whether to add a constraint at the joints of two feet.
N)r%   r@   _ResetPoseForLeg)rw   r   r-   s      r.   r   Minitaur.ResetPose  s%     4==!
A. "r0   c           
      "   Sn[         R                  S-  nSn[        U   nU R                  R	                  U R
                  U R                  SU-   S-      U R                  SU-     U-  SS9  U R                  R	                  U R
                  U R                  SU-   S	-      U R                  SU-     U-  SS9  U R                  R	                  U R
                  U R                  SU-   S
-      U R                  SU-  S-      U-  SS9  U R                  R	                  U R
                  U R                  SU-   S-      U R                  SU-  S-      U-  SS9  U(       az  U R                  R                  U R
                  U R                  SU-   S-      U R
                  U R                  SU-   S	-      U R                  R                  / SQ[        [        5        U R                  R                  U R
                  U R                  SU-   S-      U R                  R                  SUS9  U R                  R                  U R
                  U R                  SU-   S
-      U R                  R                  SUS9  U R                  R                  U R
                  U R                  SU-   S	-      U R                  R                  SUS9  U R                  R                  U R
                  U R                  SU-   S-      U R                  R                  SUS9  g)zReset the initial pose for the leg.

Args:
  leg_id: It should be 0, 1, 2, or 3, which represents the leg at
    front_left, back_left, front_right and back_right.
  add_constraint: Whether to add a constraint at the joints of two feet.
r   g       @gPkwmotor_L_jointr!   )targetVelocityknee_L_linkR_jointr   R_linkr;   r  r  r  r8  r  N)r'   r*   LEG_POSITIONrA   resetJointStater   r   rE   r   JOINT_POINT2POINTKNEE_CONSTRAINT_POINT_RIGHTKNEE_CONSTRAINT_POINT_LEFTr  VELOCITY_CONTROL)rw   r   r   knee_friction_forcehalf_pi
knee_angleleg_positions          r.   r3  Minitaur._ResetPoseForLeg  s1    ggmGJ'L))x,6BCa&j)G3	 * 
 	))w5@Aa&j)J6	 * 
 	))x,6BCa&j1n-7	 * 
 	))w5@Aa&j1n-
:	 * 
 
,,
..

 
 <!7(!B
C
..

 
 <!7(!B
C



1
19
%'AC 	//..**8l+B+4,5 6))::! 0 # 	//..**8l+B+4,5 6))::! 0 # 	//..**7\+AH+LM))::! 0 # 	//..**7\+AH+LM))::! 0 #r0   c                     U R                   $ )zRGet the position of minitaur's base.

Returns:
  The position of minitaur's base.
)_base_positionr   s    r.   GetBasePositionMinitaur.GetBasePositionV  s     r0   c                 T    U R                   R                  U R                  5      u  pU$ )zYGet the linear velocity of minitaur's base.

Returns:
  The velocity of minitaur's base.
)rA   getBaseVelocityr   )rw   velocityr   s      r.   GetBaseVelocityMinitaur.GetBaseVelocity^  s%     ''77GKHOr0   c                     U R                  5       nU R                  R                  U5      n[        R                  " U5      $ )zGet minitaur's base orientation in euler angle in the world frame.

Returns:
  A tuple (roll, pitch, yaw) of the base in world frame.
)GetTrueBaseOrientationrA   getEulerFromQuaternionrG   rf   )rw   r=   roll_pitch_yaws      r.   GetTrueBaseRollPitchYaw Minitaur.GetTrueBaseRollPitchYawg  s7     --/K**AA+NN::n%%r0   c                 "   [         R                  " U R                  SU R                  -  SU R                  -  S-    5      nU R                  R                  U5      nU R                  [         R                  " U5      U R                  S   5      nU$ )zGet minitaur's base orientation in euler angle in the world frame.

This function mimicks the noisy sensor reading and adds latency.
Returns:
  A tuple (roll, pitch, yaw) of the base in world frame polluted by noise
  and latency.
      )rG   arrayrR   r?   rA   rT  _AddSensorNoiserN   )rw   delayed_orientationdelayed_roll_pitch_yawrU  s       r.   GetBaseRollPitchYawMinitaur.GetBaseRollPitchYawq  s     ((!!!doo"5a$//6IA6MNP!22II))
'($*G*G*JLNr0   c                     [        S5      e)z?Get the hip joint positions of the robot within its base frame.zNot implemented for Minitaur.)NotImplementedErrorr   s    r.   GetHipPositionsInBaseFrame#Minitaur.GetHipPositionsInBaseFrame  s    
=
>>r0   c                    [        U R                  5      U R                  :X  d   eU R                  U   nU R                  U R                  -  n[	        X-  X-  U-   5       Vs/ s H  ofPM     nn[
        R                  " U UUUUS9n[        R                  " [        R                  " U5      [        R                  " U R                  5      U   -
  U R                  U   5      nXxR                  5       4$ s  snf )z=Calculate the joint positions from the end effector position.)robotlink_positionlink_id	joint_idsposition_in_world_frame)r&   rV   r@   r?   r%   r   joint_angles_from_link_positionrG   multiplyrf   rF   rE   tolist)	rw   r   r<   rj  toe_idmotors_per_legr-   joint_position_idxsjoint_angless	            r.   _EndEffectorIKMinitaur._EndEffectorIK  s    t""#t}}444  (F__5N0&2I'3( ))a )   ==% 79L ;;


< 


4%%&':;	<124L  3 3 555#s   #C5c                 "    U R                  XSS9$ )a_  Use IK to compute the motor angles, given the foot link's position.

Args:
  leg_id: The leg index.
  foot_world_position: The foot link's position in the world frame.

Returns:
  A tuple. The position indices and the angles for all joints along the
  leg. The position indices is consistent with the joint orders as returned
  by GetMotorAngles API.
Trj  rr  )rw   r   foot_world_positions      r.   'ComputeMotorAnglesFromFootWorldPosition0Minitaur.ComputeMotorAnglesFromFootWorldPosition  s#     T  C Cr0   c                 "    U R                  XSS9$ )ad  Use IK to compute the motor angles, given the foot link's local position.

Args:
  leg_id: The leg index.
  foot_local_position: The foot link's position in the base frame.

Returns:
  A tuple. The position indices and the angles for all joints along the
  leg. The position indices is consistent with the joint orders as returned
  by GetMotorAngles API.
Fru  rv  )rw   r   foot_local_positions      r.   'ComputeMotorAnglesFromFootLocalPosition0Minitaur.ComputeMotorAnglesFromFootLocalPosition  s#     U  D Dr0   c                     [        U R                  5      U R                  :X  d   e[        R                  " U U R                  U   S9$ )z%Compute the Jacobian for a given leg.rf  rh  )r&   rV   r@   r   compute_jacobian)rw   r   s     r.   ComputeJacobianMinitaur.ComputeJacobian  sE     t""#t}}444&&##F+ r0   c                     U R                  U5      n[        R                  " X#5      n0 nU R                  U R                  -  nSn[        X-  US-   U-  5       H  nUXx-      U R                  U   -  XX'   M     U$ )z5Maps the foot contact force to the leg joint torques.   r   )r  rG   matmulr?   r@   r%   rE   )	rw   r   contact_forcejvall_motor_torquesmotor_torquesro  com_dofr   s	            r.   MapContactForceToJointTorques&Minitaur.MapContactForceToJointTorques  s    			f	%B		-4M__5NG&1!A:79 1

! $ 5 5h ?!@m9
 r0   c           
      z   / n[        [        S-  5       H  nU R                  US-     nU R                  US-  S-      n[        U R                  R                  SU R                  SUS95      n[        U R                  R                  SU R                  SUS95      nUR                  U=(       d    U5        M     U$ )zGet minitaur's foot contact situation with the ground.

Returns:
  A list of 4 booleans. The ith boolean is True if leg i is in contact with
  ground.
r!   r   r   r   )bodyAbodyB
linkIndexA
linkIndexB)r%   MINITAUR_NUM_MOTORSrV   boolrA   getContactPointsr   r   )rw   contactsleg_idx	link_id_1	link_id_2	contact_1	contact_2s          r.   GetFootContactsMinitaur.GetFootContacts  s     H,12%%gk2i%%gkAo6i



0
0NN"	 1 $%i 



0
0NN"	 1 $%i ooi,9- 3  Or0   c                     [        U R                  5      U R                  :X  d   e/ nU R                  5        H'  nUR	                  [
        R                  " U US95        M)     [        R                  " U5      $ z0Get the robot's foot position in the base frame.r  )	r&   rV   r@   r   r   r   link_position_in_world_framerG   r[  rw   foot_positionsfoot_ids      r.   GetFootPositionsInWorldFrame%Minitaur.GetFootPositionsInWorldFrame  sj    t""#t}}444N&&(

1
1 ) 88N##r0   c                     [        U R                  5      U R                  :X  d   e/ nU R                  5        H'  nUR	                  [
        R                  " U US95        M)     [        R                  " U5      $ r  )	r&   rV   r@   r   r   r   link_position_in_base_framerG   r[  r  s      r.   GetFootPositionsInBaseFrame$Minitaur.GetFootPositionsInBaseFrame  sj    t""#t}}444N&&(

0
0 ) 88N##r0   c                     U R                    Vs/ s H  oS   PM	     nn[        R                  " [        R                  " U5      [        R                  " U R                  5      -
  U R
                  5      nU$ s  snf )zwGets the eight motor angles at the current moment, mapped to [-pi, pi].

Returns:
  Motor angles, mapped to [-pi, pi].
r   )_joint_statesrG   rl  rf   rF   rE   )rw   statemotor_angless      r.   GetTrueMotorAnglesMinitaur.GetTrueMotorAngles  sf     +/*<*<=*<!H*<L=;;


< 2::d.@.@#AAL 	 >s   A7c                     U R                  [        R                  " U R                  SU R                   5      U R
                  S   5      n[        U5      $ )zGets the eight motor angles.

This function mimicks the noisy sensor reading and adds latency. The motor
angles that are delayed, noise polluted, and mapped to [-pi, pi].

Returns:
  Motor angles polluted by noise and latency, mapped to [-pi, pi].
r   )r\  rG   r[  rR   r?   rN   r/   )rw   r  s     r.   GetMotorAnglesMinitaur.GetMotorAngles  sL     ''
**1T__=>%%a(*L L))r0   c                     U R                    Vs/ s H  oS   PM	     nn[        R                  " X R                  5      nU$ s  snf )zRGet the velocity of all eight motors.

Returns:
  Velocities of all eight motors.
r   )r  rG   rl  rE   )rw   r  motor_velocitiess      r.   GetTrueMotorVelocitiesMinitaur.GetTrueMotorVelocities!  sE     /3.@.@A.@Ua.@A{{#35J5JK Bs   Ac                     U R                  [        R                  " U R                  U R                  SU R                  -   5      U R
                  S   5      $ )zGet the velocity of all eight motors.

This function mimicks the noisy sensor reading and adds latency.
Returns:
  Velocities of all eight motors polluted by noise and latency.
r!   r   r\  rG   r[  rR   r?   rN   r   s    r.   GetMotorVelocitiesMinitaur.GetMotorVelocities,  sR     
**4??1+/??<; < 	=%%a(* *r0   c                     U R                   $ )zaGet the amount of torque the motors are exerting.

Returns:
  Motor torques of all eight motors.
)rI   r   s    r.   GetTrueMotorTorquesMinitaur.GetTrueMotorTorques8  s     '''r0   c                     U R                  [        R                  " U R                  SU R                  -  SU R                  -   5      U R
                  S   5      $ )zGet the amount of torque the motors are exerting.

This function mimicks the noisy sensor reading and adds latency.
Returns:
  Motor torques of all eight motors polluted by noise and latency.
r!   rY  r  r   s    r.   GetMotorTorquesMinitaur.GetMotorTorques@  sX     
**1t+>q+/??@; < 	=%%a(* *r0   c                     [         R                  " [         R                  " U R                  5       U R	                  5       5      5      U R
                  -  U R                  -  $ )zGet the amount of energy used in last one time step.

Returns:
  Energy Consumption based on motor velocities and torques (Nm^2/s).
)rG   absdotr  r  rm   rB   r   s    r.   "GetEnergyConsumptionPerControlStep+Minitaur.GetEnergyConsumptionPerControlStepL  sT     66"&&!# $&*nn57;7J7JK Kr0   c                     U R                   $ )zsGet the orientation of minitaur's base, represented as quaternion.

Returns:
  The orientation of minitaur's base.
)_base_orientationr   s    r.   rS  Minitaur.GetTrueBaseOrientationV  s     !!!r0   c                 T    U R                   R                  U R                  5       5      $ )zGet the orientation of minitaur's base, represented as quaternion.

This function mimicks the noisy sensor reading and adds latency.
Returns:
  The orientation of minitaur's base polluted by noise and latency.
)rA   getQuaternionFromEulerr_  r   s    r.   GetBaseOrientationMinitaur.GetBaseOrientation^  s)       77  "$ $r0   c                     U R                   R                  U R                  5      S   nU R                  5       nU R	                  UU5      $ )zGet the rate of orientation change of the minitaur's base in euler angle.

Returns:
  rate of (roll, pitch, yaw) change of the minitaur's base.
r   )rA   rN  r   rS  $TransformAngularVelocityToLocalFrame)rw   angular_velocityr=   s      r.   GetTrueBaseRollPitchYawRate$Minitaur.GetTrueBaseRollPitchYawRateh  sM     ,,<<T^^LQO--/K445E5@B Br0   c           	          U R                   R                  / SQU5      u  p4U R                   R                  / SQXAU R                   R                  / SQ5      5      u  pS[        R
                  " U5      $ )a  Transform the angular velocity from world frame to robot's frame.

Args:
  angular_velocity: Angular velocity of the robot in world frame.
  orientation: Orientation of the robot represented as a quaternion.

Returns:
  angular velocity of based on the given orientation.
r;   )rA   rp   multiplyTransformsr  rG   rf   )rw   r  r=   r   orientation_inversedrelative_velocitys         r.   r  -Minitaur.TransformAngularVelocityToLocalFrames  sl     #33CCIDOQA  00CC'44Y?A ::'((r0   c                     U R                  [        R                  " U R                  SU R                  -  S-   SU R                  -  S-    5      U R
                  S   5      $ )zGet the rate of orientation change of the minitaur's base in euler angle.

This function mimicks the noisy sensor reading and adds latency.
Returns:
  rate of (roll, pitch, yaw) change of the minitaur's base polluted by noise
  and latency.
rY  rZ     r  r   s    r.   GetBaseRollPitchYawRate Minitaur.GetBaseRollPitchYawRate  sd     
**1t+>+,,--.-@1-DF 	G%%a(* *r0   c                     U R                   $ )zNGet the length of the action list.

Returns:
  The length of the action list.
)r?   r   s    r.   GetActionDimensionMinitaur.GetActionDimension       ??r0   c                 B   U R                   (       a  [        U R                  5       Ht  n[        X   5      [        :  a  U R
                  U==   S-  ss'   OSU R
                  U'   U R
                  U   [        U R                  -  :  d  Me  SU R                  U'   Mv     g g )Nr   r   F)	rW   r%   r?   r  OVERHEAT_SHUTDOWN_TORQUEr  OVERHEAT_SHUTDOWN_TIMErm   r  )rw   actual_torquer-   s      r.   _ApplyOverheatProtection!Minitaur._ApplyOverheatProtection  s    &&T__%!} #;;

 
 
#q
(
#&'$
 
 
#""1%"T^^34(-$
"
"1
% & 'r0   c                    U R                   U R                  -  U l        UnUc  U R                  nU R                  (       a   U R                  R                  X5        [        R                  " U5      nU R                  5       u  pVU R                  5       nU R                  R!                  XXgU5      u  pU R#                  U5        Xl        [        R&                  " UU R(                  5      U l        / n
/ n[-        U R.                  U R*                  U R0                  5       HS  u  pnU(       a$  U
R3                  U5        UR3                  U5        M1  U
R3                  U5        UR3                  S5        MU     U R5                  X5        g! [        R                   a(  n[        R                  " SU5        SU l
         SnAgSnAff = f)zApply the motor commands using the motor model.

Args:
  motor_commands: np.array. Can be motor angles, torques, hybrid commands,
    or motor pwms (for Minitaur only).
  motor_control_mode: A MotorControlMode enum.
NA safety error occurred: %sFr   )ro   rm   last_action_timerk   r^   check_motor_actionr   SafetyErrorlogginginfor]   rG   rf   _GetPDObservationr  rl   convert_to_torquer  rI   rl  rE   _applied_motor_torquezipr   r  r   r)  )rw   motor_commandsr:   control_modeeqqdot	qdot_truer  observed_torquer'  r  r   motor_torquemotor_enableds                  r.   r   Minitaur.ApplyAction  s    !66GD%L--l//M
 ZZ/N$$&GA++-I%)%6%6%H%H4L&:"M 	!!-0 $3  "$]-1-B-B"DDIM14T5H5H595O5O595M5M2O- 
"\*"Q2O 	i7C %% 2A6s   F	 	GG  Gc                 <   [         R                  " U5      nSnSnU R                  S-  n[        R                  S-  n[        U R                  5       HF  nUS-  nU* U-  XU-      U-   -  n	SU-  U-  X   -  n
Xu:  a  U
* n
[        R                  U	-   U
-   X''   MH     U$ )zConvert the actions that use leg model to the real motor actions.

Args:
  actions: The theta, phi of the leg model.

Returns:
  The eight desired motor angles that can be used in ApplyActions().
r   g      ?r!   rZ  r   )r#   r$   r?   r'   r*   r%   )rw   actionsmotor_anglescale_for_singularityoffset_for_singularityhalf_num_motors	quater_pir-   
action_idxforward_backward_componentextension_components              r.   ConvertFromLegModelMinitaur.ConvertFromLegModel  s     --(K oo*O!I4??#6j 
 9
,/03IIK !  !Gi/'2EE	
	22
''.
.1D
D n $ r0   c                     U R                   $ )z,Get the mass of the base from the URDF file.)r   r   s    r.   GetBaseMassesFromURDFMinitaur.GetBaseMassesFromURDF  s    r0   c                     U R                   $ )z/Get the inertia of the base from the URDF file.)r   r   s    r.   GetBaseInertiasFromURDF Minitaur.GetBaseInertiasFromURDF  s    """r0   c                     U R                   $ )z,Get the mass of the legs from the URDF file.)r   r   s    r.   GetLegMassesFromURDFMinitaur.GetLegMassesFromURDF         r0   c                     U R                   $ )z/Get the inertia of the legs from the URDF file.)r   r   s    r.   GetLegInertiasFromURDFMinitaur.GetLegInertiasFromURDF      !!!r0   c                 <   [        U5      [        U R                  5      :w  a7  [        SR                  [        U5      [        U R                  5      5      5      e[	        U R                  U5       H)  u  p#U R
                  R                  U R                  X#S9  M+     g)a;  Set the mass of minitaur's base.

Args:
  base_mass: A list of masses of each body link in CHASIS_LINK_IDS. The
    length of this list should be the same as the length of CHASIS_LINK_IDS.

Raises:
  ValueError: It is raised when the length of base_mass is not the same as
    the length of self._chassis_link_ids.
zJThe length of base_mass {} and self._chassis_link_ids {} are not the same.massN)r&   rS   rb   formatr  rA   r   r   )rw   	base_massr   chassis_masss       r.   SetBaseMassesMinitaur.SetBaseMasses  s     9~T3344fS^S1G1G-HIK K %((>(>	$J 

**
..* + 9 %Kr0   c                    [        U5      [        U R                  5      [        U R                  5      -   :w  a  [        S5      e[	        U R                  U5       H)  u  p#U R
                  R                  U R                  X#S9  M+     U[        U R                  5      S n[	        U R                  U5       H)  u  pVU R
                  R                  U R                  XVS9  M+     g)a  Set the mass of the legs.

A leg includes leg_link and motor. 4 legs contain 16 links (4 links each)
and 8 motors. First 16 numbers correspond to link masses, last 8 correspond
to motor masses (24 total).

Args:
  leg_masses: The leg and motor masses for all the leg links and motors.

Raises:
  ValueError: It is raised when the length of masses is not equal to number
    of links + motors.
^The number of values passed to SetLegMasses are different than number of leg links and motors.r  N)r&   rT   rU   rb   r  rA   r   r   )rw   
leg_massesr   leg_massmotor_massesrh  
motor_masss          r.   SetLegMassesMinitaur.SetLegMasses  s     :#d001C8L8L4MMM H I I 2 2J?
**
..& + 1 @ c$"4"4567L"4#7#7F
**
..' + 4  Gr0   c                    [        U5      [        U R                  5      :w  a7  [        SR                  [        U5      [        U R                  5      5      5      e[	        U R                  U5       Hi  u  p#U H:  n[
        R                  " U5      S:  R                  5       (       d  M1  [        S5      e   U R                  R                  U R                  X#S9  Mk     g)a|  Set the inertias of minitaur's base.

Args:
  base_inertias: A list of inertias of each body link in CHASIS_LINK_IDS.
    The length of this list should be the same as the length of
    CHASIS_LINK_IDS.

Raises:
  ValueError: It is raised when the length of base_inertias is not the same
    as the length of self._chassis_link_ids and base_inertias contains
    negative values.
zNThe length of base_inertias {} and self._chassis_link_ids {} are not the same.r   0Values in inertia matrix should be non-negative.localInertiaDiagonalN)r&   rS   rb   r  r  rG   rf   anyrA   r   r   )rw   base_inertiasr   chassis_inertiainertia_values        r.   SetBaseInertiasMinitaur.SetBaseInertias1  s     =S!7!788 &- #d&<&<"=?@ @ (+4+A+A+8(:#
*-JJ}%)..00MN
N + **
..* + L(:r0   c                    [        U5      [        U R                  5      [        U R                  5      -   :w  a  [        S5      e[	        U R                  U5       Hi  u  p#U H:  n[
        R                  " U5      S:  R                  5       (       d  M1  [        S5      e   U R                  R                  U R                  X#S9  Mk     U[        U R                  5      S n[	        U R                  U5       Hi  u  pgU H:  n[
        R                  " U5      S:  R                  5       (       d  M1  [        S5      e   U R                  R                  U R                  XgS9  Mk     g)a  Set the inertias of the legs.

A leg includes leg_link and motor. 4 legs contain 16 links (4 links each)
and 8 motors. First 16 numbers correspond to link inertia, last 8 correspond
to motor inertia (24 total).

Args:
  leg_inertias: The leg and motor inertias for all the leg links and motors.

Raises:
  ValueError: It is raised when the length of inertias is not equal to
  the number of links + motors or leg_inertias contains negative values.
r  r   r$  r%  N)r&   rT   rU   rb   r  rG   rf   r'  rA   r   r   )rw   leg_inertiasr   leg_inertiar*  motor_inertiasrh  motor_inertias           r.   SetLegInertiasMinitaur.SetLegInertiasK  sA    <C 2 23c$:N:N6OOO H I I"4#5#5|D'-JJ}%)..00MN
N ( **
..& + D	  E "#d&8&8"9":;N"%d&:&:N"K)-JJ}%)..00MN
N * **
..' + G	 #Lr0   c                 r    U R                    H'  nU R                  R                  U R                  X!S9  M)     g)zSet the lateral friction of the feet.

Args:
  foot_friction: The lateral friction coefficient of the foot. This value is
    shared by all four feet.
)lateralFrictionNrV   rA   r   r   )rw   foot_frictionrh  s      r.   SetFootFrictionMinitaur.SetFootFrictionl  s7     &&
**
..' + B 'r0   c                 r    U R                    H'  nU R                  R                  U R                  X!S9  M)     g)zSet the coefficient of restitution at the feet.

Args:
  foot_restitution: The coefficient of restitution (bounciness) of the feet.
    This value is shared by all four feet.
)restitutionNr6  )rw   foot_restitutionrh  s      r.   SetFootRestitutionMinitaur.SetFootRestitutionx  s7     &&
**
..' + A 'r0   c           	          [        U R                  U5       H@  u  p#U R                  R                  U R                  UU R                  R
                  SUS9  MB     g )Nr   r=  )r  rV   rA   r  r   rC  )rw   joint_frictionsknee_joint_idfrictions       r.   SetJointFrictionMinitaur.SetJointFriction  sU    #&t':':O#L
11NN"++<< 2  $Mr0   c                 ,    [        U R                  5      $ r   )r&   rV   r   s    r.   GetNumKneeJointsMinitaur.GetNumKneeJoints  s    t""##r0   c                 :    U R                   R                  U5        g r   )rl   set_voltage)rw   voltages     r.   SetBatteryVoltageMinitaur.SetBatteryVoltage  s    !!'*r0   c                 :    U R                   R                  U5        g r   )rl   set_viscous_damping)rw   viscous_dampings     r.   SetMotorViscousDampingMinitaur.SetMotorViscousDamping  s    ))/:r0   c                 @   / nUR                  U R                  5       5        UR                  U R                  5       5        UR                  U R                  5       5        UR                  U R	                  5       5        UR                  U R                  5       5        U$ r   )r   r  r  r  rS  r  )rw   observations     r.   GetTrueObservationMinitaur.GetTrueObservation  s    Kt..01t2245t//12t2245t779:r0   c                    U R                   R                  U R                  U R                  5      U l        U R                   R                  U R                  5      u  U l        nU R                   R                  / SQU/ SQU R                  S9u  o l	        U R                  R                  U R                  5       5        U R                  5       U l        U R                  U R                   -  U l        U R$                  (       a   U R$                  R'                  5         gg! [(        R*                   a(  n[,        R.                  " SU5        SU l         SnAgSnAff = f)zReceive the observation from sensors.

This function is called once per step. The observations are only updated
when this function is called.
r;   )	positionAorientationA	positionBorientationBr  FN)rA   getJointStatesr   r   r  getBasePositionAndOrientationrJ  r  rr   r  rQ   
appendleftrT  _GetControlObservationrR   ro   rm   last_state_timer^   check_stater   r  r  r  r]   )rw   r=   r   r  s       r.   rv   Minitaur.ReceiveObservation  s$    ..==++-D 	;;DNNK %D !% 5 5 H H //	 !I !1A
 	(()@)@)BC $ ; ; =D55FD((*  %% 2A6s   D E2EEc                    US::  d  [        U R                  5      S:X  a  U R                  S   nU$ [        XR                  -  5      nUS-   [        U R                  5      :  a  U R                  S   $ XU R                  -  -
  nX@R                  -  nSU-
  [        R
                  " U R                  U   5      -  U[        R
                  " U R                  US-      5      -  -   nU$ )zGet observation that is delayed by the amount specified in latency.

Args:
  latency: The latency (in seconds) of the delayed observation.

Returns:
  observation: The observation which was actually latency seconds ago.
r   r   r   r   )r&   rQ   r  rm   rG   r[  )rw   latencyrS  n_steps_agoremaining_latencyblend_alphas         r.   _GetDelayedObservationMinitaur._GetDelayedObservation  s     !|s4445:--a0k  ..01k	qC 9 9:	:((,,!$..$@@%6k)B)B;)O P
P"((4#<#<[1_#MNNO  r0   c                     U R                  U R                  5      nUSU R                   nXR                  SU R                  -   n[        R                  " U5      [        R                  " U5      4$ )Nr   r!   )rg  rL   r?   rG   r[  )rw   pd_delayed_observationr  r  s       r.   r  Minitaur._GetPDObservation  s[    !889I9IJq1A!//!doo2EFDHHQK$((r0   c                 <    U R                  U R                  5      nU$ r   )rg  rM   )rw   control_delayed_observations     r.   r^  Minitaur._GetControlObservation  s"    "&"="=#&&r0   c                 j    US::  a  U$ U[         R                  R                  X!R                  S9-   nU$ )Nr   )scalesize)rG   randomnormalshape)rw   sensor_valuesnoise_stdevrS  s       r.   r\  Minitaur._AddSensorNoise  s?    a"))"2"2 3 3 #3 #5 5Kr0   c                     Xl         g)zSet the latency of the control loop.

It measures the duration between sending an action from Nvidia TX2 and
receiving the observation from microcontroller.

Args:
  latency: The latency (in seconds) of the control loop.
NrM   )rw   rc  s     r.   SetControlLatencyMinitaur.SetControlLatency  s
     $r0   c                     U R                   $ )zGet the control latency.

Returns:
  The latency (in seconds) between when the motor command is sent and when
    the sensor measurements are reported back to the controller.
ry  r   s    r.   GetControlLatencyMinitaur.GetControlLatency  s        r0   c                    [        U[        R                  [        R                  45      (       a  [        R
                  " U5      U l        O&[        R                  " U R                  U5      U l        [        U[        R                  [        R                  45      (       a  [        R
                  " U5      U l	        O&[        R                  " U R                  U5      U l	        U R                  R                  X5        g)zSet the gains of all motors.

These gains are PD gains for motor positional control. kp is the
proportional gain and kd is the derivative gain.

Args:
  kp: proportional gain(s) of the motors.
  kd: derivative gain(s) of the motors.
N)rc   rO   rd   rG   re   rf   rg   rh   r?   ri   rl   set_motor_gains)rw   r7   r8   s      r.   SetMotorGainsMinitaur.SetMotorGains  s     "{++RZZ899

2do4do"{++RZZ899

2do4do%%b-r0   c                 2    U R                   U R                  4$ )zVGet the gains of the motor.

Returns:
  The proportional gain.
  The derivative gain.
)rg   ri   r   s    r.   GetMotorGainsMinitaur.GetMotorGains  s     ??DOO++r0   c                     U R                   $ )zHGet the position gains of the motor.

Returns:
  The proportional gain.
)rg   r   s    r.   GetMotorPositionGainsMinitaur.GetMotorPositionGains  r  r0   c                     U R                   $ )zFGet the velocity gains of the motor.

Returns:
  The derivative gain.
)ri   r   s    r.   GetMotorVelocityGainsMinitaur.GetMotorVelocityGains  r  r0   c                 V    U R                   R                  U/U R                  -  5        g)zSet the strength of all motors relative to the default value.

Args:
  ratio: The relative strength. A scalar range from 0.0 to 1.0.
N)rl   set_strength_ratiosr?   )rw   ratios     r.   SetMotorStrengthRatioMinitaur.SetMotorStrengthRatio&  s#     	))5'DOO*CDr0   c                 :    U R                   R                  U5        g)zSet the strength of each motor relative to the default value.

Args:
  ratios: The relative strength. A numpy array ranging from 0.0 to 1.0.
N)rl   r  )rw   ratioss     r.   SetMotorStrengthRatiosMinitaur.SetMotorStrengthRatios.  s     	))&1r0   c                     X l         Xl        g)zSet the time steps of the control and simulation.

Args:
  action_repeat: The number of simulation steps that the same action is
    repeated.
  simulation_step: The simulation time step.
N)rm   rB   )rw   r{   simulation_steps      r.   SetTimeStepsMinitaur.SetTimeSteps6  s     %N'r0   c                     [         $ r   )MOTOR_NAMESr   s    r.   r   Minitaur._GetMotorNamesA  s    r0   c                     U R                   (       a1  U R                  (       a   U R                  5       u  pn[        u    p4XU/$ U R                  (       a  [
        $ [        $ )zReturns the init position of the robot.

It can be either 1) origin (INIT_POSITION), 2) origin with a rack
(INIT_RACK_POSITION), or 3) the previous position.
)rY   rQ   rK  INIT_POSITIONrX   INIT_RACK_POSITION)rw   xyr   zs        r.   r    Minitaur._GetDefaultInitPositionD  sM     &&4+D+D$$&gaAgaAY}}r0   c                     U R                   (       aB  U R                  (       a1  U R                  5       u    pU R                  R	                  SSU/5      $ [
        $ )zrReturns the init position of the robot.

It can be either 1) INIT_ORIENTATION or 2) the previous rotation in yaw.
r   )rY   rQ   r_  rA   r  INIT_ORIENTATION)rw   r   yaws      r.   rq   #Minitaur._GetDefaultInitOrientationU  sJ     &&4+D+D**,ia""993S/JJr0   c                     U R                   $ r   )rS   r   s    r.   chassis_link_idsMinitaur.chassis_link_ids`  r  r0   c                 D    U H  nUR                  U 5        M     Xl        g)zwset all sensors to this robot and move the ownership to this robot.

Args:
  sensors: a list of sensors to this robot.
N)	set_robot_sensors)rw   r   ss      r.   rZ   Minitaur.SetAllSensorsd  s     kk$ Mr0   c                     U R                   $ )zXget all sensors associated with this robot.

Returns:
  sensors: a list of all sensors.
)r  r   s    r.   GetAllSensorsMinitaur.GetAllSensorsn  s     ==r0   c                 Z    U R                    H  nUR                  5       U:X  d  M  Us  $    g)zget the first sensor with the given name.

This function return None if a sensor with the given name does not exist.

Args:
  name: the name of the sensor we are looking

Returns:
  sensor: a sensor with the given name. None if not exists.
N)r  get_name)rw   namer  s      r.   	GetSensorMinitaur.GetSensorv  s*     ]]	
	  r0   c                     U R                   $ r   )r]   r   s    r.   is_safeMinitaur.is_safe  s    ==r0   c                     U R                   $ r   )ra   r   s    r.   last_actionMinitaur.last_action  s    r0   c                     U R                   (       aP  U R                  b  U R                  nOU R                  5       n[        US-   5      U R                  -  nX4X-
  -  -   nU$ UnU$ )a(  If enabled, interpolates between the current and previous actions.

Args:
  action: current action.
  substep_count: the step count should be between [0, self.__action_repeat).

Returns:
  If interpolation is enabled, returns interpolated action depending on
  the current action repeat substep.
r   )r_   ra   r  floatrB   )rw   r   substep_countprev_actionlerpr   s         r.   r   Minitaur.ProcessAction  sq     ((				&''))+=1$%(;(;;d&*>"??k  kr0   c                     SU R                   U R                  -  -  nU R                  5       n[        R                  " XS9nU$ )Nr   )sampling_rater   )rm   rB   r  r   ActionFilterButter)rw   r  r   a_filters       r.   rs   Minitaur._BuildActionFilter  sA    $*=*==>M((*J//#<HOr0   c                 8    U R                   R                  5         g r   )rt   resetr   s    r.   r  Minitaur._ResetActionFilter  s    
r0   c                     U R                   S:X  a+  U R                  5       nU R                  R                  U5        U R                  R	                  U5      nU$ )Nr   )rn   r  rt   init_historyfilter)rw   r   default_actionfiltered_actions       r.   r   Minitaur._FilterAction  sP     Q**,n
&&~6))008Or0   c                     U R                   $ r   )rA   r   s    r.   rx   Minitaur.pybullet_client  r  r0   c                     U R                   $ r   )r  r   s    r.   joint_statesMinitaur.joint_states  s    r0   c                     A [         $ r   r   )clss    r.   GetConstantsMinitaur.GetConstants  s    r0   ):rt   rB   r  rJ   r   r   r  rJ  r   rS   rM   rR   r`   r_   rV   rr   r]   r   r  ra   r   rT   r   r   rK   rk   rE   r  r   ri   rg   rU   rl   rF   rW   rj   rQ   rN   rI   rX   r  rL   rA   rY   r^   rD   r  ro   rn   rC   r  r_  r@   r?   r   r   r\   rm   r   )TNg      @)u__name__
__module____qualname____firstlineno____doc__r  MINITAUR_DOFS_PER_LEG	URDF_ROOTr	   r  r  r   
MotorModelSENSOR_NOISE_STDDEV!MINITAUR_DEFAULT_MOTOR_DIRECTIONSMINITAUR_DEFAULT_MOTOR_OFFSETSr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ru   r   r  r!  r)  r.  r  r   r3  rK  rP  rV  r_  rc  rr  rx  r|  r  r  r  r  r  r  r  r  r  r  r  r  rS  r  r  r  r  r  r  r   r  r  r	  r  r  r  r!  r+  r2  r8  r=  rC  rF  rK  rP  rT  rv   rg  r  r^  r\  rz  r}  r  r  r  r  r  r  r  r   r   rq   propertyr  rZ   r  r  r  r  r   rs   r  r   rx   r  classmethodr  __static_attributes__r   r0   r.   r2   r2   G   s   P .1"&+"."?"?"H"H!/!:!:#'"':).@:).!+0$)1[z/	NN&L%"N@2
2h-!F2:/E#N& ?62C D 4
$
$
*	
*(
*K"$	B),*	.08d4 #!"9&42L4GB	B	A$+;6,)'
	$!..,E2	("	 " "     0	 ! !    r0   r2   )4r  
__future__r   r   r   rO   r#   r  r'   renumpyrG   ginpybullet_envs.minitaur.robotsr   r   r	   $pybullet_envs.minitaur.robots.safetyr
   r   'pybullet_envs.minitaur.robots.utilitiesr   r   r  r  r  rA  rB  r  r  r>  r  compiler   r   r   r   r   r   r   r  r  r  r  r*   r)   r  r  r/   configurableobjectr2   r   r0   r.   <module>r     s7   L &  %     	  
 < 8 6 ? = A >  - +   G 

#67 jj!12 ZZ
+ 

#<= ZZ/ ZZ. ZZ0 / $@ !!9  	
TWW H	& v  r0   