158 lines
4.6 KiB
C
158 lines
4.6 KiB
C
/*
|
|
* 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 _GRADIENT_H_
|
|
#define _GRADIENT_H_
|
|
|
|
/*! \brief Contains public interface to various functions related
|
|
* to the 1st order linear curve with GRADIENT
|
|
*/
|
|
|
|
/*******************************************************************************
|
|
* the includes
|
|
******************************************************************************/
|
|
|
|
#include <stdint.h>
|
|
#include "common/iqmath/iqmath.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/*******************************************************************************
|
|
* the defines
|
|
******************************************************************************/
|
|
|
|
/*******************************************************************************
|
|
* the typedefs
|
|
******************************************************************************/
|
|
|
|
/*! \brief Declaration of Gradient Class
|
|
*
|
|
*/
|
|
typedef struct _GradientType_
|
|
{
|
|
_iq target; /*!< Input: target input */
|
|
_iq upStep; /*!< Parameter: up step of ramp */
|
|
_iq downStep; /*!< Parameter: down step of ramp */
|
|
_iq output; /*!< Output: output value */
|
|
|
|
} GradientType;
|
|
|
|
/*******************************************************************************
|
|
* the globals
|
|
******************************************************************************/
|
|
|
|
/*******************************************************************************
|
|
* the function prototypes
|
|
******************************************************************************/
|
|
|
|
/*! \brief Initialize the Gradient instance
|
|
*
|
|
* This function initializes the Gradient instance
|
|
*
|
|
* \param[in] obj : pointer to Gradient instance
|
|
* \param[in] upStep : step when going up
|
|
* \param[in] downStep : step when going down
|
|
*/
|
|
static inline void Gradient_Init(GradientType *obj, _iq upStep, _iq downStep)
|
|
{
|
|
obj->target = 0;
|
|
obj->output = 0;
|
|
obj->upStep = upStep;
|
|
obj->downStep = downStep;
|
|
}
|
|
|
|
/*! \brief Calculates the output
|
|
*
|
|
* This function calculates the output
|
|
*
|
|
* \param[in] obj : pointer to Gradient instance
|
|
*/
|
|
static inline void Gradient_Calc(GradientType *obj)
|
|
{
|
|
_iq upStep = _IQabs(obj->upStep); /* step must be a positive value */
|
|
_iq downStep = _IQabs(obj->downStep); /* step must be a positive value */
|
|
|
|
if(obj->target > obj->output)
|
|
{
|
|
if((obj->target - obj->output) > upStep)
|
|
{
|
|
obj->output += upStep;
|
|
}
|
|
else
|
|
{
|
|
obj->output = obj->target; /* less than one step needed, just jump to the target */
|
|
}
|
|
}
|
|
else if(obj->target < obj->output)
|
|
{
|
|
if((obj->output - obj->target) > downStep)
|
|
{
|
|
obj->output -= downStep;
|
|
}
|
|
else
|
|
{
|
|
obj->output = obj->target; /* less than one step needed, just jump to the target */
|
|
}
|
|
}
|
|
}
|
|
|
|
/*! \brief Calculate the output using the external reference
|
|
*
|
|
* This function calculates the output using the external reference,
|
|
* instead of the internal output
|
|
*
|
|
* \param[in] obj : pointer to Gradient instance
|
|
* \param[in] pOutput : pointer to external output
|
|
*/
|
|
static inline void Gradient_CalcExternal(GradientType *obj, _iq *pOutput)
|
|
{
|
|
_iq upStep = _IQabs(obj->upStep); /* step must be a positive value */
|
|
_iq downStep = _IQabs(obj->downStep); /* step must be a positive value */
|
|
|
|
if(obj->target > (*pOutput))
|
|
{
|
|
if((obj->target - (*pOutput)) > upStep)
|
|
{
|
|
(*pOutput) += upStep;
|
|
}
|
|
else
|
|
{
|
|
(*pOutput) = obj->target; /* less than one step needed, just jump to the target */
|
|
}
|
|
}
|
|
else if(obj->target < (*pOutput))
|
|
{
|
|
if(((*pOutput) - obj->target) > downStep)
|
|
{
|
|
(*pOutput) -= downStep;
|
|
}
|
|
else
|
|
{
|
|
(*pOutput) = obj->target; /* less than one step needed, just jump to the target */
|
|
}
|
|
}
|
|
|
|
obj->output = *pOutput; /* We don't use the obj->output normally in this function, just be safe */
|
|
}
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif /* extern "C" */
|
|
|
|
#endif /* _GRADIENT_H_ */
|