
#include "onechip_include_header.h"

#define HEATER_NEED_PREHEAT    37.0

#define HEATER_MIN_TEMP  15.0
#define  HEATER_MAX_TEMP 95.0
#define HEATER_MIN_ERROR_TEMP  5.0
#define  HEATER_MAX_ERROR_TEMP 115.0
uint32_t get_heater_master_st(float *temp,unsigned int *st) {
	*temp = sysinf.heaterMaster.pid.realtemp + sysinf.heaterMaster.offset;
	*st = sysinf.heaterMaster.HeatState;
	return 0;
}


uint32_t get_heater_slave_st(float *temp,unsigned int *st) {
	*temp = sysinf.heaterSlave.pid.realtemp + sysinf.heaterSlave.offset;
	*st = sysinf.heaterSlave.HeatState;
	return 0;
}

uint32_t check_heater_master_needPreheat(void) {
        
    float temp = getADC_Chanel_value(0);
    sysinf.heaterMaster.pid.realtemp = temp + sysinf.heaterMaster.offset;
    if(sysinf.heaterMaster.pid.realtemp < HEATER_NEED_PREHEAT){
        sysinf.heaterMaster.needpreheat = 1;
        sysinf.heaterMaster.HeatState = SysPreHeatUP;
        sysinf.heaterMaster.stabletime = 0;
    }else{
        sysinf.heaterMaster.needpreheat = 0;
        sysinf.heaterMaster.HeatState = SystemPreHeat;
    }
	return 0;
}

void updatParm_heater_master(void) {
	sysinf.heaterMaster.PreHeatTime = sysinf.PreHeatTime;
	sysinf.heaterMaster.HoldTemp = sysinf.holdTemp;
	sysinf.heaterMaster.readyTemp =  sysinf.readyTemp;
	sysinf.heaterMaster.PresetTempVal = sysinf.PresetTempVal;
	sysinf.heaterMaster.PresetTempTime = sysinf.PresetTempTime*10;
	sysinf.heaterMaster.adJTarget =  sysinf.adJTarget;
	sysinf.heaterMaster.SetTempVal =  sysinf.SetTempVal;
	sysinf.heaterMaster.pid.Kp = sysinf.amplif_Kp;
	sysinf.heaterMaster.pid.Ki = sysinf.amplif_Ki ;
	sysinf.heaterMaster.pid.Kd = sysinf.amplif_Kd;




}

