from enum import Enum


class FSAFunctionResult:
    SUCCESS = 0
    FAIL = -1
    RUNNING = 1
    PREPARE = 2
    EXECUTE = 3
    NOT_EXECUTE = 4
    TIMEOUT = 5


class FSAFlagState:
    CLEAR = 0
    SET = 1


class FSAActuatorType:
    TYPE_DEFAULT = 0x00000001


class FSAActuatorDirection:
    DIRECTION_NORMAL = 1
    DIRECTION_REVERSE = -1


class FSAActuatorReductionRatio:
    REDUCTION_RATIO_DEFAULT = 1
    REDUCTION_RATIO_1 = 1
    REDUCTION_RATIO_7 = 7
    REDUCTION_RATIO_9_75 = 9.75
    REDUCTION_RATIO_13_7 = 13.7
    REDUCTION_RATIO_28_8 = 28.8
    REDUCTION_RATIO_30 = 30
    REDUCTION_RATIO_31 = 31
    REDUCTION_RATIO_36 = 36
    REDUCTION_RATIO_50 = 50
    REDUCTION_RATIO_51 = 51
    REDUCTION_RATIO_80 = 80
    REDUCTION_RATIO_81 = 81
    REDUCTION_RATIO_100 = 100
    REDUCTION_RATIO_101 = 101
    REDUCTION_RATIO_120 = 120
    REDUCTION_RATIO_121 = 121


class FSAActuatorCommHardwareType:
    TYPE_NULL = 0
    TYPE_V1 = 1
    TYPE_V2 = 2


class FSAControlWord:
    NONE = 0
    CALIBRATE_PREPARE = 0xA0
    CALIBRATE_ADC = 0xA1
    CALIBRATE_MOTOR = 0xA2
    CALIBRATE_ENCODER = 0xA3
    CALIBRATE_DIRECTION = 0xA4
    CALIBRATE_OFFSET = 0xA5
    OPEN_LOOP_CONTORL = 0x04
    SERVO_OFF = 0x06
    SERVO_ON = 0x0F
    MOTION_ABS = 0x103F
    CLEAR_FAULT = 0x86


class FSAModeOfOperation:
    NONE = 0
    TORQUE_CONTROL = 6
    CURRENT_CONTROL = 4
    VELOCITY_CONTROL = 3
    POSITION_CONTROL = 1
    CURRENT_CLOSE_LOOP_CONTROL = 4
    POSITION_CONTROL_PD = 7


class FSAState:
    IDLE = 0
    INIT = 1
    NORMAL = 2
    FAULT = 3
    ENCODER_CAIL = 4


class FSAErrorCode(Enum):
    ERROR_NONE = 0x0
    ERROR_ADC_CAL_FAULT = 0x01
    ERROR_CAN_COM_TIMEOUT = 0x02
    ERROR_OVER_CURRENT = 0x04
    ERROR_OVER_VBUS = 0x08

    ERROR_UNDER_VBUS = 0x10
    ERROR_OVER_TEMP_TRIP = 0x20
    ERROR_OVER_TEMP_MOSFET = 0x40
    ERROR_OVER_PHASE_A_CURRENT = 0x80

    ERROR_OVER_PHASE_B_CURRENT = 0x100
    ERROR_OVER_PHASE_C_CURRENT = 0x200
    ERROR_OVER_HARD_PHASE_CURRENT = 0x400
    ERROR_OPD_FAULT = 0x800

    ERROR_ENCODER_NOT_CALIBRATED = 0x1000
    ERROR_ENCODER_LOSS = 0x2000
    ERROR_FLASH_ERROR = 0x4000
    ERROR_MOTOR_STALL = 0x8000

    ERROR_POSITION_LIMIT_ERROR = 0x10000
    ERROR_ENCODER_REVERSAL = 0x00020000
    ERROR_MOTOR_TYPE_NULL = 0x00040000
    ERROR_HARDWARE_TYPE_NULL = 0x00080000

    ERROR_ENCODER_OTHERS_ERROR = 0x00100000
    ERROR_OVER_TEMP_COIL = 0x00200000
    ERROR_ADC_DMA_INIT_ERROR = 0x00400000
    ERROR_DOUBLE_ENCODER_ERROR = 0x00800000

    ERROR_LINERHALL_NOT_CALI = 0x01000000
    ERROR_LINERHALL_REVERSAL = 0x02000000
    ERROR_LINERHALL_LOSS = 0x04000000
    ERROR_LINERHALL_WAVEFORM_ERROR = 0x08000000

    ERROR_U_PHASE_LOSS = 0x10000000
    ERROR_V_PHASE_LOSS = 0x20000000
    ERROR_W_PHASE_LOSS = 0x40000000
    ERROR_ETH_COM_TIMEOUT = 0x80000000

class FSAErrorCodeExt2:
    TYPE_EXT2_ENCODER = 0x01
    TYPE_EXT2_LINER_HALL = 0x02

