SafeCourse.c 7.28 KB

#include "includes.h"
#include "all_value.h"
#include "tcp_client_demo.h"

u8  MotorStopMiss_Time5Nums;//电机强制停止操作消失延时计时

void TouchHMI_Safe(void)//外部触摸屏急停触发时的安全保护+复位过程的暂停保护
{
	if(VaIndexDW_R(&Touch_Contr,6)|VaIndexB_R(&AGV.OtheSta1,3))
	{ MotorStopMiss_Time5Nums=0;}
}

void Slam_Safe(void)//Slam过程相应的保护
{
		/***依赖SLAM动作时置信度丢失时间过长****/	
 	if( (Tim5_Slam3Nums>=2) &&((AGV.RotationSlam_Step>1)&&(AGV.RotationSlam_Step<100)) )//行走过程的保护在行走程序中
	{ MotorStopMiss_Time5Nums=0;}
}

u8  TLaRadarDelay_Time4Nums;//雷达触发信号计时
void LaRadar_Safe(void)//
{
	/***自动旋转/行走时有障碍物进入时行走电机停止****/	
	if( (AGV.Line_Step>12 && AGV.Line_Step<90) |
			( (AGV.Line_Step==12) && ((AGV.EndP_Type1<3)|(AGV.EndP_Type1>4))) |
			( (AGV.Charge_Step==31) && ( (AGV.Type3_Ac==7)|(fabs(Motor[1].MToPc.Angle_Actual-Motor[1].PcToM.Angle_Target)>500)))
		 )
	{
		if(LRadar_Near==0 )TLaRadarDelay_Time4Nums=0;
	}
	else TLaRadarDelay_Time4Nums=0;
							
}

void Motor_Safe(void)//
{
	/***舵轮组合运动过程中任意一个舵轮轴状态异常都全部停止动作****/
	if ( (AGV.Load_Step>0 && AGV.Load_Step<100)|
			 (AGV.Line_Step>0 && AGV.Line_Step<100)|
		   (AGV.RotationSlam_Step>1 &&AGV.RotationSlam_Step<100)|
			 (AGV.RotationCCD_Step>1 &&AGV.RotationCCD_Step<100) )
	{ 
		if(VaIndexB_R(&AGV.AllAxisOK,2)==0)MotorStopMiss_Time5Nums=0;	
	}
	
	if (VaIndexDW_R(&Touch_Contr,9)==0)//未屏蔽所有保护(触控上)下述是可以做屏蔽考虑的
	{
/************雷达保护****/	
		if(TLaRadarDelay_Time4Nums>10)MotorStopMiss_Time5Nums=0;
/***********触边保护****/	
		if( IN_Safe && VaIndexDW_R(&F_AgvContr,2))	
		{ MotorStopMiss_Time5Nums=0;}
	
/*******升降过程的保护****/	
		if(VaIndexDW_R(&F_AgvContr,5))//升降限位保护启用
		{
 			if(VaIndexDW_R(&F_AgvContr,1))//液压	
			{ if( IN_DownLimit && Out_DownValve ){ MotorStop_Fc(1);}	
				}	
			else 
			{ if(IN_DownLimit  && Motor[0].PcToM.Speed<0 ){ MotorStop_Fc(1);}
			  }
			
			if(IN_UpLimit && Motor[0].PcToM.Speed>0) { MotorStop_Fc(1);}
		}
		
		if(VaIndexDW_R(&F_AgvContr,18))//升降测距软限位启用
		{
			if( ((High_Dis>HighMax) && Motor[0].PcToM.Speed>0)|
					((High_Dis<Highmin)&&(Out_DownValve|(Motor[0].PcToM.Speed<0)) )
				)
			{ MotorStop_Fc(1);}		
		}
		
/********移栽过程的保护****/
		if(VaIndexDW_R(&F_AgvContr,6))//移载限位保护启用
		{
			if( (IN_LimitL  && Motor[5].PcToM.Speed<0 )|(IN_LimitR  && Motor[5].PcToM.Speed>0 ) )
			{ Motor[5].PcToM.Speed=0;}
		}
		
/********升降过程的限重保护****/
	  if( VaIndexDW_R(&F_AgvContr,7)&&( (AGV.UpDown_Step>1&&AGV.UpDown_Step<100)|(VaIndexDW_R(&Touch_Contr,18)) ))//限重保护启用
	  { if( (WeightAll>=F_WeightMax)|(WeightAll>=Union16_1.Weight&& Union16_1.Weight>0))
		 { Motor[0].PcToM.Speed=0;Set_ErrID(58,1);}//ErrID=58;超重报警
	  }
/********输送边界保护****/
		if(VaIndexDW_R(&F_AgvContr,10))//边界保护启用
		{
			if(VaIndexDW_R(&F_AgvContr,8)==0)IN_LimitL2=IN_LimitR2=0;    //无下层输送
			if(VaIndexDW_R(&F_AgvContr,9)==0)IN_LimitL=IN_LimitR=0;    //无上层输送
			if(IN_LimitL|IN_LimitR|IN_LimitL2|IN_LimitR2)VaIndexB_W(&AGV.OtheSta,5,1);else VaIndexB_W(&AGV.OtheSta,5,0);
			if(VaIndexB_R(&AGV.OtheSta,5))
			{
				if ( (AGV.Load_Step>0 && AGV.Load_Step<100)|
						 (AGV.RotationSlam_Step>1 &&AGV.RotationSlam_Step<100)|
						 (AGV.RotationCCD_Step>1 &&AGV.RotationCCD_Step<100)|
						 (AGV.UpDown_Step>1 &&AGV.UpDown_Step<100)
				   )
				{ MotorStopMiss_Time5Nums=0;Set_ErrID(39,1);}//滚筒限位触发					
			}							
		}		
  }		 
}

