<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.13">
  <POU Name="FB_Simulation" Id="{50c1ac04-e15e-4cd9-8f17-13eb315605ff}" SpecialFunc="None">
    <Declaration><![CDATA[FUNCTION_BLOCK FB_Simulation

VAR
	fbVisu						: FB_Visu;	

	// =============== 特定位置参数 ======================
	SimuParam					: SimuParameter;
	
	// =============== 四个物料代表灰色和绿色随机切换的标志位 ======================
	ArrColorSwitch				: ARRAY[1..4] OF BOOL;

	// =============== 气缸位置状态 ======================
	bClampAtBase		AT %Q*	: BOOL;						// 如果为TRUE，则正位气缸在基始位置
	bClampAtWork		AT %Q*	: BOOL;						// 如果为TRUE，则正位气缸在目标位置
	bBarrierAtBase		AT %Q*	: BOOL;						// 如果为TRUE，则栏杆气缸在基始位置
	bBarrierAtWork		AT %Q*	: BOOL;						// 如果为TRUE，则栏杆气缸在目标位置
	bMetalAtBase		AT %Q*	: BOOL;						// 如果为TRUE，则金属分拣气缸在基始位置
	bMetalAtWork		AT %Q*	: BOOL;						// 如果为TRUE，则金属分拣气缸在目标位置
	bPlasticAtBase		AT %Q*	: BOOL;						// 如果为TRUE，则塑料分拣气缸在基始位置
	bPlasticAtWork		AT %Q*	: BOOL;						// 如果为TRUE，则塑料分拣气缸在目标位置
	
	// =============== 气缸温度数据 =========================
	bReset				AT %I*	: BOOL;						// 如果为TRUE，则复位气缸温度
	fClampTempCurrent	AT %Q*	: LREAL;					// 正位气缸的实际温度
	fClampTempMax		AT %Q*	: LREAL;					// 正位气缸的设定最大温度
	fClampTempMin		AT %Q*	: LREAL;					// 正位气缸的设定最小温度
	fBarrierTemp		AT %Q*	: LREAL;					// 栏杆气缸的温度

	// =============== 检测物料的传感器信号 ==============
	bBarrierSensor		AT %Q*	: BOOL;						// 如果为TRUE，则栏杆前检测到物料
	bMetalSensor		AT %Q*	: BOOL;						// 如果为TRUE，则检测到金属物料
	bPlasticSensor		AT %Q*	: BOOL;						// 如果为TRUE，则检测到塑料物料

	// =============== 特定点坐标以及物料坐标 ===================
	fYClamp						: LREAL;					// 正位气缸的纵坐标
	fYBarrier					: LREAL;					// 栏杆气缸的纵坐标
	fYMetal						: LREAL;					// 金属分拣气缸的纵坐标
	fYPlastic					: LREAL;					// 塑料分拣气缸的纵坐标
	fBoxX						: ARRAY[1..4] OF LREAL;		// 物料的X轴坐标
	fBoxY						: ARRAY[1..4] OF LREAL;		// 物料的Y轴坐标
	
	bTempUp						: BOOL;						// 如果为TRUE，则升高正位气缸的温度
	fbTimerTempChange			: TON;						// 升高温度过程所需要的 TON 功能块
	tTempChange					: TIME 	:= T#1S;			// 升高温度过程时间
END_VAR
]]></Declaration>
    <Implementation>
      <ST><![CDATA[

// 气缸位置检测
CylinderSensorSignal();

// 物料位置检测
SelectLightSensorSignal();

//温度处理
Temperatures();

// HMI
General_Visu();]]></ST>
    </Implementation>
    <Method Name="CylinderSensorSignal" Id="{4486e541-acdf-47c1-96e2-85167e34166d}">
      <Declaration><![CDATA[METHOD PRIVATE CylinderSensorSignal
VAR_INPUT
END_VAR
]]></Declaration>
      <Implementation>
        <ST><![CDATA[// =========================================================
// 根据 SimuParam 中的参数判断各个气缸在基始位置或者目标位置
	
	bClampAtWork 	:= (fYClamp = SimuParam.cClampAtWork);
	bClampAtBase 	:= (fYClamp = SimuParam.cClampAtBase);
	bBarrierAtWork 	:= (fYBarrier = SimuParam.cBarrierAtWork);
	bBarrierAtBase 	:= (fYBarrier = SimuParam.cBarrierAtBase);
	bMetalAtWork 	:= (fYMetal < SimuParam.cMetalAtWorkMin) AND (fYMetal > SimuParam.cMetalAtWorkMax);
	bMetalAtBase 	:= (fYMetal = SimuParam.cMetalAtBase);
	bPlasticAtWork 	:= (fYPlastic < SimuParam.cPlasticAtWorkMin) AND (fYPlastic > SimuParam.cPlasticAtWorkMax);
	bPlasticAtBase 	:= (fYPlastic = SimuParam.cPlasticAtBase);

// =========================================================]]></ST>
      </Implementation>
    </Method>
    <Method Name="General_Visu" Id="{ca0c0b92-1459-4883-9b44-0e24bfc4b064}">
      <Declaration><![CDATA[METHOD PRIVATE General_Visu
VAR_INPUT
END_VAR

VAR

END_VAR]]></Declaration>
      <Implementation>
        <ST><![CDATA[// =========================================================
// 从 fbVisu 的属性当中传递数据到功能块当中
fYClamp			:= fbVisu.P_fYClamp;
fYBarrier		:= fbVisu.P_fYBarrier;
fYMetal			:= fbVisu.P_fYMetal;
fYPlastic		:= fbVisu.P_fYPlastic;
fBoxX			:= fbVisu.P_ArrfBoxX;
fBoxY			:= fbVisu.P_ArrfBoxY;
ArrColorSwitch	:= fbVisu.P_ArrChangeColor;

fbVisu();

// =========================================================
]]></ST>
      </Implementation>
    </Method>
    <Method Name="SelectLightSensorSignal" Id="{42a29cb5-06ce-4cc8-88a8-1f3b3fadf30a}">
      <Declaration><![CDATA[METHOD PRIVATE SelectLightSensorSignal
VAR_INPUT
END_VAR

VAR
	
END_VAR]]></Declaration>
      <Implementation>
        <ST><![CDATA[// =========================================================
// 检测每个物料是否被栏杆前的传感器检测到/被金属传感器检测到/被塑料传感器检测到
	
	bBarrierSensor := 	( (fBoxY[1] >= SimuParam.cSensorsBoxesY) AND (fBoxX[1] > SimuParam.cBarrierSensorBox1Xmin) 	AND (fBoxX[1] < SimuParam.cBarrierSensorBox1Xmax) )
					OR 	( (fBoxY[2] >= SimuParam.cSensorsBoxesY) AND (fBoxX[2] > SimuParam.cBarrierSensorBox2Xmin) 	AND (fBoxX[2] < SimuParam.cBarrierSensorBox2Xmax) )
					OR 	( (fBoxY[3] >= SimuParam.cSensorsBoxesY) AND (fBoxX[3] > SimuParam.cBarrierSensorBox3Xmin) 	AND (fBoxX[3] < SimuParam.cBarrierSensorBox3Xmax) )
					OR 	( (fBoxY[4] >= SimuParam.cSensorsBoxesY) AND (fBoxX[4] > SimuParam.cBarrierSensorBox4Xmin) 	AND (fBoxX[4] < SimuParam.cBarrierSensorBox4Xmax) );
	

	bMetalSensor := 	( (fBoxY[1] >= SimuParam.cSensorsBoxesY) AND (fBoxX[1] > SimuParam.cMetalSensorBox1Xmin) 	AND (fBoxX[1] < SimuParam.cMetalSensorBox1Xmax)  AND (ArrColorSwitch[1] = TRUE))
					OR 	( (fBoxY[2] >= SimuParam.cSensorsBoxesY) AND (fBoxX[2] > SimuParam.cMetalSensorBox2Xmin) 	AND (fBoxX[2] < SimuParam.cMetalSensorBox2Xmax)  AND (ArrColorSwitch[2] = TRUE)) 
					OR 	( (fBoxY[3] >= SimuParam.cSensorsBoxesY) AND (fBoxX[3] > SimuParam.cMetalSensorBox3Xmin) 	AND (fBoxX[3] < SimuParam.cMetalSensorBox3Xmax)  AND (ArrColorSwitch[3] = TRUE))
					OR 	( (fBoxY[4] >= SimuParam.cSensorsBoxesY) AND (fBoxX[4] > SimuParam.cMetalSensorBox4Xmin) 	AND (fBoxX[4] < SimuParam.cMetalSensorBox4Xmax)  AND (ArrColorSwitch[4] = TRUE))  ;
					
	bPlasticSensor := 	( (fBoxY[1] >= SimuParam.cSensorsBoxesY) AND (fBoxX[1] > SimuParam.cPlasticSensorBox1Xmin) 	AND (fBoxX[1] < SimuParam.cPlasticSensorBox1Xmax) AND (ArrColorSwitch[1] = FALSE))
					OR 	( (fBoxY[2] >= SimuParam.cSensorsBoxesY) AND (fBoxX[2] > SimuParam.cPlasticSensorBox2Xmin) 	AND (fBoxX[2] < SimuParam.cPlasticSensorBox2Xmax) AND (ArrColorSwitch[2] = FALSE))
					OR 	( (fBoxY[3] >= SimuParam.cSensorsBoxesY) AND (fBoxX[3] > SimuParam.cPlasticSensorBox3Xmin) 	AND (fBoxX[3] < SimuParam.cPlasticSensorBox3Xmax) AND (ArrColorSwitch[3] = FALSE))
					OR 	( (fBoxY[4] >= SimuParam.cSensorsBoxesY) AND (fBoxX[4] > SimuParam.cPlasticSensorBox4Xmin) 	AND (fBoxX[4] < SimuParam.cPlasticSensorBox4Xmax) AND (ArrColorSwitch[4] = FALSE));

// =========================================================]]></ST>
      </Implementation>
    </Method>
    <Method Name="Temperatures" Id="{cac0105e-33aa-4b08-b086-24ceda579c1b}">
      <Declaration><![CDATA[METHOD PRIVATE Temperatures
VAR_INPUT
END_VAR
]]></Declaration>
      <Implementation>
        <ST><![CDATA[// =========================================================

// 复位温度
IF bReset THEN
	fClampTempCurrent 	:= 0;
	fBarrierTemp 		:= 0;
END_IF

// 从 SimuParam 中传递温度参数数据

	fClampTempMax 		:= SimuParam.cClampTempMax;
	fClampTempMin 		:= SimuParam.cClampTempMin;
	fClampTempCurrent 	:= SimuParam.cClampTempCurrent;

// =========================================================
// 温度处理

	fbTimerTempChange(PT := tTempChange);
	
	IF NOT fbTimerTempChange.Q THEN
		fbTimerTempChange(IN := TRUE);
	END_IF
	
	IF fBarrierTemp >= SimuParam.cBarrierTempMax THEN
		bTempUp := FALSE;
	ELSIF fBarrierTemp <= SimuParam.cBarrierTempMin THEN
		bTempUp := TRUE;
	END_IF
	
	IF fbTimerTempChange.Q THEN
		
		fbTimerTempChange(IN := FALSE);
		
		IF bTempUp THEN
			fBarrierTemp := fBarrierTemp + SimuParam.cBarrierTempChange;
		ELSE
			fBarrierTemp := fBarrierTemp - SimuParam.cBarrierTempChange;
		END_IF
	END_IF

// =========================================================]]></ST>
      </Implementation>
    </Method>
    <LineIds Name="FB_Simulation">
      <LineId Id="53" Count="0" />
      <LineId Id="152" Count="0" />
      <LineId Id="54" Count="0" />
      <LineId Id="9" Count="0" />
      <LineId Id="153" Count="0" />
      <LineId Id="50" Count="0" />
      <LineId Id="49" Count="0" />
      <LineId Id="154" Count="0" />
      <LineId Id="52" Count="0" />
      <LineId Id="51" Count="0" />
      <LineId Id="104" Count="1" />
      <LineId Id="103" Count="0" />
    </LineIds>
    <LineIds Name="FB_Simulation.CylinderSensorSignal">
      <LineId Id="6" Count="11" />
      <LineId Id="5" Count="0" />
    </LineIds>
    <LineIds Name="FB_Simulation.General_Visu">
      <LineId Id="9" Count="0" />
      <LineId Id="129" Count="0" />
      <LineId Id="116" Count="0" />
      <LineId Id="118" Count="4" />
      <LineId Id="117" Count="0" />
      <LineId Id="115" Count="0" />
      <LineId Id="99" Count="0" />
      <LineId Id="101" Count="0" />
      <LineId Id="103" Count="0" />
      <LineId Id="5" Count="0" />
    </LineIds>
    <LineIds Name="FB_Simulation.SelectLightSensorSignal">
      <LineId Id="36" Count="0" />
      <LineId Id="16" Count="6" />
      <LineId Id="54" Count="0" />
      <LineId Id="23" Count="1" />
      <LineId Id="43" Count="1" />
      <LineId Id="25" Count="5" />
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Simulation.Temperatures">
      <LineId Id="3" Count="0" />
      <LineId Id="40" Count="0" />
      <LineId Id="44" Count="3" />
      <LineId Id="41" Count="0" />
      <LineId Id="39" Count="0" />
      <LineId Id="4" Count="31" />
      <LineId Id="2" Count="0" />
    </LineIds>
  </POU>
</TcPlcObject>