
    ni1                     h    S r SSKrSSKJr  SSKrSSKJr  SrS\-  r	 " S S\R                  5      rg)	zGImplements the gym environment of minitaur moving with trotting style.
    N)spaces)minitaur_gym_env      c                      ^  \ rS rSrSrSS/SS.r                      SU 4S jjrU 4S 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U =r$ )MinitaurTrottingEnv   aC  The trotting gym environment for the minitaur.

In this env, Minitaur performs a trotting style locomotion specified by
extension_amplitude, swing_amplitude, and step_frequency. Each diagonal pair
of legs will move according to the reference trajectory:
    extension = extsion_amplitude * cos(2 * pi * step_frequency * t + phi)
    swing = swing_amplitude * sin(2 * pi * step_frequency * t + phi)
And the two diagonal leg pairs have a phase (phi) difference of pi. The
reference signal may be modified by the feedback actions from a balance
controller (e.g. a neural network).

human	rgb_array   )zrender.modeszvideo.frames_per_secondc                   > [         R                  " [        5      U l        [         R                  " [        5      U l        UUUUUUUU/U l        UU l        UU l        UU l        Xl	        Xl
        [        [        U ]7  UUSUUU	UUUU
UUUUUUS9  [        S-  n[         R                  " S/U-  5      n[        R                   " U* U5      U l        SU l        SU l        SU l        g)	a  Initialize the minitaur trotting gym environment.

Args:
  urdf_version: [DEFAULT_URDF_VERSION, DERPY_V0_URDF_VERSION,
    RAINBOW_DASH_V0_URDF_VERSION] are allowable versions. If None,
      DEFAULT_URDF_VERSION is used. Refer to minitaur_gym_env for more
      details.
  control_time_step: The time step between two successive control signals.
  action_repeat: The number of simulation steps that an action is repeated.
  control_latency: The latency between get_observation() and the actual
    observation. See minituar.py for more details.
  pd_latency: The latency used to get motor angles/velocities used to
    compute PD controllers. See minitaur.py for more details.
  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 hung midair so
    that its walking gait is clearer to visualize.
  motor_kp: The P gain of the motor.
  motor_kd: The D gain of the motor.
  remove_default_joint_damping: Whether to remove the default joint damping.
  render: Whether to render the simulation.
  num_steps_to_log: The max number of control steps in one episode. If the
    number of steps is over num_steps_to_log, the environment will still
    be running, but only first num_steps_to_log will be recorded in logging.
  accurate_motor_model_enabled: Uses the nonlinear DC motor model if set to
    True.
  use_signal_in_observation: Includes the reference motor angles in the
    observation vector.
  use_angle_in_observation: Includes the measured motor angles in the
    observation vector.
  hard_reset: Whether to reset the whole simulation environment or just
    reposition the robot.
  env_randomizer: A list of EnvRandomizers that can randomize the
    environment during when env.reset() is called and add perturbation
    forces when env.step() is called.
  log_path: The path to write out logs. For the details of logging, refer to
    minitaur_logging.proto.
  init_extension: The initial reset length of the leg.
  init_swing: The initial reset swing position of the leg.
  step_frequency: The desired leg stepping frequency.
  extension_amplitude: The maximum leg extension change within a locomotion
    cycle.
  swing_amplitude: The maximum leg swing change within a cycle.
T)urdf_versionaccurate_motor_model_enabledmotor_overheat_protectionmotor_kpmotor_kdremove_default_joint_dampingcontrol_latency
pd_latencyon_rackrender
hard_resetnum_steps_to_logenv_randomizerlog_pathcontrol_time_stepaction_repeatr   g      ?      ?   iN)npzerosNUM_LEGS_swing_offset_extension_offset
_init_pose_step_frequency_extension_amplitude_swing_amplitude_use_signal_in_observation_use_angle_in_observationsuperr   __init__arrayr   Boxaction_space	_cam_dist_cam_yaw
_cam_pitch)selfr   r   r   r   r   r   r   r   r   r   r   r   use_signal_in_observationuse_angle_in_observationr   r   r   init_extension
init_swingstep_frequencyextension_amplitudeswing_amplitude
action_dimaction_high	__class__s                            k/home/james-whalen/.local/lib/python3.13/site-packages/pybullet_envs/minitaur/envs/minitaur_trotting_env.pyr,   MinitaurTrottingEnv.__init__   s    J (+DXXh/D 	J
JDO
 *D 3D+D&?#%="	

&26R37"*"*6R)8$.!( &$.*:(6"*+<'4  6" AJ((D6J./K

K<=D DNDMDO    c                 ~   > U R                  U R                  5      n[        [        U ]  USS9  U R                  5       $ )Ng      ?)initial_motor_anglesreset_duration)_convert_from_leg_modelr%   r+   r   reset_get_observation)r3   rB   r=   s     r>   rE   MinitaurTrottingEnv.reset   sD      77H	
t*@T:= + ?  ""r@   c                 4   [         R                  " [        5      n[        [        5       Hj  nU[        U-      S[        US-  5      -  X   -  -
  U[        SU-  5      '   U[        U-      S[        US-  5      -  X   -  -   U[        SU-  S-   5      '   Ml     U$ )a  Converts leg space action into motor commands.

Args:
  leg_pose: A numpy array. leg_pose[0:NUM_LEGS] are leg swing angles
    and leg_pose[NUM_LEGS:2*NUM_LEGS] contains leg extensions.

Returns:
  A numpy array of the corresponding motor angles for the given leg pose.
r      )r    r!   
NUM_MOTORSranger"   int)r3   leg_pose
motor_poseis       r>   rD   +MinitaurTrottingEnv._convert_from_leg_model   s     *%J8_'15c!a%j8H8;8VVjQU#+HqL#9R#a!e*<Lx{<Z#ZjQUQY   r@   c                    SU R                   -  nU R                  [        R                  " S[        R                  -  U-  U-  U-   5      -  nU R
                  [        R                  " S[        R                  -  U-  U-  U-   5      -  nXE4$ )a2  Generates a sinusoidal reference leg trajectory.

The foot (leg tip) will move in a ellipse specified by extension and swing
amplitude.

Args:
  t: Current time in simulation.
  phase: The phase offset for the periodic trajectory.

Returns:
  The desired leg extension and swing angle at the current time.
rJ   r   )r&   r'   mathcospir(   sin)r3   tphaseperiod	extensionswings         r>   _gen_signalMinitaurTrottingEnv._gen_signal   s~     %%%F))DHHQ[65IA5MPU5U,VVI!!DHHQ[6-AA-E-M$NNEr@   c           
          U R                  US5      u  p#U R                  U[        R                  5      u  pE[        R                  " X5XSUXDU/5      n[        R                  " U R
                  5      U-   nU$ )zGenerates the trotting gait for the robot.

Args:
  t: Current time in simulation.

Returns:
  A numpy array of the reference leg positions.
r   )r\   rS   rU   r    r-   r%   )r3   rW   ext_first_pairsw_first_pairext_second_pairsw_second_pairtrotting_signalsignals           r>   _signalMinitaurTrottingEnv._signal   so     %)$4$4Q$:!N&*&6&6q$''&B#Ohh~n.  O XXdoo&8FMr@   c                     US[         === U R                  -  sss& U[         S[         -  === U R                  -  sss& XR                  U R                  R                  5       5      -  nU R                  U5      $ )a  Generates the motor commands for the given action.

