//#############################################################################
// $Copyright:
// Copyright (C) 2017-2023 Texas Instruments Incorporated - http://www.ti.com/
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//   Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
//
//   Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the
//   distribution.
//
//   Neither the name of Texas Instruments Incorporated nor the names of
//   its contributors may be used to endorse or promote products derived
//   from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//#############################################################################


//! \file   /solutions/tidm_02010_dmpfc/common/include/user_mtr2.h
//! \brief  Contains the user related definitions for motor 2
//!         This file is shared with all of devices
//!


#ifndef USER_MTR2_H
#define USER_MTR2_H


//*****************************************************************************
//
// If building with a C++ compiler, make all of the definitions in this header
// have a C binding.
//
//*****************************************************************************
#ifdef __cplusplus
extern "C"
{
#endif

//*****************************************************************************
//
//! \defgroup USER_MTR2 USER_MTR2
//! @{
//
//*****************************************************************************

//
// the includes

// modules
#include "userParams.h"

#include "user_dms.h"

// the defines
// Supports F28002x, F28003x... devices

//******************************************************************************
#if defined(DMCPFC_REV3P2) || defined(DMCPFC_REV3P1)
//! \brief Defines the nominal DC bus voltage, V
//!
#define USER_M2_NOMINAL_DC_BUS_VOLTAGE_V         (200.0f)

//! \brief Defines the maximum voltage at the AD converter
//! Full scale voltage of AD converter, not the current voltage
#define USER_M2_ADC_FULL_SCALE_VOLTAGE_V         (404.1292683f)     // 8.2k

//! \brief Defines the analog voltage filter pole location, Hz
//!
#define USER_M2_VOLTAGE_FILTER_POLE_Hz           (405.1455619f)     // 8.2k/470nF

#if !defined(MOTOR2_DCLINKSS)
//! \brief Defines the maximum current at the AD converter
//!
#define USER_M2_ADC_FULL_SCALE_CURRENT_A         (8.45f)           // 0.1/3.3k

//! \brief Defines the sign of the current_sf based on
//!        the polarity of the current feedback circuit
//!
//!        the "sign" = -1.0f if the current feedback polarity is positive that
//!        means the same pin of the shunt resistor is connected to ground and
//!        is also connected to the inverting pin of the operational amplifier
//!
//!        the "sign" = 1.0f if the current feedback polarity is negative that
//!        means the same pin of the shunt resistor is connected to ground and
//!        is also connected to the noninverting pin of the operational amplifier
#define USER_M2_SIGN_CURRENT_SF     (1.0f)

//! \brief ADC current offsets for A, B, and C phases
#define USER_M2_IA_OFFSET_A     (4.225f)        // ~0.5 * USER_M2_ADC_FULL_SCALE_CURRENT_A
#define USER_M2_IB_OFFSET_A     (4.225f)        // ~0.5 * USER_M2_ADC_FULL_SCALE_CURRENT_A
#define USER_M2_IC_OFFSET_A     (4.225f)        // ~0.5 * USER_M2_ADC_FULL_SCALE_CURRENT_A

// The maximum offset setting value to check if offset calibration is correct
#define USER_M2_IA_OFFSET_A_MAX     (USER_M2_IA_OFFSET_A * 1.10f)
#define USER_M2_IB_OFFSET_A_MAX     (USER_M2_IB_OFFSET_A * 1.10f)
#define USER_M2_IC_OFFSET_A_MAX     (USER_M2_IC_OFFSET_A * 1.10f)

// The minimum offset setting value to check if offset calibration is correct
#define USER_M2_IA_OFFSET_A_MIN     (USER_M2_IA_OFFSET_A * 0.90f)
#define USER_M2_IB_OFFSET_A_MIN     (USER_M2_IB_OFFSET_A * 0.90f)
#define USER_M2_IC_OFFSET_A_MIN     (USER_M2_IC_OFFSET_A * 0.90f)

#else   // MOTOR2_DCLINKSS
#define USER_M2_ADC_FULL_SCALE_CURRENT_A         (8.45f)           // 0.1/3.3k

//! \brief Defines the sign of the current_sf based on
//!        the polarity of the current feedback circuit
//!
//!        the "sign" = -1.0f if the current feedback polarity is positive that
//!        means the same pin of the shunt resistor is connected to ground and
//!        is also connected to the noninverting pin of the operational amplifier
//!
//!        the "sign" = 1.0f if the current feedback polarity is negative that
//!        means the same pin of the shunt resistor is connected to ground and
//!        is also connected to the inverting pin of the operational amplifier
#define USER_M2_SIGN_CURRENT_SF         (-1.0f)

//! \brief ADC current offsets for dc-link
//#define USER_M2_IDC_OFFSET_A            (-7.689176245f)     // 4.7K(D)/47.5K(S)
#define USER_M2_IDC_OFFSET_A            (-6.95149517f)      // Calibration value

// The dc-link offset setting value to check if offset calibration is correct
#define USER_M2_IDC_OFFSET_A_MAX       (USER_M2_IDC_OFFSET_A * 1.10f)
#define USER_M2_IDC_OFFSET_A_MIN       (USER_M2_IDC_OFFSET_A * 0.90f)

// Tdt  =  1.80us (Dead-time between top and bottom switch)
// Tpd  =  0.75us (Gate driver propagation delay)
// Tr   =  0.35us (Rise time of amplifier including power switches turn on time)
// Ts   =  1.0us  (Settling time of amplifier)
// Ts&h =  180ns  (ADC sample&holder = (16 + 2) SYSCLK)
// T_MinAVDuration = Tdt+Tpd+Tr+Ts+Ts&h
//                 = 1800+750+350+1000+180 = 4080ns => 410 SYSCLK cycles
// T_SampleDelay   = Tdt+Tpd+Tr+Ts
//                 = 1800+750+350+1000 = 3900ns => 390 SYSCLK cycles
//! \brief Defines the minimum duration, Clock Cycle
//!
#define USER_M2_DCLINKSS_MIN_DURATION   (410U)

//! \brief Defines the sample delay, Clock Cycle
//!
#define USER_M2_DCLINKSS_SAMPLE_DELAY   (390U)
#endif  // MOTOR2_DCLINKSS

//! \brief ADC voltage offsets for A, B, and C phases
#define USER_M2_VA_OFFSET_SF        (0.497367263f)
#define USER_M2_VB_OFFSET_SF        (0.496987134f)
#define USER_M2_VC_OFFSET_SF        (0.501186967f)

//! \brief Defines the number of current sensors
#define USER_M2_NUM_CURRENT_SENSORS         (3)

//! \brief Defines the number of voltage sensors
#define USER_M2_NUM_VOLTAGE_SENSORS         (3)

//! \brief motor lost phase current threshold
#define USER_M2_LOST_PHASE_CURRENT_A        (0.2f)

//! \brief motor unbalance ratio percent threshold
#define USER_M2_UNBALANCE_RATIO             (0.2f)

//! \brief motor over load power threshold
#define USER_M2_OVER_LOAD_POWER_W           (50.0f)

//! \brief motor stall current threshold
#define USER_M2_STALL_CURRENT_A             (10.0f)

//! \brief motor fault check current threshold
#define USER_M2_FAULT_CHECK_CURRENT_A       (0.2f)

//! \brief motor failed maximum speed threshold
#define USER_M2_FAIL_SPEED_MAX_Hz           (500.0f)

//! \brief motor failed minimum speed threshold
#define USER_M2_FAIL_SPEED_MIN_Hz           (5.0f)

//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
//!
#ifndef PFC_DISABLE
#if defined(_F28002x)       // 100MHz CPU Clock
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.01152738f))   // 18kHz/72kHz PFC
#define USER_M2_PWM_TBPRD_NUM       (2776U)

//! \brief Defines the number of pwm clock ticks per isr clock tick
//#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK      (1)       // fpwm=6kHz
//#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK      (2)       // fpwm=12kHz
#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK        (3)       // fpwm=18kHz->6kHz
#elif defined(_F28003x)     // 120MHz CPU Clock
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.00720288f))   // 18kHz/72kHz PFC
#define USER_M2_PWM_TBPRD_NUM       (3332U)

//! \brief Defines the number of pwm clock ticks per isr clock tick
//#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK      (1)       // fpwm=6kHz
//#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK      (2)       // fpwm=12kHz
#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK        (3)       // fpwm=18kHz->6kHz

#elif defined(_F280013x)     // 120MHz CPU Clock
//#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.00720288f))   // 18kHz/72kHz PFC
//#define USER_M2_PWM_TBPRD_NUM       (3332U)
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(12.00480192f))   // 12kHz/72kHz PFC
#define USER_M2_PWM_TBPRD_NUM       (4998U)
//#define USER_M2_PWM_FREQ_kHz        ((float32_t)(6.00240096f))   // 6kHz/72kHz PFC
//#define USER_M2_PWM_TBPRD_NUM       (9996U)

//! \brief Defines the number of pwm clock ticks per isr clock tick
#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK      (1)       // fpwm=6kHz
//#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK      (2)       // fpwm=12kHz
//#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK        (3)       // fpwm=18kHz->6kHz

#elif defined(_F280015x)     // 120MHz CPU Clock
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.00720288f))   // 18kHz/72kHz PFC
#define USER_M2_PWM_TBPRD_NUM       (3332)

//! \brief Defines the number of pwm clock ticks per isr clock tick
//#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK      (1)       // fpwm=6kHz
//#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK      (2)       // fpwm=12kHz
#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK        (3)       // fpwm=18kHz->6kHz

#else  // !_F28002x & !_F28003x
#error Not select the right device for this reference design
#endif  // !_F28002x & !_F28003x
#else   // PFC_DISABLE
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.0f))
#define USER_M2_PWM_TBPRD_NUM       (USER_SYSTEM_FREQ_MHz * 1000 / USER_M2_PWM_FREQ_kHz / 2)

