/**************************** (C) COPYRIGHT 2017 Fortiortech shenzhen *****************************
* File Name          : AddFunction.c
* Author             : Vina Peng,Fortiortech  Appliction Team
* Version            : V1.0
* Date               : 2017-12-26
* Description        : This file contains all the add function used for Motor Control.
***************************************************************************************************
* All Rights Reserved
**************************************************************************************************/


/* Includes -------------------------------------------------------------------------------------*/
#include <FU68xx_2.h>
#include <Myproject.h>

{
	int16 TempCurrentA;
	static int16 TempADC0Value=0; 
	if(mcState==mcTailWind)
	{
		static uint16 MaxCurrent =0;
		static uint16 CurrentDetectTime =0;
		static uint16 CurrentDetectRound =0;
		
//		if(mcFRState.FR	== CW)
//		{
		TempADC0Value = LPFFunction((ADC0_DR<<3), TempADC0Value, 12);
//		}
//		else if(mcFRState.FR	== CCW)
//		{
//			TempADC0Value = LPFFunction((ADC1_DR<<3), TempADC0Value, 12);
//		}
		if(TailWindDetect.TailWindBrakeStatus)
		{
	//		FOC_CR1    = 0x06;	 				// enable break function
	//		FOC_ARR	   = PWM_VALUE_LOAD;
	//		FOC_SWDUTY = FOC_ARR; 
			MOE = 0;
			FOC_CR1 = 0x00;
			ClrBit(DRV_CR, FOCEN);
			DRV_DR  = DRV_ARR+1;
			DRV_CMR &= 0xFFC0;
			DRV_CMR |= 0x015;            // 三相下桥臂通，刹车
			ClrBit(DRV_CR, OCS);         // OCS = 0, DRV_COMR;OCS = 1, FOC/SVPWM/SPWM
			MOE = 1;
			
			mcFocCtrl.State_Count = TailWindDetect.BrakeRelaxTime;
			
			CurrentDetectTime ++ ;
			if(CurrentDetectTime < 400)
			{
				TempCurrentA = abs((TempADC0Value+100) - mcCurOffset.IuOffset);	
				if(TempCurrentA > MaxCurrent)
				{
					MaxCurrent = TempCurrentA;
				}
			}
			else
			{
				CurrentDetectTime =0;
				if(MaxCurrent < BrakeCurrentTh)
				{
					TailWindDetect.TailWindBrakeStatus = 0;
					TailWindDetect.BrakeCurrentDetectStatus = 1;
					mcFocCtrl.State_Count = TailWindDetect.BrakeRelaxTime;
				}
				MaxCurrent = 0;
				CurrentDetectRound ++;
				if(CurrentDetectRound > 20)
				{
					CurrentDetectRound = 0;
					TailWindDetect.TailWindBrakeStatus = 0;
					TailWindDetect.BrakeCurrentDetectStatus = 1;
					mcFocCtrl.State_Count = TailWindDetect.BrakeRelaxTime;
				}
			}
		}
		else
		{
			CurrentDetectTime = 0;
			CurrentDetectRound = 0;
			MaxCurrent = 0;
		}
	}
}