void MotorStop_Fc(u8 No)//电机速度指令清零
{
	u8 i;	
	if(No==0)
	{	for(i=0;i<MaxMotNums;i++)			
		{	
			Motor[i].PcToM.Speed=0;
			Motor[i].PcToM.TargetSpeed=0;
		}
	}
	else if(No==1)
	{ Motor[0].PcToM.Speed=0;Out_DownValve=0;}

}


#define StopSafeEn 1u  //遥控急停+本体急停,无条件响应
u8 IN_EmergOldStop;
void Stop_Safe(void)//急停处理
{	u8 i;
 if(StopSafeEn)
 { if (IN_Stop)
 	 { 
		 MessSpecialClear();
	 }
	 if(IN_EmergStop<IN_EmergOldStop)
	 { MessSpecialClear();
		 for(i=0;i<MaxMotNums;i++)
		 {
				Motor[i].MToPc.Home_Found=0;
				Motor[i].PcToM.PowerOn=0;
				Motor[i].MToPc.Powered=0;
				if (NodeProEn==1)
				{ 
					if(Motor[i].Y)
					{ Motor[i].PcToM.NodeProtTime_Step=1;
						VaIndexB_W(&AGV.Axis_No,i,1);
					}
				}
		 }

  	 AGV.AllAxisResetStep=1;
	 }
	 IN_EmergOldStop=IN_EmergStop;
	 
	 if(IN_Stop|IN_EmergStop){ Out_Charge=Out_Fan1Power=Out_Fan2Power=Out_DownValve=0;	}
	 
	 switch(AGV.AllAxisResetStep)//AGV所有轴恢复操作
	 {	
			case 1://	清空轴采集状态
						if ((AGV.AllAxisOK&0x0F)==15 )AGV.AllAxisResetStep=100;
						else
						{	for(i=0;i<MaxMotNums;i++)
							{ 
	//							VaIndexW_W(&Motor[i].MToPc.Ax_Status,1,0);//清空轴报文标记
	//							VaIndexW_W(&Motor[i].MToPc.Ax_Status,2,0);//清空轴报文标记
	//							VaIndexW_W(&Motor[i].MToPc.Ax_Status,3,0);//清空轴报文标记
								Motor[i].MToPc.Ax_Status&=0xFFF1;//清空轴报文标记1\2\3位清0
							}
		//				 	AGV.AxisEn_Step=1;
							AGV.AllAxisResetStep++;
						}
						AGV.AxisEn_Step=0;
						break;
			case 2://	判断是否有轴故障,有故障的则清错
						for(i=0;i<MaxMotNums;i++)
						{ if(Motor[i].Y)
							Motor[i].PcToM.Reset=1;	//外部轴函数 会自动清除该状态
						}
						AGV.AllAxisResetStep++;
						AGV.AllAct_Time5Nums=0;	
						break;	
			case 3://	
						if(AGV.AllAct_Time5Nums>1)
						AGV.AllAxisResetStep++;
						break;
					
			case 4://	对轴使能+找原点
						AGV.AxisEn_Step=1;
	//					AGV.Axis_No=0x3F;
						AGV.AllAxisResetStep=90;
						break;
			case 90://	
						AGV.AllAxisResetStep=100;
						break;		
			case 100://	
				
						break;	
		}	 
 }
}


