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

/*! \brief  Contains public interface to various functions related
 *          to the Zero angle Offset (ZERO_OFFSET) 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 ZERO_OFFSET object
 */
typedef struct _ZeroOffset_ParamsType_
{
    float calibBaseOnSpd_Hz; /*!< calibrate zero position base on speed in Hz */
    float fBase_Hz;          /*!< Base value of frequency used for P.U. */
    float delay_us;          /*!< The delay time for the current in */
} ZeroOffset_ParamsType;

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

    _iq scale;  /*!< Parameter: The scale for scale*speed+offset */
    _iq offset; /*!< Parameter: The offset for scale*speed+offset */

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

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

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

/*! \brief Initialize the ZERO_OFFSET object
 *
 * This function initalizes Zero_Offset by setting configuration parameters
 *
 * \param[in] obj : pointer to Zero_Offset instance
 * \param[in] pParams : pointer to Zero_Offset configuration parameters
 */
extern void ZeroOffset_Init(ZeroOffsetType *obj, const ZeroOffset_ParamsType *pParams);

/*! \brief Run the ZERO_OFFSET Compensation
 *
 * This function runs Zero_Offset function, calculates zero offset value
 *
 * \param[in] obj : pointer to Zero_Offset instance
 */
static inline void ZeroOffset_Run(ZeroOffsetType *obj)
{
    _iq angleTmp_pu = obj->angleUncomp_pu + _IQmpy(obj->scale, obj->speed_pu) + obj->offset;

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

    obj->angleComp_pu = angleTmp_pu;
}

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

#endif /* _ZERO_OFFSET_H_ */
