100g算出对应的值
const uint32_t ref_weight[]=
{
16988315,
17033820,
17081381,
17126525,
17172556,
17218010,
17268282,
17314650,
17361539,
17408550,
17455301,
17501379,
17547412,
17593408,
17640239,
17686084,
17732588,
17777505,
17824336,
17870599,
17921117,
17966931,
18014547,
18050000,
18108768,
18154829,
18202172,
18248547,
18294292,
18340343,
18383359,
18434820,
18482744,
18528033,
18577024,
18622500,
18669262,
18714498,
18760283,
18805527,
18850392,
18897703,
18944395,
18990333,
19036570,
19082382,
19128796,
19177157,
19223828,
19265722,
19308698,
};
uint32_t weight=0;
/* 计算称重的算法
**
*/
uint32_t calc_weight(uint32_t orignal_data)
{
double tmp_f[3];
uint32_t i;
for(i=1; i<=(MAX_ITEMS/1); i++)
{// 0
tmp_f[0]=orignal_data;
tmp_f[1]=ref_weight[i*1];
tmp_f[2]=ref_weight[(i-1)*1];
if(tmp_f[0]<tmp_f[1] || (i==MAX_ITEMS))
{
if(i==MAX_ITEMS)
{
tmp_f[1]=ref_weight[i-1];
tmp_f[2]=ref_weight[i-2];
}
if(tmp_f[0]<tmp_f[2])
{
return 0;//err to litter
}
weight = (uint32_t)(100 * ( i+(tmp_f[0]-tmp_f[2])/(tmp_f[1]-tmp_f[2])-1) );
if(i==MAX_ITEMS)
{
weight-=100;
}
return weight;
}
}
weight = 5000;//err over5kg
return weight;
}
/* 策略处理
**每10ms调用一次
*/
void task_calibrete_weight(void)
{
static unsigned char times20ms=0;
static uint32_t weightb=0;
static uint8_t err_cnt=0xff;
uint32_t tmp;
if(times20ms++<=1) return;
times20ms=0;
calibrate_index%=MAX_CALIBRATE_TIMES;
s_tCurrentValue[0][calibrate_index]=HX711_Read(MODE_A);
s_tCurrentValue[1][calibrate_index]=HX711_Read(MODE_B);
//HX7XX_LOGRTT("AB_TATOL,%d,A,%d,B,%d-->,%d,%d\r\n",AB_TATOL,hx_711A_buff,hx_711B_buff,gtatol,get_weight());"W:%d--%ld,%ld\r\n",StuDatas.weight,s_tCurrentValue[0][calibrate_index],s_tCurrentValue[1][calibrate_index]);
calibrate_index++;
if(calibrate_index>=MAX_CALIBRATE_TIMES)//20
{
calibrate_index=0;
hx_711A_buff=filter_weight(&s_tCurrentValue[0][0],MAX_CALIBRATE_TIMES);
hx_711B_buff=filter_weight(&s_tCurrentValue[1][0],MAX_CALIBRATE_TIMES);
AB_TATOL=(hx_711A_buff+hx_711B_buff);
if(0==weightb || err_cnt==0xff)
{
weightb=AB_TATOL;
err_cnt=0;
}
if(weightb>=AB_TATOL)
{
tmp=weightb-AB_TATOL;
} else
{
tmp=AB_TATOL-weightb;
}
if(tmp>4000)
{
if(err_cnt<0xfd)
err_cnt++;
} else
{
err_cnt=0;
}
if(err_cnt==1)
{
AB_TATOL=weightb;
}
weightb=AB_TATOL;
if(0==gtatol)
gtatol=AB_TATOL;
gtatol=(gtatol*9+AB_TATOL)/10;
calc_weight(AB_TATOL);
HX7XX_LOGRTT("AB_TATOL,%d,A,%d,B,%d-->,%d,%d\r\n",AB_TATOL,hx_711A_buff,hx_711B_buff,gtatol,get_weight());
}
}
//去皮用的
void set_weight2zero(void)
{
StuDatas.weight_off_set = weight;
//weight_off_set=weight;
}
//app调用后去重量
uint16_t get_weight(void)
{
uint16_t t_weight;
if( weight > StuDatas.weight_off_set )
{
t_weight = (weight-StuDatas.weight_off_set );
return (((t_weight+9)/10)*10);
} else
{
return 0;//err
}
}
//=================================
/* 第二种算法,不需要测量点。
**
*/
uint32_t calc_weight(uint32_t orignal_data)
{
double weight_temp;
double weight_zero = 17338836;
double weight_5000 = 19523399;
double tmp_f;
tmp_f=orignal_data;
if(tmp_f<weight_zero)
tmp_f=weight_zero;
weight_temp=5000*(tmp_f-weight_zero)/(weight_5000-weight_zero);
weight=weight_temp;
return weight;
}
称重算法
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 今年算是百度算法推出最频繁的一年,也是百度针对用户体验做出最大努力的一年,首先随着移动流量持续增长已经远远超过PC...
- 版权声明作者:zuoxiaolong(左潇龙)出处:博客园左潇龙的技术博客--http://www.cnblogs...