class FSAErrorCodeExt2Encoder(Enum):
    ERROR_EXT2_ENCODER_NOT_CAIL = 0x04
    ERROR_EXT2_ENCODER_CALIBRATION_ERR = 0x08
    
    ERROR_EXT2_ENCODER_COMMUNICATION_ERR = 0x10
    ERROR_EXT2_ENCODER_PORT_NULL = 0x20
    ERROR_EXT2_ENCODER_AMPLITUDE_ERR = 0x40
    ERROR_EXT2_ENCODER_ANGLE_FULL_ERR = 0x80
    
    ERROR_EXT2_ENCODER_REVERSAL_ERR = 0x100
    ERROR_EXT2_ENCODER_OVER_MAX_PP = 0x200
    ERROR_EXT2_ENCODER_ECCENTRIC_ERR = 0x400
    ERROR_EXT2_ENCODER_RESERVE_0x800 = 0x800 
    
    ERROR_EXT2_ENCODER_RESERVE_0x1000 = 0x1000
    ERROR_EXT2_ENCODER_RESERVE_0x2000 = 0x2000
    ERROR_EXT2_ENCODER_RESERVE_0x4000 = 0x4000
    ERROR_EXT2_ENCODER_RESERVE_0x8000 = 0x8000
    
    ERROR_EXT2_ENCODER_RESERVE_0x10000 = 0x10000
    ERROR_EXT2_ENCODER_INNER_ROTOR_NEAR = 0x20000
    ERROR_EXT2_ENCODER_INNER_ROTOR_FAR = 0x40000
    ERROR_EXT2_ENCODER_OUTER_ROTOR_NEAR = 0x80000
    
    ERROR_EXT2_ENCODER_OUTER_ROTOR_FAR = 0x100000
    ERROR_EXT2_ENCODER_RESERVE_0x200000 = 0x200000 
    ERROR_EXT2_ENCODER_RESERVE_0x400000 = 0x400000
    ERROR_EXT2_ENCODER_RESERVE_0x800000 = 0x800000
    
    ERROR_EXT2_ENCODER_RESERVE_0x1000000 = 0x1000000
    ERROR_EXT2_ENCODER_RESERVE_0x2000000 = 0x2000000
    ERROR_EXT2_ENCODER_RESERVE_0x4000000 = 0x4000000
    ERROR_EXT2_ENCODER_RESERVE_0x8000000 = 0x8000000
    
    ERROR_EXT2_ENCODER_RESERVE_0x10000000 = 0x10000000
    ERROR_EXT2_ENCODER_RESERVE_0x20000000 = 0x20000000
    ERROR_EXT2_ENCODER_RESERVE_0x40000000 = 0x40000000
    ERROR_EXT2_ENCODER_RESERVE_0x80000000 = 0x80000000

class FSAErrorCodeExt2LinerHall(Enum):
    ERROR_EXT2_LINER_HALL_NOT_CAIL = 0x04
    ERROR_EXT2_LINER_HALL_AMP_ERR = 0x08
    
    ERROR_EXT2_LINER_HALL_MED_ERR = 0x10
    ERROR_EXT2_LINER_HALL_MIN_ERR = 0x20
    ERROR_EXT2_LINER_HALL_MAX_ERR = 0x40
    ERROR_EXT2_LINER_HALL_ANGLE_OFFSET_ERR = 0x80
    
    ERROR_EXT2_LINER_HALL_REVERSAL_ERR = 0x100
    ERROR_EXT2_LINER_HALL_PORT_NULL = 0x200
    ERROR_EXT2_LINER_HALL_AMP_ARRAY_ERR = 0x400
    ERROR_EXT2_LINER_HALL_ANGLE_OFFSET_AVEG_ERR = 0x800
    
    ERROR_EXT2_LINER_HALL_MED_AVEG_ERR = 0x1000
    ERROR_EXT2_LINER_HALL_PHASE_DELTA_ERR = 0x2000
    ERROR_EXT2_LINER_HALL_RESERVE_0x4000 = 0x4000 
    ERROR_EXT2_LINER_HALL_RESERVE_0x8000 = 0x8000
    
    ERROR_EXT2_LINER_HALL_RESERVE_0x10000 = 0x10000
    ERROR_EXT2_LINER_HALL_RESERVE_0x20000 = 0x20000 
    ERROR_EXT2_LINER_HALL_RESERVE_0x40000 = 0x40000
    ERROR_EXT2_LINER_HALL_RESERVE_0x80000 = 0x80000
    
    ERROR_EXT2_LINER_HALL_RESERVE_0x100000 = 0x100000
    ERROR_EXT2_LINER_HALL_RESERVE_0x200000 = 0x200000
    ERROR_EXT2_LINER_HALL_RESERVE_0x400000 = 0x400000
    ERROR_EXT2_LINER_HALL_RESERVE_0x800000 = 0x800000
    
    ERROR_EXT2_LINER_HALL_RESERVE_0x1000000 = 0x1000000
    ERROR_EXT2_LINER_HALL_RESERVE_0x2000000 = 0x2000000
    ERROR_EXT2_LINER_HALL_RESERVE_0x4000000 = 0x4000000
    ERROR_EXT2_LINER_HALL_RESERVE_0x8000000 = 0x8000000
    
    ERROR_EXT2_LINER_HALL_RESERVE_0x10000000 = 0x10000000
    ERROR_EXT2_LINER_HALL_RESERVE_0x20000000 = 0x20000000
    ERROR_EXT2_LINER_HALL_RESERVE_0x40000000 = 0x40000000
    ERROR_EXT2_LINER_HALL_RESERVE_0x80000000 = 0x80000000