void heater_work(heat_ctrl_def *heater)
{
    if(heater->needpreheat){
        heater->stabletime ++;
        if(heater->stabletime > heater->PreHeatTime * 600){
            /*提前预热阶段完成，进入正式控温流程*/
            sysinf.heaterMaster.needpreheat = 0;
            sysinf.heaterMaster.HeatState = SystemPreHeat;
            sysinf.heaterMaster.stabletime = 0;  
        }
    }
    
    if((heater->pid.realtemp < HEATER_MIN_TEMP) || (heater->pid.realtemp > HEATER_MAX_TEMP)){
        /*稍后添加*/
         com_pwm_Set(heater->pwdId,0);  
         heater->HeatState = SystemPreHeat;
         return;

    }
    if((heater->pid.realtemp < HEATER_MIN_ERROR_TEMP) || (heater->pid.realtemp > HEATER_MAX_ERROR_TEMP)){
        /*稍后添加*/
         com_pwm_Set(heater->pwdId,0);  
         heater->HeatState = SysERRORHeat;
         error_set(ERROR_HEAT_SELF_CHECK);
         return;

    }
    heater->timenum ++;
    switch (heater->HeatState)
    {
        case SystemPreHeat:{
            heater->pid.goal = heater->HoldTemp;  
            if(heater->pid.realtemp -  heater->HoldTemp  > 0.6){
                com_pwm_Set(heater->pwdId,0);                   
                heater->pid.integral = 0;	  
                heater->timenum = 0;       
            }else{
                PIDRealize(&heater->pid);
                com_pwm_Set(heater->pwdId,heater->pid.variate);                
                if(heater->timenum>=60*10)//system led turn red while the temperature could not heat up to PresetTemp over 10min
                {
                    if((heater->pid.realtemp<heater->HoldTemp))
                    {
                        com_pwm_Set(heater->pwdId,0);
                        /*TODO 加热模块故障*/
                        error_set(ERROR_HEAT_TIMEOUT);
                        heater->HeatState = SysERRORHeat;
                        return;
                    }
                }
            }
            
                //稳定条件，大于待机温度-0.5度，并且小于就绪温度
            if(heater->pid.realtemp > ( heater->HoldTemp  - 0.5) && heater->pid.realtemp < heater->readyTemp)
            {
                if(heater->stabletime++ > 10)
                {
                    heater->temp_stable_sta = 1;
                    heater->HeatState = SystemIdle;
                }
            }
            else
            {
                heater->temp_stable_sta = 0;
                heater->stabletime = 0;
            }
        }break;
        case SystemIdle:{                
            PIDRealize(&heater->pid);
            com_pwm_Set(heater->pwdId,heater->pid.variate);
        }break;
        case SysPreHeatUP: {
            com_pwm_Set(heater->pwdId,100);
            if((heater->pid.realtemp>=(heater->PresetTempVal-1)))
            {
                heater->timenum=0;
                heater->HeatState=SysPreKepTemp;
                heater->pid.integral = 0;
                heater->pid.goal = heater->PresetTempVal;
            }
            if(heater->timenum>=60*10)//system led turn red while the temperature could not heat up to PresetTemp over 10min
            {
                if((heater->pid.realtemp<heater->PresetTempVal))
                {
                    /*TODO 加热模块故障*/
                    com_pwm_Set(heater->pwdId,0);                    
                    error_set(ERROR_HEAT_TIMEOUT);
                    heater->HeatState = SysERRORHeat;
                    return;
                }
            }
        }
        break;
        case SysPreKepTemp:{            
            PIDRealize(&heater->pid);
            com_pwm_Set(heater->pwdId,heater->pid.variate);
            if(heater->timenum>=heater->PresetTempTime)//time of duration while initial segment constant temperature stage,timenum*1S
            {
                heater->timenum=0;
                heater->HeatState=SysPreCoolDown;
                com_pwm_Set(heater->pwdId,0);
            }
        }
        break;
        case SysPreCoolDown: {
            if(heater->pid.realtemp<=(heater->SetTempVal-1))
            {
                heater->HeatState=SysPreConTemp;
                heater->overtempnum = 0;
                heater->timenum=0;
                heater->targetAfterAdj = heater->adJTarget + heater->SetTempVal;
                heater->pid.integral = 0;
                heater->pid.goal = heater->targetAfterAdj ;

            }
            heater->stabletime = 0;
        }
        break;
        
        case SysPreConTemp:{        
            PIDRealize(&heater->pid);
            com_pwm_Set(heater->pwdId,heater->pid.variate);
            if(heater->timenum>=20)
            {
                /*system report error while the difference of the temperature over 5℃  3times*/
                if(fabs(heater->pid.realtemp-heater->SetTempVal)>5)
                {
                    heater->overtempnum++;
                    if(heater->overtempnum>=3)
                    {
                        com_pwm_Set(heater->pwdId,0);
                        error_set(ERROR_HEAT_TIMEOUT);
                        heater->HeatState = SysERRORHeat;
                        return;
                    }
                    else
                    {
                        heater->overtempnum = 0;
                    }
                }
                else if(fabs(heater->pid.realtemp- (heater->targetAfterAdj))<=0.5)
                {
                    if(heater->stabletime++ >10)
                    {
                        heater->temp_stable_sta = 2;    //二次加热稳定
                    }
                }
                else{
                    heater->stabletime = 0;
                }
            }
        }
        break;
        default:
            break;
    }    
}
