STS_M2/PCR_High_Measure - judgment/Src/user_data_process.c

244 lines
5.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "stdint.h"
#include "string.h"
#include "stdio.h"
#include "stm32F1xx_ll_gpio.h"
#include "tim.h"
#include "user_data_send.h"
#include "user_data_process.h"
#include "lora.h"
#include "main.h"
#include "fifo.h"
#include <stdlib.h>
#include <math.h>
uint32_t stable_value = 0;
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);
StartTIM(TIM4);
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)
{
case 1:
stable_value = 0;
break;
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));
}
}
}
int Check_StandardDeviation(void)
{
static uint8_t stable_value_send_status = 0;
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)
{
if(stable_value == 0)
{
stable_value = (uint32_t)avg;
stable_value_send_status = 0;
//printf("stable_value is %d!\r\n", stable_value);
}
else
{
if(!stable_value_send_status)
{
uint32_t value = stable_value;
distance_data[0] = BLOCKAGE_S;
for(int i=4; i>=1; i--)
{
distance_data[i] = value%10;
value /= 10;
}
if(SendStableData(distance_data))
{
stable_value_send_status = 1;
}
else
{
stable_value_send_status = 0;
}
}
if((80 < (stable_value - (uint32_t)avg)) && ((stable_value - (uint32_t)avg)< 200))
{
distance_data[0] = BLOCKAGE_Y;
//printf("Blockage! %d - %d = %d \r\n",stable_value,(uint32_t)avg,stable_value - (uint32_t)avg);
//return 1;
}
else
{
distance_data[0] = BLOCKAGE_N;
}
SendBlockageData(distance_data);
}
}
}
return 0;
}