TwinCAT 3 alarm程序

ST

TYPE ST_alarm :
STRUCT
	bState:BOOL;//报警状态
	nCode:INT;//报警码
	sTime:STRING;//报警时间
	sComment:WSTRING;//报警信息

	rTrig:R_TRIG;
	fTrig:F_TRIG;
END_STRUCT
END_TYPE

TYPE ST_alarm_list :
STRUCT
	sTime:STRING;
	sCode:STRING;
	sComment:WSTRING;
END_STRUCT
END_TYPE

GVL_alarm

VAR_GLOBAL CONSTANT
	//报警最大编号
	nAlarmMaxNum:INT:=1500;

	//历史报警最大编号
	nAlarmHistoryMaxNum:INT:=1500;

	//系统急停索引编号
	indexAlarmSystemEmergency:INT:=1;

	//系统自动模式未初始化完成索引编号
	indexAlarmSystemNoInitialDone:INT:=2;

	//急停按钮起始编号
	nAlarmEmergencyStartNum:INT:=10;
	//急停按钮索引编号
	indexAlarmEmergency:INT:=10;

	//安全光栅起始编号
	nAlarmSafetySensorStartNum:INT:=101;
	//安全光栅索引编号
	indexAlarmSafetySensor:INT:=101;

	//安全门起始编号
	nAlarmSafetyDoorStartNum:INT:=201;
	//安全门索引编号
	indexAlarmSafetyDoorExample:INT:=201;
	indexAlarmSafetyDoorExample2:INT:=202;
	indexAlarmSafetyDoorExample3:INT:=203;

	//轴起始编号
	nAlarmAxisStartNum:INT:=301;
	//轴索引编号
	indexAlarmAxisExample:INT:=301;
	indexAlarmAxisExample2:INT:=302;
	indexAlarmAxisExample3:INT:=303;

	//马达起始编号
	nAlarmMotorStartNum:INT:=501;
	//马达索引编号
	indexAlarmMotorExample:INT:=501;
	indexAlarmMotorExample2:INT:=502;
	indexAlarmMotorExample3:INT:=503;

	//气缸起始编号
	nAlarmCylinderStartNum:INT:=601;
	//气缸索引编号
	indexAlarmCylinderExample:INT:=601;
	indexAlarmCylinderExample2:INT:=602;
	indexAlarmCylinderExample3:INT:=603;

	//工位报警索引编号
	indexAlarmExampleStartNum:INT:=901;

END_VAR

VAR_GLOBAL
	alarm:ARRAY[0..nAlarmMaxNum] OF ST_alarm;

	alarmList:ARRAY [0..nAlarmMaxNum] OF ST_alarm_list;

	alarmHistoryList:ARRAY[0..nAlarmHistoryMaxNum] OF ST_alarm_list;
END_VAR

P_alarm

//******报警码赋值******//
FOR i:=0 TO nAlarmMaxNum BY 1 DO
	alarm[i].nCode:=i;
END_FOR

//******系统急停报警******//
IF system.control.bEmergency THEN
	alarm[indexAlarmSystemEmergency].bState:=TRUE;
	alarm[indexAlarmSystemEmergency].sComment:="系统急停--按下报警";
ELSIF system.control.bReset THEN
	alarm[indexAlarmSystemEmergency].bState:=FALSE;
END_IF

//******系统自动模式未初始化完成******//
IF system.control.bManualOrAutomatic AND NOT system.status.bInitialDone THEN
	alarm[indexAlarmSystemNoInitialDone].bState:=TRUE;
	alarm[indexAlarmSystemNoInitialDone].sComment:="系统自动模式未初始化完成";
ELSIF system.control.bReset THEN
	alarm[indexAlarmSystemNoInitialDone].bState:=FALSE;
END_IF

