#include "protect.h"
#include "measure.h"
#include <stdio.h>
#include "nrf.h"


extern uint32_t currFact[3];
extern uint32_t Ie;
extern uint32_t gndcurrent;
extern uint32_t Imbalancecurrent;

extern uint32_t Maxhotcap;
extern uint32_t Dtime;
extern uint32_t Ctime;

extern uint32_t Tripbit;
extern uint32_t AutoResetCount;
extern uint8_t FaultLEDFlash;

uint32_t tempcur=0;
uint32_t tempsum=0;
uint32_t Addcurrent=0;

uint32_t hotcap=0;
uint32_t lasthotcap=0;

uint32_t test=1;
uint32_t Max_I=0;
uint32_t hotstate=0;

uint32_t PhaseLosstime=0;
uint32_t Groundtime=0;
uint32_t Imbalancetime=0;
uint32_t Jamtime=0;

uint32_t Lowlimit=10;
uint8_t HotCapMaxFlag=0;//热容饱和标志
uint8_t HotCapCountFlag=0;//热容饱和标志


void PhaseLossProtect(void)
{
	if(((currFact[0]<3*Ie)&&(currFact[1]>=9*Ie)&&(currFact[2]>=9*Ie))||((currFact[0]>=9*Ie)&&(currFact[1]<3*Ie)&&(currFact[2]>=9*Ie))||((currFact[0]>=9*Ie)&&(currFact[1]>=9*Ie)&&(currFact[2]<3*Ie)))
	{
		PhaseLosstime++;
		if(PhaseLosstime >= 100)
		{
			PhaseLosstime=0;//脱扣处理			
			
			RelayOn;
			RunLedOff;
			FaultLed1On;
			
			Tripbit=1;
			AutoResetCount=0;
			FaultLEDFlash=5;
		}
	}
	else
	{
		PhaseLosstime=0;
	}
}

void GroundProtect(void)
{
	if(gndcurrent >= GroundProtectValue)//待定！！！
	{
		Groundtime++;
		if(Groundtime >= 100)
		{
			Groundtime=0;//脱扣处理
			RelayOn;
			
			RunLedOff;
			Tripbit=1;
			AutoResetCount=0;
			FaultLEDFlash=4;
		}
	}
	else
	{
		Groundtime=0;
	}
}

void ImbalanceProtect(void)
{
	if(Imbalancecurrent>30)
	{
			Imbalancetime++;
			if(Imbalancetime >= 100)
			{
				Imbalancetime=0;//脱扣处理
				RelayOn;
				
				RunLedOff;
				Tripbit=1;
				AutoResetCount=0;
				FaultLEDFlash=1;
			}
	}
	else
	{
			Imbalancetime=0;
	}
}


void JamProtect(void)
{	
	Max_I=currFact[0];
	if(Max_I<currFact[1])	
	{
		Max_I=currFact[1];
	}
	if(Max_I<currFact[2])
	{
		Max_I=currFact[2];
	}
			
	tempcur=Max_I*10/Ie;
	if(tempcur>400)//大于4倍电流，通电
	{
		Jamtime++;
		if(Jamtime>100)
		{
			Jamtime=0;
			RelayOn;
			RunLedOff;
			
			Tripbit=1;
			AutoResetCount=0;
			FaultLEDFlash=3;
		}	
	}
	else
	{
			Jamtime=0;
	}
}

void StallProtect(void)
{
	Max_I=currFact[0];
	if(Max_I<currFact[1])	
	{
		Max_I=currFact[1];
	}
	if(Max_I<currFact[2])
	{
		Max_I=currFact[2];
	}
			
	tempcur=Max_I*10/Ie;
	if(tempcur>400)//大于4倍电流，通电
	{
		Jamtime++;
		if(Jamtime>100)
		{
			Jamtime=0;
			RelayOn;
			RunLedOff;
			
			Tripbit=1;
			AutoResetCount=0;
			FaultLEDFlash=2;
		}	
	}
	else
	{
			Jamtime=0;
	}
}