Swing/extension offsets and the reference leg trajectory will be added on
top of the inputs before the conversion.

Args:
  action: A numpy array contains the leg swings and extensions that will be
    added to the reference trotting trajectory. action[0:NUM_LEGS] are leg
    swing angles, and action[NUM_LEGS:2*NUM_LEGS] contains leg extensions.

Returns:
  A numpy array of the desired motor angles for the given leg space action.
r   r   )r"   r#   r$   re   minitaurGetTimeSinceResetrD   )r3   actions     r>   "_transform_action_to_motor_command6MinitaurTrottingEnv._transform_action_to_motor_command   si     1X$,,,
8AL!T%;%;;! ll4==::<==F''//r@   c                     U R                   R                  5       u  pn[        R                  " U5      S:  =(       d    [        R                  " U5      S:  nU$ )zrDecide whether the minitaur has fallen.

Returns:
  Boolean value that indicates whether the minitaur has fallen.
333333?)rh   GetTrueBaseRollPitchYawrS   fabs)r3   rollpitch_	is_fallens        r>   rt   MinitaurTrottingEnv.is_fallen   sE     ]]::<ND		$#%?5)9C)?Ir@   c                    / nU R                   R                  5       u  p#nU R                   R                  5       u  pVnUR                  X#XV/5        U R                  (       a$  UR                  U R                  S/S-  5      5        U R                  (       a7  UR                  U R                   R                  5       R                  5       5        [        R                  " U5      U l        U R                  $ )zGet the true observations of this environment.