//******急停按钮报警******//
FOR i:=nAlarmEmergencyStartNum TO nAlarmEmergencyStartNum+nEmergencyMaxNum-1 BY 1 DO
	IF emergency[i-nAlarmEmergencyStartNum+1].bError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("急停按钮:",INT_TO_WSTRING(i-nAlarmEmergencyStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,emergency[i-nAlarmEmergencyStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--按下报警");
	ELSIF system.control.bReset THEN
		alarm[i].bState:=FALSE;
	END_IF
END_FOR

//******安全光栅报警******//
FOR i:=nAlarmSafetySensorStartNum TO nAlarmSafetySensorStartNum+nSafetySensorMaxNum-1 BY 1 DO
	IF safetySensor[i-nAlarmSafetySensorStartNum+1].bError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("安全光栅:",INT_TO_WSTRING(i-nAlarmSafetySensorStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,safetySensor[i-nAlarmSafetySensorStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--遮挡报警");
	ELSIF system.control.bReset THEN
		alarm[i].bState:=FALSE;
	END_IF
END_FOR

//******安全门报警******//
FOR i:=nAlarmSafetyDoorStartNum TO nAlarmSafetyDoorStartNum+nSafetyDoorMaxNum-1 BY 1 DO
	IF safetyDoor[i-nAlarmSafetyDoorStartNum+1].status.bCloseError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("安全门:",INT_TO_WSTRING(i-nAlarmSafetyDoorStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,safetyDoor[i-nAlarmSafetyDoorStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--关闭报警");
	ELSIF system.control.bReset THEN
		alarm[i].bState:=FALSE;
	END_IF
END_FOR

//******轴报警******//
FOR i:=nAlarmAxisStartNum TO nAlarmAxisStartNum+nAxisMaxNum-1 BY 1 DO
	IF axis[i-nAlarmAxisStartNum+1].com.nErrorCode>0 THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("轴:",INT_TO_WSTRING(i-nAlarmAxisStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,axis[i-nAlarmAxisStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--驱动器报警");
	ELSIF axis[i-nAlarmAxisStartNum+1].status.nAxisErrorID>0 THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("轴:",INT_TO_WSTRING(i-nAlarmAxisStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,axis[i-nAlarmAxisStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--NC轴报警");
	ELSIF axis[i-nAlarmAxisStartNum+1].status.bHomeError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("轴:",INT_TO_WSTRING(i-nAlarmAxisStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,axis[i-nAlarmAxisStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--回原点报警");
	ELSIF axis[i-nAlarmAxisStartNum+1].status.bJogError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("轴:",INT_TO_WSTRING(i-nAlarmAxisStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,axis[i-nAlarmAxisStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--点动报警");
	ELSIF axis[i-nAlarmAxisStartNum+1].status.bMoveAbsoluteError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("轴:",INT_TO_WSTRING(i-nAlarmAxisStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,axis[i-nAlarmAxisStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--绝对运动报警");
	ELSIF axis[i-nAlarmAxisStartNum+1].status.bMoveRelativeError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("轴:",INT_TO_WSTRING(i-nAlarmAxisStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,axis[i-nAlarmAxisStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--相对运动报警");
	ELSIF axis[i-nAlarmAxisStartNum+1].status.bInterferenceError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("轴:",INT_TO_WSTRING(i-nAlarmAxisStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,axis[i-nAlarmAxisStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--干涉报警");
	ELSIF axis[i-nAlarmAxisStartNum+1].status.bPositiveLimitError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("轴:",INT_TO_WSTRING(i-nAlarmAxisStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,axis[i-nAlarmAxisStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--正极限值报警");
	ELSIF axis[i-nAlarmAxisStartNum+1].status.bNegativeLimitError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("轴:",INT_TO_WSTRING(i-nAlarmAxisStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,axis[i-nAlarmAxisStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--负极限值报警");
	ELSIF axis[i-nAlarmAxisStartNum+1].status.bTorqueLimitError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("轴:",INT_TO_WSTRING(i-nAlarmAxisStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,axis[i-nAlarmAxisStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--扭矩限值报警");
	ELSIF system.control.bReset THEN
		alarm[i].bState:=FALSE;
	END_IF
END_FOR

//******马达报警******//
FOR i:=nAlarmMotorStartNum TO nAlarmMotorStartNum+nMotorMaxNum-1 BY 1 DO
	IF motor[i-nAlarmMotorStartNum+1].status.bForwardError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("马达:",INT_TO_WSTRING(i-nAlarmMotorStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,motor[i-nAlarmMotorStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--正转报警");
	ELSIF motor[i-nAlarmMotorStartNum+1].status.bBackwardError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("马达:",INT_TO_WSTRING(i-nAlarmMotorStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,motor[i-nAlarmMotorStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--反转报警");
	ELSIF system.control.bReset THEN
		alarm[i].bState:=FALSE;
	END_IF
END_FOR

//******气缸报警******//
FOR i:=nAlarmCylinderStartNum TO nAlarmCylinderStartNum+nCylinderMaxNum-1 BY 1 DO
	IF cylinder[i-nAlarmCylinderStartNum+1].status.bCheckError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("气缸:",INT_TO_WSTRING(i-nAlarmCylinderStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,cylinder[i-nAlarmCylinderStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--当前状态和自动状态不一致报警");
	ELSIF cylinder[i-nAlarmCylinderStartNum+1].status.bOpenError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("气缸:",INT_TO_WSTRING(i-nAlarmCylinderStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,cylinder[i-nAlarmCylinderStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--打开报警");
	ELSIF cylinder[i-nAlarmCylinderStartNum+1].status.bCloseError THEN
		alarm[i].bState:=TRUE;
		alarm[i].sComment:=WCONCAT("气缸:",INT_TO_WSTRING(i-nAlarmCylinderStartNum+1));
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--");
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,cylinder[i-nAlarmCylinderStartNum+1].sComment);
		alarm[i].sComment:=WCONCAT(alarm[i].sComment,"--关闭报警");
	ELSIF system.control.bReset THEN
		alarm[i].bState:=FALSE;
	END_IF
END_FOR

//******报警上升沿、下降沿******//
FOR i:=1 TO nAlarmMaxNum BY 1 DO 
	alarm[i].rTrig(CLK:=alarm[i].bState , Q=> );
	alarm[i].fTrig(CLK:=alarm[i].bState , Q=> );

	IF alarm[i].rTrig.Q OR alarm[i].fTrig.Q THEN
		alarm[i].sTime:=sSystemTime;
	END_IF
END_FOR

//******当前报警列表******//
j:=0;
FOR i:=0 TO nAlarmMaxNum BY 1 DO
	IF alarm[i].bState THEN
		alarmList[j].sTime:=alarm[i].sTime;
		alarmList[j].sCode:=DINT_TO_DECSTR(INT_TO_DINT(alarm[i].nCode),4);
		alarmList[j].sComment:=alarm[i].sComment;
		j:=j+1;
	END_IF
END_FOR
FOR i:=j TO nAlarmMaxNum BY 1 DO
	alarmList[i].sTime:='';
	alarmList[i].sCode:='';
	alarmList[i].sComment:="";
END_FOR

//******历史报警列表******//
FOR i:=0 TO nAlarmMaxNum BY 1 DO
	IF alarm[i].rTrig.Q THEN
		FOR j:=nAlarmHistoryMaxNum TO 1 BY -1 DO
			alarmHistoryList[j]:=alarmHistoryList[j-1];
		END_FOR
		alarmHistoryList[0].sTime:=alarm[i].sTime;
		alarmHistoryList[0].sCode:=DINT_TO_DECSTR(INT_TO_DINT(alarm[i].nCode),4);
		alarmHistoryList[0].sComment:=alarm[i].sComment;
	END_IF
END_FOR

你可能感兴趣的:(TwinCAT,3,TwinCAT,3)