void OverloadProtect(void)
{		
	Max_I=currFact[0];
	if(Max_I<currFact[1])
	{	
		Max_I=currFact[1];	
	}
	
	if(Max_I<currFact[2])
	{	
		Max_I=currFact[2];	
	}
	
	tempcur=Max_I*10/Ie;//比例
	
	if(SetCurrent_Min==543600)//0.1-0.5A电流规格
	{
		Addcurrent=(uint32_t)(10-0.2*Ie);
		tempcur+=Addcurrent;
		
		if(Ie < 30)
		{
			Lowlimit = 25;
		}
		else
		{
			Lowlimit = 10;
		}
	}
	
	if(SetCurrent_Min==217400)//0.4-2A电流规格
	{
		Addcurrent=(uint32_t)(6.25-0.3125*Ie);
		tempcur+=Addcurrent;
	}
			
	if(hotcap<4500000)//没有达到热平衡
	{		
		if(tempcur < Lowlimit)//断电热容衰减
		{						
			if(hotcap<5000)//热容衰减
			{	
				hotcap=0;
				HotCapMaxFlag = 0;//冷态标志位
			}
			else
			{	
				hotcap-=1296;
			}
		}
		else if((tempcur>=Lowlimit)&&(tempcur<650))
					{
							tempsum=tempcur*tempcur/100;
							hotcap+=tempsum;//热容累加
					}
					else if(tempcur>=650)//7.2倍D倍电流
							{
									tempsum=tempcur*tempcur/100;
									hotcap+=tempsum;//热容累加
									
									if(HotCapMaxFlag == 0)//冷态启动
									{
										switch(Dtime)	
										{
											case 100:
															if(hotcap > 518400)
															{	
																RelayOn;
																RunLedOff;
																FaultLed1On;
																
																Tripbit=1;
																AutoResetCount=0;
																FaultLEDFlash=5;
															}
															break;
											case 300:
															if(hotcap > 1555200)
															{	
																RelayOn;
																RunLedOff;
																FaultLed1On;
																
																Tripbit=1;
																AutoResetCount=0;
																FaultLEDFlash=5;
															}
															break;
											case 450:
															if(hotcap > 2332800)
															{	
																RelayOn;
																RunLedOff;
																FaultLed1On;
																
																Tripbit=1;
																AutoResetCount=0;
																FaultLEDFlash=5;
															}
															break;
											case 600:
															if(hotcap > 3110400)
															{	
																RelayOn;
																RunLedOff;
																FaultLed1On;
																
																Tripbit=1;
																AutoResetCount=0;
																FaultLEDFlash=5;
															}
															break;
										}
									}
									else//热态
									{
										if(HotCapCountFlag == 1)//热容重新计算一次
										{
											switch(Dtime)	
											{
												case 100:
																hotcap = 4240800;
																break;
												case 300:
																hotcap = 3722400;
																break;
												case 450:
																hotcap = 3333600;
																break;
												case 600:
																hotcap = 2944800;
																break;
											}
											HotCapCountFlag = 0;
										}																		
									}	
								}
	}
	else//达到热稳态
	{
		HotCapMaxFlag = 1;//热容饱和标志
		HotCapCountFlag = 1;//热容重新赋值标志
		
		if(tempcur < Lowlimit)//断电热容衰减
		{							
			hotcap-=1296;	//热容衰减		
		}
		else if((tempcur>=Lowlimit)&&(tempcur<112))//额定电流以下
					{
						tempsum=tempcur*tempcur/100;//热容不做处理
					}
					else if((tempcur>=112)&&(tempcur<130))//1.2倍，5min脱扣115
							 {
									tempsum=tempcur*tempcur/100;
									hotcap+=tempsum;//热容累加
									
									if(hotcap>=6660000)//5min	
									{
										RelayOn;											
										RunLedOff;
										
										FaultLed1On;
										Tripbit=1;
										AutoResetCount=0;
										FaultLEDFlash=5;
									}					
							 }
							else	if((tempcur>=130)&&(tempcur<165))//1.5倍C倍电流
										{												
												tempsum=tempcur*tempcur/100;
												hotcap+=tempsum;//热容累加
											
												switch(Ctime)	
												{
													case 1:
																	if(hotcap>4837500)
																	{	
																		RelayOn; 																		
																		RunLedOff;
																		FaultLed1On;
																		
																		Tripbit=1;
																		AutoResetCount=0;
																		FaultLEDFlash=5;
																	}
																	break;
													case 2:
																	if(hotcap>5512500)
																	{
																		RelayOn;
																		RunLedOff;
																		FaultLed1On;
																		
																		Tripbit=1;
																		AutoResetCount=0;
																		FaultLEDFlash=5;
																	}
																	break;
													case 4:
																	if(hotcap>6525000)
																	{	
																		RelayOn; 
																		RunLedOff;
																		FaultLed1On;
																		
																		Tripbit=1;
																		AutoResetCount=0;
																		FaultLEDFlash=5;
																	}
																	break;
													case 6:
																	if(hotcap>7200000)
																	{	
																		RelayOn;
																		RunLedOff;
																		FaultLed1On;
																		
																		Tripbit=1;
																		AutoResetCount=0;
																		FaultLEDFlash=5;
																	}
																	break;
												}																										
										}
										else	if((tempcur>=165)&&(tempcur<650))
													{
														tempsum=tempcur*tempcur/100;
														hotcap+=tempsum;//热容累加
														
														if(hotcap>=6660000)	
														{		
															RelayOn;	
															RunLedOff;
															FaultLed1On;
															
															Tripbit=1;
															AutoResetCount=0;
															FaultLEDFlash=5;
														}
													}
													else if(tempcur>=650)//互感器采集电流衰减补偿  7.2取6.7*2/3=450
															{
																RelayOn;	
																	RunLedOff;
																	FaultLed1On;
																	
																	Tripbit=1;
																	AutoResetCount=0;
																	FaultLEDFlash=5;
															}	
	}		
}
