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

/*! \brief  Contains public interface to various functions related
 *         to the Auto Field-Weakening Speed Control of (AFWSC) object
 */

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

#include <stdint.h>
#include "common/iqmath/iqmath.h"
#include "common/pid/pid.h"
#include "common/curve/ramp.h"
#include "md_lib/fw/fw.h"

#ifdef __cplusplus
extern "C" {
#endif

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

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

/*! \brief Defines the initial parameters for AFWSC_IPM object
 */
typedef struct _Afwsc_ParamsType_
{
    uint16_t polePairs;         /*!< Pole pairs of the motor */
    float    runFreq_kHz;       /*!< Frequency to run */
    float    fBase_Hz;          /*!< Base value of frequency used for P.U. */
    float    IBase_A;           /*!< Base value of current used for P.U. */
    float    inertia_kgm2;      /*!< The inertia of the rotoring system */
    float    rotorFlux_Wb;      /*!< The rotor flux, equal to BEMF in v*s/rad */
    float    spdFilterPole_rps; /*!< Pole value of speed filter calculation */
    float    minId_A;           /*!< Max value of Id to prevent demagnetization for PM motors */
    float    maxCurrent_A;      /*!< Max value of speed PID output */
} Afwsc_ParamsType;

/*! \brief Defines the AFWSC_IPM object
 */
typedef struct _AfwscType_
{
    _iq      speedRef; /*!< Input: The speed reference value of speed loop control */
    _iq      speed;    /*!< Input: The speed feedback */
    _iq      VsRef;    /*!< Input: The reference value of Vs */
    _iq      Vs;       /*!< Input: The feedback value of Vs */

    PidType  pidSpd;   /*!< Internal Variable: The PID for speed loop */
    RampType spdRamp;  /*!< Internal Variable: The ramp for speed reference value */
    FwType   fwObj;    /*!< Internal Variable: The field-weakening object */

    _iq      IdRef;    /*!< Output: The Id reference value */
    _iq      IqRef;    /*!< Output: The Iq reference value */
} AfwscType;

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

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

/*! \brief Initialize the Afwsc object
 *
 * This function initalizes Afwsc by setting configuration parameters
 *
 * \param[in] obj : pointer to Afwsc instance
 * \param[in] pParams : pointer to Afwsc configuration parameters
 */
extern void Afwsc_Init(AfwscType *obj, Afwsc_ParamsType *pParams);

/*! \brief The main process function of Afwsc
 *
 * This function runs Afwsc function, perform motor control functions
 *
 * \param[in] obj : pointer to Svgen instance
 */
extern void Afwsc_Run(AfwscType *obj);

/*! \brief Reset the Afwsc object
 *
 * This function resets the Afwsc relevant calculation parameters
 *
 * \param[in] obj : pointer to Afwsc instance
 */
extern void Afwsc_Reset(AfwscType *obj);

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

#endif /* _AFWSC_H_ */
