<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.13">
  <POU Name="FB_Subsystem_Root" Id="{e85bf9e5-f36b-4997-a7a4-ea6d26f1fe43}" SpecialFunc="None">
    <Declaration><![CDATA[// 所有带轴和手动控制系统的基本对象（基类）
FUNCTION_BLOCK FB_Subsystem_Root
VAR_INPUT	
	// =============== 控制轴的手动按钮 =====================
	bButtonAxisFwIn		AT%I*	: BOOL;						// 正转信号(按钮)
	bButtonAxisBwIn		AT%I*	: BOOL;						// 反转信号(按钮)
END_VAR
VAR_OUTPUT
	// =============== 错误变量 ===========================
	bAxisError					: BOOL;						// 轴报错标志位
	nAxisErrorID				: UDINT;					// 轴报错ID

	// =============== 轴、按钮输出信号 ====
	bAxisMoves					: BOOL;						// 如果为TRUE，则轴正在运动
	bButtonAxisFwOut			: BOOL;						// 正转输出信号
	bButtonAxisBwOut			: BOOL;						// 反转输出信号		
END_VAR
VAR
	// =============== 停止、复位以及启动的完成信号 ======
	bHaltDone					: BOOL;						// 如果为TRUE，则停止完成
	bResetDone					: BOOL;						// 如果为TRUE，则复位完成
	bStartingDone				: BOOL;						// 如果为TRUE，则启动完成
	
	// =============== 使能信号 ===================================
	bComprAirEnabledLocal		: BOOL;						// 成功压缩（气缸）空气
	bAxisEnableLocal			: BOOL;						// 如果为TRUE，则轴使能
	bSensorEnableLocal			: BOOL;						// 启用传感器信号
	bManualAxisEnableLocal		: BOOL;						// 允许轴被手动控制
	bManualCylinderEnable		: BOOL;						// 允许气缸被手动控制

	// =============== 请求 ===========================
	bHaltRequest				: BOOL;						// 如果为TRUE，则设备接受到停止请求
	bResetRequest				: BOOL;						// 如果为TRUE，则设备接受到复位请求

	// =============== 轴 ======================================
	fbAxis						: FB_Axis;					// 轴对象
	fAxisVelo					: LREAL;					// 轴运动的设定速度
	
	// =============== 信号处理功能块 =======================
	fbButtonAxisFw				: FB_SignalHandling;	// 处理轴正转
	fbButtonAxisBw				: FB_SignalHandling;	// 处理轴反转
	
	// =============== 其他 ==========================
	fbTriggerStartingStart		: R_TRIG;					// 检测启动上升沿
	fbTriggerResetStart			: R_TRIG;					// 检测复位上升沿			
	bMoved						: BOOL;						// 如果为TRUE，则轴运动过
END_VAR
]]></Declaration>
    <Implementation>
      <ST><![CDATA[// =========================================================
;
// =========================================================]]></ST>
    </Implementation>
    <Property Name="bState_HaltDone" Id="{7b826129-2ebf-4b7a-886b-a93a9c06d9aa}">
      <Declaration><![CDATA[PROPERTY bState_HaltDone : BOOL]]></Declaration>
      <Get Name="Get" Id="{f3370124-0e91-4b84-8650-d85942fcfb6b}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[bState_HaltDone := bHaltDone;]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="bState_ResetDone" Id="{91a63a3d-8190-4490-8a0c-04aade6a4162}">
      <Declaration><![CDATA[PROPERTY bState_ResetDone : BOOL]]></Declaration>
      <Get Name="Get" Id="{cf2ec45f-83dc-4601-95e2-2abfc7cc2486}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[bState_ResetDone := bResetDone;]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Property Name="bState_StartingDone" Id="{82153124-14ee-4fd3-a315-0d06fd2fbfe3}">
      <Declaration><![CDATA[PROPERTY bState_StartingDone : BOOL]]></Declaration>
      <Get Name="Get" Id="{c56ede52-8e3a-4645-854d-7f8579220aeb}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[bState_StartingDone := bStartingDone;]]></ST>
        </Implementation>
      </Get>
    </Property>
    <Method Name="CylicCheck" Id="{cf391a29-09ab-4f78-8b6c-5642a067c8c0}">
      <Declaration><![CDATA[METHOD CylicCheck
VAR_INPUT
END_VAR

]]></Declaration>
      <Implementation>
        <ST><![CDATA[
// 循环检查轴的状态
fbAxis.CylicCheck();]]></ST>
      </Implementation>
    </Method>
    <Method Name="Enable" Id="{fe44839a-82c0-40e8-aa44-d7ff038c53a7}">
      <Declaration><![CDATA[METHOD PUBLIC Enable
VAR_INPUT
	bComprAirEnabled		: BOOL;			// 压缩（气缸）空气
	bAxisEnable				: BOOL;			// 使能轴
	bSensorEnable			: BOOL;			// 启用传感器
	bManualAxisEnable		: BOOL;			// 允许手动控制轴
END_VAR
]]></Declaration>
      <Implementation>
        <ST><![CDATA[// =========================================================
// 将方法的输入传递给功能块本地

	bComprAirEnabledLocal	:= bComprAirEnabled;
	bAxisEnableLocal		:= bAxisEnable;
	bSensorEnableLocal		:= bSensorEnable;
	bManualAxisEnableLocal	:= bManualAxisEnable;
	
// =========================================================	
// 使能轴

	fbAxis.Enable(bEnable := bAxisEnableLocal);					

// =========================================================
// 手动压缩气缸空气 
		
	bManualCylinderEnable := bManualAxisEnableLocal AND bComprAirEnabledLocal;
 
// =========================================================
// 手动控制轴运动

	fbButtonAxisFw.Enable(bEnable := bManualAxisEnableLocal );
	fbButtonAxisBw.Enable(bEnable := bManualAxisEnableLocal );

	
// =========================================================]]></ST>
      </Implementation>
    </Method>
    <Property Name="fAxisVelocity" Id="{a099eff0-f141-449a-80e1-83261f4d38be}">
      <Declaration><![CDATA[PROPERTY fAxisVelocity : LREAL]]></Declaration>
      <Get Name="Get" Id="{e032c5cf-2524-4ec6-a694-b0fca73d486b}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[fAxisVelocity := fAxisVelo;]]></ST>
        </Implementation>
      </Get>
      <Set Name="Set" Id="{ea13ca39-a63a-41ac-8fc7-896e64363594}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[fAxisVelo := fAxisVelocity;]]></ST>
        </Implementation>
      </Set>
    </Property>
    <Method Name="Halt" Id="{aa6524a1-9db9-403c-9754-75a1b7435afa}">
      <Declaration><![CDATA[METHOD PUBLIC Halt
VAR_INPUT
	bHalt		: BOOL;				// 停止请求
END_VAR
]]></Declaration>
      <Implementation>
        <ST><![CDATA[
// =========================================================
// 停止轴

	fbAxis.Halt(bDriveHalt := bHaltRequest := bHalt);

// =========================================================
// 获取停止状态

	bHaltDone := fbAxis.bState_HaltDone;

// =========================================================]]></ST>
      </Implementation>
    </Method>
    <Method Name="InputOutput" Id="{8f13bdb8-6078-43f9-b512-270f70c191db}">
      <Declaration><![CDATA[METHOD PUBLIC InputOutput
VAR_INPUT
END_VAR
]]></Declaration>
      <Implementation>
        <ST><![CDATA[// =========================================================
// 关联输入输出状态变量
		
	fbButtonAxisFw.SetOutput();	
	fbButtonAxisBw.SetOutput();
	
	bAxisMoves 		:= fbAxis.bMoves;
	bAxisError		:= fbAxis.bError;
	nAxisErrorID	:= fbAxis.nErrorID;
	
	
	fbButtonAxisFw( bInput:=bButtonAxisFwIn , bOut=>bButtonAxisFwOut );
	fbButtonAxisBw( bInput:=bButtonAxisBwIn , bOut=>bButtonAxisBwOut );


// =========================================================]]></ST>
      </Implementation>
    </Method>
    <Method Name="Manual" Id="{5d41758d-79dd-4e51-a84d-386573225056}">
      <Declaration><![CDATA[METHOD PUBLIC Manual
VAR_INPUT
END_VAR
]]></Declaration>
      <Implementation>
        <ST><![CDATA[// =========================================================
// 手动模式的轴控

// 手动控制轴运动时，重置bHaltDone，将bMove置为TRUE
IF fbButtonAxisBw.bOut OR fbButtonAxisFw.bOut THEN
    bHaltDone := FALSE;
    bMoved := TRUE;
	
	// 如果反转按钮输出信号为TRUE，反转
	IF fbButtonAxisBw.bOut THEN
		fbAxis.MoveBw();
		
	// 如果正转按钮输出信号为TRUE，正转
	ELSIF fbButtonAxisFw.bOut THEN
		fbAxis.MoveFw();
	END_IF
	
// 如果轴没有受到正反转的控制，则保持停止
ELSIF (NOT fbButtonAxisBw.bOut AND NOT fbButtonAxisFw.bOut)  THEN
		
	IF bMoved AND NOT bHaltDone THEN
		Halt(bHalt := TRUE);		
	ELSE
		Halt(bHalt := FALSE);
		bMoved := FALSE;
	END_IF
		
END_IF
// =========================================================]]></ST>
      </Implementation>
    </Method>
    <Property Name="R_Axis" Id="{7e93549f-8991-4ae9-ad1a-47e5589ef9c5}">
      <Declaration><![CDATA[PROPERTY R_Axis : Reference TO FB_Axis]]></Declaration>
      <Get Name="Get" Id="{764dda4d-9568-4fb9-abd2-38d920c4ba2c}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[R_Axis REF= fbAxis;]]></ST>
        </Implementation>
      </Get>
      <Set Name="Set" Id="{78adf5f4-02fc-4de6-9736-b3d918165e31}">
        <Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
        <Implementation>
          <ST><![CDATA[fbAxis := R_Axis;]]></ST>
        </Implementation>
      </Set>
    </Property>
    <Method Name="Reset" Id="{f2f2f9ba-40fb-430a-b345-b85d9a795cf6}">
      <Declaration><![CDATA[METHOD PUBLIC Reset
VAR_INPUT
	bReset		: BOOL;				// 复位请求
END_VAR
]]></Declaration>
      <Implementation>
        <ST><![CDATA[// =========================================================
// 重置轴状态变量

	bMoved 			:= FALSE;
	bHaltDone 		:= FALSE;
	bHaltRequest	:= FALSE;

// =========================================================
// 复位轴，检测复位请求信号，传递复位相关状态变量
						
	fbTriggerResetStart(CLK := bReset);
	
	fbAxis.Reset(bDriveReset := bResetRequest:= bReset);
	
	bResetDone := fbAxis.bState_ResetDone;

// =========================================================]]></ST>
      </Implementation>
    </Method>
    <Method Name="ResetState" Id="{e5be592b-ecc8-4e81-842d-fae5a70238c6}">
      <Declaration><![CDATA[METHOD PROTECTED ResetState
VAR_INPUT
END_VAR
]]></Declaration>
      <Implementation>
        <ST><![CDATA[// =========================================================
// 为子类的具体子系统（输送系统和分拣系统）所预先创建的虚方法
;

// =========================================================]]></ST>
      </Implementation>
    </Method>
    <Method Name="Starting" Id="{698a27ee-7cd5-4b57-910b-952daf66e874}">
      <Declaration><![CDATA[METHOD PUBLIC Starting
VAR_INPUT
	bStarting		: BOOL;			// 启动请求
END_VAR
]]></Declaration>
      <Implementation>
        <ST><![CDATA[// =========================================================

// 检测启动请求信号
fbTriggerStartingStart(CLK := bStarting);

IF fbTriggerStartingStart.Q THEN
	
	// 关联相关信号
	bStartingDone 			:= FALSE;
	
	fbAxis.fTargetVelocity 	:= fAxisVelo;
	// 初始化时，复位相关子系统状态
	ResetState();
END_IF

// 轴使能成功也意味着启动成功
bStartingDone 			:= fbAxis.bState_Enable;


// =========================================================]]></ST>
      </Implementation>
    </Method>
    <LineIds Name="FB_Subsystem_Root">
      <LineId Id="3" Count="1" />
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Subsystem_Root.bState_HaltDone.Get">
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Subsystem_Root.bState_ResetDone.Get">
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Subsystem_Root.bState_StartingDone.Get">
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Subsystem_Root.CylicCheck">
      <LineId Id="18" Count="0" />
      <LineId Id="6" Count="0" />
      <LineId Id="5" Count="0" />
    </LineIds>
    <LineIds Name="FB_Subsystem_Root.Enable">
      <LineId Id="34" Count="22" />
      <LineId Id="58" Count="0" />
      <LineId Id="57" Count="0" />
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Subsystem_Root.fAxisVelocity.Get">
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Subsystem_Root.fAxisVelocity.Set">
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Subsystem_Root.Halt">
      <LineId Id="7" Count="10" />
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Subsystem_Root.InputOutput">
      <LineId Id="21" Count="14" />
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Subsystem_Root.Manual">
      <LineId Id="3" Count="0" />
      <LineId Id="102" Count="0" />
      <LineId Id="147" Count="0" />
      <LineId Id="146" Count="0" />
      <LineId Id="138" Count="2" />
      <LineId Id="148" Count="1" />
      <LineId Id="141" Count="1" />
      <LineId Id="150" Count="1" />
      <LineId Id="144" Count="1" />
      <LineId Id="143" Count="0" />
      <LineId Id="152" Count="0" />
      <LineId Id="122" Count="4" />
      <LineId Id="160" Count="1" />
      <LineId Id="163" Count="0" />
      <LineId Id="127" Count="0" />
      <LineId Id="137" Count="0" />
      <LineId Id="45" Count="0" />
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Subsystem_Root.R_Axis.Get">
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Subsystem_Root.R_Axis.Set">
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Subsystem_Root.Reset">
      <LineId Id="7" Count="0" />
      <LineId Id="9" Count="14" />
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Subsystem_Root.ResetState">
      <LineId Id="10" Count="3" />
      <LineId Id="2" Count="0" />
    </LineIds>
    <LineIds Name="FB_Subsystem_Root.Starting">
      <LineId Id="3" Count="0" />
      <LineId Id="45" Count="0" />
      <LineId Id="31" Count="3" />
      <LineId Id="46" Count="1" />
      <LineId Id="35" Count="5" />
      <LineId Id="48" Count="0" />
      <LineId Id="12" Count="0" />
      <LineId Id="16" Count="2" />
      <LineId Id="2" Count="0" />
    </LineIds>
  </POU>
</TcPlcObject>