/*************************** (C) COPYRIGHT 2017 CXHY ****************************
 * 文件名  ：motor.c
 * 描  述  ：电机控制程序
 * 版  本  ：V1.0
 * 作  者  ：XuZhongPing
 * 日  期  ：2017/03/06 
*********************************************************************************/
#include "door_ctrl.h"
#include "motor.h"
#include "ElectronicLock.h"
#include "key.h"
#include "timer.h"
#include "data_typedef.h"	

uint8_t doorResetMark = 0;								//门复位状态

/*
***********************************************************************
*	函 数 名: MotorControlTask_1
*	功能说明: 电机1控制任务
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void MotorControlTask_1(void)
{
	uint8_t tempLimKeyVal = 0;
	static uint8_t controlSteps = 0;
	uint8_t motorStopMark = 0;											//电机停止标志
	uint16_t tempWeight = 0;
	
	static uint32_t startTimer = 0;									//开关门开始时间
	static uint32_t endTimer = 0;										//开关门结束时间
	
	static uint32_t preRemainingTimer = 0;					//上一次还剩时间
	
	static timer waitingTimer_100ms;								//等待100ms定时器
	static timer waitingTimer_300ms;								//等待300ms定时器
	static timer waitingTimer_500ms;								//等待500ms定时器
	static timer waitingTimer_1S;										//等待1S定时器
	static timer waitingUnlockingTimer;							//电磁锁开启时间
	static timer unlockingTimer;										//电磁锁开启时间
	static timer openDoorTimer;											//用于开门时间限制
	static timer closeDoorTimer;										//用于关门时间限制
	static timer waitingTimer_300S;									//5分钟后自动关门
	
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		
		timer_set(&waitingTimer_100ms, 1);
		timer_set(&waitingTimer_300ms, 3);
		timer_set(&waitingTimer_500ms, 5);
		timer_set(&waitingTimer_1S, CLOCK_SECOND*1);
		timer_set(&waitingUnlockingTimer, 2);							//创建1个定时器，用于控制先打开电磁锁然后再开门
		timer_set(&unlockingTimer, 25);											//创建1个定时器，用于控制电磁锁打开时间
		timer_set(&openDoorTimer, 16);											//创建1个定时器，用于开门时间限制
		timer_set(&closeDoorTimer, 20);											//创建1个定时器，用于关门时间限制
		timer_set(&waitingTimer_300S, CLOCK_SECOND*300);		//创建1个定时器，用于5分钟后自动关门
	}

	switch(controlSteps)
	{
		case 0x00:			//初始状态
			if(isOpenDoor_1 == 1)		//电机1请求打开
			{
				printf("\r\n--> 电机1打开!\r\n");	
				
				//开始时先清除用到的变量
				isOpenDoor_1 = 0;				
				isCloseDoorMark_1 = 0;
				openDoorCtrlEndMark_1 = 0;
				
				faultCode_1 = 0x14;
				
				controlSteps = 0x01;											//有开门请求操作，跳转到正在投放操作流程
			}
			break;
			
		case 0x01:		//打开电磁锁
// 			Unlock(1);										//打开电磁锁
			
			timer_restart(&waitingUnlockingTimer);
			timer_restart(&unlockingTimer);
			controlSteps = 0x02;
			break;
			
		case 0x02:		//等待电磁锁打开
			if(timer_expired(&waitingUnlockingTimer))
			{
				controlSteps = 0x03;
			}
			break;
			
		case 0x03:		//开门
			printf("\r\n门1打开\r\n");
			DoorOpen(1);
			
			controlSteps = 0x04;
			printf("\r\n开门限位检测1\r\n");
			timer_restart(&openDoorTimer);							//从当前时间重新开始n秒定时器
			break;
			
		case 0x04:			//开门限位检测
			if(timer_expired(&unlockingTimer))					//开启电磁锁时间到，上锁
			{
// 				Locked(1);
			}
			
			if(timer_expired(&openDoorTimer))						//如果定时时间到了，则开门停止
			{
				DoorStop(1);															//门停止
// 				Locked(1);
	
				printf("\r\n限位超时停止1\r\n");
				printf("\r\n等待投放1\r\n");
				timer_restart(&waitingTimer_300S);				//从当前时间重新开始n秒定时器
				controlSteps = 0x05;
				break;
			}
			
			if(motorRunningStatus_1 == 0)								//电机已经停止了
			{
// 				Locked(1);
				printf("\r\n等待投放1\r\n");
				
				timer_restart(&waitingTimer_300S);				//从当前时间重新开始n秒定时器
				controlSteps = 0x05;
				break;
			}
			
			break;
			
		case 0x05:		//等待关门信号
			if(isCloseDoorMark_1 == 1)
			{
				isCloseDoorMark_1 = 0;
				controlSteps = 0x06;										//转到关门
				break;
			}
			
			if(timer_expired(&waitingTimer_300S))			//如果5分钟定时时间到了，则开门停止
			{
				printf("\r\n5分钟超时自动关门1\r\n");
				controlSteps = 0x06;										//转到关门
				break;
			}
			break;
			
		case 0x06:			//关门		
			printf("\r\n门1关闭中\r\n");
			DoorClose(1);																//关门
			
// 			printf("\r\n关门限位检测1\r\n");
			timer_restart(&closeDoorTimer);							//从当前时间重新开始n秒定时器
			controlSteps = 0x07;
			break;
			
		case 0x07:			//关门
			if(timer_expired(&closeDoorTimer))					//如果定时时间到了，则关门停止
			{
				DoorStop(1);															//门停止

// 				printf("\r\n关门时间到停止1\r\n");				
				controlSteps = 0x08;
				break;
			}
			
			//限位开关检测
			if(LIMI_KEY1 == 0)
			{				
				DoorStop(1);																//门停止	
			}
			
			if(motorRunningStatus_1 == 0)											//电机已经停止了
			{
				DoorStop(1);
// 				printf("\r\nM1->Stop\r\n");
				controlSteps = 0x08;											//转到等待称重数据
				break;
			}
			break;
			
		case 0x08:		//关门限位开关故障检测
			if(LIMI_KEY1 != 0)					//限位开关检测
			{
				faultCode_1 = 0x32;
				limitKeyExceptionMark = 0x20;
				devChannel1FaultMark = 1;
				printf("\r\n投口1限位开关有异常！\r\n");
			}else{
				faultCode_1 = 0x14;
			}
			
			controlSteps = 0x09;
			
//			if(LIMI_KEY1 != 0)					//限位开关检测
//			{
//				controlSteps = 0x30;
//			}else{
//				controlSteps = 0x09;
//			}
			break;
			
		case 0x09:		//开关门控制结束
			isOpenDoor_1 = 0;
			isCloseDoorMark_1 = 0;
			
			openDoorCtrlEndMark_1 = 1;					//标志开关门控制结束
			printf("\r\nNo.1 OK！\r\n");
			controlSteps = 0x00;
			break;
	
//-------------------------------------------------------------------------------------
//关门异常处理		
		case 0x30:
			printf("\r\nE->开锁1\r\n");
// 			Unlock(1);															//打开电磁锁
			timer_restart(&waitingUnlockingTimer);
			controlSteps = 0x31;
			break;
			
		case 0x31:
			if(timer_expired(&waitingUnlockingTimer))
			{
				printf("\r\nE->开门1\r\n");
				DoorOpen(1);
				
				controlSteps = 0x32;
				timer_restart(&waitingTimer_100ms);
			}
			break;
			
		case 0x32:
			if(timer_expired(&waitingTimer_100ms))
			{
				printf("\r\nE->关锁，门1停止\r\n");
// 				Locked(1);
				DoorStop(1);															//门停止
				controlSteps = 0x33;
				printf("\r\nE->门1复位\r\n");
			}
			break;
			
		case 0x33:
			if(DoorReset_1() == 1)
			{
				controlSteps = 0x34;
			}
			break;
			
		case 0x34:
			if(LIMI_KEY1 != 0)
			{
				faultCode_1 = 0x32;
				limitKeyExceptionMark = 0x20;
				devChannel1FaultMark = 1;
				controlSteps = 0x09;
				break;
			}else{																				//已经恢复则回到正常的处理流程
				faultCode_1 = 0x14;
				timer_restart(&waitingTimer_100ms);
				controlSteps = 0x09;
			}
			break;
			
		default:
			controlSteps = 0x00;
			break;
	}
}

/*
***********************************************************************
*	函 数 名: MotorControlTask_1
*	功能说明: 电机1控制任务
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void MotorControlTask_2(void)
{
	uint8_t tempLimKeyVal = 0;
	static uint8_t controlSteps = 0;
	uint8_t motorStopMark = 0;											//电机停止标志
	uint16_t tempWeight = 0;
	
	static uint32_t startTimer = 0;									//开关门开始时间
	static uint32_t endTimer = 0;										//开关门结束时间
	
	static uint32_t preRemainingTimer = 0;					//上一次还剩时间
	
	static timer waitingTimer_100ms;								//等待100ms定时器
	static timer waitingTimer_300ms;								//等待300ms定时器
	static timer waitingTimer_500ms;								//等待500ms定时器
	static timer waitingTimer_1S;										//等待1S定时器
	static timer waitingUnlockingTimer;							//电磁锁开启时间
	static timer unlockingTimer;										//电磁锁开启时间
	static timer openDoorTimer;											//用于开门时间限制
	static timer closeDoorTimer;										//用于关门时间限制
	static timer waitingTimer_300S;									//5分钟后自动关门
	
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		
		timer_set(&waitingTimer_100ms, 1);
		timer_set(&waitingTimer_300ms, 3);
		timer_set(&waitingTimer_500ms, 5);
		timer_set(&waitingTimer_1S, CLOCK_SECOND*1);
		timer_set(&waitingUnlockingTimer, 2);								//创建1个定时器，用于控制先打开电磁锁然后再开门
		timer_set(&unlockingTimer, 25);											//创建1个定时器，用于控制电磁锁打开时间
		timer_set(&openDoorTimer, 16);											//创建1个定时器，用于开门时间限制
		timer_set(&closeDoorTimer, 20);											//创建1个定时器，用于关门时间限制
		timer_set(&waitingTimer_300S, CLOCK_SECOND*300);		//创建1个定时器，用于5分钟后自动关门
	}

	switch(controlSteps)
	{
		case 0x00:			//初始状态
			if(isOpenDoor_2 == 1)		//电机1请求打开
			{
				printf("\r\n--> 电机2打开!\r\n");	
				
				//开始时先清除用到的变量
				isOpenDoor_2 = 0;				
				isCloseDoorMark_2 = 0;
				openDoorCtrlEndMark_2 = 0;
				
				faultCode_2 = 0x14;
				
				controlSteps = 0x01;											//有开门请求操作，跳转到正在投放操作流程
			}
			break;
			
		case 0x01:		//打开电磁锁
// 			Unlock(2);										//打开电磁锁
			
			timer_restart(&waitingUnlockingTimer);
			timer_restart(&unlockingTimer);
			controlSteps = 0x02;
			break;
			
		case 0x02:		//等待电磁锁打开
			if(timer_expired(&waitingUnlockingTimer))
			{
				controlSteps = 0x03;
			}
			break;
			
		case 0x03:		//开门
			printf("\r\n门2打开\r\n");
			DoorOpen(2);
			
			controlSteps = 0x04;
			printf("\r\n开门限位检测2\r\n");
			timer_restart(&openDoorTimer);							//从当前时间重新开始n秒定时器
			break;
			
		case 0x04:			//开门限位检测
			if(timer_expired(&unlockingTimer))					//开启电磁锁时间到，上锁
			{
// 				Locked(2);
			}
			
			if(timer_expired(&openDoorTimer))						//如果定时时间到了，则开门停止
			{
				DoorStop(2);															//门停止
// 				Locked(2);
	
				printf("\r\n限位超时停止2\r\n");
				printf("\r\n等待投放2\r\n");
				timer_restart(&waitingTimer_300S);				//从当前时间重新开始n秒定时器
				controlSteps = 0x05;
				break;
			}
			
			if(motorRunningStatus_2 == 0)								//电机已经停止了
			{
// 				Locked(2);
				printf("\r\n等待投放2\r\n");
				
				timer_restart(&waitingTimer_300S);				//从当前时间重新开始n秒定时器
				controlSteps = 0x05;
				break;
			}
			
			break;
			
		case 0x05:		//等待关门信号
			if(isCloseDoorMark_2 == 1)
			{
				isCloseDoorMark_2 = 0;
				controlSteps = 0x06;										//转到关门
				break;
			}
			
			if(timer_expired(&waitingTimer_300S))			//如果5分钟定时时间到了，则开门停止
			{
				printf("\r\n5分钟超时自动关门2\r\n");
				controlSteps = 0x06;										//转到关门
				break;
			}
			break;
			
		case 0x06:			//关门		
			printf("\r\n门2关闭中\r\n");
			DoorClose(2);																//关门
			
// 			printf("\r\n关门限位检测2\r\n");
			timer_restart(&closeDoorTimer);							//从当前时间重新开始n秒定时器
			controlSteps = 0x07;
			break;
			
		case 0x07:			//关门
			if(timer_expired(&closeDoorTimer))					//如果定时时间到了，则关门停止
			{
				DoorStop(2);															//门停止

// 				printf("\r\n关门时间到停止2\r\n");				
				controlSteps = 0x08;
				break;
			}
			
			//限位开关检测
			if(LIMI_KEY2 == 0)
			{				
				DoorStop(2);																//门停止	
			}
			
			if(motorRunningStatus_2 == 0)											//电机已经停止了
			{
				DoorStop(2);
// 				printf("\r\nM2->Stop\r\n");
				controlSteps = 0x08;											//转到等待称重数据
				break;
			}
			break;
			
		case 0x08:		//关门限位开关故障检测
			if(LIMI_KEY2 != 0)					//限位开关检测
			{
				faultCode_2 = 0x32;
				limitKeyExceptionMark = 0x20;
				devChannel2FaultMark = 1;
				printf("\r\n投口2限位开关有异常！\r\n");
			}else{
				faultCode_2 = 0x14;
			}
			
			controlSteps = 0x09;
			
//			if(LIMI_KEY2 != 0)					//限位开关检测
//			{
//				controlSteps = 0x30;
//			}else{
//				controlSteps = 0x09;
//			}
			break;
			
		case 0x09:		//开关门控制结束
			isOpenDoor_2 = 0;
			isCloseDoorMark_2 = 0;
			
			openDoorCtrlEndMark_2 = 1;					//标志开关门控制结束
			printf("\r\nNo.2 OK！\r\n");
			controlSteps = 0x00;
			break;
	
//-------------------------------------------------------------------------------------
//关门异常处理		
		case 0x30:
			printf("\r\nE->开锁2\r\n");
// 			Unlock(2);															//打开电磁锁
			timer_restart(&waitingUnlockingTimer);
			controlSteps = 0x31;
			break;
			
		case 0x31:
			if(timer_expired(&waitingUnlockingTimer))
			{
				printf("\r\nE->开门2\r\n");
				DoorOpen(2);
				
				controlSteps = 0x32;
				timer_restart(&waitingTimer_100ms);
			}
			break;
			
		case 0x32:
			if(timer_expired(&waitingTimer_100ms))
			{
				printf("\r\nE->关锁，门2停止\r\n");
// 				Locked(2);
				DoorStop(2);															//门停止
				controlSteps = 0x33;
				printf("\r\nE->门2复位\r\n");
			}
			break;
			
		case 0x33:
			if(DoorReset_2() == 1)
			{
				controlSteps = 0x34;
			}
			break;
			
		case 0x34:
			if(LIMI_KEY2 != 0)
			{
				faultCode_2 = 0x32;
				limitKeyExceptionMark = 0x20;
				devChannel2FaultMark = 1;
				controlSteps = 0x09;
				break;
			}else{																				//已经恢复则回到正常的处理流程
				faultCode_2 = 0x14;
				timer_restart(&waitingTimer_100ms);
				controlSteps = 0x09;
			}
			break;
			
		default:
			controlSteps = 0x00;
			break;
	}
}

/*
***********************************************************************
*	函 数 名: MotorControlTask_3
*	功能说明: 电机1控制任务
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void MotorControlTask_3(void)
{
	uint8_t tempLimKeyVal = 0;
	static uint8_t controlSteps = 0;
	uint8_t motorStopMark = 0;											//电机停止标志
	uint16_t tempWeight = 0;
	
	static uint32_t startTimer = 0;									//开关门开始时间
	static uint32_t endTimer = 0;										//开关门结束时间
	
	static uint32_t preRemainingTimer = 0;					//上一次还剩时间
	
	static timer waitingTimer_100ms;								//等待100ms定时器
	static timer waitingTimer_300ms;								//等待300ms定时器
	static timer waitingTimer_500ms;								//等待500ms定时器
	static timer waitingTimer_1S;										//等待1S定时器
	static timer waitingUnlockingTimer;							//电磁锁开启时间
	static timer unlockingTimer;										//电磁锁开启时间
	static timer openDoorTimer;											//用于开门时间限制
	static timer closeDoorTimer;										//用于关门时间限制
	static timer waitingTimer_300S;									//5分钟后自动关门
	
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		
		timer_set(&waitingTimer_100ms, 1);
		timer_set(&waitingTimer_300ms, 3);
		timer_set(&waitingTimer_500ms, 5);
		timer_set(&waitingTimer_1S, CLOCK_SECOND*1);
		timer_set(&waitingUnlockingTimer, 15);							//创建1个定时器，用于控制先打开电磁锁然后再开门
		timer_set(&unlockingTimer, 25);											//创建1个定时器，用于控制电磁锁打开时间
		timer_set(&openDoorTimer, 15);											//创建1个定时器，用于开门时间限制
		timer_set(&closeDoorTimer, 20);											//创建1个定时器，用于关门时间限制
		timer_set(&waitingTimer_300S, CLOCK_SECOND*300);		//创建1个定时器，用于5分钟后自动关门
	}

	switch(controlSteps)
	{
		case 0x00:			//初始状态
			if(isOpenDoor_3 == 1)		//电机1请求打开
			{
				printf("\r\n--> 电机3打开!\r\n");	
				
				//开始时先清除用到的变量
				isOpenDoor_3 = 0;				
				isCloseDoorMark_3 = 0;
				openDoorCtrlEndMark_3 = 0;
				
				faultCode_3 = 0x14;
				
				controlSteps = 0x01;											//有开门请求操作，跳转到正在投放操作流程
			}
			break;
			
		case 0x01:		//打开电磁锁
			Unlock(3);										//打开电磁锁
			
			timer_restart(&waitingUnlockingTimer);
			timer_restart(&unlockingTimer);
			controlSteps = 0x02;
			break;
			
		case 0x02:		//等待电磁锁打开
			if(timer_expired(&waitingUnlockingTimer))
			{
				controlSteps = 0x03;
			}
			break;
			
		case 0x03:		//开门
			printf("\r\n门3打开\r\n");
			DoorOpen(3);
			
			controlSteps = 0x04;
			printf("\r\n开门限位检测3\r\n");
			timer_restart(&openDoorTimer);							//从当前时间重新开始n秒定时器
			break;
			
		case 0x04:			//开门限位检测
			if(timer_expired(&unlockingTimer))					//开启电磁锁时间到，上锁
			{
				Locked(3);
			}
			
			if(timer_expired(&openDoorTimer))						//如果定时时间到了，则开门停止
			{
				DoorStop(3);															//门停止
				Locked(3);
	
				printf("\r\n限位超时停止3\r\n");
				printf("\r\n等待投放3\r\n");
				timer_restart(&waitingTimer_300S);				//从当前时间重新开始n秒定时器
				controlSteps = 0x05;
				break;
			}
			
			if(motorRunningStatus_3 == 0)								//电机已经停止了
			{
				Locked(3);
				printf("\r\n等待投放3\r\n");
				
				timer_restart(&waitingTimer_300S);				//从当前时间重新开始n秒定时器
				controlSteps = 0x05;
				break;
			}
			
			break;
			
		case 0x05:		//等待关门信号
			if(isCloseDoorMark_3 == 1)
			{
				isCloseDoorMark_3 = 0;
				controlSteps = 0x06;										//转到关门
				break;
			}
			
			if(timer_expired(&waitingTimer_300S))			//如果5分钟定时时间到了，则开门停止
			{
				printf("\r\n5分钟超时自动关门3\r\n");
				controlSteps = 0x06;										//转到关门
				break;
			}
			break;
			
		case 0x06:			//关门		
			printf("\r\n门3关闭中\r\n");
			DoorClose(3);																//关门
			
// 			printf("\r\n关门限位检测3\r\n");
			timer_restart(&closeDoorTimer);							//从当前时间重新开始n秒定时器
			controlSteps = 0x07;
			break;
			
		case 0x07:			//关门
			if(timer_expired(&closeDoorTimer))					//如果定时时间到了，则关门停止
			{
				DoorStop(3);															//门停止

// 				printf("\r\n关门时间到停止3\r\n");				
				controlSteps = 0x08;
				break;
			}
			
			//限位开关检测
			if(LIMI_KEY3 == 0)
			{				
				DoorStop(3);																//门停止	
			}
			
			if(motorRunningStatus_3 == 0)											//电机已经停止了
			{
				DoorStop(3);
// 				printf("\r\nM3->Stop\r\n");
				controlSteps = 0x08;											//转到等待称重数据
				break;
			}
			break;
			
		case 0x08:		//关门限位开关故障检测
			if(LIMI_KEY3 != 0)					//限位开关检测
			{
				controlSteps = 0x30;
			}else{
				controlSteps = 0x09;
			}
			break;
			
		case 0x09:		//开关门控制结束
			isOpenDoor_3 = 0;
			isCloseDoorMark_3 = 0;
			
			openDoorCtrlEndMark_3 = 1;					//标志开关门控制结束
			printf("\r\nNo.3 OK！\r\n");
			controlSteps = 0x00;
			break;
	
//-------------------------------------------------------------------------------------
//关门异常处理		
		case 0x30:
			printf("\r\nE->开锁3\r\n");
			Unlock(3);															//打开电磁锁
			timer_restart(&waitingUnlockingTimer);
			controlSteps = 0x31;
			break;
			
		case 0x31:
			if(timer_expired(&waitingUnlockingTimer))
			{
				printf("\r\nE->开门3\r\n");
				DoorOpen(3);
				
				controlSteps = 0x32;
				timer_restart(&waitingTimer_100ms);
			}
			break;
			
		case 0x32:
			if(timer_expired(&waitingTimer_100ms))
			{
				printf("\r\nE->关锁，门3停止\r\n");
				Locked(3);
				DoorStop(3);															//门停止
				controlSteps = 0x33;
				printf("\r\nE->门3复位\r\n");
			}
			break;
			
		case 0x33:
			if(DoorReset_3() == 1)
			{
				controlSteps = 0x34;
			}
			break;
			
		case 0x34:
			if(LIMI_KEY3 != 0)
			{
				faultCode_3 = 0x32;
				limitKeyExceptionMark = 0x20;
				devChannel3FaultMark = 1;
				controlSteps = 0x09;
				break;
			}else{																				//已经恢复则回到正常的处理流程
				faultCode_3 = 0x14;
				timer_restart(&waitingTimer_100ms);
				controlSteps = 0x09;
			}
			break;
			
		default:
			controlSteps = 0x00;
			break;
	}
}

/*
***********************************************************************
*	函 数 名: MotorControlTask_4
*	功能说明: 电机1控制任务
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void MotorControlTask_4(void)
{
	uint8_t tempLimKeyVal = 0;
	static uint8_t controlSteps = 0;
	uint8_t motorStopMark = 0;											//电机停止标志
	uint16_t tempWeight = 0;
	
	static uint32_t startTimer = 0;									//开关门开始时间
	static uint32_t endTimer = 0;										//开关门结束时间
	
	static uint32_t preRemainingTimer = 0;					//上一次还剩时间
	
	static timer waitingTimer_100ms;								//等待100ms定时器
	static timer waitingTimer_300ms;								//等待300ms定时器
	static timer waitingTimer_500ms;								//等待500ms定时器
	static timer waitingTimer_1S;										//等待1S定时器
	static timer waitingUnlockingTimer;							//电磁锁开启时间
	static timer unlockingTimer;										//电磁锁开启时间
	static timer openDoorTimer;											//用于开门时间限制
	static timer closeDoorTimer;										//用于关门时间限制
	static timer waitingTimer_300S;									//5分钟后自动关门
	
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		
		timer_set(&waitingTimer_100ms, 1);
		timer_set(&waitingTimer_300ms, 3);
		timer_set(&waitingTimer_500ms, 5);
		timer_set(&waitingTimer_1S, CLOCK_SECOND*1);
		timer_set(&waitingUnlockingTimer, 15);							//创建1个定时器，用于控制先打开电磁锁然后再开门
		timer_set(&unlockingTimer, 25);											//创建1个定时器，用于控制电磁锁打开时间
		timer_set(&openDoorTimer, 15);											//创建1个定时器，用于开门时间限制
		timer_set(&closeDoorTimer, 20);											//创建1个定时器，用于关门时间限制
		timer_set(&waitingTimer_300S, CLOCK_SECOND*300);		//创建1个定时器，用于5分钟后自动关门
	}

	switch(controlSteps)
	{
		case 0x00:			//初始状态
			if(isOpenDoor_4 == 1)		//电机1请求打开
			{
				printf("\r\n--> 电机4打开!\r\n");	
				
				//开始时先清除用到的变量
				isOpenDoor_4 = 0;				
				isCloseDoorMark_4 = 0;
				openDoorCtrlEndMark_4 = 0;
				
				faultCode_4 = 0x14;
				
				controlSteps = 0x01;											//有开门请求操作，跳转到正在投放操作流程
			}
			break;
			
		case 0x01:		//打开电磁锁
			Unlock(4);										//打开电磁锁
			
			timer_restart(&waitingUnlockingTimer);
			timer_restart(&unlockingTimer);
			controlSteps = 0x02;
			break;
			
		case 0x02:		//等待电磁锁打开
			if(timer_expired(&waitingUnlockingTimer))
			{
				controlSteps = 0x03;
			}
			break;
			
		case 0x03:		//开门
			printf("\r\n门4打开\r\n");
			DoorOpen(4);
			
			controlSteps = 0x04;
			printf("\r\n开门限位检测4\r\n");
			timer_restart(&openDoorTimer);							//从当前时间重新开始n秒定时器
			break;
			
		case 0x04:			//开门限位检测
			if(timer_expired(&unlockingTimer))					//开启电磁锁时间到，上锁
			{
				Locked(4);
			}
			
			if(timer_expired(&openDoorTimer))						//如果定时时间到了，则开门停止
			{
				DoorStop(4);															//门停止
				Locked(4);
	
				printf("\r\n限位超时停止4\r\n");
				printf("\r\n等待投放4\r\n");
				timer_restart(&waitingTimer_300S);				//从当前时间重新开始n秒定时器
				controlSteps = 0x05;
				break;
			}
			
			if(motorRunningStatus_4 == 0)								//电机已经停止了
			{
				Locked(4);
				printf("\r\n等待投放4\r\n");
				
				timer_restart(&waitingTimer_300S);				//从当前时间重新开始n秒定时器
				controlSteps = 0x05;
				break;
			}
			
			break;
			
		case 0x05:		//等待关门信号
			if(isCloseDoorMark_4 == 1)
			{
				isCloseDoorMark_4 = 0;
				controlSteps = 0x06;										//转到关门
				break;
			}
			
			if(timer_expired(&waitingTimer_300S))			//如果5分钟定时时间到了，则开门停止
			{
				printf("\r\n5分钟超时自动关门4\r\n");
				controlSteps = 0x06;										//转到关门
				break;
			}
			break;
			
		case 0x06:			//关门		
			printf("\r\n门4关闭中\r\n");
			DoorClose(4);																//关门
			
// 			printf("\r\n关门限位检测4\r\n");
			timer_restart(&closeDoorTimer);							//从当前时间重新开始n秒定时器
			controlSteps = 0x07;
			break;
			
		case 0x07:			//关门
			if(timer_expired(&closeDoorTimer))					//如果定时时间到了，则关门停止
			{
				DoorStop(4);															//门停止

// 				printf("\r\n关门时间到停止4\r\n");				
				controlSteps = 0x08;
				break;
			}
			
			//限位开关检测
			if(LIMI_KEY4 == 0)
			{				
				DoorStop(4);																//门停止	
			}
			
			if(motorRunningStatus_4 == 0)									//电机已经停止了
			{
				DoorStop(4);
// 				printf("\r\nM4->Stop\r\n");
				controlSteps = 0x08;											//转到等待称重数据
				break;
			}
			break;
			
		case 0x08:		//关门限位开关故障检测
			if(LIMI_KEY4 != 0)					//限位开关检测
			{
				controlSteps = 0x30;
			}else{
				controlSteps = 0x09;
			}
			break;
			
		case 0x09:		//开关门控制结束
			isOpenDoor_4 = 0;
			isCloseDoorMark_4 = 0;
			
			openDoorCtrlEndMark_4 = 1;					//标志开关门控制结束
			printf("\r\nNo.4 OK！\r\n");
			controlSteps = 0x00;
			break;
	
//-------------------------------------------------------------------------------------
//关门异常处理		
		case 0x30:
			printf("\r\nE->开锁4\r\n");
			Unlock(4);															//打开电磁锁
			timer_restart(&waitingUnlockingTimer);
			controlSteps = 0x31;
			break;
			
		case 0x31:
			if(timer_expired(&waitingUnlockingTimer))
			{
				printf("\r\nE->开门4\r\n");
				DoorOpen(4);
				
				controlSteps = 0x32;
				timer_restart(&waitingTimer_100ms);
			}
			break;
			
		case 0x32:
			if(timer_expired(&waitingTimer_100ms))
			{
				printf("\r\nE->关锁，门4停止\r\n");
				Locked(4);
				DoorStop(4);															//门停止
				controlSteps = 0x33;
				printf("\r\nE->门4复位\r\n");
			}
			break;
			
		case 0x33:
			if(DoorReset_4() == 1)
			{
				controlSteps = 0x34;
			}
			break;
			
		case 0x34:
			if(LIMI_KEY4 != 0)
			{
				faultCode_4 = 0x32;
				limitKeyExceptionMark = 0x20;
				devChannel4FaultMark = 1;
				controlSteps = 0x09;
				break;
			}else{																				//已经恢复则回到正常的处理流程
				faultCode_4 = 0x14;
				timer_restart(&waitingTimer_100ms);
				controlSteps = 0x09;
			}
			break;
			
		default:
			controlSteps = 0x00;
			break;
	}
}

/*
***********************************************************************
*	函 数 名: OpenDoorControlTask
*	功能说明: 开关门控制任务
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void OpenDoorControlTask(void)
{
	MotorControlTask_1();
	MotorControlTask_2();
	MotorControlTask_3();
	MotorControlTask_4();
}

/*
***********************************************************************
*	函 数 名: DoorReset_1
*	功能说明: 门1复位回到起点
*	形    参：无
*	返 回 值: 复位完成后返回1
***********************************************************************
*/
uint8_t DoorReset_1(void)
{
	static uint8_t resetStatus = 0x00;			//门复位状态
	uint8_t returnVal = 0;
	
	static timer resetMotorTimer;
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		timer_set(&resetMotorTimer, 20);	
	}
	
	//电机复位流程控制
	switch(resetStatus)
	{
		case 0x00:		//初始状态
// 			printf("\r\n门1复位操作\r\n");
			if(LIMI_KEY1 == 0)			//限位开关已经正常限位
			{
				resetStatus = 0x03;
			}else{
				resetStatus = 0x01;
			}
			break;
			
		case 0x01:		//门复位控制
			DoorClose(1);																//关门
			resetStatus = 0x02;
// 			printf("\r\n门1复位限位检测\r\n");
			timer_restart(&resetMotorTimer);							//从当前时间重新开始n秒定时器
			break;
			
		case 0x02:		//门已复位成功判断
			if(timer_expired(&resetMotorTimer))					//如果定时时间到了，则关门停止
			{
				DoorStop(1);															//门停止
		
				resetStatus = 0x03;
// 				printf("\r\n门1复位时间到停止\r\n");
				break;
			}
			
			//限位开关检测
			if(LIMI_KEY1 == 0)												//限位开关检测
			{				
				DoorStop(1);																//门停止	
// 				printf("\r\nR->电机1已开关限位停止\r\n");
			}

// 			//电机过流检测
// 			if(motorCurrentVal_1 > M1_CLOSE_MAX_CurrentVal)			//开门限位检测，通过检测电机电流判断限位
// 			{
// 				DoorStop(1);																//门停止		
// // 				printf("\r\n电机1已限位停止，电流数值 = %d\r\n", motorCurrentVal_1);
// 			}
			
			if(motorRunningStatus_1 == 0)								//电机已经停止了
			{
// 				printf("\r\n电机1已经停止\r\n");
				resetStatus = 0x03;										
				break;
			}
			break;
			
		case 0x03:		//门已完成复位
			printf("\r\n门1已完成复位！！！\r\n");
			
			//到这一步不管怎样电机都停止
			DoorStop(1);															//门停止
			returnVal = 1;
			resetStatus = 0x00;
			break;
			
		default:
			printf("\r\n 电机1复位未知状态！！！\r\n");
			
			//到这一步骤不管怎样电机都停止
			DoorStop(1);															//门停止
			returnVal = 1;
			resetStatus = 0x00;
			break;
	}
	
	return returnVal;
}

