104 lines
3.2 KiB
C
104 lines
3.2 KiB
C
#ifndef __SHAKE_DETECT_H
|
||
#define __SHAKE_DETECT_H
|
||
|
||
#include <stdint.h>
|
||
|
||
#define LIS3DSH_I2C_MASTER_NUM 0
|
||
#define LIS3DSH_I2C_SEL_IO 11 // 地址选择
|
||
#define LIS3DSH_I2C_MASTER_SDA_IO 12
|
||
#define LIS3DSH_I2C_MASTER_SCL_IO 13
|
||
#define LIS3DSH_I2C_MASTER_FREQ_HZ 400000
|
||
#define LIS3DSH_I2C_SALVE_ADDR_L 0x18 // 7bit地址0b0011000
|
||
#define LIS3DSH_I2C_MASTER_TIMEOUT_MS 1000
|
||
#define ACK_CHECK_EN 0x1
|
||
#define ACK_CHECK_DIS 0x0
|
||
|
||
#define LIS3DSH_INT1_IO 9 // 数据就绪信号
|
||
#define LIS3DSH_CS_IO 10 // SPI I2C模式选择,1-I2C
|
||
#define LIS3DSH_LED_IO 3
|
||
#define BOOT_TIME 10
|
||
|
||
|
||
#define N_SAMPLES 1024
|
||
#define ANGLE_N_SAMPLES 128 // 需能整除N_SAMPLES
|
||
|
||
#define LIS3DSH_CONFIG_FS_NVS_KAY "l_cfg_fs"
|
||
#define LIS3DSH_CONFIG_ODR_NVS_KAY "l_cfg_odr"
|
||
#define SHAKE_CONFIG_FMIN_NVS_KAY "s_cfg_fmin"
|
||
#define SHAKE_CONFIG_FMAX_NVS_KAY "s_cfg_fmax"
|
||
#define SHAKE_CONFIG_INATH_NVS_KAY "s_cfg_inath"
|
||
#define SHAKE_CONFIG_OUTATH_NVS_KAY "s_cfg_outath"
|
||
#define SHAKE_CONFIG_CH1_NVS_KAY "s_cfg_ch1"
|
||
#define SHAKE_CONFIG_CH2_NVS_KAY "s_cfg_ch2"
|
||
#define SHAKE_CONFIG_SEL_NVS_KAY "s_cfg_sel"
|
||
|
||
typedef float fft_type_t;
|
||
|
||
typedef struct
|
||
{
|
||
uint8_t fscale; // 寄存器配置的参数
|
||
uint8_t odr;
|
||
|
||
int fscale_v; // 实际值
|
||
int odr_v;
|
||
} lis3dsh_config_t;
|
||
|
||
// 三轴加速度,经过单位转换,g
|
||
typedef struct
|
||
{
|
||
fft_type_t a[3]; // x,y,z
|
||
} lis3dsh_adata_t;
|
||
|
||
typedef struct
|
||
{
|
||
uint16_t feq_min_id; // 注意,单位并不是HZ,为: 1600/N_SAMPLES HZ
|
||
uint16_t feq_max_id;
|
||
fft_type_t enter_amplitude_th;
|
||
fft_type_t exit_amplitude_th;
|
||
uint8_t channel1; // FFT可以同时计算两轴的频谱图,选择[1,2,3]->[x,y,z]
|
||
uint8_t channel2;
|
||
uint8_t select; // 选择进行震动检测的是频谱图,选择[1,2,3]->[ch1,ch2,both]
|
||
} shake_detect_cfg_t;
|
||
|
||
typedef struct
|
||
{
|
||
uint16_t count;
|
||
uint8_t is_shake;
|
||
float wind[N_SAMPLES];
|
||
fft_type_t y_cf[N_SAMPLES * 2];
|
||
fft_type_t *y1_cf;
|
||
fft_type_t *y2_cf;
|
||
fft_type_t sum_y[N_SAMPLES / 2];
|
||
} fft_data_t;
|
||
|
||
typedef struct
|
||
{
|
||
uint16_t count;
|
||
float x;
|
||
float y;
|
||
float z;
|
||
} angle_data_t;
|
||
|
||
|
||
extern int buf_r;
|
||
extern shake_detect_cfg_t shake_cfg;
|
||
extern lis3dsh_adata_t lis3dsh_data[N_SAMPLES * 2];
|
||
extern fft_data_t fft_data;
|
||
extern angle_data_t angle_data;
|
||
|
||
void shake_detect_init(void);
|
||
void lis3dsh_calculating_angle(float ax, float ay, float az, float *pitch, float *roll, float *yaw);
|
||
|
||
int lis3dsh_set_fscale(uint8_t fscale);
|
||
int lis3dsh_set_odr(uint8_t odr);
|
||
uint8_t lis3dsh_get_fscale(void);
|
||
uint8_t lis3dsh_get_odr(void);
|
||
int lis3dsh_config_restore_default(void);
|
||
int lis3dsh_config_nvs_load(void);
|
||
int lis3dsh_config_nvs_save(void);
|
||
|
||
int shake_config_restore_default(void);
|
||
int shake_config_nvs_load(void);
|
||
int shake_config_nvs_save(void);
|
||
|
||
#endif |