/*
 * 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 _IMEFE_H_
#define _IMEFE_H_

/*! \brief  Contains public interface to various functions related
 *          to the Induction Motor Enhance Flux Estimator (IMEFE) object.
 */

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

#include <stdint.h>
#include "common/iqmath/iqmath.h"
#include "common/filter/iir_fo.h"
#include "common/pid/pid.h"

#ifdef __cplusplus
extern "C" {
#endif

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

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

/*! \brief  Defines all the parameters used to initialize IMEFE object
 */
typedef struct _Imefe_ParamsType_
{
    float Rs_ohm;            /*!< Stator resistance (ohm) */
    float Rr_ohm;            /*!< Rotor resistance (ohm) */
    float Ls_H;              /*!< Stator inductance (H) */
    float Lr_H;              /*!< Rotor inductance (H) */
    float Lm_H;              /*!< Magnetizing inductance (H) */
    float IBase_A;           /*!< Base phase current (amp) */
    float VBase_V;           /*!< Base phase voltage (volt) */
    float fBase_Hz;          /*!< Base value of frequency used for P.U. */
    float phaseCalcFreq_kHz; /*!< Frequency to run phase calculation */
    float responseSpeed_rps; /*!< The response speed in rad/s */
    float spdCalcFreq_kHz;   /*!< Frequency to run phase calculation */
    float spdFilterPole_rps; /*!< Pole value of speed filter calculation */
    float gain;              /*!< Controller Gain (0.0 to 0.9999) */
    float linearError;       /*!< Controller Max allowed linear error (0.0 to 0.9999) */

} Imefe_ParamsType;

/*! \brief Defines the IMEFE object
 */
typedef struct _ImefeType_
{
    _iq       Ialpha;      /*!< Input: The stationary d-axis stator current */
    _iq       Ibeta;       /*!< Input: The stationary q-axis stator current */
    _iq       Valpha;      /*!< Input: The stationary d-axis stator voltage */
    _iq       Vbeta;       /*!< Input: The stationary q-axis stator voltage */
    _iq       emfAs;       /*!< Internal Variable: The stationary d-axis emf */
    _iq       emfBs;       /*!< Internal Variable: The stationary q-axis emf */
    _iq       psiAs;       /*!< Internal Variable: The stationary d-axis psi */
    _iq       psiBs;       /*!< Internal Variable: The stationary q-axis psi */
    _iq       psiAr;       /*!< Internal Variable: The stationary d-axis psi */
    _iq       psiBr;       /*!< Internal Variable: The stationary q-axis psi */
    _iq       gain1;       /*!< Parameter: Motor dependent control gain */
    _iq       sigma1;      /*!< Parameter: Motor dependent control gain */
    _iq       gain2;       /*!< Parameter: Motor dependent control gain */
    _iq       sigma2;      /*!< Parameter: Motor dependent control gain */
    _iq       Rs;          /*!< Parameter: Motor dependent control gain */
    _iq       model;       /*!< Parameter: Motor dependent control gain */
    _iq       gain3;       /*!< Parameter: Motor dependent control gain */
    _iq       gain4;       /*!< Parameter: Motor dependent control gain */
    _iq       gain5;       /*!< Parameter: Motor dependent control gain */
    _iq       factor;      /*!< Parameter: Motor dependent control gain */
    _iq       maxErr;      /*!< Parameter: Motor dependent control max Error */
    IirFoType lpfFlx;      /*!< Internal Variable: The filter for electric flux calculation */
    IirFoType lpfSpd;      /*!< Internal Variable: The filter for electric speed calculation */
    PidType   pidVAs;      /*!< Internal Variable: The Stationary d-axis stator voltage PID */
    PidType   pidVBs;      /*!< Internal Variable: The Stationary q-axis stator voltage PID */
    PidType   pidPLL;      /*!< Internal Variable: The PLL PID */
    _iq       rawSpeed_pu; /*!< Internal variable: The raw speed before filter in P.U. */
    _iq       rawWslip_pu; /*!< Internal variable: The raw slip speed before filter in P.U. */
    _iq       angle_pu;    /*!< Output: The electric angle */
    _iq       speed_pu;    /*!< Output: The filtered electric speed updated every electric period */

} ImefeType;

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

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

/*! \brief Initialize the IMEFE object
 *
 * This function initalizes IMEFE by setting configuration parameters
 *
 * \param[in] obj : pointer to IMEFE instance
 * \param[in] pParams : pointer to IMEFE configuration parameters
 */
extern int32_t Imefe_Init(ImefeType *obj, const Imefe_ParamsType *pParams);

/*! \brief Run the Angle Compensation
 *
 * This function runs IMEFE function, calculates Angle compensation value
 *
 * \param[in] obj : pointer to IMEFE instance
 */
extern int32_t Imefe_Run(ImefeType *obj);

/*! \brief  Calculate the speed
 *  \note   Might run slower than current control frequency
 *
 *
 * \param[in] obj : pointer to IMEFE instance
 * \return the MCUs authorized evaluation
 *         - -1 : Functions not available
 *         -  0 : Functions available
 */
extern int32_t Imefe_CalcSpeed(ImefeType *obj);

/*! \brief Reset the IMEFE object
 *
 * This function resets the IMEFE relevant calculation parameters
 *
 * \param[in] obj : pointer to IMEFE instance
 * \return the MCUs authorized evaluation
 *         - -1 : Functions not available
 *         -  0 : Functions available
 */
extern int32_t Imefe_Reset(ImefeType *obj);

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

#endif /* _IMEFE_H_ */
