/*
 * Copyright (c) 2022, Shenzhen CVA Innovation CO.,LTD
 * All rights reserved.
 *
 * Shenzhen CVA Innovation CO.,LTD (CVA chip) is supplying this file for use
 * exclusively with CVA's microcontroller products. This file can be freely
 * distributed within development tools that are supporting such microcontroller
 * products.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
 * CVA SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
 * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 */

#ifndef _ESTTRQ_TF_H_
#define _ESTTRQ_TF_H_

/*! \brief  Contains public interface to various functions related
 *          to the Torque Estimation using Torque Formula method.
 */

/*******************************************************************************
 * the includes
 ******************************************************************************/

#include "common/iqmath/iqmath.h"

#ifdef __cplusplus
extern "C" {
#endif

/*******************************************************************************
 * the defines
 ******************************************************************************/

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

/*! \brief  Defines all the parameters used to initialize ESTTRQ_TF object
 */
typedef struct _EstTrqTf_ParamsType_
{
    float    IBase_A;       /*!< Base value of current used for P.U. */
    float    fBase_Hz;      /*!< Base value of frequency used for P.U. */
    float    torqueBase_Nm; /*!< Base value of torque used for P.U. */
    uint32_t polePairs;     /*!< Pole pairs of the motor */
    float    Ld_mH;         /*!< d-axis inductance in mH */
    float    Lq_mH;         /*!< q-axis inductance in mH */
    float    flux_Wb;       /*!< The flux of PM in Webers */
} EstTrqTf_ParamsType;

/*! \brief Defines the ESTTRQ_TF object
 */
typedef struct _EstTrqTfType_
{
    _iq filteredIdFdb_pu; /*!< Input: The Id feedback in P.U. */
    _iq filteredIqFdb_pu; /*!< Input: The Iq feedback in P.U. */

    _iq   flux_Wb;        /*!< Parameter: The flux of PM in Webers */
    _iq   torqueFactor_I; /*!< Parameter: Current factor to calculate torque */
    _iq   torqueFactor_F; /*!< Parameter: Flux factor to calculate torque */
    float IBaseFactor;    /*!< Parameter: Base current factor to calculate torque */

    _iq torque_pu; /*!< Output: The estimated torque */
} EstTrqTfType;

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

/*******************************************************************************
 * the function prototypes
 ******************************************************************************/

/*! \brief Initialize the ESTTRQ_TF object
 *
 * This function initalizes Esttrq_Tf by setting configuration parameters
 *
 * \param[in] obj : pointer to Esttrq_Tf instance
 * \param[in] pParams : pointer to parameters
 */
extern void EstTrqTf_Init(EstTrqTfType *obj, const EstTrqTf_ParamsType *pParams);

/*! \brief Run the Torque estimation
 *
 * This function runs Esttrq_Tf function, calculates torque estimation value
 *
 * \param[in] obj : pointer to Esttrq_Tf instance
 */
extern void EstTrqTf_Calc(EstTrqTfType *obj);

/*! \brief Update the parameters of ESTTRQ_TF object
 *
 * This function updates the Esttrq_Tf function configuration parameters
 *
 * \param[in] obj : pointer to Esttrq_Tf instance
 * \param[in] Ld_mH : d-axis inductance in mH
 * \param[in] Lq_mH : q-axis inductance in mH
 * \param[in] flux_Wb : the flux of PM in Webers
 */
extern void EstTrqTf_UpdateParams(EstTrqTfType *obj, float Ld_mH, float Lq_mH, float flux_Wb);

#ifdef __cplusplus
}
#endif /* extern "C" */

#endif /* _ESTTRQ_TF_H_ */
