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

/*! \brief  Contains public interface to various functions related
 *          to the Incremental PI (IPI) object.
 *  \note   The form of this PID is parallel.
 */

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

#include "common/iqmath/iqmath.h"

#ifdef __cplusplus
extern "C" {
#endif

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

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

typedef struct _IpiType_
{
    _iq ref; /*!< Input: Reference input */
    _iq fdb; /*!< Input: Feedback input */

    _iq Kp; /*!< Parameter: Proportional gain */
    _iq Ki; /*!< Parameter: Integral gain */

    _iq lastError; /*!< Internal Variable: last error */

    _iq delta; /*!< Output: The output of increment value */
} IpiType;

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

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

/*! \brief Initialize the IPI object
 *
 * This function intialize the IPI object
 *
 * \param[in] obj : pointer to IPI instance
 * \param[in] Kp : Kp value in IQ format
 * \param[in] Ki : Ki value in IQ format
 */
static inline void Ipi_Init(IpiType *obj, _iq Kp, _iq Ki)
{
    obj->ref       = 0;
    obj->fdb       = 0;
    obj->delta     = 0;
    obj->Kp        = Kp;
    obj->Ki        = Ki;
    obj->lastError = 0;
}

/*! \brief Run the IPI control
 *
 * This function run the IPI calculation
 *
 * \param[in] obj : pointer to IPI instance
 */
static inline void Ipi_Run(IpiType *obj)
{
    /* Compute the error */
    _iq error = obj->ref - obj->fdb;
    /* Compute the proportional output */
    _iq Up = _IQmpy(obj->Kp, error - obj->lastError);
    /* Compute the integral output */
    _iq Ui = _IQmpy(obj->Ki, error);
    /* Remember error for next time */
    obj->lastError = error;
    /* Assign the output */
    obj->delta = Up + Ui;
}

/*! \brief Reset the IPI variables
 *
 * This function reset the IPI instance to the initial state
 *
 * \param[in] obj : pointer to IPI instance
 */
static inline void Ipi_Reset(IpiType *obj)
{
    obj->lastError = 0;
    obj->delta     = 0;
}

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

#endif /* _IPI_H_ */