//! \brief Defines the number of pwm clock ticks per isr clock tick
//#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK      (1)       // fpwm=6kHz
//#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK      (2)       // fpwm=12kHz
#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK        (3)       // fpwm=18kHz->6kHz
#endif  // PFC_DISABLE

// end of DMCPFC_REV3P2 | DMCPFC_REV3P1
//******************************************************************************
#elif defined(HVMTRPFC_REV1P1)
//! \brief Defines the nominal DC bus voltage, V
//!
#define USER_M2_NOMINAL_DC_BUS_VOLTAGE_V         (220.0f)

//! \brief Defines the maximum voltage at the AD converter
//! Full scale voltage of AD converter, not the current voltage
#define USER_M2_ADC_FULL_SCALE_VOLTAGE_V         (409.6f)

//! \brief Defines the analog voltage filter pole location, Hz
//!
#define USER_M2_VOLTAGE_FILTER_POLE_Hz           (372.5f)

//! \brief Defines the maximum current at the AD converter
//!
#define USER_M2_ADC_FULL_SCALE_CURRENT_A         (19.89f)

//! \brief Defines the sign of the current_sf based on
//!        the polarity of the current feedback circuit
//!
//!        the "sign" = -1.0f if the current feedback polarity is positive that
//!        means the same pin of the shunt resistor is connected to ground and
//!        is also connected to the inverting pin of the operational amplifier
//!
//!        the "sign" = 1.0f if the current feedback polarity is negative that
//!        means the same pin of the shunt resistor is connected to ground and
//!        is also connected to the noninverting pin of the operational amplifier
#define USER_M2_SIGN_CURRENT_SF     (1.0f)

//! \brief ADC current offsets for A, B, and C phases
#define USER_M2_IA_OFFSET_A     (9.945f)        // ~0.5 * USER_M2_ADC_FULL_SCALE_CURRENT_A
#define USER_M2_IB_OFFSET_A     (9.945f)        // ~0.5 * USER_M2_ADC_FULL_SCALE_CURRENT_A
#define USER_M2_IC_OFFSET_A     (9.945f)        // ~0.5 * USER_M2_ADC_FULL_SCALE_CURRENT_A

// The maximum offset setting value to check if offset calibration is correct
#define USER_M2_IA_OFFSET_A_MAX     (USER_M2_IA_OFFSET_A * 1.10f)
#define USER_M2_IB_OFFSET_A_MAX     (USER_M2_IB_OFFSET_A * 1.10f)
#define USER_M2_IC_OFFSET_A_MAX     (USER_M2_IC_OFFSET_A * 1.10f)

// The minimum offset setting value to check if offset calibration is correct
#define USER_M2_IA_OFFSET_A_MIN     (USER_M2_IA_OFFSET_A * 0.90f)
#define USER_M2_IB_OFFSET_A_MIN     (USER_M2_IB_OFFSET_A * 0.90f)
#define USER_M2_IC_OFFSET_A_MIN     (USER_M2_IC_OFFSET_A * 0.90f)

//! \brief ADC voltage offsets for A, B, and C phases
#define USER_M2_VA_OFFSET_SF    (0.990514159f)
#define USER_M2_VB_OFFSET_SF    (0.986255884f)
#define USER_M2_VC_OFFSET_SF    (0.983381569f)

//! \brief Defines the number of current sensors
#define USER_M2_NUM_CURRENT_SENSORS         (3)

//! \brief Defines the number of voltage sensors
#define USER_M2_NUM_VOLTAGE_SENSORS         (3)

//! \brief motor lost phase current threshold
#define USER_M2_LOST_PHASE_CURRENT_A        (0.2f)

//! \brief motor unbalance ratio percent threshold
#define USER_M2_UNBALANCE_RATIO             (0.2f)

//! \brief motor over load power threshold
#define USER_M2_OVER_LOAD_POWER_W           (50.0f)

//! \brief motor stall current threshold
#define USER_M2_STALL_CURRENT_A             (10.0f)

//! \brief motor fault check current threshold
#define USER_M2_FAULT_CHECK_CURRENT_A       (0.2f)

//! \brief motor failed maximum speed threshold
#define USER_M2_FAIL_SPEED_MAX_Hz           (500.0f)

//! \brief motor failed minimum speed threshold
#define USER_M2_FAIL_SPEED_MIN_Hz           (5.0f)

//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
//!
#ifndef PFC_DISABLE
#if defined(_F28002x)       // 100MHz CPU Clock
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.01152738f))   // 18kHz/72kHz PFC
#define USER_M2_PWM_TBPRD_NUM       (2776)
#elif defined(_F28003x)       // 120MHz CPU Clock
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.00720288f))   // 18kHz/72kHz PFC
#define USER_M2_PWM_TBPRD_NUM       (3332)
#elif defined(_F280013x)       // 120MHz CPU Clock
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.00720288f))   // 18kHz/72kHz PFC
#define USER_M2_PWM_TBPRD_NUM       (3332)
#elif defined(_F280015x)       // 120MHz CPU Clock
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.00720288f))   // 18kHz/72kHz PFC
#define USER_M2_PWM_TBPRD_NUM       (3332)
#else   // !_F28002x && !_F28003x
#error Not select the right device for this reference design
#endif  // !_F28002x && !_F28003x
#else  // PFC_DISABLE
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.0f))
#define USER_M2_PWM_TBPRD_NUM       (USER_SYSTEM_FREQ_MHz * 1000 / USER_M2_PWM_FREQ_kHz / 2)
#endif  // PFC_DISABLE

//! \brief Defines the number of pwm clock ticks per isr clock tick
//!        Note: Valid values are 1, 2 or 3 only
#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK        (3)       // fpwm=18kHz

// end of HVMTRPFC_REV1P1
//******************************************************************************
#elif defined(BSXL8323RH_REVB)
//! \brief Defines the nominal DC bus voltage, V
//!
#define USER_M2_NOMINAL_DC_BUS_VOLTAGE_V         (48.0f)

//! \brief Defines the maximum voltage at the AD converter
#define USER_M2_ADC_FULL_SCALE_VOLTAGE_V         (57.52845691f)

//! \brief Defines the analog voltage filter pole location, Hz
#define USER_M2_VOLTAGE_FILTER_POLE_Hz           (680.4839141f)

//! \brief Defines the maximum current at the AD converter
#define USER_M2_ADC_FULL_SCALE_CURRENT_A         (47.14285714f)     // gain = 10

//! \brief Defines the sign of the current_sf based on
//!        the polarity of the current feedback circuit
//!
//!        the "sign" = -1.0f if the current feedback polarity is positive that
//!        means the same pin of the shunt resistor is connected to ground and
//!        is also connected to the inverting pin of the operational amplifier
//!
//!        the "sign" = 1.0f if the current feedback polarity is negative that
//!        means the same pin of the shunt resistor is connected to ground and
//!        is also connected to the noninverting pin of the operational amplifier
#define USER_M2_SIGN_CURRENT_SF     (1.0f)

//! \brief ADC current offsets for A, B, and C phases
#define USER_M2_IA_OFFSET_A     (23.57f)        // ~0.5 * USER_M2_ADC_FULL_SCALE_CURRENT_A
#define USER_M2_IB_OFFSET_A     (23.57f)        // ~0.5 * USER_M2_ADC_FULL_SCALE_CURRENT_A
#define USER_M2_IC_OFFSET_A     (23.57f)        // ~0.5 * USER_M2_ADC_FULL_SCALE_CURRENT_A

// The maximum offset setting value to check if offset calibration is correct
#define USER_M2_IA_OFFSET_A_MAX     (USER_M2_IA_OFFSET_A * 1.10f)
#define USER_M2_IB_OFFSET_A_MAX     (USER_M2_IB_OFFSET_A * 1.10f)
#define USER_M2_IC_OFFSET_A_MAX     (USER_M2_IC_OFFSET_A * 1.10f)

// The minimum offset setting value to check if offset calibration is correct
#define USER_M2_IA_OFFSET_A_MIN     (USER_M2_IA_OFFSET_A * 0.90f)
#define USER_M2_IB_OFFSET_A_MIN     (USER_M2_IB_OFFSET_A * 0.90f)
#define USER_M2_IC_OFFSET_A_MIN     (USER_M2_IC_OFFSET_A * 0.90f)


//! \brief ADC voltage offsets for A, B, and C phases
#define USER_M2_VA_OFFSET_SF    (0.990514159f)
#define USER_M2_VB_OFFSET_SF    (0.986255884f)
#define USER_M2_VC_OFFSET_SF    (0.983381569f)

//! \brief Defines the number of current sensors
#define USER_M2_NUM_CURRENT_SENSORS         (3)

//! \brief Defines the number of voltage sensors
#define USER_M2_NUM_VOLTAGE_SENSORS         (3)

//! \brief motor lost phase current threshold
#define USER_M2_LOST_PHASE_CURRENT_A        (0.02f)

//! \brief motor unbalance ratio percent threshold
#define USER_M2_UNBALANCE_RATIO             (1.0f)

//! \brief motor over load power threshold
#define USER_M2_OVER_LOAD_POWER_W           (50.0f)

//! \brief motor stall current threshold
#define USER_M2_STALL_CURRENT_A             (10.0f)

//! \brief motor fault check current threshold
#define USER_M2_FAULT_CHECK_CURRENT_A       (0.2f)

//! \brief motor failed maximum speed threshold
#define USER_M2_FAIL_SPEED_MAX_Hz           (1000.0f)

//! \brief motor failed minimum speed threshold
#define USER_M2_FAIL_SPEED_MIN_Hz           (5.0f)

#if defined(_F28002x)       // 100MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
//!
#ifndef PFC_DISABLE
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.01152738f))   // 18kHz/72kHz PFC
#define USER_M2_PWM_TBPRD_NUM       (2776)

#else
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.0f))