void MessSpecialClear(void)//主要信息清空
{ u8 i;
			memset(&AGV,0,sizeof(AGV));
			memset(&TaskP,0,sizeof(TaskP));
			memset(&Err_sign,0,sizeof(Err_sign));
	
			Out_DownValve=0;		 
			for(i=0;i<MaxMotNums;i++)			
			{	
//			memset(&Motor[i].PcToM,0,sizeof(Motor[i].PcToM));					
				Motor[i].PcToM.AbsoluteHold_Step=0;
				Motor[i].PcToM.Relative_Step=0;
				Motor[i].PcToM.SetZero_Step=0;
				Motor[i].PcToM.SpeedAbsolute_Step=0;
					//Motor[i].PcToM.FindHome_Step=0;
				Motor[i].PcToM.Speed=0;
				Motor[i].PcToM.TargetSpeed=0;
				
				if(i>0&&Motor[i].Max_rpm<2500)
				{ 
					Motor[i].PcToM.Acc=2000;										
					Motor[i].PcToM.Dcc=2200;	//避免停止时过大晃动					
				}
			 }
			TouckActionNo=0;
			TCP_AcNo=0;
}




void MotorErrReset_Pro(void)//轴清除故障程序
{ u8 i;
	
	if(VaIndexDW_R(&Touch_Contr,8))
	{ for(i=0;i<MaxMotNums;i++)			
		{	if(VaIndexW_R(&Motor[i].MToPc.StatusWord,3))i=10;
		}
		if(VaIndexB_R(&AGV.OtheSta,4)){;}
		else AGV.Axis_No=0x3F;
				
		if(VaIndexB_R(&AGV.OtheSta1,3)==0)
		{	if((i>9)|((Motor[2].MToPc.Home_Found==0)|(Motor[4].MToPc.Home_Found==0)) )
			{ AGV.AllAxisResetStep=1;
			  VaIndexB_W(&AGV.OtheSta1,3,1);
			}
		}
	}
}

#define Energy_PowerOff 15u
u8 Tim7_LowPoweroffNums;
void Battery_Safe(void)//
{	if(Workmode&&VaIndexDW_R(&F_AgvContr,19)&&(VaIndexDW_R(&Touch_Contr,9)==0))
	{ if(BatteryVa.Energy>0.1)
		{ if(BatteryVa.Energy<Energy_PowerOff &&(VaIndexB_R(&AGV.OtheSta,4)==0)){if(Tim7_LowPoweroffNums>15)Out_Poweroff=1;}//低电量空闲5min
			else if(AGV.Err[0]&&BatteryVa.Energy<40){if(Tim7_LowPoweroffNums>180)Out_Poweroff=1;}//电量高于一定值,但故障时间过长
			else Tim7_LowPoweroffNums=0;
		}
		else Tim7_LowPoweroffNums=0;
	}
	else Tim7_LowPoweroffNums=0;								
}

void Safe_Pro(void)//
{	MotorErrReset_Pro();
	TouchHMI_Safe();
	LaRadar_Safe();
	Stop_Safe();
	Battery_Safe();
	Motor_Safe();
	if ((MotorStopMiss_Time5Nums<4))MotorStop_Fc(0);//延时启动
}