称重算法

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],%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

你可能感兴趣的:(称重算法)