
    ni                        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\R                  R                  \R                  R                  \R                  " \R                  " 5       5      5      5      r\R                  R                  \R                  R                  \5      5      r\R                  R                  R                  S\5        SSKrSSKrSSKrSSKrSSKJr  SrS rS	 rS
 r " S S\5      rg)az  A proto buffer based logging system for minitaur experiments.

The logging system records the time since reset, base position, orientation,
angular velocity and motor information (joint angle, speed, and torque) into a
proto buffer. See minitaur_logging.proto for more details. The episode_proto is
updated per time step by the environment and saved onto disk for each episode.
    )absolute_import)division)print_functionN)minitaur_logging_pb2   c                 @    US   U l         US   U l        US   U l        g )Nr         )xyz)
base_statevaluess     f/home/james-whalen/.local/lib/python3.13/site-packages/pybullet_envs/minitaur/envs/minitaur_logging.py_update_base_stater      s#    *,*,*,    c                    [        U5       H  nU R                  R                  5       nSUl        SUR                  l        SUR                  l        [        [        5       H9  nUR                  R                  5       nSUl	        SUl
        SUl        SUl        M;     [        UR                  / SQ5        [        UR                  / SQ5        [        UR                   / SQ5        M     g)a  Preallocate the memory for proto buffer.

Dynamically allocating memory as the protobuf expands causes unexpected delay
that is not tolerable with locomotion control.

Args:
  episode_proto: The proto that holds the state/action data for the current
    episode.
  max_num_steps: The max number of steps that will be recorded in the proto.
    The state/data over max_num_steps will not be stored in the proto.
Fr   )r   r   r   N)rangestate_actionadd
info_validtimesecondsnanos
NUM_MOTORSmotor_statesanglevelocitytorqueactionr   base_positionbase_orientationbase_angular_vel)episode_protomax_num_steps_step_logmotor_states        r   preallocate_episode_protor)   "   s     a))--/HHHMMHMM:))--/kkkkk  x--y9x00)<x00)<  r   c                    [        U R                  5      nX4:  a&  [        R                  " SR	                  X45      5        gU R                  U   nUR                  5       Ul        UR                  5       n[        U5      UR                  l
        [        U[        U5      -
  S-  5      UR                  l        UR                  5       nUR                  5       nUR                  5       n	[        UR                   5       H[  n
Xz   UR"                  U
   l        X   UR"                  U
   l        X   UR"                  U
   l        X*   UR"                  U
   l        M]     [-        UR.                  UR1                  5       5        [-        UR2                  UR5                  5       5        [-        UR6                  UR9                  5       5        g)a  Update the episode proto by appending the states/action of the minitaur.

Note that the state/data over max_num_steps preallocated
(len(episode_proto.state_action)) will not be stored in the proto.
Args:
  episode_proto: The proto that holds the state/action data for the current
    episode.
  minitaur: The minitaur instance. See envs.minitaur for details.
  action: The action applied at this time step. The action is an 8-element
    numpy floating-point array.
  step: The current step index.
zP{}th step is not recorded in the logging since only {} steps were pre-allocated.Ng    eA)lenr   loggingwarningformatIsObservationValidr   GetTimeSinceResetintr   r   r   GetMotorAnglesGetMotorVelocitiesGetMotorTorquesr   
num_motorsr   r   r   r   r    r   r!   GetBasePositionr"   GetBaseRollPitchYawr#   GetBaseRollPitchYawRate)r$   minitaurr    stepr%   r'   time_in_secondsmotor_anglesmotor_velocitiesmotor_torquesis              r   update_episode_protor@   >   s|    m001-	OO ((.t(CE
''-( 335(..0/o.(--_s?/CCsJK(--((*,002**,-$$%a%1_H!"(8(;H!%&3&6H!#&,iH!#	 & X++X-E-E-GHX..0L0L0NOX..0P0P0RSr   c                   .    \ rS rSrSrSS jrS rS rSrg)	MinitaurLoggingd   z@A logging system that records the states/action of the minitaur.Nc                     Xl         g N	_log_path)selflog_paths     r   __init__MinitaurLogging.__init__g   s    Nr   c                    U R                   (       a  UR                  (       d  U R                   $ [        R                  R	                  U R                   5      (       d   [        R
                  " U R                   5        [        R                  " 5       n[        R                  R                  U5      R                  S5      n[        R                  R                  U R                   SR                  U5      5      n[        US5       nUR                  UR                  5       5        SSS5        U$ ! , (       d  f       U$ = f)a  Save episode_proto to self._log_path.

self._log_path is the directory name. A time stamp is the file name of the
log file. For example, when self._log_path is "/tmp/logs/", the actual
log file would be "/tmp/logs/yyyy-mm-dd-hh:mm:ss".

Args:
  episode_proto: The proto that holds the states/action for the current
    episode that needs to be save to disk.
Returns:
  The full log path, including the directory name and the file name.
z%Y-%m-%d-%H%M%Szminitaur_log_{}wbN)rG   r   ospathexistsmkdirr   datetimefromtimestampstrftimejoinr.   openwriteSerializeToString)rH   r$   ts
time_stamprI   fs         r   save_episodeMinitaurLogging.save_episodek   s     >>!;!;^^77>>$..))hht~~	B""004==>OPJww||DNN,=,D,DZ,PQH	h	ggm--/0 
O 
	Os    D//
D>c                     [        US5       nUR                  5       n[        R                  " 5       nUR	                  U5        UsSSS5        $ ! , (       d  f       g= f)zRestore the episodic proto from the log path.

Args:
  log_path: The full path of the log file.
Returns:
  The minitaur episode proto.
rbN)rV   readr   MinitaurEpisodeParseFromString)rH   rI   r[   contentr$   s        r   restore_episodeMinitaurLogging.restore_episode   sF     
h	g*::<m##G,	 
		s   8A
ArF   rE   )	__name__
__module____qualname____firstlineno____doc__rJ   r\   rd   __static_attributes__ r   r   rB   rB   d   s    H2r   rB   )rj   
__future__r   r   r   rN   inspectrO   dirnameabspathgetfilecurrentframe
currentdir	parentdirsysinsertrR   r   r,   pybullet_envs.minitaur.envsr   r   r   r)   r@   objectrB   rl   r   r   <module>ry      s    '  % WW__RWW__W__W=Q=Q=S-TUV
GGOOBGGOOJ78	   1i    	   <
=8#TL,f ,r   