199 lines
4.6 KiB
C
199 lines
4.6 KiB
C
#include "stdint.h"
|
||
#include "string.h"
|
||
#include "stdio.h"
|
||
#include "stm32F1xx_ll_gpio.h"
|
||
|
||
#include "user_data_send.h"
|
||
#include "user_data_process.h"
|
||
#include "lora.h"
|
||
#include "main.h"
|
||
#include "fifo.h"
|
||
|
||
#include <math.h>
|
||
uint32_t pcr_data_distance[10] = {0};
|
||
|
||
|
||
struct recData_process {
|
||
/** The data of the recData */
|
||
const char *recData;
|
||
/** The help text associated with the command */
|
||
//const char *help;
|
||
/** The function that should be invoked for this recData. */
|
||
void (*function) (int argc, char *argv[]);
|
||
};
|
||
|
||
static void lora_state (int argc, char *argv[]);
|
||
static void lora_recv(int argc, char *argv[]);
|
||
//static void lora_signal(int argc, char *argv[]);
|
||
//static void lora_version(int argc, char *argv[]);
|
||
|
||
struct recData_process recData_processes[] = {
|
||
/*lora status check*/
|
||
"\r\n+STATUS:", lora_state,
|
||
"\r\n^STATUS:", lora_state,
|
||
"^STATUS:", lora_state,
|
||
"\r\n^LRRECV:", lora_recv,
|
||
};
|
||
|
||
|
||
/*******************************************************************************
|
||
**lora_state(int argc, char *argv[])
|
||
**功能描述:
|
||
**入口参数:int argc
|
||
char *argv[]
|
||
**输出:无
|
||
*******************************************************************************/
|
||
static void lora_state(int argc, char *argv[])
|
||
{
|
||
int i = 8;
|
||
if((argc == 0) || (argc == 1))
|
||
{
|
||
i = 10;
|
||
}
|
||
switch(argv[0][i]-0x30)
|
||
{
|
||
case LORA_RESET:
|
||
set_lora_state(LORA_RESET);
|
||
break;
|
||
case LORA_P2P:
|
||
set_lora_state(LORA_P2P);
|
||
break;
|
||
case LORA_NOT_JOIN:
|
||
set_lora_state(LORA_NOT_JOIN);
|
||
break;
|
||
case LORA_JOINED:
|
||
set_lora_state(LORA_JOINED);
|
||
break;
|
||
}
|
||
}
|
||
|
||
/*******************************************************************************
|
||
**函数名称:lora_recv(int argc, char *argv[])
|
||
**功能描述:
|
||
**入口参数:int argc
|
||
char *argv[]
|
||
**输出:无
|
||
*******************************************************************************/
|
||
static void lora_recv(int argc, char *argv[])
|
||
{
|
||
for(int i = 0; i< strlen(argv[0]);i++)
|
||
{
|
||
if(argv[0][i] == '<')
|
||
{
|
||
switch(argv[0][i+2]-0x30)
|
||
{
|
||
|
||
default :
|
||
break;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
/*******************************************************************************
|
||
**函数名称:data_process(char *str)
|
||
**功能描述:处理lora模块接收的数据
|
||
**入口参数:char *str 要处理的数据
|
||
**输出:无
|
||
*******************************************************************************/
|
||
void data_process(char *str)
|
||
{
|
||
int i;
|
||
int argc;
|
||
char* argv[5]={NULL};
|
||
argv[0] = str;
|
||
for (i = 0; i < sizeof(recData_processes)/sizeof(struct recData_process); i++)
|
||
{
|
||
if(strncmp(str, recData_processes[i].recData,8) == 0)
|
||
{
|
||
argc = i;
|
||
recData_processes[i].function(argc, argv);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
/*******************************************************************************
|
||
**pcr_data_process(char *str)
|
||
**功能描述:处理pcr数据
|
||
**入口参数:char *str 要处理的数据
|
||
**输出:无
|
||
*******************************************************************************/
|
||
void pcr_data_process(char *str)
|
||
{
|
||
if(strncmp(str, "Distance=",9) == 0)
|
||
{
|
||
uint8_t distance_data[5] = {0x30};
|
||
str +=9;
|
||
|
||
uint8_t space_count = 0;
|
||
while((*str) == ' ')
|
||
{
|
||
str++;
|
||
space_count++;
|
||
}
|
||
if(space_count == 1)
|
||
{
|
||
memcpy(distance_data,str,4);
|
||
Write_RingBuff(((*str)-0x30)*1000+(*(str+1)-0x30)*100+(*(str+2)-0x30)*10+(*(str+3)-0x30));
|
||
}
|
||
else if(space_count == 2)
|
||
{
|
||
memcpy(distance_data+1,str,3);
|
||
Write_RingBuff((*(str)-0x30)*100+(*(str+1)-0x30)*10+(*(str+2)-0x30));
|
||
//Write_RingBuff((distance_data[0]-0x30)*1000+(distance_data[1]-0x30)*100+(distance_data[2]-0x30)*10+(distance_data[3]-0x30));
|
||
}
|
||
}
|
||
}
|
||
void Check_StandardDeviation(void)
|
||
{
|
||
if(Read_RingBuff_Length() >= 10)
|
||
{
|
||
for(uint8_t i = 0; i<10; i++)
|
||
{
|
||
Read_RingBuff(&pcr_data_distance[i]);
|
||
}
|
||
uint32_t number =sizeof(pcr_data_distance)/sizeof(pcr_data_distance[0]);
|
||
uint32_t sum = 0;
|
||
double avg = 0;
|
||
double var = 0;
|
||
double standard = 0;
|
||
for(uint8_t i= 0; i<10; i++)
|
||
{
|
||
sum += pcr_data_distance[i];
|
||
}
|
||
avg = sum/number;
|
||
for (int j = 0; j <= 9;j++)
|
||
{
|
||
var += pow(pcr_data_distance[j]-avg,2)/number;//求方差
|
||
}
|
||
standard = pow(var,0.5);//求标准差
|
||
|
||
|
||
|
||
//printf("sum is: %d ;avg is:%f; var is: %f ;standard is:%f\r\n",sum,avg,var,standard);
|
||
|
||
|
||
|
||
uint8_t distance_data[5] = {0};
|
||
uint32_t avg_u32 = avg;
|
||
for(int i=4; i>=1; i--)
|
||
{
|
||
distance_data[i] = avg_u32%10;
|
||
avg_u32 /= 10;
|
||
}
|
||
if(standard <= 10)
|
||
{
|
||
distance_data[0] = 1;
|
||
|
||
}
|
||
else
|
||
{
|
||
distance_data[0] = 0;
|
||
}
|
||
|
||
SendData(distance_data);
|
||
}
|
||
|
||
}
|
||
|