#define USER_M2_PWM_TBPRD_NUM       (USER_SYSTEM_FREQ_MHz * 1000 / USER_M2_PWM_FREQ_kHz / 2)
#endif  // PFC_DISABLE
#elif defined(_F28003x)       // 120MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
//!
#ifndef PFC_DISABLE
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.00720288f))   // 18kHz/72kHz PFC
#define USER_M2_PWM_TBPRD_NUM       (3332)

#else
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.0f))

#define USER_M2_PWM_TBPRD_NUM       (USER_SYSTEM_FREQ_MHz * 1000 / USER_M2_PWM_FREQ_kHz / 2)
#endif  // PFC_DISABLE
#elif defined(_F280013x)       // 120MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
//!
#ifndef PFC_DISABLE
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.00720288f))   // 18kHz/72kHz PFC
#define USER_M2_PWM_TBPRD_NUM       (3332)

#else
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.0f))

#define USER_M2_PWM_TBPRD_NUM       (USER_SYSTEM_FREQ_MHz * 1000 / USER_M2_PWM_FREQ_kHz / 2)
#endif  // PFC_DISABLE
#elif defined(_F280015x)       // 120MHz CPU Clock
//! \brief Defines the Pulse Width Modulation (PWM) frequency, kHz
//!
#ifndef PFC_DISABLE
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.00720288f))   // 18kHz/72kHz PFC
#define USER_M2_PWM_TBPRD_NUM       (3332)

#else
#define USER_M2_PWM_FREQ_kHz        ((float32_t)(18.0f))

#define USER_M2_PWM_TBPRD_NUM       (USER_SYSTEM_FREQ_MHz * 1000 / USER_M2_PWM_FREQ_kHz / 2)
#endif  // PFC_DISABLE
#else     // !_F28002x && !_F28003x  && !_F280013x
#error Not select the right device for this reference design
#endif    // !_F28002x && !_F28003x  && !_F280013x

//! \brief Defines the number of pwm clock ticks per isr clock tick
//!        Note: Valid values are 1, 2 or 3 only
#ifndef PFC_DISABLE
//#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK      (2)       // fpwm=12kHz
#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK        (3)       // fpwm=18kHz
#else
#define USER_M2_NUM_PWM_TICKS_PER_ISR_TICK        (3)
#endif  // PFC_DISABLE

// end of BSXL8323RH_REVB
//******************************************************************************
//******************************************************************************
#else
#error Not set the hardware board configuration for PFC
#endif

//! \brief Vbus used to calculate the voltage offsets A, B, and C
#define USER_M2_VBUS_OFFSET_V  (0.5*USER_M2_ADC_FULL_SCALE_VOLTAGE_V)     // =0.5*USER_M2_NOMINAL_DC_BUS_VOLTAGE_V


//! \brief Defines the maximum negative current to be applied in Id reference
//!
#define USER_M2_MAX_NEGATIVE_ID_REF_CURRENT_A    ((float32_t)(-2.0f))


//! \brief Defines the number of ISR clock ticks per current controller clock tick
//!
#define USER_M2_NUM_ISR_TICKS_PER_CURRENT_TICK    (1)


//! \brief Defines the number of ISR clock ticks per speed controller clock tick
//!
#define USER_M2_NUM_ISR_TICKS_PER_SPEED_TICK     (10)


//! \brief Defines the Pulse Width Modulation (PWM) period, usec
//!
#define USER_M2_PWM_PERIOD_usec     ((float32_t)1000.0f/USER_M2_PWM_FREQ_kHz)


//! \brief Defines the Interrupt Service Routine (ISR) frequency, Hz
//!
#define USER_M2_ISR_FREQ_Hz         (USER_M2_PWM_FREQ_kHz * (float32_t)1000.0f / (float32_t)USER_M2_NUM_PWM_TICKS_PER_ISR_TICK)

//! \brief Defines the SFRA sampling, Hz
//!
#define MOTOR2_SAMPLING_FREQ_Hz     USER_M2_ISR_FREQ_Hz

//! \brief Defines the Interrupt Service Routine (ISR) period, usec
//!
#define USER_M2_ISR_PERIOD_usec     (USER_M2_PWM_PERIOD_usec * (float32_t)USER_M2_NUM_PWM_TICKS_PER_ISR_TICK)


//! \brief Defines the direct voltage (Vd) scale factor
//!
#define USER_M2_VD_SF               ((float32_t)(0.95f))


//! \brief Defines the voltage scale factor for the system
//!
#define USER_M2_VOLTAGE_SF          (USER_M2_ADC_FULL_SCALE_VOLTAGE_V / 4096.0f)

//! \brief Defines the current scale factor for the system
//!
#define USER_M2_CURRENT_SF          (USER_M2_ADC_FULL_SCALE_CURRENT_A / 4096.0f)


//! \brief Defines the current scale invert factor for the system
//!
#define USER_M2_CURRENT_INV_SF      (4096.0f / USER_M2_ADC_FULL_SCALE_CURRENT_A)


//! \brief Defines the analog voltage filter pole location, rad/s
//!
#define USER_M2_VOLTAGE_FILTER_POLE_rps  (MATH_TWO_PI * USER_M2_VOLTAGE_FILTER_POLE_Hz)

//! \brief Defines the maximum Vs magnitude in per units allowed
//! \brief This value sets the maximum magnitude for the output of the Id and
//! \brief Iq PI current controllers. The Id and Iq current controller outputs
//! \brief are Vd and Vq. The relationship between Vs, Vd, and Vq is:
//! \brief Vs = sqrt(Vd^2 + Vq^2).  In this FOC controller, the Vd value is set
//! \brief equal to USER_MAX_VS_MAG*USER_VD_MAG_FACTOR.
//! \brief so the Vq value is set equal to sqrt(USER_MAX_VS_MAG^2 - Vd^2).
//!
//! \brief Set USER_MAX_VS_MAG = 0.5 for a pure sinewave with a peak at
//! \brief SQRT(3)/2 = 86.6% duty cycle.  No current reconstruction
//! \brief is needed for this scenario.
//!
//! \brief Set USER_MAX_VS_MAG = 1/SQRT(3) = 0.5774 for a pure sinewave
//! \brief with a peak at 100% duty cycle.  Current reconstruction
//! \brief will be needed for this scenario (Lab08).
//!
//! \brief Set USER_MAX_VS_MAG = 2/3 = 0.6666 to create a trapezoidal
//! \brief voltage waveform.  Current reconstruction will be needed
//! \brief for this scenario (Lab08).
//!
//! \brief For space vector over-modulation, see lab08 for details on
//! \brief system requirements that will allow the SVM generator to
//! \brief go all the way to trapezoidal.
//!
//#define USER_M2_MAX_VS_MAG_PU            (0.66f)
//#define USER_M2_MAX_VS_MAG_PU            (0.57f)
#define USER_M2_MAX_VS_MAG_PU              (0.5f)


//! \brief Defines the reference Vs magnitude in per units allowed
//! \      Set the value equal from 0.5 to 0.95 of the maximum Vs magnitude
#define USER_M2_VS_REF_MAG_PU             (float32_t)(0.8f) * USER_MAX_VS_MAG_PU)

//! \brief Defines the R/L excitation frequency, Hz
//!
#define USER_M2_R_OVER_L_EXC_FREQ_Hz  ((float32_t)(300.0f))


//! \brief Defines the R/L Kp scale factor, pu
//! \brief Kp used during R/L is USER_M2_R_OVER_L_KP_SF * USER_M2_NOMINAL_DC_BUS_VOLTAGE_V / USER_MOTOR2_MAX_CURRENT_A;
//!
#define USER_M2_R_OVER_L_KP_SF        ((float32_t)(0.02f))


//! \brief Defines maximum acceleration for the estimation speed profiles, Hz/sec
//!
#define USER_M2_MAX_ACCEL_Hzps        ((float32_t)(2.0f))


//! \brief Defines the controller execution period, usec
//!
#define USER_M2_CTRL_PERIOD_usec      (USER_M2_ISR_PERIOD_usec)


//! \brief Defines the controller execution period, sec
//!
#define USER_M2_CTRL_PERIOD_sec       ((float32_t)USER_M2_CTRL_PERIOD_usec/(float32_t)1000000.0f)


//! \brief Defines the IdRated delta to use during estimation
//!
#define USER_M2_IDRATED_DELTA_A                 ((float32_t)(0.0001f))

//! \brief Defines the forced angle frequency, Hz
#define USER_M2_FORCE_ANGLE_FREQ_Hz             ((float32_t)(1.0))

//! \brief Defines the forced angle acceleration, Hz
#define USER_M2_FORCE_ANGLE_ACCEL_Hzps          ((float32_t)(10.0))

//! \brief Defines the fraction of IdRated to use during inductance estimation
//!
#define USER_M2_IDRATED_FRACTION_FOR_L_IDENT    ((float32_t)(0.5f))


//! \brief Defines the fraction of SpeedMax to use during inductance estimation
//!
#define USER_M2_SPEEDMAX_FRACTION_FOR_L_IDENT  ((float32_t)(1.0f))


//! \brief Defines the Power Warp gain for computing Id reference
//! \brief If motor parameters are known, set this gain to:
//! \brief USER_M2_PW_GAIN = SQRT(1.0 + USER_MOTOR2_Rr_Ohm / USER_MOTOR2_Rs_Ohm)
//!
#define USER_M2_PW_GAIN                        ((float32_t)(1.0f))


//! \brief Defines the pole location for the DC bus filter, rad/sec
//!
#define USER_M2_DCBUS_POLE_rps                  ((float32_t)(100.0f))


//! \brief Defines the pole location for the voltage and current offset estimation, rad/s
//!
#define USER_M2_OFFSET_POLE_rps                 ((float32_t)(20.0f))


//! \brief Defines the pole location for the speed control filter, rad/sec
//!
#define USER_M2_SPEED_POLE_rps                  ((float32_t)(100.0f))


//! \brief Defines the near zero speed limit for electrical frequency estimation, Hz
//!        The flux integrator uses this limit to regulate flux integration
//!
#define USER_M2_FREQ_NEARZEROSPEEDLIMIT_Hz     ((float32_t)(1.0f))


