
    ni                     R    S r SSKrSrSrSrSrSrSr\\\-   -  r	Sr
 " S	 S
\5      rg)z-This file implements an accurate motor model.    N2   g@g      0@g+?g	h"l?   c                   Z    \ rS rSrSrSS jrS rS rS rS r	S r
S	 r  SS jrS rSrg
)
MotorModel   a  The accurate motor model, which is based on the physics of DC motors.

The motor model support two types of control: position control and torque
control. In position control mode, a desired motor angle is specified, and a
torque is computed based on the internal motor model. When the torque control
is specified, a pwm signal in the range of [-1.0, 1.0] is converted to the
torque.

The internal motor model takes the following factors into consideration:
pd gains, viscous friction, back-EMF voltage and current-torque profile.
c                     Xl         X l        X0l        [        U l        [
        U l        [        U l        [        U l
        / SQU l        / SQU l        S/[        -  U l        g )N)r   
         (   r   <   )r      gffffff?g@g      @g      
@g      @      ?)_torque_control_enabled_kp_kdMOTOR_RESISTANCE_resistanceMOTOR_VOLTAGE_voltageMOTOR_TORQUE_CONSTANT_torque_constantMOTOR_VISCOUS_DAMPING_viscous_damping_current_table_torque_table
NUM_MOTORS_strength_ratios)selftorque_control_enabledkpkds       [/home/james-whalen/.local/lib/python3.13/site-packages/pybullet_envs/minitaur/envs/motor.py__init__MotorModel.__init__   sP    #9 HH'D!DM1D1D5D:D EJ.D    c                 :    [         R                  " U5      U l        g)zSet the strength of each motors relative to the default value.

Args:
  ratios: The relative strength of motor output. A numpy array ranging from
    0.0 to 1.0.
N)nparrayr   )r   ratioss     r#   set_strength_ratiosMotorModel.set_strength_ratios(   s     HHV,Dr&   c                     Xl         X l        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 of the motors.
  kd: derivative gain of the motors.
N)r   r   )r   r!   r"   s      r#   set_motor_gainsMotorModel.set_motor_gains1   s     HHr&   c                     Xl         g Nr   )r   voltages     r#   set_voltageMotorModel.set_voltage>   s    Mr&   c                     U R                   $ r1   r2   r   s    r#   get_voltageMotorModel.get_voltageA   s    ==r&   c                     Xl         g r1   r   )r   viscous_dampings     r#   set_viscous_dampingMotorModel.set_viscous_dampingD   s    +r&   c                     U R                   $ r1   r;   r7   s    r#   get_viscous_damplingMotorModel.get_viscous_damplingG   s       r&   Nc                 :   U R                   (       a  UnO_Uc%  [        R                  " [        U R                  5      nUc%  [        R                  " [        U R
                  5      nSU-  X!-
  -  Xc-  -
  n[        R                  " USS5      nU R                  Xt5      $ )a!  Convert the commands (position control or torque control) to torque.

Args:
  motor_commands: The desired motor angle if the motor is in position
    control mode. The pwm signal if the motor is in torque control mode.
  motor_angle: The motor angle observed at the current time step. It is
    actually the true motor angle observed a few milliseconds ago (pd
    latency).
  motor_velocity: The motor velocity observed at the current time step, it
    is actually the true motor velocity a few milliseconds ago (pd latency).
  true_motor_velocity: The true motor velocity. The true velocity is used
    to compute back EMF voltage and viscous damping.
  kp: Proportional gains for the motors' PD controllers. If not provided, it
    uses the default kp of the minitaur for all the motors.
  kd: Derivative gains for the motors' PD controllers. If not provided, it
    uses the default kp of the minitaur for all the motors.

Returns:
  actual_torque: The torque that needs to be applied to the motor.
  observed_torque: The torque observed by the sensor.
g      r   )r   r(   fullr   r   r   clip_convert_to_torque_from_pwm)r   motor_commandsmotor_anglemotor_velocitytrue_motor_velocityr!   r"   pwms           r#   convert_to_torqueMotorModel.convert_to_torqueJ   s    8 ##c	WWZ*	WWZ*G{34r7JJc
''#tS
!C++CEEr&   c                    [         R                  " U R                  [         R                  " U5      U R                  -  U R
                  -  -  [        * [        5      n[         R                  " [         R                  " U5      U R                  -  U R                  U R                  -   [         R                  " U5      -  -
  [        * [        5      nX@R
                  -  n[         R                  " U5      n[         R                  " U5      n[         R                  " XpR                  U R                  5      n[         R                  " Xh5      n[         R                  " U R                  U5      nX4$ )a\  Convert the pwm signal to torque.

Args:
  pwm: The pulse width modulation.
  true_motor_velocity: The true motor velocity at the current moment. It is
    used to compute the back EMF voltage and the viscous damping.
Returns:
  actual_torque: The torque that needs to be applied to the motor.
  observed_torque: The torque observed by the sensor.
)r(   rE   r   asarrayr   r   OBSERVED_TORQUE_LIMITr   VOLTAGE_CLIPPINGsignabsoluteinterpr   r   multiplyr   )	r   rK   rJ   observed_torquevoltage_netcurrentcurrent_signcurrent_magnitudeactual_torques	            r#   rF   &MotorModel._convert_to_torque_from_pwmr   s    ggC4==!@4CSCS!ST	 57O
 ''


3$--'			!6!6	6"**EX:YY	Z	+-K ,,,G777#LG,II/1D1DdFXFXYMKK<MKK 5 5}EM))r&   )
r   r   r   r   r   r   r   r   r   r   )Fg333333?r   )NN)__name__
__module____qualname____firstlineno____doc__r$   r+   r.   r4   r8   r=   r@   rL   rF   __static_attributes__ r&   r#   r   r      s>    

/-,!  &FP*r&   r   )ra   numpyr(   rQ   rP   r   r   r   r   MOTOR_SPEED_LIMITr   objectr   rc   r&   r#   <module>rg      sS    3      !%:=R%RS 
~* ~*r&   