class FSAInputMode:
    INPUT_MODE_INACTIVE = 0
    INPUT_MODE_PASSTHROUGH = 1
    INPUT_MODE_VEL_RAMP = 2
    INPUT_MODE_POS_FILTER = 3
    INPUT_MODE_TRAP_TRAJ = 5
    INPUT_MODE_TORQUE_RAMP = 6


class FSAMotorType:
    TYPE_MOTOR_NULL = 0
    TYPE_JIOALONG = 1
    FSA130_20V0 = 2
    FSA100_15V0 = 3
    FSA80_10V0 = 4
    FSA60_08V0 = 5
    FSA36_08V0 = 6
    FSA25_08V0 = 7
    FSA36_10V0 = 8
    FSA130_20V1 = 9
    FSA100_15V1 = 10
    FSA80_10V1 = 11
    FSA60_08V1 = 12
    FSA45_15V1 = 13


class FSAHardwareType:
    TYPE_HARDWARE_NULL = 0
    TYPE_ODrive_V36 = 1
    TYPE_H95V104 = 2
    TYPE_H66V104 = 3
    TYPE_H46V104 = 4
    TYPE_H30V303 = 5
    TYPE_HARDWARE_8323 = 6
    TYPE_H49V100 = 7
    TYPE_H66V106 = 8
    TYPE_H88V100 = 9
    TYPE_H106V100 = 10
    TYPE_H142V100 = 11
    TYPE_H142V102 = 12
    TYPE_H66V202 = 13
    TPYE_HARDWARE_8353 = 15
    TYPE_H100V100 = 16
    TYPE_H58V100 = 17
    Hardware_ALL = 18


class FSAMotorDirection:
    ABC = 1
    ACB = -1


class FSAMotorVBUS:
    VBUS_DEFAULT = 48
    VBUS_36V = 36
    VBUS_48V = 48


class FSAMotorPolePairs:
    POLE_PAIRS_7 = 7
    POLE_PAIRS_10 = 10
    POLE_PAIRS_21 = 21


class FSAMotorMaxSpeed:
    MAX_SPEED_1000 = 1000  # rpm
    MAX_SPEED_1600 = 1600  # rpm
    MAX_SPEED_2000 = 2000  # rpm
    MAX_SPEED_3000 = 3000  # rpm
    MAX_SPEED_3400 = 3400  # rpm
    MAX_SPEED_4800 = 4800  # rpm
    MAX_SPEED_7000 = 7000  # rpm


class FSAMotorMaxAcceleration:
    MAX_ACCELERATION_6000 = 6000  # rpm/s
    MAX_ACCELERATION_10000 = 10000  # rpm/s
    MAX_ACCELERATION_30000 = 30000  # rpm/s
    MAX_ACCELERATION_60000 = 60000  # rpm/s


class FSAMotorVIBCADCRatio:
    _ADC_PRECISION = 4096.0
    _ADC_SHUNT_RESISTANCE_2mR = 0.002
    _ADC_SHUNT_RESISTANCE_05mR = 0.0005
    _ADC_REF_V = 3.3
    _ADC_AMPLIFIER = 20
    VIBC_ADC_RATIO_2mR = (
        _ADC_REF_V / _ADC_PRECISION / _ADC_SHUNT_RESISTANCE_2mR / _ADC_AMPLIFIER
    )
    VIBC_ADC_RATIO_05mR = (
        _ADC_REF_V / _ADC_PRECISION / _ADC_SHUNT_RESISTANCE_05mR / _ADC_AMPLIFIER
    )


class FSAMotorVBUSADCRatio:
    _ADC_PRECISION = 4096.0
    _UP_SHUNT_RESISTANCE = 18000
    _DOWN_SHUNT_RESISTANCE = 1000
    _ADC_REF_V = 3.3
    VBUS_ADC_RATIO = (
        _ADC_REF_V
        / _ADC_PRECISION
        * (_UP_SHUNT_RESISTANCE + _DOWN_SHUNT_RESISTANCE)
        / _DOWN_SHUNT_RESISTANCE
    )


class FSAEncoderDirection:
    DIRECTION_CW = -1
    DIRECTION_CCW = 1


class FSAEncoderResolution:
    RESOLUTION_4000 = 4000  # 4000 pulse/rev