/*
***********************************************************************
*	函 数 名: DoorReset_2
*	功能说明: 门2复位回到起点
*	形    参：无
*	返 回 值: 复位完成后返回1
***********************************************************************
*/
uint8_t DoorReset_2(void)
{
	static uint8_t resetStatus = 0x00;			//门复位状态
	uint8_t returnVal = 0;
	
	static timer resetMotorTimer;
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		timer_set(&resetMotorTimer, 20);	
	}
	
	//电机复位流程控制
	switch(resetStatus)
	{
		case 0x00:		//初始状态
// 			printf("\r\n门2复位操作\r\n");
			
			if(LIMI_KEY2 == 0)			//限位开关已经正常限位
			{
				resetStatus = 0x03;
			}else{
				resetStatus = 0x01;
			}
			break;
			
		case 0x01:		//门复位控制
			DoorClose(2);																//关门
			resetStatus = 0x02;
// 			printf("\r\n门2复位限位检测\r\n");
			timer_restart(&resetMotorTimer);							//从当前时间重新开始n秒定时器
			break;
			
		case 0x02:		//门已复位成功判断
			if(timer_expired(&resetMotorTimer))					//如果定时时间到了，则关门停止
			{
				DoorStop(2);															//门停止
		
				resetStatus = 0x03;
// 				printf("\r\n门2复位时间到停止\r\n");
				break;
			}
			
			//限位开关检测
			if(LIMI_KEY2 == 0)												//限位开关检测
			{				
				DoorStop(2);																//门停止	
// 				printf("\r\nR->电机2已开关限位停止\r\n");
			}

// 			//电机过流检测
// 			if(motorCurrentVal_2 > M2_CLOSE_MAX_CurrentVal)			//开门限位检测，通过检测电机电流判断限位
// 			{
// 				DoorStop(2);																//门停止		
// // 				printf("\r\n电机2已限位停止，电流数值 = %d\r\n", motorCurrentVal_2);
// 			}
			
			if(motorRunningStatus_2 == 0)								//电机已经停止了
			{
// 				printf("\r\n电机2已经停止\r\n");
				resetStatus = 0x03;										
				break;
			}
			break;
			
		case 0x03:		//门已完成复位
			printf("\r\n门2已完成复位！！！\r\n");
			
			//到这一步不管怎样电机都停止
			DoorStop(2);															//门停止
			returnVal = 1;
			resetStatus = 0x00;
			break;
			
		default:
			printf("\r\n 电机2复位未知状态！！！\r\n");
			
			//到这一步骤不管怎样电机都停止
			DoorStop(2);															//门停止
			returnVal = 1;
			resetStatus = 0x00;
			break;
	}
	
	return returnVal;
}