//! \brief Defines the pole location for the direction filter, rad/sec
//!
#define USER_M2_DIRECTION_POLE_rps             (MATH_TWO_PI * (float32_t)10.0f)


//! \brief Defines the pole location for the second direction filter, rad/sec
//!
#define USER_M2_DIRECTION_POLE_2_rps           (MATH_TWO_PI * (float32_t)100.0f)


//! \brief Defines the pole location for the flux estimation, rad/sec
//!
#define USER_M2_FLUX_POLE_rps                  ((float32_t)(10.0f))


//! \brief Defines the pole location for the R/L estimation, rad/sec
//!
#define USER_M2_R_OVER_L_POLE_rps              ((float32_t)10.0f)


//! \brief Defines the convergence factor for the estimator
//!
#define USER_M2_EST_KAPPAQ                     ((float32_t)(1.5f))


//! brief Define the Kp gain for Field Weakening Control
#define USER_M2_FWC_KP                 0.05

//! brief Define the Ki gain for Field Weakening Control
#define USER_M2_FWC_KI                 0.0002

//! brief Define the maximum current vector angle for Field Weakening Control
#define USER_M2_FWC_MAX_ANGLE          -75.0                        // degree
#define USER_M2_FWC_MAX_ANGLE_RAD      USER_M2_FWC_MAX_ANGLE / 180.0f * MATH_PI

//! brief Define the minimum current vector angle for Field Weakening Control
#define USER_M2_FWC_MIN_ANGLE          0.0                          // degree
#define USER_M2_FWC_MIN_ANGLE_RAD      USER_M2_FWC_MIN_ANGLE / 180.0f * MATH_PI


//! \brief Defines the number of motor over load setting time
//!  timer base = 5ms, 1s
#define USER_M2_OVER_LOAD_TIME_SET          (200)

//! \brief Defines the number of motor stall setting time
//!  timer base = 5ms, 1s
#define USER_M2_STALL_TIME_SET              (1000)

//! \brief Defines the number of phase unbalance setting time
//!  timer base = 5ms, 3s
#define USER_M2_UNBALANCE_TIME_SET          (4000)

//! \brief Defines the number of lost phase setting time
//!  timer base = 5ms, 10s
#define USER_M2_LOST_PHASE_TIME_SET         (4000)

//! \brief Defines the number of over speed setting time
//!  timer base = 5ms, 3s
#define USER_M2_OVER_SPEED_TIME_SET         (600)

//! \brief Defines the number of startup failed setting time
//!  timer base = 5ms, 5s
#define USER_M2_STARTUP_FAIL_TIME_SET       (4000)

//! \brief Defines the number of over load setting times
//!
#define USER_M2_OVER_CURRENT_TIMES_SET      (5)

//! \brief Defines the number of stop wait time
//!  timer base = 5ms, 30s
#define USER_M2_STOP_WAIT_TIME_SET          (100)

//! \brief Defines the number of restart wait time
//!  timer base = 5ms, 10s
#define USER_M2_RESTART_WAIT_TIME_SET       (2000)

//! \brief Defines the number of restart time
//!
#define USER_M2_START_TIMES_SET             (3)

//! \brief Defines the current filter pole location, Hz
#define USER_M2_IS_FILTER_POLE_Hz           (USER_M2_PWM_FREQ_kHz * 500.0f)      // Half of PWM frequency

//! \brief Defines the current filter pole location, rad/s
//!
#define USER_M2_IS_FILTER_POLE_rps          (MATH_TWO_PI * USER_M2_IS_FILTER_POLE_Hz)


//! \brief Defines the voltage filter pole location, Hz
#define USER_M2_VS_FILTER_POLE_Hz           (USER_M2_PWM_FREQ_kHz * 2000.0f)     // twice of PWM frequency

//! \brief Defines the voltage filter pole location, rad/s
//!
#define USER_M2_VS_FILTER_POLE_rps          (MATH_TWO_PI * USER_M2_VS_FILTER_POLE_Hz)

//==============================================================================
// Motor defines

#define USER_MOTOR2 Estun_EMJ_04APB22          //*Tested, Load
//#define USER_MOTOR2 Anaheim_BLWS235D            //*Tested
//#define USER_MOTOR2 Baldor_BSM90N175         //*Tested
//#define USER_MOTOR2 Teknic_M2310PLN04K          //*Tested
//#define USER_MOTOR2 Marathon_N56PNRA10102
//#define USER_MOTOR2 ziehlab_Fan160hv          //*Tested
//#define USER_MOTOR2 industry_Fan220hv         //*Tested
//#define USER_MOTOR2 Dhvac_Fan110hv
//#define USER_MOTOR2 Anaheim_BLY172S_24V   //*Tested

#if (USER_MOTOR2 == Estun_EMJ_04APB22)
#define USER_MOTOR2_TYPE                    MOTOR_TYPE_PM
#define USER_MOTOR2_NUM_POLE_PAIRS          (4)
#define USER_MOTOR2_Rr_Ohm                  (0.0)

#define USER_MOTOR2_Rs_Ohm                  (2.62655902)
#define USER_MOTOR2_Ls_d_H                  (0.00860825367)
#define USER_MOTOR2_Ls_q_H                  (0.00860825367)
#define USER_MOTOR2_RATED_FLUX_VpHz         (0.377903223)

#define USER_MOTOR2_MAGNETIZING_CURRENT_A   (NULL)
#define USER_MOTOR2_RES_EST_CURRENT_A       (1.0f)
#define USER_MOTOR2_IND_EST_CURRENT_A       (-0.5f)
#define USER_MOTOR2_MAX_CURRENT_A           (3.82f)
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz        (40.0f)
#define USER_MOTOR2_NUM_ENC_SLOTS           (2500.0)
#define USER_MOTOR2_INERTIA_Kgm2            (3.100017e-5)

#define USER_MOTOR2_RATED_VOLTAGE_V         (200.0f)
#define USER_MOTOR2_RATED_SPEED_KRPM        (3.0f)

#define USER_MOTOR2_FREQ_MIN_Hz             (5.0f)            // Hz
#define USER_MOTOR2_FREQ_MAX_Hz             (400.0f)          // Hz

#define USER_MOTOR2_FREQ_LOW_Hz             (5.0f)            // Hz
#define USER_MOTOR2_FREQ_HIGH_Hz            (200.0f)          // Hz
#define USER_MOTOR2_VOLT_MIN_V              (9.0f)            // Volt
#define USER_MOTOR2_VOLT_MAX_V              (180.0f)          // Volt

#define USER_MOTOR2_ALIGN_CURRENT_A         (1.0f)           //
#define USER_MOTOR2_STARTUP_CURRENT_A       (1.0f)           //
#define USER_MOTOR2_OVER_CURRENT_A          (3.6f)           //
#define USER_MOTOR2_LOCK_CURRENT_A          (1.0f)           // 1.0A

#define USER_MOTOR2_DELAY_FLYST_s           (uint16_t)(0.5f * USER_M2_ISR_FREQ_Hz) // 0.5s
#define USER_MOTOR2_SPEED_FLYST_Hz          (3.0f)
#define USER_MOTOR2_SPEED_START_Hz          (20.0f)           //
#define USER_MOTOR2_SPEED_FORCE_Hz          (30.0)

#define USER_MOTOR2_ACCEL_START_Hzps        (5.0f)           //
#define USER_MOTOR2_ACCEL_MAX_Hzps          (10.0f)          //

// Only for eSMO
#define USER_MOTOR2_KSLIDE_MAX             (0.25f)       // 2.0f
#define USER_MOTOR2_KSLIDE_MIN             (0.10f)

#define USER_MOTOR2_PLL_KP_MAX             (10.0f)
#define USER_MOTOR2_PLL_KP_MIN             (2.0f)
#define USER_MOTOR2_PLL_KP_SF              (5.0f)

#define USER_MOTOR2_BEMF_THRESHOLD         (0.5f)
#define USER_MOTOR2_BEMF_KSLF_FC_SF        (1.0f)
#define USER_MOTOR2_THETA_OFFSET_SF        (1.0f)       // 2.5f
#define USER_MOTOR2_SPEED_LPF_FC_Hz        (200.0f)     // 100.0f

#elif (USER_MOTOR2 == Baldor_BSM90N175)
#define USER_MOTOR2_TYPE                    MOTOR_TYPE_PM
#define USER_MOTOR2_NUM_POLE_PAIRS          (4)
#define USER_MOTOR2_Rr_Ohm                  (0.0)
#define USER_MOTOR2_Rs_Ohm                  (2.66273594f)
#define USER_MOTOR2_Ls_d_H                  (0.00943629723f)
#define USER_MOTOR2_Ls_q_H                  (0.00943629723f)
#define USER_MOTOR2_RATED_FLUX_VpHz         (0.390171647f)
#define USER_MOTOR2_MAGNETIZING_CURRENT_A   (NULL)
#define USER_MOTOR2_RES_EST_CURRENT_A       (2.5f)      // A - 10~30% of rated current of the motor
#define USER_MOTOR2_IND_EST_CURRENT_A       (-2.0f)     // A - 10~30% of rated current of the motor, just enough to enable rotation
#define USER_MOTOR2_MAX_CURRENT_A           (4.5f)      // A - 30~150% of rated current of the motor
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz        (20.0f)     // Hz - 10~30% of rated frequency of the motor
#define USER_MOTOR2_NUM_ENC_SLOTS           (2500.0)
#define USER_MOTOR2_INERTIA_Kgm2            (6.327e-4)

#define USER_MOTOR2_RATED_VOLTAGE_V         (200.0f)
#define USER_MOTOR2_RATED_SPEED_KRPM        (3.0f)

#define USER_MOTOR2_FREQ_MIN_Hz              (5.0f)           // Hz
#define USER_MOTOR2_FREQ_MAX_Hz             (400.0f)         // Hz

