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

/*! \brief  Contains public interface to various functions related
 *          to the Angle Compensation (ANGLE_COMP) object.
 */

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

#include "common/iqmath/iqmath.h"

#ifdef __cplusplus
extern "C" {
#endif

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

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

/*! \brief  Defines all the parameters used to initialize ANGLE_COMP object
 */
typedef struct _AngleComp_ParamsType_
{
    float runFreq_kHz;     /*!< the frequency of running current loop in kHz */
    float fBase_Hz;        /*!< Base value of frequency used for P.U. */
    float delayCompFactor; /*!< The factor used to compensate the delay caused by PWM generation. */
} AngleComp_ParamsType;

/*! \brief Defines the ANGLE_COMP object
 */
typedef struct _AngleCompType_
{
    _iq speed_pu;       /*!< Input: the electrical speed in P.U. */
    _iq angleUncomp_pu; /*!< Input: the angle before compensation in P.U. */

    _iq   compFactor;  /*!< Internal variable: The factor used to calculate delta angle */
    float runFreq_kHz; /*!< Internal variable: The frequency of running current loop in kHz */

    _iq angleComp_pu; /*!< Output: the angle after compensation in P.U. */
} AngleCompType;

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

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

/*! \brief Initialize the ANGLE_COMP object
 *
 * This function initalizes ANGLE_COMP by setting configuration parameters
 *
 * \param[in] obj : pointer to Angle_Comp instance
 * \param[in] pParams : pointer to Angle_Comp configuration parameters
 */
extern void AngleComp_Init(AngleCompType *obj, const AngleComp_ParamsType *pParams);

/*! \brief Run the Angle Compensation
 *
 * This function runs Angle_Comp function, calculates Angle compensation value
 *
 * \param[in] obj : pointer to Angle_Comp instance
 */
static inline void AngleComp_Run(AngleCompType *obj)
{
    _iq angleTmp_pu = obj->angleUncomp_pu + _IQmpy(obj->compFactor, obj->speed_pu);

    angleTmp_pu = _IQfrac(angleTmp_pu); /* get the fraction part */
    if(angleTmp_pu < 0)
    {
        angleTmp_pu += _IQ(1.0);
    }

    obj->angleComp_pu = angleTmp_pu;
}

/*! \brief Update the Angle Compensation configuration parameters
 *
 * This function updates the Angle_Comp function configuration parameters
 *
 * \param[in] obj : pointer to Angle_Comp instance
 * \param[in] runFreq_kHz : the frequency of running current loop
 */
static inline void AngleComp_UpdateParams(AngleCompType *obj, float runFreq_kHz)
{
    obj->compFactor  = _IQmpy(obj->compFactor, _IQ(obj->runFreq_kHz / runFreq_kHz));
    obj->runFreq_kHz = runFreq_kHz;
}

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

#endif /* _ANGLE_COMP_H_ */