/*
***********************************************************************
*	函 数 名: DoorReset_3
*	功能说明: 门3复位回到起点
*	形    参：无
*	返 回 值: 复位完成后返回1
***********************************************************************
*/
uint8_t DoorReset_3(void)
{
	static uint8_t resetStatus = 0x00;			//门复位状态
	uint8_t returnVal = 0;
	
	static timer resetMotorTimer;
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		timer_set(&resetMotorTimer, 20);	
	}
	
	//电机复位流程控制
	switch(resetStatus)
	{
		case 0x00:		//初始状态
// 			printf("\r\n门3复位操作\r\n");
			if(LIMI_KEY3 == 0)			//限位开关已经正常限位
			{
				resetStatus = 0x03;
			}else{
				resetStatus = 0x01;
			}
			break;
			
		case 0x01:		//门复位控制
			DoorClose(3);																//关门
			resetStatus = 0x02;
// 			printf("\r\n门3复位限位检测\r\n");
			timer_restart(&resetMotorTimer);							//从当前时间重新开始n秒定时器
			break;
			
		case 0x02:		//门已复位成功判断
			if(timer_expired(&resetMotorTimer))					//如果定时时间到了，则关门停止
			{
				DoorStop(3);															//门停止
		
				resetStatus = 0x03;
// 				printf("\r\n门3复位时间到停止\r\n");
				break;
			}
			
			//限位开关检测
			if(LIMI_KEY3 == 0)												//限位开关检测
			{				
				DoorStop(3);																//门停止	
// 				printf("\r\nR->电机3已开关限位停止\r\n");
			}

// 			//电机过流检测
// 			if(motorCurrentVal_2 > M2_CLOSE_MAX_CurrentVal)			//开门限位检测，通过检测电机电流判断限位
// 			{
// 				DoorStop(2);																//门停止		
// // 				printf("\r\n电机2已限位停止，电流数值 = %d\r\n", motorCurrentVal_2);
// 			}
			
			if(motorRunningStatus_3 == 0)								//电机已经停止了
			{
// 				printf("\r\n电机3已经停止\r\n");
				resetStatus = 0x03;										
				break;
			}
			break;
			
		case 0x03:		//门已完成复位
			printf("\r\n门3已完成复位！！！\r\n");
			
			//到这一步不管怎样电机都停止
			DoorStop(3);															//门停止
			returnVal = 1;
			resetStatus = 0x00;
			break;
			
		default:
			printf("\r\n 电机3复位未知状态！！！\r\n");
			
			//到这一步骤不管怎样电机都停止
			DoorStop(3);															//门停止
			returnVal = 1;
			resetStatus = 0x00;
			break;
	}
	
	return returnVal;
}