#define USER_MOTOR2_FREQ_LOW_Hz             (10.0f)          // Hz
#define USER_MOTOR2_FREQ_HIGH_Hz            (200.0f)         // Hz
#define USER_MOTOR2_VOLT_MIN_V              (10.0f)          // Volt
#define USER_MOTOR2_VOLT_MAX_V              (200.0f)         // Volt

#define USER_MOTOR2_STARTUP_CURRENT_A       (0.5f)           //
#define USER_MOTOR2_OVER_CURRENT_A          (5.0f)           //
#define USER_MOTOR2_LOCK_CURRENT_A          (1.0f)           // 1.0A

#define USER_MOTOR2_SPEED_FLYST_Hz          (3.0f)
#define USER_MOTOR2_SPEED_START_Hz          (10.0f)          //
#define USER_MOTOR2_SPEED_FORCE_Hz          (20.0f)          //

#define USER_MOTOR2_ACCEL_START_Hzps        (5.0f)           //
#define USER_MOTOR2_ACCEL_MAX_Hzps          (10.0f)          //

#elif (USER_MOTOR2 == Marathon_N56PNRA10102)
#define USER_MOTOR2_TYPE                    MOTOR_TYPE_PM
#define USER_MOTOR2_NUM_POLE_PAIRS          (3)
#define USER_MOTOR2_Rr_Ohm                  (0.0)
#define USER_MOTOR2_Rs_Ohm                  (2.21499157)
#define USER_MOTOR2_Ls_d_H                  (0.0274559446)
#define USER_MOTOR2_Ls_q_H                  (0.0274559446)
#define USER_MOTOR2_RATED_FLUX_VpHz         (2.69975376)
#define USER_MOTOR2_MAGNETIZING_CURRENT_A   (NULL)
#define USER_MOTOR2_RES_EST_CURRENT_A       (2.5)
#define USER_MOTOR2_IND_EST_CURRENT_A       (-2.0)
#define USER_MOTOR2_MAX_CURRENT_A           (8.0)
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz        (20.0)
#define USER_MOTOR2_NUM_ENC_SLOTS           (2500.0)
#define USER_MOTOR2_INERTIA_Kgm2            (3.100017e-5)

#define USER_MOTOR2_RATED_VOLTAGE_V         (200.0)
#define USER_MOTOR2_RATED_SPEED_KRPM        (3.0)

#define USER_MOTOR2_FREQ_MIN_Hz              (5.0)            // Hz
#define USER_MOTOR2_FREQ_MAX_Hz             (400.0)          // Hz

#define USER_MOTOR2_FREQ_LOW_Hz             (10.0)           // Hz
#define USER_MOTOR2_FREQ_HIGH_Hz            (200.0)          // Hz
#define USER_MOTOR2_VOLT_MIN_V              (20.0)           // Volt
#define USER_MOTOR2_VOLT_MAX_V              (200.0)          // Volt

#define USER_MOTOR2_STARTUP_CURRENT_A       (2.5)            //
#define USER_MOTOR2_OVER_CURRENT_A          (9.5)            //

#define USER_MOTOR2_SPEED_START_Hz          (10.0)           //
#define USER_MOTOR2_SPEED_FORCE_Hz          (20.0)
#define USER_MOTOR2_ACCEL_START_Hz          (10.0)           //
#define USER_MOTOR2_ACCEL_MAX_Hzps          (100.0)          //

#define USER_MOTOR2_SPEED_FS_Hz             (3.0)

#elif (USER_MOTOR2 == Anaheim_BLY172S_24V)
#define USER_MOTOR2_TYPE                   MOTOR_TYPE_PM
#define USER_MOTOR2_NUM_POLE_PAIRS         (4)
#define USER_MOTOR2_Rr_Ohm                 (NULL)
#define USER_MOTOR2_Rs_Ohm                 (0.384005845f)
#define USER_MOTOR2_Ls_d_H                 (0.000639994687f)
#define USER_MOTOR2_Ls_q_H                 (0.000639994687f)
#define USER_MOTOR2_RATED_FLUX_VpHz        (0.0327013217f)
#define USER_MOTOR2_MAGNETIZING_CURRENT_A  (NULL)
#define USER_MOTOR2_RES_EST_CURRENT_A      (1.5f)
#define USER_MOTOR2_IND_EST_CURRENT_A      (-1.5f)
#define USER_MOTOR2_MAX_CURRENT_A          (7.5f)
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz       (40.0f)
#define USER_MOTOR2_NUM_ENC_SLOTS          (NULL)
#define USER_MOTOR2_INERTIA_Kgm2           (4.80185e-06f)

#define USER_MOTOR2_RATED_VOLTAGE_V        (24.0f)
#define USER_MOTOR2_RATED_SPEED_KRPM       (3.0f)

#define USER_MOTOR2_FREQ_MIN_Hz            (5.0f)
#define USER_MOTOR2_FREQ_MAX_Hz            (800.0f)

#define USER_MOTOR2_FREQ_LOW_Hz            (10.0f)
#define USER_MOTOR2_FREQ_HIGH_Hz           (400.0f)
#define USER_MOTOR2_VOLT_MIN_V             (1.5f)
#define USER_MOTOR2_VOLT_MAX_V             (24.0f)

#define USER_MOTOR2_ALIGN_CURRENT_A        (1.0f)
#define USER_MOTOR2_STARTUP_CURRENT_A      (1.0f)
#define USER_MOTOR2_OVER_CURRENT_A         (6.5f)
#define USER_MOTOR2_LOCK_CURRENT_A         (1.0f)

#define USER_MOTOR2_DELAY_FLYST_s          (uint16_t)(0.5f * USER_M2_ISR_FREQ_Hz) // 0.5s
#define USER_MOTOR2_SPEED_FLYST_Hz         (3.0f)
#define USER_MOTOR2_SPEED_START_Hz         (25.0f)
#define USER_MOTOR2_SPEED_FORCE_Hz         (25.0f)

#define USER_MOTOR2_ACCEL_START_Hzps       (5.0f)
#define USER_MOTOR2_ACCEL_MAX_Hzps         (20.0f)

// Only for eSMO
// Low PWM frequency needs a low Kslide_max
#define USER_MOTOR2_KSLIDE_MAX             (0.55f)
#define USER_MOTOR2_KSLIDE_MIN             (0.10f)

#define USER_MOTOR2_PLL_KP_MAX             (7.25f)
#define USER_MOTOR2_PLL_KP_MIN             (1.50f)
#define USER_MOTOR2_PLL_KP_SF              (20.0f)

#define USER_MOTOR2_BEMF_THRESHOLD         (0.5f)
#define USER_MOTOR2_BEMF_KSLF_FC_Hz        (300.0f)
#define USER_MOTOR2_THETA_OFFSET_SF        (1.0f)
#define USER_MOTOR2_SPEED_LPF_FC_Hz        (200.0f)

#elif (USER_MOTOR2 == Teknic_M2310PLN04K)
#define USER_MOTOR2_TYPE                   MOTOR_TYPE_PM
#define USER_MOTOR2_NUM_POLE_PAIRS         (4)
#define USER_MOTOR2_Rr_Ohm                 (NULL)
#define USER_MOTOR2_Rs_Ohm                 (0.39135775f)
#define USER_MOTOR2_Ls_d_H                 (0.000195666362f)
#define USER_MOTOR2_Ls_q_H                 (0.000195666362f)
#define USER_MOTOR2_RATED_FLUX_VpHz        (0.0407074876f)
#define USER_MOTOR2_MAGNETIZING_CURRENT_A  (NULL)
#define USER_MOTOR2_RES_EST_CURRENT_A      (2.0f)
#define USER_MOTOR2_IND_EST_CURRENT_A      (-1.5f)
#define USER_MOTOR2_MAX_CURRENT_A          (9.0f)
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz       (40.0f)
#define USER_MOTOR2_NUM_ENC_SLOTS          (1000)
#define USER_MOTOR2_INERTIA_Kgm2           (7.06154e-06)

#define USER_MOTOR2_RATED_VOLTAGE_V        (24.0f)
#define USER_MOTOR2_RATED_SPEED_KRPM       (3.0f)

#define USER_MOTOR2_FREQ_MIN_Hz            (5.0f)           // Hz
#define USER_MOTOR2_FREQ_MAX_Hz            (800.0f)         // Hz

#define USER_MOTOR2_FREQ_LOW_Hz            (10.0f)
#define USER_MOTOR2_FREQ_HIGH_Hz           (400.0f)
#define USER_MOTOR2_VOLT_MIN_V             (1.5f)
#define USER_MOTOR2_VOLT_MAX_V             (24.0f)

#define USER_MOTOR2_ALIGN_CURRENT_A        (1.0f)           //
#define USER_MOTOR2_STARTUP_CURRENT_A      (1.0f)           // 1.0A
#define USER_MOTOR2_OVER_CURRENT_A         (8.5f)           // 3.5A
#define USER_MOTOR2_LOCK_CURRENT_A         (1.0f)           // 1.0A

#define USER_MOTOR2_DELAY_FLYST_s          (uint16_t)(0.5f * USER_M2_ISR_FREQ_Hz) // 0.5s
#define USER_MOTOR2_SPEED_FLYST_Hz         (3.0f)
#define USER_MOTOR2_SPEED_START_Hz         (25.0f)
#define USER_MOTOR2_SPEED_FORCE_Hz         (25.0f)

#define USER_MOTOR2_ACCEL_START_Hzps       (5.0f)
#define USER_MOTOR2_ACCEL_MAX_Hzps         (20.0f)

// Only for eSMO
// Low PWM frequency needs a low Kslide_max
#define USER_MOTOR2_KSLIDE_MAX             (0.55f)
#define USER_MOTOR2_KSLIDE_MIN             (0.10f)

#define USER_MOTOR2_PLL_KP_MAX             (7.25f)
#define USER_MOTOR2_PLL_KP_MIN             (1.50f)
#define USER_MOTOR2_PLL_KP_SF              (20.0f)

