Industry Online Support
Technical Forum
6/27/2013 5:10 PM | |
Posts: 49 Rating: (0) |
hii minhajie first of all i wanna to thank u for yr respond. 1- i will wait your answer Anxiously 2-for change sample that u mean as in my attachment this right or not ? best regard
ahmed Attachmentsample.pdf (260 Downloads) |
6/28/2013 7:38 AM | |
Posts: 49 Rating: (0) |
hii minhajie thxs for yr answer i understand what u means. 1-i will wait yr code and please till how i can add it to my software ? 2-how i can convert from real out to integer? to connect 2 blocks as in attachments Attachmentreal-integer.pdf (282 Downloads) |
6/28/2013 8:44 PM | |
Posts: 49 Rating: (0) |
hii thxs alot 1-can u help me about how i can calculate the average?!! best regards
hady |
7/9/2013 9:31 AM | |
Joined: 11/4/2011 Last visit: 8/25/2024 Posts: 253 Rating: (53) |
I have not enough knowledge to create this FB. But I've found such function at the Russian Siemens forum and I'd asked the author for a permission to publish his code here. I'd just translated the comments. There are 3 functions needed, the main is FilterAI. I've not tested them, but only compiled. Sources in attachment. [code]FUNCTION FAST_TCK : TIME TITLE = 'debugging ticks' AUTHOR : 'BETEP59' (* wrapper for SFC time_tck() for test timer overflow acceleration factor = 1 displacement 16#7FFF_0000 (overflow after ~65sec) *) VAR_TEMP now : DINT; t : DINT; b AT t : ARRAY[0..7] OF BOOL; // MSB of t END_VAR BEGIN now := TIME_TO_DINT(time_tck()); // current time IF now = 0 OR now = 16#7fff_ffff THEN // replace special values now := 1; END_IF; t := now * 1; // apply acceleration factor b[7] := 0; t := t + 16#7FFF_0000; // apply displacement b[7] := 0; // exclude negative numbers now := t; fast_tck := DINT_TO_TIME(now); OK := true; END_FUNCTION[/code] [code]FUNCTION T_ABS : DINT TITLE = 'time difference module' AUTHOR : 'BETEP59' VAR_INPUT d: DINT; // time difference (now-t) END_VAR VAR_TEMP t: DINT; END_VAR BEGIN t := d; IF t < 0 THEN t := 16#7fff_ffff - ABS(t); END_IF; t_abs := t; OK := true; END_FUNCTION[/code] [code]FUNCTION_BLOCK FilterAI TITLE = 'Analog value filtering for period of Ti. Rolling/Sliding average' AUTHOR : 'BETEP59' VAR_INPUT Ti: TIME; //accumulation period Val: REAL; //current value Err: BOOL; //input fault rst: BOOL; //reset to current state END_VAR VAR_OUTPUT Res: BOOL; //got result (=ENO) Vavg: REAL; //average value over the period Vsum: REAL; //current sum in buffer END_VAR VAR full: BOOL; //buffer overflow (Ti passed) oldV: REAL; //max value vx: ARRAY[0..255] OF REAL; //circular buffer on time Ti px: INT; //pointer at the current position in buffer tx: DINT:=0; //time of the last entry in buffer t_err: DINT:=0; //fault time sum: REAL; //temp sum before saving to buffer cnt: INT; //number of accumulated values END_VAR VAR_TEMP t_now, t_i: DINT; END_VAR BEGIN t_i := TIME_TO_DINT(Ti); // when reset or if the integration period is not set (==0) - initial state IF rst OR t_i <= 0 THEN tx := 0; t_err := 0; Vavg := 0.0; Res := NOT(rst) AND NOT(Err); ELSE t_now := TIME_TO_DINT(fast_tck()); // current time IF Err THEN IF t_err = 0 THEN // latch the fault time t_err := t_now; END_IF; // if the duration of fault is greater than the integration period - reset calculation IF T_ABS(d:=t_now-t_err) >= t_i THEN tx := 0; END_IF; ELSE // in this cycle the pre-fault value would be added - oldV t_err := 0; // reset fault time oldV := Val; // add current value END_IF; // don’t start calculation if fault Res := NOT(Err AND (tx = 0)); IF Res THEN // during start of calculations IF tx = 0 THEN px := 0; sum := 0.0; cnt := 0; full := false; Vsum := 0.0; vx[0] := 0.0; tx := t_now; // set the last buffer update time END_IF; // summing up current values (during update period) sum := sum + oldV; cnt := cnt + 1; // if the time that passed after the last update is more than the buffer update period (Ti/size) IF T_ABS(d:=t_now-tx) >= (t_i/256) THEN // add to buffer IF full THEN Vsum := Vsum - vx[px]; // subtract old value from sum END_IF; vx[px] := sum / cnt; // add new Vsum := Vsum + vx[px]; // recalculate sum px := px + 1; // next sum := 0; cnt := 0; tx := t_now; // latch the last buffer update time // filling the circular buffer IF px >= 256 THEN full := 1; px := 0; END_IF; // if buffer is full after every update - recalc result IF full THEN Vavg := Vsum / 256.0; END_IF; END_IF; Res := full; // if buffer is overflown - got the result END_IF; END_IF; OK := Res; // set result to output END_FUNCTION_BLOCK[/code] AttachmentSCL Source FilterAI. By BETEP59 (3 in 1).zip (274 Downloads) |
Follow us on