/*
***********************************************************************
*	函 数 名: DoorReset_4
*	功能说明: 门4复位回到起点
*	形    参：无
*	返 回 值: 复位完成后返回1
***********************************************************************
*/
uint8_t DoorReset_4(void)
{
	static uint8_t resetStatus = 0x00;			//门复位状态
	uint8_t returnVal = 0;
	
	static timer resetMotorTimer;
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		timer_set(&resetMotorTimer, 20);	
	}
	
	//电机复位流程控制
	switch(resetStatus)
	{
		case 0x00:		//初始状态
// 			printf("\r\n门4复位操作\r\n");
			
			if(LIMI_KEY4 == 0)			//限位开关已经正常限位
			{
				resetStatus = 0x03;
			}else{
				resetStatus = 0x01;
			}
			break;
			
		case 0x01:		//门复位控制
			DoorClose(4);																//关门
			resetStatus = 0x02;
// 			printf("\r\n门4复位限位检测\r\n");
			timer_restart(&resetMotorTimer);							//从当前时间重新开始n秒定时器
			break;
			
		case 0x02:		//门已复位成功判断
			if(timer_expired(&resetMotorTimer))					//如果定时时间到了，则关门停止
			{
				DoorStop(4);															//门停止
		
				resetStatus = 0x03;
// 				printf("\r\n门4复位时间到停止\r\n");
				break;
			}
			
			//限位开关检测
			if(LIMI_KEY4 == 0)												//限位开关检测
			{				
				DoorStop(4);																//门停止	
// 				printf("\r\nR->电机2已开关限位停止\r\n");
			}

// 			//电机过流检测
// 			if(motorCurrentVal_2 > M2_CLOSE_MAX_CurrentVal)			//开门限位检测，通过检测电机电流判断限位
// 			{
// 				DoorStop(2);																//门停止		
// // 				printf("\r\n电机2已限位停止，电流数值 = %d\r\n", motorCurrentVal_2);
// 			}
			
			if(motorRunningStatus_4 == 0)								//电机已经停止了
			{
// 				printf("\r\n电机4已经停止\r\n");
				resetStatus = 0x03;										
				break;
			}
			break;
			
		case 0x03:		//门已完成复位
			printf("\r\n门4已完成复位！！！\r\n");
			
			//到这一步不管怎样电机都停止
			DoorStop(4);															//门停止
			returnVal = 1;
			resetStatus = 0x00;
			break;
			
		default:
			printf("\r\n 电机4复位未知状态！！！\r\n");
			
			//到这一步骤不管怎样电机都停止
			DoorStop(4);															//门停止
			returnVal = 1;
			resetStatus = 0x00;
			break;
	}
	
	return returnVal;
}

/*
***********************************************************************
*	函 数 名: DoorResetTask
*	功能说明: 初始上电门复位回到起点
*	形    参：无
*	返 回 值: 无（改变doorResetMark状态）
***********************************************************************
*/
void DoorResetTask(void)
{
	static uint8_t doorResetStatus_1 = 0;
	static uint8_t doorResetStatus_2 = 0;
	static uint8_t doorResetStatus_3 = 0;
	static uint8_t doorResetStatus_4 = 0;
	
	if(doorResetStatus_1 == 0)
	{
		doorResetStatus_1 = DoorReset_1();
	}
	
	if(doorResetStatus_2 == 0)
	{
		doorResetStatus_2 = DoorReset_2();
	}
	
	if(doorResetStatus_3 == 0)
	{
		doorResetStatus_3 = DoorReset_3();
	}
	
	if(doorResetStatus_4 == 0)
	{
		doorResetStatus_4 = DoorReset_4();
	}
	
	if((doorResetStatus_1 == 1) && (doorResetStatus_2 == 1) &&
		 (doorResetStatus_3 == 1) && (doorResetStatus_4 == 1))
	{
		doorResetMark = 1;
	}
}

/******************** (C) COPYRIGHT 2017 ping *****END OF FILE*******************/
