#include "measure.h"
#include "math.h"
#include "string.h"

#include <stdio.h>
static 	uint32_t currcal[5]={0};
static  uint32_t currcala[5]={0};
static  uint32_t currcnt[5]={0};
uint32_t currFact[3]={0};

uint16_t gndcurrent=0;
uint32_t Imbalancecurrent=0;

uint16_t gndcurrentMax=0;

uint32_t triplevel=0;//脱扣等级
uint32_t ratedcurrent=0;//整定电流（AD采样值）
uint32_t Ie=0;//整定电流（实际值）
uint32_t Ctime=0;//C倍电流时间 min
uint32_t Dtime=0;//D倍电流时间
uint32_t Stalltime=0;//堵转时间设定
uint8_t GNDProtectEn=0;//接地保护使能，0关闭，1使能。
uint8_t LEDRunFlag=0;//0:主回路接通，1：检测到电流

uint16_t B_LEVEL_BLE = 0;	//       额定电流等级蓝牙输入
uint16_t A_LEVEL_BLE = 0;	//       脱扣等级蓝牙输入

uint16_t T1_LEVEL_BLE=1;    //    不平衡率蓝牙
uint16_t T2_LEVEL_BLE=1;    //    阻塞保护蓝牙
uint16_t STALL_BLE = 1; 	//    堵转保护开关蓝牙
uint16_t IMBA_BLE  = 1;		//    自动复位蓝牙
uint16_t Grand_BLE = 1;     //    接地保护蓝牙

uint16_t Resetbit_BLE = 1;  //	  蓝牙复位开关，默认为1.按下为0
uint16_t Stopbit_BLE = 1;	//	  蓝牙停止按键，默认为1，按下为0


uint32_t V_Ie_Min=10;
uint8_t Trip_Ie_Flag=0;

extern float CoeffA;
extern float CoeffB;
extern float CoeffC;	

static uint32_t abs(uint32_t r,uint32_t cnt);
void measureImbalance(uint32_t pcurrFact[],uint32_t *pImbalancecurrent);

void counttriplevel(uint32_t trip1);
void countratedcurrent(uint32_t ratedcurrent1);
//void CountStallTime(uint32_t trip1);

void measureTask(void)
{
	int i,j;
	do
	{
		
	}
	while(!ADC_done);//20ms计算一次
	ADC_done=0;

	
	for(i=0;i<5;i++)//累加
	{
		for(j=0;j<16;j++)
		{
			currcal[i]+=result_buffer[i][j];
//			if(i==3)
//			{								
//				if(gndcurrentMax<result_buffer[i][j])//读取接地电流最大值
//				{
//					gndcurrentMax=result_buffer[i][j];
//				}
//			}
		}
	}
	for(i=0;i<5;i++)//求平均
	{
		currcnt[i]=currcal[i]/16;
		currcal[i]=0;
	}
	
	//gndcurrent=currcnt[3];//接地电流
	//gndcurrent=gndcurrentMax;
	ratedcurrent = currcnt[3];//B_LEVEL（额定电流）
	triplevel = currcnt[4];//A_LEVEL(脱扣等级)	
	
	if(!nrf_gpio_pin_read(BLE_KEY))
	{
		counttriplevel_BLE(A_LEVEL_BLE);//计算脱扣等级,C倍，D倍,堵转时间
		countratedcurrent_BLE(B_LEVEL_BLE);//计算额定电流

	}
	else{
		counttriplevel(triplevel);//计算脱扣等级,C倍，D倍,堵转时间
		countratedcurrent(ratedcurrent);//计算额定电流
	}
	
	
//	if(Trip_Ie_Flag==0)//脱扣额定电流上电只计算一次,20200910改为每次计算
//	{

//		Trip_Ie_Flag=1;
//	}
	
//	gndcurrent=(gndcurrentMax-currcnt[3])*10/Ie;//需要整定
	
	for(i=0;i<3;i++)
	{
		for(j=0;j<16;j++)
		{
			currcal[i]+=abs(result_buffer[i][j],currcnt[i]);//减去抬压值，累加
		}
		currcala[i]=currcal[i];//累加值
		currcal[i]=0;
	}
		
	
//	currFact[0]=sqrt(currcala[0]/64);//三相电流有效值
//	currFact[1]=sqrt(currcala[1]/64);
//	currFact[2]=sqrt(currcala[2]/64);
	
 	currFact[0]=CoeffA*100*sqrt(currcala[0]/16)/AD_coefficient;//系数调整（比实际扩大10倍）
	currFact[1]=CoeffB*100*sqrt(currcala[1]/16)/AD_coefficient;
	currFact[2]=CoeffC*100*sqrt(currcala[2]/16)/AD_coefficient;
	
//	printf("A:%d,B:%d,C:%d\n",currFact[0], currFact[1], currFact[2]);
//	currFact[0]=230;//测试
//	currFact[1]=250;
// 	currFact[2]=240;

	measureImbalance(currFact,&Imbalancecurrent);
	
	if(FaultLEDFlashCount > 200)//50*4=100,4s
	{
		FaultLEDFlashCount = 0;
		
	}
	
	if((currFact[0]>StopCurrent)||(currFact[1]>StopCurrent)||(currFact[2]>StopCurrent))//不同规格大于值不同
	{
		LEDRunFlag=1;
	}
	else
	{
		LEDRunFlag=0;
	}
}

