注册会员
- 积分
- 11
- 威望
- 7
- 贡献
- 4
- 兑换币
- 5
- 注册时间
- 2017-7-24
- 在线时间
- 0 小时
|
u16 AD_L_num_old = 0 , AD_R_num_old = 0 ;
u16 nLeft , nRight ;
void Ad_Value(void)
{
u16 AD_L_num = 0 , AD_R_num = 0 ;
u16 AD_L_ave = 0 , AD_R_ave = 0 ;
float Add, Sub ;
/****************平均滤波****************/
u8 flog ;
for( flog = 0 ; flog < 10 ; flog++ )
{
AD_L_num += Get_ADC10bitResult(4) ;
AD_R_num += Get_ADC10bitResult(5) ;
}
AD_L_ave = AD_L_num / 2 ;
AD_R_ave = AD_R_num / 2 ;
/*************一阶滞后滤波法*************/
AD_L_num = (AD_L_ave * NUM_AD + AD_L_num * (100 - NUM_AD))/100;
AD_R_num = (AD_R_ave * NUM_AD + AD_R_num * (100 - NUM_AD))/100;
AD_L_num_old = AD_L_num ;
AD_R_num_old = AD_R_num ;
//fashong(3,AD_L_num);
//fashong(4,AD_R_num);
/*****************归一化*****************/
nLeft = GYH(310 , 15 , AD_L_num);
nRight = GYH(400 , 15 , AD_R_num);
/**************防止数据等于零,在拟合偏差时出错**************/
//if( nLeft == 0) nLeft = 1 ;
//if( nRight == 0 ) nRight = 1 ;
if( ! nLeft ) nLeft = 1 ; // 与if( nLeft == 0) nLeft = 1 ; 等效
if( ! nRight ) nRight = 1 ; // 与if( nRight == 0 ) nRight = 1 ; 等效
/************模拟串口发送数据************/
//fashong(1,nLeft);
//fashong(2,nRight);
/*************丢线处理,若电感超出检测范围,执行上次打角方向的最大角************/
if( nLeft < 50 && nRight < 50 )
{
if(Value_old>0) Value = Value_max;
else if(Value_old<0) Value = -Value_max;
//else Value = Value_old;
}
/******************正常处理*************************/
else
{
/****************电感值差比和,拟合偏差*********************/
Add = nLeft + nRight;
Sub = sqrt(nLeft) - sqrt(nRight); //为保证拟合偏差与实际偏差呈单调变化,对电感值开根号
Value = -600 * Sub / Add;
/*******另一种拟合偏差的方法,小范围内接近线性变化**********/
//Value = 4*( 200 / (float) nLeft - 200 / (float) nRight) ;
/**************对拟合偏差进行限幅,防止数据溢出**************/
Value = (Value > Value_max) ? Value_max: Value;
Value = (Value < -Value_max) ? ( -Value_max) : Value;
}
Value_old = Value; //记录上一次的偏差值
//fashong(5,(u16)Value);
}
//========================================================================
// 函数: int GYH(int max,int min,int value)
// 描述: 归一化电感
// 参数: max,min是电感最大最小值,value是实时电感值
// 返回:无符号结果值
// 版本: V1.0, 2017-5-18
//========================================================================
int GYH(int max,int min,int value)
{
float val = value;
val = (val > max) ? max : val;
val = (val < min) ? min : val;
val = (val - min)/(max - min);
val *= 200;
return (int)val;
}
|
|