#define USER_MOTOR2_BEMF_THRESHOLD         (0.5f)
#define USER_MOTOR2_BEMF_KSLF_FC_Hz        (300.0f)
#define USER_MOTOR2_THETA_OFFSET_SF        (1.0f)
#define USER_MOTOR2_SPEED_LPF_FC_Hz        (200.0f)

#elif (USER_MOTOR2 == ziehlab_Fan160hv)
#define USER_MOTOR2_TYPE                    MOTOR_TYPE_PM
#define USER_MOTOR2_NUM_POLE_PAIRS          (4)
#define USER_MOTOR2_Rr_Ohm                  (0.0)

#define USER_MOTOR2_Rs_Ohm                  (22.7004929f)
#define USER_MOTOR2_Ls_d_H                  (0.123299159f)
#define USER_MOTOR2_Ls_q_H                  (0.123299159f)
#define USER_MOTOR2_RATED_FLUX_VpHz         (2.26737704f)

#define USER_MOTOR2_MAGNETIZING_CURRENT_A   (NULL)
#define USER_MOTOR2_RES_EST_CURRENT_A       (1.0f)
#define USER_MOTOR2_IND_EST_CURRENT_A       (-1.0f)
#define USER_MOTOR2_MAX_CURRENT_A           (3.50f)
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz        (20.0f)
#define USER_MOTOR2_NUM_ENC_SLOTS           (2500.0f)
#define USER_MOTOR2_INERTIA_Kgm2            (3.100017e-5)

#define USER_MOTOR2_RATED_VOLTAGE_V         (200.0f)
#define USER_MOTOR2_RATED_SPEED_KRPM        (3.0f)

#define USER_MOTOR2_FREQ_MIN_Hz              (5.0f)            // Hz
#define USER_MOTOR2_FREQ_MAX_Hz             (400.0f)          // Hz

#define USER_MOTOR2_FREQ_LOW_Hz             (10.0f)           // Hz
#define USER_MOTOR2_FREQ_HIGH_Hz            (200.0f)          // Hz
#define USER_MOTOR2_VOLT_MIN_V              (20.0f)           // Volt
#define USER_MOTOR2_VOLT_MAX_V              (200.0f)          // Volt

#define USER_MOTOR2_ALIGN_CURRENT_A         (1.0f)           //
#define USER_MOTOR2_STARTUP_CURRENT_A       (1.0f)           //
#define USER_MOTOR2_OVER_CURRENT_A          (3.6f)           //
#define USER_MOTOR2_LOCK_CURRENT_A          (1.5f)           // 1.0A

#define USER_MOTOR2_DELAY_FLYST_s           (uint16_t)(0.5f * USER_M2_ISR_FREQ_Hz) // 0.5s
#define USER_MOTOR2_SPEED_FLYST_Hz          (3.0f)
#define USER_MOTOR2_SPEED_START_Hz          (10.0f)           //
#define USER_MOTOR2_SPEED_FORCE_Hz          (40.0)

#define USER_MOTOR2_ACCEL_START_Hzps        (5.0f)           //
#define USER_MOTOR2_ACCEL_MAX_Hzps          (10.0f)          //

// Only for eSMO
// Low pwm frequency needs a low Kslide_max
#define USER_MOTOR2_KSLIDE_MAX             (0.400f)
#define USER_MOTOR2_KSLIDE_MIN             (0.125f)

#define USER_MOTOR2_PLL_KP_MAX             (6.0f)
#define USER_MOTOR2_PLL_KP_MIN             (1.0f)
#define USER_MOTOR2_PLL_KP_SF              (20.0f)

#define USER_MOTOR2_BEMF_THRESHOLD         (0.5f)
#define USER_MOTOR2_BEMF_KSLF_FC_Hz        (300.0f)
#define USER_MOTOR2_THETA_OFFSET_SF        (2.5f)
#define USER_MOTOR2_SPEED_LPF_FC_Hz        (200.0f)

#elif (USER_MOTOR2 == Dhvac_Fan110hv)
#define USER_MOTOR2_TYPE                    MOTOR_TYPE_PM
#define USER_MOTOR2_NUM_POLE_PAIRS          (5)
#define USER_MOTOR2_Rr_Ohm                  (0.0f)

//#define USER_MOTOR2_Rs_Ohm                  (5.75555897f)
//#define USER_MOTOR2_Ls_d_H                  (0.0273226835f)
//#define USER_MOTOR2_Ls_q_H                  (0.0273226835f)
//#define USER_MOTOR2_RATED_FLUX_VpHz         (0.490144432f)

#define USER_MOTOR2_Rs_Ohm                  (6.54142952f)
#define USER_MOTOR2_Ls_d_H                  (0.00732990634f)
#define USER_MOTOR2_Ls_q_H                  (0.00732990634f)
#define USER_MOTOR2_RATED_FLUX_VpHz         (0.469156891f)

#define USER_MOTOR2_MAGNETIZING_CURRENT_A   (NULL)
#define USER_MOTOR2_RES_EST_CURRENT_A       (1.5f)
#define USER_MOTOR2_IND_EST_CURRENT_A       (-0.5f)
#define USER_MOTOR2_MAX_CURRENT_A           (3.50f)
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz        (40.0f)
#define USER_MOTOR2_NUM_ENC_SLOTS           (2500.0f)
#define USER_MOTOR2_INERTIA_Kgm2            (27.5e-5)

#define USER_MOTOR2_RATED_VOLTAGE_V         (200.0f)
#define USER_MOTOR2_RATED_SPEED_KRPM        (3.0f)

#define USER_MOTOR2_FREQ_MIN_Hz             (5.0f)            // Hz
#define USER_MOTOR2_FREQ_MAX_Hz             (400.0f)          // Hz

#define USER_MOTOR2_FREQ_LOW_Hz             (10.0f)           // Hz
#define USER_MOTOR2_FREQ_HIGH_Hz            (200.0f)          // Hz
#define USER_MOTOR2_VOLT_MIN_V              (20.0f)           // Volt
#define USER_MOTOR2_VOLT_MAX_V              (200.0f)          // Volt

#define USER_MOTOR2_ALIGN_CURRENT_A         (0.0f)           //
#define USER_MOTOR2_STARTUP_CURRENT_A       (1.0f)           //
#define USER_MOTOR2_OVER_CURRENT_A          (3.6f)           //
#define USER_MOTOR2_LOCK_CURRENT_A          (0.5f)           // 1.0A

#define USER_MOTOR2_DELAY_FLYST_s           (uint16_t)(0.5f * USER_M2_ISR_FREQ_Hz) // 0.5s
#define USER_MOTOR2_SPEED_FLYST_Hz          (5.0f)
#define USER_MOTOR2_SPEED_START_Hz          (10.0f)           //
#define USER_MOTOR2_SPEED_FORCE_Hz          (40.0)

#define USER_MOTOR2_ACCEL_START_Hzps        (5.0f)           //
#define USER_MOTOR2_ACCEL_MAX_Hzps          (10.0f)          //

// Only for eSMO
// Low pwm frequency needs a low Kslide_max
#define USER_MOTOR2_KSLIDE_MAX             (0.725f)
#define USER_MOTOR2_KSLIDE_MIN             (0.125f)

#define USER_MOTOR2_PLL_KP_MAX             (6.0f)
#define USER_MOTOR2_PLL_KP_MIN             (1.0f)
#define USER_MOTOR2_PLL_KP_SF              (20.0f)

#define USER_MOTOR2_BEMF_THRESHOLD         (0.35f)
#define USER_MOTOR2_BEMF_KSLF_FC_SF        (1.0f)
#define USER_MOTOR2_THETA_OFFSET_SF        (1.0f)
#define USER_MOTOR2_SPEED_LPF_FC_Hz        (200.0f)

#elif (USER_MOTOR2 == industry_Fan220hv)
#define USER_MOTOR2_TYPE                    MOTOR_TYPE_PM
#define USER_MOTOR2_NUM_POLE_PAIRS          (4)
#define USER_MOTOR2_Rr_Ohm                  (0.0f)

#define USER_MOTOR2_Rs_Ohm                  (8.76899433f)
#define USER_MOTOR2_Ls_d_H                  (0.0284830406f)
#define USER_MOTOR2_Ls_q_H                  (0.0284830406f)
#define USER_MOTOR2_RATED_FLUX_VpHz         (0.280854076f)

#define USER_MOTOR2_MAGNETIZING_CURRENT_A   (NULL)
#define USER_MOTOR2_RES_EST_CURRENT_A       (1.5f)
#define USER_MOTOR2_IND_EST_CURRENT_A       (-0.5f)
#define USER_MOTOR2_MAX_CURRENT_A           (3.50f)
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz        (40.0f)
#define USER_MOTOR2_NUM_ENC_SLOTS           (2500.0f)
#define USER_MOTOR2_INERTIA_Kgm2            (27.5e-5)

#define USER_MOTOR2_RATED_VOLTAGE_V         (200.0f)
#define USER_MOTOR2_RATED_SPEED_KRPM        (3.0f)

#define USER_MOTOR2_FREQ_MIN_Hz             (5.0f)            // Hz
#define USER_MOTOR2_FREQ_MAX_Hz             (400.0f)          // Hz

#define USER_MOTOR2_FREQ_LOW_Hz             (10.0f)           // Hz
#define USER_MOTOR2_FREQ_HIGH_Hz            (200.0f)          // Hz
#define USER_MOTOR2_VOLT_MIN_V              (20.0f)           // Volt
#define USER_MOTOR2_VOLT_MAX_V              (200.0f)          // Volt

#define USER_MOTOR2_ALIGN_CURRENT_A         (0.0f)           //
#define USER_MOTOR2_STARTUP_CURRENT_A       (1.0f)           //
#define USER_MOTOR2_OVER_CURRENT_A          (3.6f)           //
#define USER_MOTOR2_LOCK_CURRENT_A          (0.5f)           // 1.0A

