
    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SSKJr  SSK	J
r
  SSK	Jr  S	\R                  S
\S\
R                  4S jrS\R                   S\R"                  \   S\R$                  4S jr " S S\5      rg)zUtilities for safety layers.    )absolute_import)division)print_functionN)robot_config)
data_types)safety_errornamevalueboundc                     UR                   Us=::  a  UR                  ::  a   g  [        R                  " SR	                  XU 5      5      e)a  Check if the given value is within the provided bound.

Args:
  name: The name of the value.
  value: Number to be checked.
  bound: Contains the lower and upper bounds. The bound is inclusive.

Raises:
  safety_error.OutofBoundError: when the value is outside the bound.
Nz{} is out of bound {} for {})lowerupperr   OutOfBoundErrorformat)r	   r
   r   s      h/home/james-whalen/.local/lib/python3.13/site-packages/pybullet_envs/minitaur/robots/safety/utilities.pyassert_in_boundr      sI     [[E(U[[(
 ) 
&
&'E'L'Ld(      robotactioncontrol_modec                 <   / nU[         R                  R                  :X  ah  [        U5       HY  u  pEUR	                  [
        R                  " U R                  UU R                  5       U   SU R                  5       U   SS95        M[     U[         R                  R                  :X  d  U[         R                  R                  :X  aF  [        U5       H7  u  pFUR	                  [
        R                  " U R                  SSSSUS95        M9     U[         R                  R                  :X  Gar  [        U R                  5       GHX  nU[         R                  -  [         R                   R                  R"                  -   nU[         R                  -  [         R                   R$                  R"                  -   nU[         R                  -  [         R                   R&                  R"                  -   n	U[         R                  -  [         R                   R(                  R"                  -   n
U[         R                  -  [         R                   R                  R"                  -   nUR	                  [
        R                  " U R                  X   X   X   X   X   S95        GM[     U$ )zConverts the input action to generic MotorAction classes.

Args:
  robot: An robot instance.
  action: The motor commands sent to the robot.
  control_mode: The motor control mode.

Returns:
  The list of converted MotorAction instances.
r   )	timestamppositionposition_gainvelocityvelocity_gaintorque)r   MotorControlModePOSITION	enumerateappendr   MotorActionlast_action_timeGetMotorPositionGainsGetMotorVelocityGainsTORQUEPWMHYBRIDrange
num_motorsHYBRID_ACTION_DIMENSIONHybridActionIndexr
   POSITION_GAINVELOCITYVELOCITY_GAIN)r   r   r   motor_action_listmotor_idr   r   position_indexposition_gain_indexvelocity_indexvelocity_gain_indextorque_indexs               r   convert_to_motor_actionr7       sQ    \22;;;'/

 
 ..!779(C!779(C 0 l33:::l33777%f-

 
 .. . \22999%**+
\99
9

(
(
1
1
7
78  \99
9

(
(
6
6
<
<=  \99
9

(
(
1
1
7
78  \99
9

(
(
6
6
<
<=  \99
9

(
(
/
/
5
56  

 
 ..-"7-"7)+,! ,2 
r   c                   F    \ rS rSrSrS\4S jrS\4S jrS\S\4S	 jr	S
r
g)MovingWindowFilterd   zA stable O(1) moving filter for incoming data streams.

We implement the Neumaier's algorithm to calculate the moving window average,
which is numerically stable.

window_sizec                 n    US:  d   eXl         [        R                  " US9U l        SU l        SU l        g)zEInitializes the class.

Args:
  window_size: The moving window size.
r   )maxlenN)_window_sizecollectionsdeque_value_deque_sum_correction)selfr;   s     r   __init__MovingWindowFilter.__init__l   s:     ??##))=DDI Dr   r
   c                    U R                   U-   n[        U R                   5      [        U5      :  a&  U =R                  U R                   U-
  U-   -  sl        O$U =R                  X-
  U R                   -   -  sl        X l         g)zUpdate the moving window sum using Neumaier's algorithm.

For more details please refer to:
https://en.wikipedia.org/wiki/Kahan_summation_algorithm#Further_enhancements

Args:
  value: The new value to be added to the window.
N)rB   absrC   )rD   r
   new_sums      r   _neumaier_sum MovingWindowFilter._neumaier_sum{   sh     ii%G
499~U#
499w.%77 5?dii77Ir   	new_valuereturnc                 B   [        U R                  5      nX R                  R                  :  a  OU R                  U R                  S   * 5        U R                  U5        U R                  R	                  U5        U R
                  U R                  -   U R                  -  $ )zComputes the moving window average in O(1) time.

Args:
  new_value: The new value to enter the moving window.

Returns:
  The average of the values in the window.

r   )lenrA   r=   rJ   r!   rB   rC   r>   )rD   rL   	deque_lens      r   calculate_average$MovingWindowFilter.calculate_average   s     D%%&I$$+++
 $++A../y!Y'II(((D,=,===r   )rC   rB   rA   r>   N)__name__
__module____qualname____firstlineno____doc__intrE   floatrJ   rQ   __static_attributes__ r   r   r9   r9   d   s2    #  (> >5 >r   r9   )rW   
__future__r   r   r   r?   typingpybullet_envs.minitaur.robotsr   $pybullet_envs.minitaur.robots.safetyr   r   TextrY   Boundr   AnySequencer   r7   objectr9   r[   r   r   <module>re      s    " &  %   6 ; =&++ e J<L<L $A::AOOE"A //AH?> ?>r   