
    ni                     N    S r SSKrSrSrSrSrSrSr\\\-   -  r	 " S S	\
5      rg)
z-This file implements an accurate motor model.    N2   g@g      0@g+?g	h"l?c                   F    \ rS rSrSrSS jrS rS rS rS r	S 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        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)selftorque_control_enabledkpkds       T/home/james-whalen/.local/lib/python3.13/site-packages/pybullet_envs/bullet/motor.py__init__MotorModel.__init__   sA    #9 HH'D!DM1D1D5D:D    c                     Xl         g Nr   )r   voltages     r   set_voltageMotorModel.set_voltage%   s    Mr"   c                     U R                   $ r$   r%   r   s    r   get_voltageMotorModel.get_voltage(   s    ==r"   c                     Xl         g r$   r   )r   viscous_dampings     r   set_viscous_dampingMotorModel.set_viscous_damping+   s    +r"   c                     U R                   $ r$   r.   r*   s    r   get_viscous_damplingMotorModel.get_viscous_dampling.   s       r"   c                     U R                   (       a  UnO"U R                  * X!-
  -  U R                  U-  -
  n[        R                  " USS5      nU R                  XC5      $ )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.
  current_motor_angle: The motor angle at the current time step.
  current_motor_velocity: The motor velocity at the current time step.
Returns:
  actual_torque: The torque that needs to be applied to the motor.
  observed_torque: The torque observed by the sensor.
g      g      ?)r   r   r   npclip_convert_to_torque_from_pwm)r   motor_commandscurrent_motor_anglecurrent_motor_velocitypwms        r   convert_to_torqueMotorModel.convert_to_torque1   s[     ##chhY->?XX../c
''#tS
!C++CHHr"   c                     [         R                  " U R                  XR                  -  U R                  -  -  [
        * [
        5      n[         R                  " XR                  -  U R                  U R                  -   U-  -
  [        * [        5      nX@R                  -  n[         R                  " U5      n[         R                  " U5      n[         R                  " XpR                  U R                  5      n[         R                  " Xh5      nX4$ )a  Convert the pwm signal to torque.

Args:
  pwm: The pulse width modulation.
  current_motor_velocity: The motor velocity at the current time step.
Returns:
  actual_torque: The torque that needs to be applied to the motor.
  observed_torque: The torque observed by the sensor.
)r6   r7   r   r   r   OBSERVED_TORQUE_LIMITr   VOLTAGE_CLIPPINGsignabsoluteinterpr   r   multiply)	r   r<   r;   observed_torquevoltage_netcurrentcurrent_signcurrent_magnitudeactual_torques	            r   r8   &MotorModel._convert_to_torque_from_pwmE   s     ggd33s]]7JTM]M]7]^446KMO ''mm			!6!6	6:PP	Q	+-K ,,,G777#LG, II/1D1DdFXFXYMKK<M))r"   )	r   r   r   r   r   r   r   r   r   N)Fg333333?r   )__name__
__module____qualname____firstlineno____doc__r    r'   r+   r0   r3   r=   r8   __static_attributes__ r"   r   r   r      s+    
	;,!I(*r"   r   )rQ   numpyr6   rA   r@   r   r   r   r   MOTOR_SPEED_LIMITobjectr   rS   r"   r   <module>rW      sN    3      !%:=R%RS Q* Q*r"   