
    niX                     f    S r SSKrSSKJr  SrSrSrSrSr	Sr
\\
\	-   -  rS	rS
rSr " S S\5      rg)z-This file implements an accurate motor model.    N)robot_config2   g@g      0@g+?g	h"l?   g      ?g      @c                       \ rS rSrSrSSS\R                  R                  4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.
g333333?r   Nc                     Xl         X l        X0l        X@l        [        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      @      ?)_kp_kd_torque_limits_motor_control_mode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kpkdtorque_limitsmotor_control_modes        f/home/james-whalen/.local/lib/python3.13/site-packages/pybullet_envs/minitaur/robots/minitaur_motor.py__init__MotorModel.__init__"   sX    
 HH'1'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_ratios3   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_gains<   s     HHr)   c                     Xl         g Nr   )r!   voltages     r&   set_voltageMotorModel.set_voltageI   s    Mr)   c                     U R                   $ r4   r5   r!   s    r&   get_voltageMotorModel.get_voltageL   s    ==r)   c                     Xl         g r4   r   )r!   viscous_dampings     r&   set_viscous_dampingMotorModel.set_viscous_dampingO   s    +r)   c                     U R                   $ r4   r>   r:   s    r&   get_viscous_damplingMotorModel.get_viscous_damplingR   s       r)   c                 p   U(       d  U R                   nU[        R                  R                  L d  U[        R                  R                  L a  [        SR                  U5      5      eU R                  nU R                  nU[        R                  R                  L ac  SU-  U[        -
  -  US-  U-  -
  nSU-  U[        -
  -  US-  U-  -
  n	U[        R                  " US5      -   [        R                  " U	S5      -   n
OSU-  X!-
  -  Xs-  -
  n
[        R                  " U
SS5      n
U R!                  X5      $ )a=  Convert the commands (position control or pwm 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.
  motor_control_mode: A MotorControlMode enum.

Returns:
  actual_torque: The torque that needs to be applied to the motor.
  observed_torque: The torque observed by the sensor.
z({} is not a supported motor control modeg       @r         r   )r   r   MotorControlModeTORQUEHYBRID
ValueErrorformatr   r   PWMMOTOR_POS_UBMOTOR_POS_LBr+   minimummaximumclip_convert_to_torque_from_pwm)r!   motor_commandsmotor_anglemotor_velocitytrue_motor_velocityr%   r"   r#   pd_maxpd_minpwms              r&   convert_to_torqueMotorModel.convert_to_torqueU   s&   0 33l;;BBBl;;BBB
4
;
;<N
OQ Q 
B	B\::>>> Bw+45R.8PPfBw+45R.8PPfRZZ22RZZ5JJcG{34r7JJc
''#tS
!C++CEEr)   c                    [         R                  " U R                  [         R                  " U5      U R                  -  U R
                  -  -  [        * [        5      nU R                  b/  [         R                  " US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U R                  b/  [         R                  " USU R                  -  U R                  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.
rG   )r+   rR   r   asarrayr   r   OBSERVED_TORQUE_LIMITr   r   VOLTAGE_CLIPPINGsignabsoluteinterpr   r   multiplyr    )	r!   rZ   rW   observed_torquevoltage_netcurrentcurrent_signcurrent_magnitudeactual_torques	            r&   rS   &MotorModel._convert_to_torque_from_pwm   s    gg	C4==	(4+;+;	;	=	 57O &8K8K1K $ 3 35o ''


3$--'			!6!6	6


&'	(	(*:):<LNK ,,,G777#LG,II/1D1D"002MKK<MKK 5 5}EM&ggmTD4G4G-G"113m))r)   )r   r   r   r   r   r    r   r   r   r   r   r4   )__name__
__module____qualname____firstlineno____doc__r   rH   POSITIONr'   r.   r1   r7   r;   r@   rC   r[   rS   __static_attributes__ r)   r&   r   r      sU    
 !"."?"?"H"H	/"-,! ,00Fd$*r)   r   )rp   numpyr+   pybullet_envs.minitaur.robotsr   r`   r_   r   r   r   r   MOTOR_SPEED_LIMITr   rO   rN   objectr   rs   r)   r&   <module>rx      sb    3  6     !113 
V* V*r)   