math_utils.h 2.4 KB
#ifndef __MATH_UTILS_H
#define __MATH_UTILS_H

#include "math.h"
#include "stdint.h"

#include "tva.h"

#define _constraint(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))

#define _PI 3.14159265358f
#define _2PI 6.28318530718f
#define _3PI_2 4.71238898038f

#define _DtoR 0.01745329251f
#define _RtoD 57.295779513f

typedef struct
{
    float Q; // 过程噪声协方差 初始化值为0.001
    float R; // 观测噪声协方差 初始化值为0.543
} KFP_CONFIG;

typedef struct
{
    float LastP; // 上次估算协方差 初始化值为0.02
    float Now_P; // 当前估算协方差 初始化值为0
    float out;   // 卡尔曼滤波器输出 初始化值为0
    float Kg;    // 卡尔曼增益 初始化值为0
    KFP_CONFIG config;
} KFP; // Kalman Filter parameter

typedef struct
{
    float Tf; //!< Low pass filter time constant

    float y_prev;    //!< filtered value in previous execution step
    TimeRec_s timeS; //!< Last execution timestamp
} LowPassFSTD_s;

typedef struct
{
    float Tf; //!< Low pass filter time constant

    float y_prev;    //!< filtered value in previous execution step
    float ringSet;   //!< 设置环大小数值将会限制在 ( -ringSet 到 ringSet 之间 )
    TimeRec_s timeS; //!< Last execution timestamp

} LowPassFSTDRing_s;

void LowPassFilter(float *value);  // 低通滤波器
void LowPassFilter1(float *value); // 低通滤波器

int kalmanFilter_Init(KFP *kfp, KFP_CONFIG config);
float kalmanFilter(KFP *kfp, float input);

uint16_t intervalFilter(uint16_t input);

// CUT##     区间滤波设置
uint16_t inter_filter_GetRange();
int inter_filter_SetRange(uint16_t range);

uint16_t inter_filterC1_GetRange();
int inter_filterC1_SetRange(uint16_t range);

uint16_t inter_filter(uint16_t x);
uint16_t inter_filterC1(uint16_t x);

// float KalmanFilter_A(float ResrcData);
// float kalmanFilter_B(float inData);
// void kalmanFilter_Set_R(float input);
// void kalmanFilter_Set_Q(float input);

void LowPassFilterSTD_Init(LowPassFSTD_s *config, float Tf);
float LowPassFilterSTD_Process(LowPassFSTD_s *config, float x);

void LowPassFilterSTDRing_Init(LowPassFSTDRing_s *config, float Tf, float ringRange);
float LowPassFilterSTDRing_Process_EX(LowPassFSTDRing_s *config, float x, float y_prev);

float _normalizeAngle(float angle); // normalizing radian angle to [0, 2pi]
float calculateArcMidpointAngle(float angle1, float angle2);

#endif