R2_UP/User/Algorithm/user_math.h

161 lines
3.5 KiB
C
Raw Permalink Normal View History

2025-03-12 10:46:02 +08:00
/*
*/
#ifndef USER_MATH_H
#define USER_MATH_H
#include "stm32f4xx.h"
#define ARM_MATH_CM4
#include <float.h>
#include <math.h>
#include <stdbool.h>
#include "struct_typedef.h"
#define M_DEG2RAD_MULT (0.01745329251f)
#define M_RAD2DEG_MULT (57.2957795131f)
#ifndef M_PI
#define M_PI 3.1415926f
#endif
#ifndef M_2PI
#define M_2PI 6.2831853f
#endif
#define max(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a > _b ? _a : _b; \
})
#define min(a, b) \
({ \
__typeof__(a) _a = (a); \
__typeof__(b) _b = (b); \
_a < _b ? _a : _b; \
})
/* 移动向量 */
typedef struct {
float vx; /* 前后平移 */
float vy; /* 左右平移 */
float wz; /* 转动 */
} MoveVector_t;
//存储极坐标的结构体
typedef struct {
fp32 r; // 极径
fp32 theta; // 极角
}PolarCoordinate_t;
// 表示直角坐标矢量的结构体
typedef struct {
fp32 x; // x
fp32 y; // y
}CartesianVector_t;
//定义获取角度的量纲
typedef enum {
DEGREE,//0-360
RADIAN,//(0-2pi)
}Angle_e;
//定义获取角度时的角度种类 相对和绝对角度
typedef enum {
RELATIVE,
ABSOLUTE,
}AngleType_e;
float InvSqrt(float x);
void abs_limit_fp(fp32 *num, fp32 Limit);
fp32 loop_fp32_constrain(fp32 Input, fp32 minValue, fp32 maxValue);
float AbsClip(float in, float limit);
void Clip(float *origin, float min, float max);
float Sign(float in);
fp32 AngleCalc(AngleType_e type, fp32 abs_angle,fp32 zero_point);
fp32 AngleChange(Angle_e hopetype,fp32 angle);
float _normalizeAngle(float angle);
float _normalizeTo360(float angle);
float calculate_rotation(float current_angle, float target_angle);
int map_int(int x, int in_min, int in_max, int out_min, int out_max);
fp32 map_fp32(fp32 x, fp32 in_min, fp32 in_max, fp32 out_min, fp32 out_max);
float abs_float_single (float a);
float abs_float_double(float a,float b);
/**
* \brief
*
* \param mv
*/
void ResetMoveVector(MoveVector_t *mv);
/**
* \brief
* 1.5PI其实等于相差-0.5PI
*
* \param sp
* \param fb
* \param range
*
* \return
*/
float CircleError(float sp, float fb, float range);
/**
* \brief
* 0-2PI内变化1.5PI + 1.5PI = 1PI
*
* \param origin
* \param delta
* \param range
*/
void CircleAdd(float *origin, float delta, float range);
/**
* @brief
*
* @param origin
*/
void CircleReverse(float *origin);
/**
* @brief
*
* @param bullet_speed
* @param fric_radius
* @param is17mm 17mm
* @return
*/
float CalculateRpm(float bullet_speed, float fric_radius, bool is17mm);
// 函数用于将直角坐标转换为极坐标
PolarCoordinate_t cartesianToPolar(fp32 x, fp32 y);
//相加两个极坐标向量并返回结果
PolarCoordinate_t addPolarVectors(PolarCoordinate_t v1, PolarCoordinate_t v2);
/// @brief
/// @param arr 数组
/// @param n 元素数
/// @return 平均值
uint8_t average(uint8_t arr[], uint8_t n);
#endif