#define USER_MOTOR2_DELAY_FLYST_s           (uint16_t)(0.5f * USER_M2_ISR_FREQ_Hz) // 0.5s
#define USER_MOTOR2_SPEED_FLYST_Hz          (5.0f)
#define USER_MOTOR2_SPEED_START_Hz          (10.0f)           //
#define USER_MOTOR2_SPEED_FORCE_Hz          (40.0)

#define USER_MOTOR2_ACCEL_START_Hzps        (5.0f)           //
#define USER_MOTOR2_ACCEL_MAX_Hzps          (10.0f)          //

// Only for eSMO
// Low pwm frequency needs a low Kslide_max
#define USER_MOTOR2_KSLIDE_MAX             (0.725f)
#define USER_MOTOR2_KSLIDE_MIN             (0.125f)

#define USER_MOTOR2_PLL_KP_MAX             (6.0f)
#define USER_MOTOR2_PLL_KP_MIN             (1.0f)
#define USER_MOTOR2_PLL_KP_SF              (20.0f)

#define USER_MOTOR2_BEMF_THRESHOLD         (0.35f)
#define USER_MOTOR2_BEMF_KSLF_FC_SF        (1.0f)
#define USER_MOTOR2_THETA_OFFSET_SF        (1.0f)
#define USER_MOTOR2_SPEED_LPF_FC_Hz        (200.0f)

#elif (USER_MOTOR2 == Anaheim_BLWS235D)
#define USER_MOTOR2_TYPE                   MOTOR_TYPE_PM
#define USER_MOTOR2_NUM_POLE_PAIRS         (2)
#define USER_MOTOR2_Rr_Ohm                 (NULL)
#define USER_MOTOR2_Rs_Ohm                 (5.09589481)
#define USER_MOTOR2_Ls_d_H                 (0.0223024581)
#define USER_MOTOR2_Ls_q_H                 (0.0223024581)
#define USER_MOTOR2_RATED_FLUX_VpHz        (0.430708647)      // 4.11V/krpm
#define USER_MOTOR2_MAGNETIZING_CURRENT_A  (NULL)
#define USER_MOTOR2_RES_EST_CURRENT_A      (1.5)
#define USER_MOTOR2_IND_EST_CURRENT_A      (-1.0)
#define USER_MOTOR2_MAX_CURRENT_A          (5.0)
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz       (40.0)
#define USER_MOTOR2_NUM_ENC_SLOTS          (NULL)
#define USER_MOTOR2_INERTIA_Kgm2           (0.0000230206204) // 0.003261 oz-in-sec2

#define USER_MOTOR2_RATED_VOLTAGE_V        (160.0)
#define USER_MOTOR2_RATED_SPEED_KRPM       (3.0)

#define USER_MOTOR2_FREQ_MIN_Hz             (5.0)             // Hz
#define USER_MOTOR2_FREQ_MAX_Hz            (300.0)           // Hz

#define USER_MOTOR2_FREQ_LOW_Hz            (10.0)            // Hz
#define USER_MOTOR2_FREQ_HIGH_Hz           (200.0)           // Hz
#define USER_MOTOR2_VOLT_MIN_V             (20.0)            // Volt
#define USER_MOTOR2_VOLT_MAX_V             (160.0)           // Volt

#define USER_MOTOR2_ALIGN_CURRENT_A         (1.0f)           //
#define USER_MOTOR2_STARTUP_CURRENT_A       (1.0f)           //
#define USER_MOTOR2_OVER_CURRENT_A          (3.6f)           //
#define USER_MOTOR2_LOCK_CURRENT_A          (1.5f)           // 1.0A

#define USER_MOTOR2_DELAY_FLYST_s           (uint16_t)(0.5f * USER_M2_ISR_FREQ_Hz)
#define USER_MOTOR2_SPEED_FLYST_Hz          (3.0f)
#define USER_MOTOR2_SPEED_START_Hz          (10.0f)
#define USER_MOTOR2_SPEED_FORCE_Hz          (40.0)

#define USER_MOTOR2_ACCEL_START_Hzps        (5.0f)
#define USER_MOTOR2_ACCEL_MAX_Hzps          (10.0f)

// Only for eSMO
#define USER_MOTOR2_KSLIDE_MAX             (0.25f)       // 2.0f
#define USER_MOTOR2_KSLIDE_MIN             (0.10f)

#define USER_MOTOR2_PLL_KP_MAX             (10.0f)
#define USER_MOTOR2_PLL_KP_MIN             (2.0f)
#define USER_MOTOR2_PLL_KP_SF              (5.0f)

#define USER_MOTOR2_BEMF_THRESHOLD         (0.5f)
#define USER_MOTOR2_BEMF_KSLF_FC_Hz        (300.0f)
#define USER_MOTOR2_THETA_OFFSET_SF        (1.0f)       // 2.5f
#define USER_MOTOR2_SPEED_LPF_FC_Hz        (200.0f)     // 100.0f

#elif (USER_MOTOR2 == Anaheim_BLY172S_24V)
#define USER_MOTOR2_TYPE                   MOTOR_TYPE_PM
#define USER_MOTOR2_NUM_POLE_PAIRS         (4)
#define USER_MOTOR2_Rr_Ohm                 (NULL)
#define USER_MOTOR2_Rs_Ohm                 (0.4)
#define USER_MOTOR2_Ls_d_H                 (0.0007190173)
#define USER_MOTOR2_Ls_q_H                 (0.0007190173)
#define USER_MOTOR2_RATED_FLUX_VpHz        (0.0327013217)
#define USER_MOTOR2_MAGNETIZING_CURRENT_A  (NULL)
#define USER_MOTOR2_RES_EST_CURRENT_A      (1.5)
#define USER_MOTOR2_IND_EST_CURRENT_A      (-1.5)
#define USER_MOTOR2_MAX_CURRENT_A          (5.0)
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz       (20.0)
#define USER_MOTOR2_NUM_ENC_SLOTS          (NULL)
#define USER_MOTOR2_INERTIA_Kgm2           (4.80185e-06)

#define USER_MOTOR2_RATED_VOLTAGE_V        (24.0)
#define USER_MOTOR2_RATED_SPEED_KRPM       (3.0)

#define USER_MOTOR2_FREQ_MIN_Hz             (5.0)           // Hz
#define USER_MOTOR2_FREQ_MAX_Hz            (300.0)         // Hz

#define USER_MOTOR2_FREQ_LOW_Hz            (10.0)          // Hz
#define USER_MOTOR2_FREQ_HIGH_Hz           (400.0)         // Hz
#define USER_MOTOR2_VOLT_MIN_V             (4.0)           // Volt
#define USER_MOTOR2_VOLT_MAX_V             (24.0)          // Volt

#elif (USER_MOTOR2 == Anaheim_BLY341S_Y24V)
#define USER_MOTOR2_TYPE                   MOTOR_TYPE_PM
#define USER_MOTOR2_NUM_POLE_PAIRS         (4)
#define USER_MOTOR2_Rr_Ohm                 (NULL)
#define USER_MOTOR2_Rs_Ohm                 (0.124559097)
#define USER_MOTOR2_Ls_d_H                 (0.000269943761)
#define USER_MOTOR2_Ls_q_H                 (0.000269943761)
#define USER_MOTOR2_RATED_FLUX_VpHz        (0.0507731885)      // 4.11V/krpm
#define USER_MOTOR2_MAGNETIZING_CURRENT_A  (NULL)
#define USER_MOTOR2_RES_EST_CURRENT_A      (4.5)
#define USER_MOTOR2_IND_EST_CURRENT_A      (-4.0)
#define USER_MOTOR2_MAX_CURRENT_A          (12.0)
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz       (20.0)
#define USER_MOTOR2_NUM_ENC_SLOTS          (NULL)
#define USER_MOTOR2_INERTIA_Kgm2           (3.99683e-05)     // 0.00566 oz-in-sec2

#define USER_MOTOR2_RATED_VOLTAGE_V        (24.0)
#define USER_MOTOR2_RATED_SPEED_KRPM       (3.0)

#define USER_MOTOR2_FREQ_MIN_Hz             (5.0)             // Hz
#define USER_MOTOR2_FREQ_MAX_Hz            (300.0)           // Hz

#define USER_MOTOR2_FREQ_LOW_Hz            (10.0)            // Hz
#define USER_MOTOR2_FREQ_HIGH_Hz           (200.0)           // Hz
#define USER_MOTOR2_VOLT_MIN_V             (4.0)             // Volt
#define USER_MOTOR2_VOLT_MAX_V             (24.0)            // Volt

#elif (USER_MOTOR2 == Anaheim_BLY341S_D24V)
#define USER_MOTOR2_TYPE                   MOTOR_TYPE_PM
#define USER_MOTOR2_NUM_POLE_PAIRS         (4)
#define USER_MOTOR2_Rr_Ohm                 (NULL)
#define USER_MOTOR2_Rs_Ohm                 (0.124559097)
#define USER_MOTOR2_Ls_d_H                 (0.000269943761)
#define USER_MOTOR2_Ls_q_H                 (0.000269943761)
#define USER_MOTOR2_RATED_FLUX_VpHz        (0.0507731885)      // 4.11V/krpm
#define USER_MOTOR2_MAGNETIZING_CURRENT_A  (NULL)
#define USER_MOTOR2_RES_EST_CURRENT_A      (4.5)
#define USER_MOTOR2_IND_EST_CURRENT_A      (-4.0)
#define USER_MOTOR2_MAX_CURRENT_A          (12.0)
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz       (20.0)
#define USER_MOTOR2_NUM_ENC_SLOTS          (NULL)
#define USER_MOTOR2_INERTIA_Kgm2           (3.99683e-05)     // 0.00566 oz-in-sec2

#define USER_MOTOR2_RATED_VOLTAGE_V        (24.0)
#define USER_MOTOR2_RATED_SPEED_KRPM       (3.0)