It includes the true roll, pitch, roll dot and pitch dot of the base. Also
includes the disired/observed motor angles if the relevant flags are set.

Returns:
  The observation list.
r      )rh   ro   GetTrueBaseRollPitchYawRateextendr)   rk   r*   GetMotorAnglestolistr    r-   _true_observationr3   observationrq   rr   rs   	roll_rate
pitch_rates          r>   _get_true_observation)MinitaurTrottingEnv._get_true_observation   s     K]]::<ND#}}HHJI1Y;<&&@@!qIJ%%557>>@AXXk2D!!!r@   c                    / nU R                   R                  5       u  p#nU R                   R                  5       u  pVnUR                  X#XV/5        U R                  (       a$  UR                  U R                  S/S-  5      5        U R                  (       a7  UR                  U R                   R                  5       R                  5       5        [        R                  " U5      U l        U R                  $ )zGet observations of this environment.

It includes the base roll, pitch, roll dot and pitch dot which may contain
noises, bias, and latency. Also includes the disired/observed motor angles
if the relevant flags are set.

Returns:
  The observation list.
r   rw   )rh   GetBaseRollPitchYawGetBaseRollPitchYawRatery   r)   rk   r*   rz   r{   r    r-   _observationr}   s          r>   rF   $MinitaurTrottingEnv._get_observation   s     K]]668ND#}}DDFI1Y;<&&@@!qIJ%%557>>@A-Dr@   c                    / nUR                  S[        R                  -  /S-  5        UR                  S[        R                  -  U R                  -  /S-  5        U R                  (       a*  UR                  S[        R                  -  /[
        -  5        U R                  (       a*  UR                  S[        R                  -  /[
        -  5        [        R                  " U5      $ )zGet the upper bound of the observation.

Returns:
  A numpy array contains the upper bound of an observation. See
  GetObservation() for the details of each element of an observation.
r   )	ry   rS   rU   
_time_stepr)   rK   r*   r    r-   )r3   upper_bounds     r>   _get_observation_upper_bound0MinitaurTrottingEnv._get_observation_upper_bound  s     KDGG}q()DGGdoo56:;&&!dgg+34%%!dgg+3488K  r@   c                 (    U R                  5       * nU$ )zxGet the lower bound of the observation.

Returns:
  The lower bound of an observation (the reverse of the upper bound).
)r   )r3   lower_bounds     r>   _get_observation_lower_bound0MinitaurTrottingEnv._get_observation_lower_bound#  s     4466Kr@   c                     Xl         g)zgSet the swing offset of each leg.

It is to mimic the bent leg.

Args:
  value: A list of four values.
N)r#   r3   values     r>   set_swing_offset$MinitaurTrottingEnv.set_swing_offset,  s
     r@   c                     Xl         g)zkSet the extension offset of each leg.

It is to mimic the bent leg.

Args:
  value: A list of four values.
N)r$   r   s     r>   set_extension_offset(MinitaurTrottingEnv.set_extension_offset6  s
     #r@   )r0   r2   r1   r'   r$   r%   r   r&   r(   r#   r|   r*   r)   r/   )Ng~jtx?   g{Gz?g~jth?Fr   gQ?TFi  TFFFNN       @g        r   gffffff?rn   )__name__
__module____qualname____firstlineno____doc__metadatar,   rE   rD   r\   re   rk   rt   r   rF   r   r   r   r   __static_attributes____classcell__)r=   s   @r>   r   r      s      '4QTU( !!&#,0 $,0).(-"!!#'"-l\# $(0,"(*! # #r@   r   )r   rS   gymr   numpyr    pybullet_envs.minitaur.envsr   r"   rK   MinitaurGymEnvr    r@   r>   <module>r      s;       8 \
p#*99 p#r@   