2022-10-20 17:13:02 +08:00
|
|
|
|
/**
|
2022-11-03 20:38:55 +08:00
|
|
|
|
******************************************************************************
|
|
|
|
|
* @file user_lib.c
|
|
|
|
|
* @author Wang Hongxi
|
|
|
|
|
* @version V1.0.0
|
|
|
|
|
* @date 2021/2/18
|
|
|
|
|
* @brief
|
|
|
|
|
******************************************************************************
|
|
|
|
|
* @attention
|
|
|
|
|
*
|
|
|
|
|
******************************************************************************
|
|
|
|
|
*/
|
2022-10-20 17:13:02 +08:00
|
|
|
|
#include "stdlib.h"
|
|
|
|
|
#include "string.h"
|
|
|
|
|
#include "user_lib.h"
|
|
|
|
|
#include "math.h"
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
|
|
#ifdef _CMSIS_OS_H
|
|
|
|
|
#define user_malloc pvPortMalloc
|
|
|
|
|
#else
|
|
|
|
|
#define user_malloc malloc
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
uint8_t GlobalDebugMode = 7;
|
|
|
|
|
|
|
|
|
|
//快速开方
|
|
|
|
|
float Sqrt(float x)
|
|
|
|
|
{
|
|
|
|
|
float y;
|
|
|
|
|
float delta;
|
|
|
|
|
float maxError;
|
|
|
|
|
|
|
|
|
|
if (x <= 0)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// initial guess
|
|
|
|
|
y = x / 2;
|
|
|
|
|
|
|
|
|
|
// refine
|
|
|
|
|
maxError = x * 0.001f;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
delta = (y * y) - x;
|
|
|
|
|
y -= delta / (2 * y);
|
|
|
|
|
} while (delta > maxError || delta < -maxError);
|
|
|
|
|
|
|
|
|
|
return y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-11-03 20:38:55 +08:00
|
|
|
|
* @brief 斜波函数计算,根据输入的值进行叠加, 输入单位为 /s 即一秒后增加输入的值
|
|
|
|
|
* @author RM
|
|
|
|
|
* @param[in] 斜波函数结构体
|
|
|
|
|
* @param[in] 输入值
|
|
|
|
|
* @retval 返回空
|
|
|
|
|
*/
|
2022-10-20 17:13:02 +08:00
|
|
|
|
float ramp_calc(ramp_function_source_t *ramp_source_type, float input)
|
|
|
|
|
{
|
|
|
|
|
ramp_source_type->input = input;
|
|
|
|
|
ramp_source_type->out += ramp_source_type->input * ramp_source_type->frame_period;
|
|
|
|
|
if (ramp_source_type->out > ramp_source_type->max_value)
|
|
|
|
|
{
|
|
|
|
|
ramp_source_type->out = ramp_source_type->max_value;
|
|
|
|
|
}
|
|
|
|
|
else if (ramp_source_type->out < ramp_source_type->min_value)
|
|
|
|
|
{
|
|
|
|
|
ramp_source_type->out = ramp_source_type->min_value;
|
|
|
|
|
}
|
|
|
|
|
return ramp_source_type->out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//绝对值限制
|
|
|
|
|
float abs_limit(float num, float Limit)
|
|
|
|
|
{
|
|
|
|
|
if (num > Limit)
|
|
|
|
|
{
|
|
|
|
|
num = Limit;
|
|
|
|
|
}
|
|
|
|
|
else if (num < -Limit)
|
|
|
|
|
{
|
|
|
|
|
num = -Limit;
|
|
|
|
|
}
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//判断符号位
|
|
|
|
|
float sign(float value)
|
|
|
|
|
{
|
|
|
|
|
if (value >= 0.0f)
|
|
|
|
|
{
|
|
|
|
|
return 1.0f;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return -1.0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//浮点死区
|
|
|
|
|
float float_deadband(float Value, float minValue, float maxValue)
|
|
|
|
|
{
|
|
|
|
|
if (Value < maxValue && Value > minValue)
|
|
|
|
|
{
|
|
|
|
|
Value = 0.0f;
|
|
|
|
|
}
|
|
|
|
|
return Value;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 20:38:55 +08:00
|
|
|
|
// int26死区
|
2022-10-20 17:13:02 +08:00
|
|
|
|
int16_t int16_deadline(int16_t Value, int16_t minValue, int16_t maxValue)
|
|
|
|
|
{
|
|
|
|
|
if (Value < maxValue && Value > minValue)
|
|
|
|
|
{
|
|
|
|
|
Value = 0;
|
|
|
|
|
}
|
|
|
|
|
return Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//限幅函数
|
|
|
|
|
float float_constrain(float Value, float minValue, float maxValue)
|
|
|
|
|
{
|
|
|
|
|
if (Value < minValue)
|
|
|
|
|
return minValue;
|
|
|
|
|
else if (Value > maxValue)
|
|
|
|
|
return maxValue;
|
|
|
|
|
else
|
|
|
|
|
return Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//限幅函数
|
|
|
|
|
int16_t int16_constrain(int16_t Value, int16_t minValue, int16_t maxValue)
|
|
|
|
|
{
|
|
|
|
|
if (Value < minValue)
|
|
|
|
|
return minValue;
|
|
|
|
|
else if (Value > maxValue)
|
|
|
|
|
return maxValue;
|
|
|
|
|
else
|
|
|
|
|
return Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//循环限幅函数
|
|
|
|
|
float loop_float_constrain(float Input, float minValue, float maxValue)
|
|
|
|
|
{
|
|
|
|
|
if (maxValue < minValue)
|
|
|
|
|
{
|
|
|
|
|
return Input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Input > maxValue)
|
|
|
|
|
{
|
|
|
|
|
float len = maxValue - minValue;
|
|
|
|
|
while (Input > maxValue)
|
|
|
|
|
{
|
|
|
|
|
Input -= len;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (Input < minValue)
|
|
|
|
|
{
|
|
|
|
|
float len = maxValue - minValue;
|
|
|
|
|
while (Input < minValue)
|
|
|
|
|
{
|
|
|
|
|
Input += len;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//弧度格式化为-PI~PI
|
|
|
|
|
|
|
|
|
|
//角度格式化为-180~180
|
|
|
|
|
float theta_format(float Ang)
|
|
|
|
|
{
|
|
|
|
|
return loop_float_constrain(Ang, -180.0f, 180.0f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int float_rounding(float raw)
|
|
|
|
|
{
|
|
|
|
|
static int integer;
|
|
|
|
|
static float decimal;
|
|
|
|
|
integer = (int)raw;
|
|
|
|
|
decimal = raw - integer;
|
|
|
|
|
if (decimal > 0.5f)
|
|
|
|
|
integer++;
|
|
|
|
|
return integer;
|
2022-10-31 20:20:16 +08:00
|
|
|
|
}
|