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

/*! \brief  Contains public interface to various functions related
 *         to the full-wave control (FULLWAVE) object.
 */

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

#include "common/iqmath/iqmath.h"
#include "common/pid/pid.h"
#include "common/curve/ramp.h"

#ifdef __cplusplus
extern "C" {
#endif

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

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

/*! \brief  Defines all the parameters used to initialize FULLWAVE object
 */
typedef struct _FullWave_ParamsType_
{
    float Rs_Ohm;         /*!< Stationary resistance in Ohm */
    float Ld_mH;          /*!< Normal D-axis inductance in mH */
    float runFreq_kHz;    /*!< Frequency to run FOC */
    float ratedVdc_V;     /*!< Rated dc bus voltage */
    float IBase_A;        /*!< Base value of current used for P.U. */
    float VBase_V;        /*!< Base value of voltage used for P.U. */
    float VsMax;          /*!< Max allowed Vs */
    float VdMaxScale;     /*!< Max allowed Vd scale factor based on Vs. */
    float VsRampStep;     /*!< The step of changing the Vs */
    float bandWidthScale; /*!< Current control run frequency / Bandwidth */
    float minVdc_V;       /*!< Minimum DC voltage allowed */
    float maxVdc_V;       /*!< Minimum DC voltage allowed */
} FullWave_ParamsType;

/*! \brief  Defines the dynamic parameters used to update FULLWAVE object
 */
typedef struct _FullWave_DynamicParamsType_
{
    float Rs_Ohm;      /*!< Stationary resistance in Ohm */
    float Ld_mH;       /*!< Normal D-axis inductance in mH */
    float runFreq_kHz; /*!< Frequency to run FOC */
    _iq   Vdc_pu;      /*!< DC bus voltage */
} FullWave_DynamicParamsType;

/*! \brief Defines the FULLWAVE object
 */
typedef struct _FullWaveType_
{
    _iq      IqRef_pu;   /*!< Input: Command Iq current in P.U. */
    _iq      Iq_pu;      /*!< Input: Feedback Iq current in P.U. */
    _iq      speed_pu;   /*!< Input: Feedback speed in P.U. */
    _iq      Vdff;       /*!< Input: Feed-forward Vd */

    float    factorKpKi; /*!< Parameter: The factor to calculate Kp and Ki */
    _iq      minVdc_pu;  /*!< Parameter: Minimum DC voltage allowed in P.U. */
    _iq      maxVdc_pu;  /*!< Parameter: Maximum DC voltage allowed in P.U. */

    PidType  pidIq;      /*!< Internal variable: The PID to tune Iq */
    RampType rampVs;     /*!< Internal variable: The ramp of changing the Vs */
    _iq      VdMaxScale; /*!< Internal variable: Max allowed Vd scale factor based on Vs. */
    _iq      VsMax;      /*!< Internal variable: The Max ABS value of Vs */

    _iq      Vd;         /*!< Output: Vd */
    _iq      Vq;         /*!< Output: Vq */
} FullWaveType;

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

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

/*! \brief  Initialize the FullWave object
 *
 * This function initalizes FullWave by setting configuration parameters
 *
 * \param[in] obj : pointer to FullWave instance
 * \param[in] pParams : pointer to FullWave configuration parameters
 * \return the MCUs authorized evaluation
 *         - -1 : Functions not available
 *         -  0 : Functions available
 */
extern int32_t FullWave_Init(FullWaveType *obj, const FullWave_ParamsType *pParams);

/*! \brief Run the full wave control
 *
 * This function runs full wave control function, perform motor control functions
 *
 * \param[in] obj : pointer to FullWave instance
 * \return the MCUs authorized evaluation
 *         - -1 : Functions not available
 *         -  0 : Functions available
 */
extern int32_t FullWave_Run(FullWaveType *obj);

/*! \brief Set the Vs max
 *
 * This function sets Vs max value
 *
 * \param[in] obj : pointer to FullWave instance
 * \param[in] VsMax : Vs max value
 */
extern void FullWave_SetVsMax(FullWaveType *obj, _iq VsMax);

/*! \brief Update the parameters
 *
 * This function updates the FullWave function configuration parameters
 *
 * \param[in] obj : pointer to FullWave instance
 * \param[in] pParams : pointer to control configuration paramter
 * \return the MCUs authorized evaluation
 *         - -1 : Functions not available
 *         -  0 : Functions available
 */
extern int32_t FullWave_UpdateParams(FullWaveType *obj, const FullWave_DynamicParamsType *pParams);

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

#endif /* _FULLWAVE_H_ */