#define USER_MOTOR2_FREQ_MIN_Hz             (5.0)             // Hz
#define USER_MOTOR2_FREQ_MAX_Hz            (300.0)           // Hz

#define USER_MOTOR2_FREQ_LOW_Hz            (10.0)            // Hz
#define USER_MOTOR2_FREQ_HIGH_Hz           (200.0)           // Hz
#define USER_MOTOR2_VOLT_MIN_V             (4.0)             // Volt
#define USER_MOTOR2_VOLT_MAX_V             (24.0)            // Volt

#elif (USER_MOTOR2 == Marathon_5K33GN2A)
#define USER_MOTOR2_TYPE                   MOTOR_TYPE_INDUCTION
#define USER_MOTOR2_NUM_POLE_PAIRS         (2)
#define USER_MOTOR2_Rr_Ohm                 (5.508003)
#define USER_MOTOR2_Rs_Ohm                 (10.71121)
#define USER_MOTOR2_Ls_d_H                 (0.05296588)
#define USER_MOTOR2_Ls_q_H                 (0.05296588)
#define USER_MOTOR2_RATED_FLUX_VpHz        (0.8165*220.0/60.0)
#define USER_MOTOR2_MAGNETIZING_CURRENT_A  (1.378)
#define USER_MOTOR2_RES_EST_CURRENT_A      (1.0)
#define USER_MOTOR2_IND_EST_CURRENT_A      (NULL)
#define USER_MOTOR2_MAX_CURRENT_A          (3.0)
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz       (5.0)
#define USER_MOTOR2_NUM_ENC_SLOTS          (NULL)

#define USER_MOTOR2_FREQ_MIN_Hz             (5.0)           // Hz
#define USER_MOTOR2_FREQ_MAX_Hz            (400.0)         // Hz

#define USER_MOTOR2_FREQ_LOW_Hz            (10.0)          // Hz
#define USER_MOTOR2_FREQ_HIGH_Hz           (200.0)         // Hz
#define USER_MOTOR2_VOLT_MIN_V             (20.0)          // Volt
#define USER_MOTOR2_VOLT_MAX_V             (200.0)         // Volt

#elif (USER_MOTOR2 == Marathon_56H17T2011A)
#define USER_MOTOR2_TYPE                   MOTOR_TYPE_INDUCTION
#define USER_MOTOR2_NUM_POLE_PAIRS         (2)
#define USER_MOTOR2_Rr_Ohm                 (5.159403)
#define USER_MOTOR2_Rs_Ohm                 (7.924815)
#define USER_MOTOR2_Ls_d_H                 (0.03904648)
#define USER_MOTOR2_Ls_q_H                 (0.03904648)
#define USER_MOTOR2_RATED_FLUX_VpHz        (0.8*0.8165*230.0/60.0)
#define USER_MOTOR2_MAGNETIZING_CURRENT_A  (0.9941965)
#define USER_MOTOR2_RES_EST_CURRENT_A      (0.5)
#define USER_MOTOR2_IND_EST_CURRENT_A      (NULL)
#define USER_MOTOR2_MAX_CURRENT_A          (2.0)
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz       (5.0)
#define USER_MOTOR2_NUM_ENC_SLOTS          (NULL)

#define USER_MOTOR2_FREQ_MIN_Hz             (5.0)           // Hz
#define USER_MOTOR2_FREQ_MAX_Hz            (400.0)         // Hz

#define USER_MOTOR2_FREQ_LOW_Hz            (10.0)          // Hz
#define USER_MOTOR2_FREQ_HIGH_Hz           (200.0)         // Hz
#define USER_MOTOR2_VOLT_MIN_V             (20.0)          // Volt
#define USER_MOTOR2_VOLT_MAX_V             (200.0)         // Volt

//------------------------------------------------------------------
#elif (USER_MOTOR2 == my_pm_motor_1)
#define USER_MOTOR2_TYPE                   MOTOR_TYPE_PM
#define USER_MOTOR2_NUM_POLE_PAIRS         (4)
#define USER_MOTOR2_Rr_Ohm                 (NULL)
#define USER_MOTOR2_Rs_Ohm                 (NULL)
#define USER_MOTOR2_Ls_d_H                 (NULL)
#define USER_MOTOR2_Ls_q_H                 (NULL)
#define USER_MOTOR2_RATED_FLUX_VpHz        (NULL)
#define USER_MOTOR2_MAGNETIZING_CURRENT_A  (NULL)
#define USER_MOTOR2_RES_EST_CURRENT_A      (2.0)
#define USER_MOTOR2_IND_EST_CURRENT_A      (-2.0)
#define USER_MOTOR2_MAX_CURRENT_A          (5.0)
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz       (40.0)

// Number of lines on the motor's quadrature encoder
#define USER_MOTOR2_NUM_ENC_SLOTS          (1000)

#define USER_MOTOR2_FREQ_MIN_Hz             (5.0)           // Hz
#define USER_MOTOR2_FREQ_MAX_Hz            (300.0)         // Hz

#define USER_MOTOR2_FREQ_LOW_Hz            (10.0)          // Hz
#define USER_MOTOR2_FREQ_HIGH_Hz           (400.0)         // Hz
#define USER_MOTOR2_VOLT_MIN_V             (4.0)           // Volt
#define USER_MOTOR2_VOLT_MAX_V             (24.0)          // Volt

#elif (USER_MOTOR2 == my_aci_motor_2)
#define USER_MOTOR2_TYPE                   MOTOR_TYPE_INDUCTION
#define USER_MOTOR2_NUM_POLE_PAIRS         (2)
#define USER_MOTOR2_Rr_Ohm                 (NULL)
#define USER_MOTOR2_Rs_Ohm                 (NULL)
#define USER_MOTOR2_Ls_d_H                 (NULL)
#define USER_MOTOR2_Ls_q_H                 (NULL)
#define USER_MOTOR2_RATED_FLUX_VpHz        (0.8165*230.0/60.0)
#define USER_MOTOR2_MAGNETIZING_CURRENT_A  (NULL)
#define USER_MOTOR2_RES_EST_CURRENT_A      (0.5)
#define USER_MOTOR2_IND_EST_CURRENT_A      (NULL)
#define USER_MOTOR2_MAX_CURRENT_A          (5.0)
#define USER_MOTOR2_FLUX_EXC_FREQ_Hz       (5.0)

// Number of lines on the motor's quadrature encoder
#define USER_MOTOR2_NUM_ENC_SLOTS          (1000)

#define USER_MOTOR2_FREQ_MIN_Hz             (5.0)           // Hz
#define USER_MOTOR2_FREQ_MAX_Hz            (300.0)         // Hz

#define USER_MOTOR2_FREQ_LOW_Hz            (10.0)          // Hz
#define USER_MOTOR2_FREQ_HIGH_Hz           (400.0)         // Hz
#define USER_MOTOR2_VOLT_MIN_V             (4.0)           // Volt
#define USER_MOTOR2_VOLT_MAX_V             (24.0)          // Volt

#else
#error No motor type specified
#endif


//! \brief Defines the maximum current slope for Id trajectory
//!
#define USER_M2_MAX_CURRENT_DELTA_A        (USER_MOTOR2_RES_EST_CURRENT_A / USER_M2_ISR_FREQ_Hz)


//! \brief Defines the maximum current slope for Id trajectory during power warp mode
//!
#define USER_M2_MAX_CURRENT_DELTA_PW_A    (0.3 * USER_MOTOR2_RES_EST_CURRENT_A / USER_M2_ISR_FREQ_Hz)


#ifndef USER_MOTOR2
#error Motor type is not defined in user_motor2.h
#endif

#ifndef USER_MOTOR2_TYPE
#error The motor type is not defined in user_motor2.h
#endif

#ifndef USER_MOTOR2_NUM_POLE_PAIRS
#error Number of motor pole pairs is not defined in user_motor2.h
#endif

#ifndef USER_MOTOR2_Rr_Ohm
#error The rotor resistance is not defined in user_motor2.h
#endif

#ifndef USER_MOTOR2_Rs_Ohm
#error The stator resistance is not defined in user_motor2.h
#endif

#ifndef USER_MOTOR2_Ls_d_H
#error The direct stator inductance is not defined in user_motor2.h
#endif

#ifndef USER_MOTOR2_Ls_q_H
#error The quadrature stator inductance is not defined in user_motor2.h
#endif

#ifndef USER_MOTOR2_RATED_FLUX_VpHz
#error The rated flux of motor is not defined in user_motor2.h
#endif

#ifndef USER_MOTOR2_MAGNETIZING_CURRENT_A
#error The magnetizing current is not defined in user_motor2.h
#endif

#ifndef USER_MOTOR2_RES_EST_CURRENT_A
#error The resistance estimation current is not defined in user_motor2.h
#endif

#ifndef USER_MOTOR2_IND_EST_CURRENT_A
#error The inductance estimation current is not defined in user_motor2.h
#endif

#ifndef USER_MOTOR2_MAX_CURRENT_A
#error The maximum current is not defined in user_motor2.h
#endif

#ifndef USER_MOTOR2_FLUX_EXC_FREQ_Hz
#error The flux excitation frequency is not defined in user_motor2.h
#endif

#if ((USER_M2_NUM_CURRENT_SENSORS < 2) || (USER_M2_NUM_CURRENT_SENSORS > 3))
#error The number of current sensors must be 2 or 3
#endif

#if (USER_M2_NUM_VOLTAGE_SENSORS != 3)
#error The number of voltage sensors must be 3
#endif


// **************************************************************************
// the typedefs


// **************************************************************************
// the globals


// **************************************************************************
// the functions
//! \param[in]  pUserParams  The pointer to the user param structure
extern void USER_setMotor2Params(USER_Params *pUserParams);

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************

//*****************************************************************************
//
// Mark the end of the C bindings section for C++ compilers.
//
//*****************************************************************************
#ifdef __cplusplus
}
#endif // extern "C"

#endif // end of USER_MTR2_H definition

