388 lines
7.7 KiB
XML
388 lines
7.7 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<TcPlcObject Version="1.1.0.1">
|
|
<POU Name="FB_KeyenceVS" Id="{2ea89630-4476-43f8-991b-76955c0d8baf}" SpecialFunc="None">
|
|
<Declaration><![CDATA[FUNCTION_BLOCK FB_KeyenceVS
|
|
VAR_INPUT
|
|
xReleaseAlarms : BOOL;
|
|
xConfirmAlarms : BOOL;
|
|
END_VAR
|
|
VAR_OUTPUT
|
|
xBusy : BOOL;
|
|
xDone : BOOL;
|
|
xError : BOOL;
|
|
END_VAR
|
|
VAR
|
|
// ======
|
|
// Inputs
|
|
// ======
|
|
i_xTriggerRdy AT %I* : BOOL;
|
|
i_xTriggerAck AT %I* : BOOL;
|
|
i_xResultRdy AT %I* : BOOL;
|
|
i_xRunStatus AT %I*: BOOL;
|
|
i_xCmdComplete AT %I* : BOOL;
|
|
i_xCmdError AT %I* : BOOL;
|
|
i_xCmdRdy AT %I* : BOOL;
|
|
i_xProgLoadComplete AT %I* : BOOL;
|
|
i_xError0State AT %I* : BOOL;
|
|
i_xError1State AT %I* : BOOL;
|
|
i_udiError0Code AT %I* : UDINT;
|
|
i_udiError1Code AT %I* : UDINT;
|
|
i_udiPrgNr AT %I* : UDINT;
|
|
i_udiTotalCnt AT %I* : UDINT;
|
|
i_abyResult128 AT %I* : ARRAY[0..127] OF BYTE;
|
|
|
|
// =======
|
|
// Outputs
|
|
// =======
|
|
o_xTrigger AT %Q* : BOOL;
|
|
o_xResultHandshakeEnable AT %Q* : BOOL;
|
|
o_xResultAck AT %Q* : BOOL;
|
|
o_xGotoRun AT %Q* : BOOL;
|
|
o_xGotoSetting AT %Q* : BOOL;
|
|
o_xError0Clear AT %Q* : BOOL;
|
|
o_xError1Clear AT %Q* : BOOL;
|
|
o_xCmdRequest AT %Q* : BOOL;
|
|
o_xPrgLoadExec AT %Q* : BOOL;
|
|
o_udiPrgNr AT %Q* : UDINT;
|
|
|
|
// ======
|
|
// Alarms
|
|
// ======
|
|
_fbError0Alarm : FB_AlarmMessage(stEventEntry := TC_EVENTS.KeyenceVS.Error0, xWithConfirmation := TRUE);
|
|
_rtError0 : R_TRIG;
|
|
|
|
_fbError1Alarm : FB_AlarmMessage(stEventEntry := TC_EVENTS.KeyenceVS.Error1, xWithConfirmation := TRUE);
|
|
_rtError1 : R_TRIG;
|
|
|
|
// ==============
|
|
// Ouputs buffers
|
|
// ==============
|
|
_xBusy : BOOL;
|
|
_xDone : BOOL;
|
|
_xError : BOOL;
|
|
|
|
// =============
|
|
// State machine
|
|
// =============
|
|
// State of state machine
|
|
_uiState : UINT;
|
|
|
|
// Command to start loading a program with defined number
|
|
_xLoadProgNr : BOOL;
|
|
// Program number to load
|
|
_udiProgNrToLoad : UDINT;
|
|
|
|
// Command to change camera into run mode
|
|
_xSwitchToRunMode : BOOL;
|
|
|
|
// Command to change to config mode
|
|
_xSwitchToConfigMode : BOOL;
|
|
|
|
// Trigger a picture
|
|
_xTrigger : BOOL;
|
|
END_VAR
|
|
]]></Declaration>
|
|
<Implementation>
|
|
<ST><![CDATA[// ==============
|
|
// Alarm handling
|
|
// ==============
|
|
// If error 0 occurs, add error code to alarm message
|
|
_rtError0(CLK := i_xError0State);
|
|
IF _rtError0.Q THEN
|
|
_fbError0Alarm.Arguments.Clear().AddUDInt(i_udiError0Code);
|
|
END_IF
|
|
|
|
_fbError0Alarm(
|
|
xActive:= i_xError0State,
|
|
xRelease:= xReleaseAlarms,
|
|
xAcknowledge:= xConfirmAlarms);
|
|
|
|
IF _fbError0Alarm.Active THEN
|
|
_xError := TRUE;
|
|
END_IF
|
|
|
|
// If error 1 occurs, add error code to alarm message
|
|
_rtError1(CLK := i_xError1State);
|
|
IF _rtError1.Q THEN
|
|
_fbError1Alarm.Arguments.Clear().AddUDInt(i_udiError1Code);
|
|
END_IF
|
|
|
|
_fbError1Alarm(
|
|
xActive:= i_xError1State,
|
|
xRelease:= xReleaseAlarms,
|
|
xAcknowledge:= xConfirmAlarms);
|
|
|
|
IF _fbError1Alarm.Active THEN
|
|
_xError := TRUE;
|
|
END_IF
|
|
|
|
|
|
// =============
|
|
// State machine
|
|
// =============
|
|
CASE _uiState OF
|
|
// Idle
|
|
0:
|
|
// Load program number
|
|
IF _xLoadProgNr THEN
|
|
_xLoadProgNr := FALSE;
|
|
_uiState := 10;
|
|
END_IF
|
|
|
|
IF _xSwitchToRunMode THEN
|
|
_xSwitchToRunMode := FALSE;
|
|
_uiState := 20;
|
|
END_IF
|
|
|
|
IF _xSwitchToConfigMode THEN
|
|
_xSwitchToConfigMode := FALSE;
|
|
_uiState := 30;
|
|
END_IF
|
|
|
|
IF _xTrigger THEN
|
|
_xTrigger := FALSE;
|
|
_uiState := 40;
|
|
END_IF
|
|
|
|
// Load program number
|
|
10:
|
|
o_udiPrgNr := _udiProgNrToLoad;
|
|
o_xPrgLoadExec := TRUE;
|
|
|
|
IF i_xProgLoadComplete THEN
|
|
o_xPrgLoadExec := FALSE;
|
|
_xDone := TRUE;
|
|
_xBusy := FALSE;
|
|
_uiState := 0;
|
|
END_IF
|
|
|
|
IF _xError THEN
|
|
_uiState := 900;
|
|
END_IF
|
|
|
|
|
|
// Switch to run mode
|
|
20:
|
|
o_xGotoRun := TRUE;
|
|
o_xGotoSetting := FALSE;
|
|
|
|
IF i_xRunStatus THEN
|
|
o_xGotoRun := FALSE;
|
|
_xDone := TRUE;
|
|
_xBusy := FALSE;
|
|
_uiState := 0;
|
|
END_IF
|
|
|
|
IF _xError THEN
|
|
_uiState := 900;
|
|
END_IF
|
|
|
|
|
|
// Switch to config mode
|
|
30:
|
|
o_xGotoSetting := TRUE;
|
|
o_xGotoRun := FALSE;
|
|
|
|
IF (NOT i_xRunStatus) THEN
|
|
o_xGotoRun := FALSE;
|
|
_xDone := TRUE;
|
|
_xBusy := FALSE;
|
|
_uiState := 0;
|
|
END_IF
|
|
|
|
IF _xError THEN
|
|
_uiState := 900;
|
|
END_IF
|
|
|
|
// Trigger
|
|
40:
|
|
o_xTrigger := TRUE;
|
|
|
|
IF i_xTriggerAck THEN
|
|
o_xTrigger := FALSE;
|
|
_uiState := 41;
|
|
END_IF
|
|
|
|
IF _xError THEN
|
|
_uiState := 900;
|
|
END_IF
|
|
|
|
// Wait for result
|
|
41:
|
|
IF i_xResultRdy THEN
|
|
o_xResultAck := TRUE;
|
|
_uiState := 42;
|
|
END_IF
|
|
|
|
IF _xError THEN
|
|
_uiState := 900;
|
|
END_IF
|
|
|
|
// Wait for result ack done
|
|
42:
|
|
IF (NOT i_xResultRdy) THEN
|
|
o_xResultAck := FALSE;
|
|
_uiState := 0;
|
|
END_IF
|
|
|
|
IF _xError THEN
|
|
_uiState := 900;
|
|
END_IF
|
|
|
|
// Error state
|
|
900:
|
|
_xError := TRUE;
|
|
_xDone := FALSE;
|
|
_xBusy := FALSE;
|
|
|
|
_uiState := 901;
|
|
|
|
// Wait for error ack
|
|
901:
|
|
IF xConfirmAlarms THEN
|
|
_xError := FALSE;
|
|
_uiState := 0;
|
|
END_IF
|
|
END_CASE
|
|
|
|
|
|
// =============================
|
|
// Copy output buffers to output
|
|
// =============================
|
|
xBusy := _xBusy;
|
|
xDone := _xDone;
|
|
xError := _xError;
|
|
]]></ST>
|
|
</Implementation>
|
|
<Method Name="M_GetResult128" Id="{80736d9c-5f6b-4185-a62e-60e64078b87d}">
|
|
<Declaration><![CDATA[// Get result data from 128 Byte input
|
|
METHOD M_GetResult128 : BOOL
|
|
VAR_INPUT
|
|
// Start index in result byte array
|
|
uiIdx : UINT;
|
|
// Target buffer
|
|
pTarget : PVOID;
|
|
// Target buffer size
|
|
udiSize : UDINT := 0;
|
|
|
|
// Result of copy
|
|
udiResult : UDINT;
|
|
END_VAR
|
|
]]></Declaration>
|
|
<Implementation>
|
|
<ST><![CDATA[// Check index size
|
|
IF (uiIdx < 0)
|
|
OR (uiIdx > 127) // Index to big
|
|
OR ((uiIdx + udiSize) > 128) // Start index and size will read over the input array bounds
|
|
OR udiSize = 0 // No target size given
|
|
OR pTarget = 0 // No valid target pointer
|
|
THEN
|
|
M_GetResult128 := FALSE;
|
|
RETURN;
|
|
END_IF
|
|
|
|
// Copy data
|
|
udiResult := MEMCPY(destAddr := pTarget, srcAddr := ADR(i_abyResult128[uiIdx]), n := udiSize);
|
|
|
|
// Check if copy was successful
|
|
IF udiResult <> udiSize THEN
|
|
M_GetResult128 := FALSE;
|
|
ELSE
|
|
M_GetResult128 := TRUE;
|
|
END_IF]]></ST>
|
|
</Implementation>
|
|
</Method>
|
|
<Method Name="M_SetProgramNr" Id="{61381180-0979-4559-a38e-7a94f647a142}">
|
|
<Declaration><![CDATA[METHOD M_SetProgramNr : BOOL
|
|
VAR_INPUT
|
|
udiPrgNr : UDINT;
|
|
END_VAR
|
|
]]></Declaration>
|
|
<Implementation>
|
|
<ST><![CDATA[IF _xBusy OR _xError THEN
|
|
M_SetProgramNr := FALSE;
|
|
RETURN;
|
|
END_IF
|
|
|
|
// Set Prg nr
|
|
_udiProgNrToLoad := udiPrgNr;
|
|
|
|
// Start command
|
|
_xLoadProgNr := TRUE;
|
|
|
|
// Set busy
|
|
_xBusy := TRUE;
|
|
_xDone := FALSE;
|
|
|
|
// Report command result
|
|
M_SetProgramNr := TRUE;]]></ST>
|
|
</Implementation>
|
|
</Method>
|
|
<Method Name="M_SwitchToConfig" Id="{97bb226a-9857-448a-8000-4f7ffa070011}">
|
|
<Declaration><![CDATA[METHOD M_SwitchToConfig : BOOL
|
|
VAR_INPUT
|
|
END_VAR
|
|
]]></Declaration>
|
|
<Implementation>
|
|
<ST><![CDATA[IF _xBusy OR _xError THEN
|
|
M_SwitchToConfig := FALSE;
|
|
RETURN;
|
|
END_IF
|
|
|
|
// Start command
|
|
_xSwitchToConfigMode := TRUE;
|
|
|
|
// Set busy
|
|
_xBusy := TRUE;
|
|
_xDone := FALSE;
|
|
|
|
// Report command result
|
|
M_SwitchToConfig := TRUE;]]></ST>
|
|
</Implementation>
|
|
</Method>
|
|
<Method Name="M_SwitchToRun" Id="{949f243b-ddfd-4924-8c44-0bc4957e34cf}">
|
|
<Declaration><![CDATA[METHOD M_SwitchToRun : BOOL
|
|
VAR_INPUT
|
|
END_VAR
|
|
]]></Declaration>
|
|
<Implementation>
|
|
<ST><![CDATA[IF _xBusy OR _xError THEN
|
|
M_SwitchToRun := FALSE;
|
|
RETURN;
|
|
END_IF
|
|
|
|
// Start command
|
|
_xSwitchToRunMode := TRUE;
|
|
|
|
// Set busy
|
|
_xBusy := TRUE;
|
|
_xDone := FALSE;
|
|
|
|
// Report command result
|
|
M_SwitchToRun := TRUE;]]></ST>
|
|
</Implementation>
|
|
</Method>
|
|
<Method Name="M_Trigger" Id="{d463f53a-1fcf-4a61-a95a-507b0a863218}">
|
|
<Declaration><![CDATA[METHOD M_Trigger : BOOL
|
|
VAR_INPUT
|
|
END_VAR
|
|
]]></Declaration>
|
|
<Implementation>
|
|
<ST><![CDATA[IF _xBusy OR _xError OR (NOT i_xTriggerRdy) THEN
|
|
M_Trigger := FALSE;
|
|
RETURN;
|
|
END_IF
|
|
|
|
// Start command
|
|
_xTrigger := TRUE;
|
|
|
|
// Set busy
|
|
_xBusy := TRUE;
|
|
_xDone := FALSE;
|
|
|
|
// Report command result
|
|
M_Trigger := TRUE;]]></ST>
|
|
</Implementation>
|
|
</Method>
|
|
</POU>
|
|
</TcPlcObject> |