CX-PROGRAMMER FUNCTION BLOCK TEACHING EXAMPLE ============================================================ Definition name: FB_PumpFeedback Body language: Structured Text (ST) Source basis: Omron W447-E1-18 Function Blocks/Structured Text manual Purpose: stop-dominant request, permissive, command, feedback timeout, retained first-out fault, controlled reset, observable state IMPORTANT --------- This is reviewable teaching source, not an importable .CXF library or a production PLC project. Create the variable table and ST body in the exact CX-Programmer/CPU/unit-version project, program-check it there, call each instance with EN continuously ON, and execute the acceptance matrix. The Tick100ms input must be a one-scan pulse whose timing has been independently verified. Replace all example timings, addresses, states and safe responses with approved project values. VARIABLE TABLE -------------- Usage,Name,Type,Retain,Description Input,StartReq,BOOL,No,Start request; edge is detected inside the instance Input,StopReq,BOOL,No,Stop request; stop dominates start Input,Permissive,BOOL,No,Combined ordinary-control permissive Input,RunFb,BOOL,No,Independent running feedback Input,ResetReq,BOOL,No,Fault reset request; edge is detected inside instance Input,Tick100ms,BOOL,No,Verified one-scan 100 ms pulse generated outside FB Input,TimeoutTicks,UINT,No,Feedback timeout in 100 ms ticks; example only Output,RunCmd,BOOL,No,Ordinary-control run command Output,Running,BOOL,No,Copy of independently mapped running feedback Output,Fault,BOOL,Yes,Latched fail-to-start fault Output,FaultCode,UINT,Yes,0 none; 1 feedback timeout Output,State,UINT,No,0 idle; 1 commanded; 2 running; 3 fault Output,ElapsedTicks,UINT,No,Elapsed no-feedback tick count for diagnostics Internal,RunLatched,BOOL,Yes,Latched run request owned by this instance Internal,StartMem,BOOL,Yes,Previous StartReq for edge detection Internal,ResetMem,BOOL,Yes,Previous ResetReq for edge detection Internal,TickMem,BOOL,Yes,Previous Tick100ms for edge detection Internal,StartEdge,BOOL,No,One-scan start edge result Internal,ResetEdge,BOOL,No,One-scan reset edge result Internal,TickEdge,BOOL,No,One-scan tick edge result ST BODY ------- (* Calculate edges before updating their memories. *) StartEdge := StartReq AND NOT StartMem; ResetEdge := ResetReq AND NOT ResetMem; TickEdge := Tick100ms AND NOT TickMem; StartMem := StartReq; ResetMem := ResetReq; TickMem := Tick100ms; Running := RunFb; (* Reset is accepted only while command and feedback are both off. *) IF ResetEdge AND NOT RunCmd AND NOT RunFb THEN Fault := FALSE; FaultCode := UINT#0; ElapsedTicks := UINT#0; END_IF; (* Stop and loss of permissive dominate a simultaneous start. *) IF StopReq OR NOT Permissive THEN RunLatched := FALSE; ELSIF StartEdge AND NOT Fault THEN RunLatched := TRUE; END_IF; RunCmd := RunLatched AND Permissive AND NOT StopReq AND NOT Fault; (* Count only verified external tick edges while command lacks feedback. *) IF RunCmd AND NOT RunFb THEN IF TickEdge AND (ElapsedTicks < TimeoutTicks) THEN ElapsedTicks := ElapsedTicks + UINT#1; END_IF; IF (TimeoutTicks > UINT#0) AND (ElapsedTicks >= TimeoutTicks) THEN Fault := TRUE; FaultCode := UINT#1; RunLatched := FALSE; RunCmd := FALSE; END_IF; ELSE ElapsedTicks := UINT#0; END_IF; (* Publish one explicit diagnostic state. *) IF Fault THEN State := UINT#3; ELSIF RunFb THEN State := UINT#2; ELSIF RunCmd THEN State := UINT#1; ELSE State := UINT#0; END_IF; CALL-SITE CONTRACT ------------------ 1. Create one instance name per independent pump, for example Pump_A_FB. 2. Use the Always ON Flag (P_On) as the EN condition when the edge and tick memories must execute continuously. Put process conditions on the explicit FB inputs; do not gate the invocation and expect timers/edges to initialize while EN is OFF. 3. Map StartReq and ResetReq from bounded request logic, not directly from an unreviewed network or HMI address. 4. Map RunFb from an independent validated feedback source. 5. Interlocks and safety functions remain outside this teaching FB under their approved ownership. Permissive is ordinary-control context only. 6. Trend StartReq, StopReq, Permissive, RunCmd, RunFb, ElapsedTicks, Fault, FaultCode and State during every acceptance test. EXPECTED 8-SCAN TRACE (TimeoutTicks = 3) --------------------------------------- Scan,StartReq,StopReq,Permissive,TickEdge,RunFb,RunCmd,ElapsedTicks,Fault,State 1,0,0,1,0,0,0,0,0,0 2,1,0,1,0,0,1,0,0,1 3,0,0,1,1,0,1,1,0,1 4,0,0,1,1,0,1,2,0,1 5,0,0,1,1,0,0,3,1,3 6,0,0,1,0,0,0,0,1,3 7,0,0,1,0,0,0,0,1,3 8,0,0,1,0,0,0,0,0,0 (one ResetReq edge accepted) The trace assumes each displayed TickEdge is separated by at least one scan with Tick100ms FALSE. Confirm the actual project trace; do not use this table as evidence that an untested implementation executed correctly.