uint32_t abs(uint32_t r,uint32_t cnt)
{
	uint32_t source,c;
	source=r;
	c=cnt;
	if(source>c)
	{
		return (source-c)*(source-c);
	}
	else 
	{
		return (c-source)*(c-source);
	}
}

void measureImbalance(uint32_t pcurrFact[],uint32_t *pImbalancecurrent)
{
	int i;
	uint32_t Ii=0;
	uint32_t Iavg=0;
	uint32_t Imax=0;
	uint32_t Imin=0;
	
	Iavg=(pcurrFact[0]+pcurrFact[1]+pcurrFact[2])/3;
	Imax=pcurrFact[0];
	Imin=pcurrFact[0];
	
	for(i=1;i<3;i++)
	{
		if(Imax<pcurrFact[i])			
		{
			Imax=pcurrFact[i];
		}
		
		if(Imin>pcurrFact[i])
		{
			Imin=pcurrFact[i];
		}
	}
	
	Imax=Imax-Iavg;
	Imin=Iavg-Imin;
	Ii=Imax;
	
	if(Imax<Imin)
	{
		Ii=Imin;
	}

	if(Iavg>0)
	{
		*pImbalancecurrent=(uint32_t)100*Ii/Iavg;
	}
	else 
	{
		*pImbalancecurrent=0;
	}
}

void counttriplevel(uint32_t trip1)//脱扣等级(Ctime单位min,Dtime单位20mS)
{
	if(trip1 < 452)//20200415修改标度、范围量
	{		
		//class=5
		Ctime = 1;	
		Dtime = 100;	//50*2=100,2s
		Stalltime = 400;//50*8=400,8s
		GNDProtectEn = 0;
	}
	else if((trip1 >= 452)&&(trip1 < 870))
	{
		//class=10
		Ctime = 2; 
		Dtime = 300; //50*6=300,6s
		Stalltime = 600;//50*12=600,12s
		GNDProtectEn = 0;
	}
	else if((trip1 >= 870)&&(trip1 < 1308))
	{
		//class=20
		Ctime = 4;		
		Dtime = 450;	//50*9=450,9s
		Stalltime = 1000;//50*20=1000,20s
		GNDProtectEn = 0;
	}
	else if((trip1 >= 1308)&&(trip1 < 2054))
	{
		//class=30
		Ctime = 6;		
		Dtime = 600;	//50*12=600,12s
		Stalltime = 1500;//50*30=1500,30s
		GNDProtectEn = 0;
	}
		else if((trip1 >= 2054)&&(trip1 < 2831))
	{		
		//class=30
		Ctime = 6;	
		Dtime = 600;	//50*12=600,12s
		Stalltime = 1500;//50*30=1500,30s
		GNDProtectEn = 1;
		
	}
		else if((trip1 >= 2831)&&(trip1 < 3270))
	{	
		//class=20
		Ctime = 4;	
		Dtime = 450;	//50*9=450,9s	
		Stalltime = 1000;//50*20=1000
		GNDProtectEn = 1;
	}
		else if((trip1 >= 3270)&&(trip1 < 3673))
	{	
		//class=10
		Ctime = 2;	
		Dtime = 300; //50*6=300,6s	
		Stalltime = 600;//50*12=600
		GNDProtectEn = 1;
	}
		else if((trip1 >= 3673))
	{		
		//class=5
		Ctime = 1;	
		Dtime = 100;	//50*2=100,2s	
		Stalltime = 400;//50*8=400,8s
		GNDProtectEn = 1;
	}
}

void countratedcurrent(uint32_t ratedcurrent1)//整定电流
{
//	uint32_t ratedcurrent2;
	uint32_t IeCount=0;
	
//	ratedcurrent2 = 4096-ratedcurrent1;//ad值与标度线性相反
//	
//	if(ratedcurrent2<V_Ie_Min)
//	{
//		Ie=SetCurrent_Min;
//	}
////	else
////	{
////		Ie=SetCurrent_Min+ratedcurrent2*SetCurrent_Add/4096;
////	}
	
	IeCount=SetCurrent_Min-ratedcurrent1*SetCurrent_Add;
	Ie=IeCount/10000;
	
	if(ratedcurrent1<450)
	{
		Ie=Ie_MAX;
	}
	
	if(ratedcurrent1>3680)
	{
		Ie=Ie_MIN;
	}
}










void counttriplevel_BLE(uint32_t trip1)//脱扣等级(Ctime单位min,Dtime单位20mS)
{
	switch(trip1)
	{
		case 1:   		//class=5
						Ctime = 1;	
						Dtime = 100;	//50*2=100,2s
						Stalltime = 400;//50*8=400,8s
						break;
					

		case 2:         //class=10
						Ctime = 2; 
						Dtime = 300; //50*6=300,6s
						Stalltime = 600;//50*12=600,12s
						break;						
		
		case 4:			//class=20
						Ctime = 4;		
						Dtime = 450;	//50*9=450,9s
						Stalltime = 1000;//50*20=1000,20s
						break;					
		
		case 6:			//class=30
						Ctime = 6;		
						Dtime = 600;	//50*12=600,12s
						Stalltime = 1500;//50*30=1500,30s
						break;
	}

}

void countratedcurrent_BLE(uint32_t ratedcurrent1)//整定电流
{
	Ie = ratedcurrent1;
	if(ratedcurrent1<10)
	{
		Ie=10;
	}
	
	if(ratedcurrent1>50)
	{
		Ie=50;
	}
}
