5/29/2014 6:18 PM | |
Posts: 5225 Rating:
|
Hi there. "huggy_d1" has already given valuable info. Do try the OSCAT route. To add more ideas, please find attached a PDF printout of how the S7-200 does its filtering. I admire the concept where a quick change is also accommodated. A general tip: (how does the SCL version of TON work?) Inside the TIA design environment you have a toolbar drawer on your right marked as INSTRUCTIONS. Even in SCL the instruction seen there can be click-and-drag into the coding. Hovering on the instruction brings out a tooltip which is selectable. From the tooltip the help on that instruction can be opened. I miss the examples the S7-200's software gave for their help, but the text is helpful. I hope this was of value to you. Good luck. w AttachmentS7-200 - Built in analog input filters_System Block Configuration.pdf (468 Downloads) |
5/29/2014 11:20 PM | |
Posts: 60 Rating:
|
Thank you William. I will try look at your example also. |
5/31/2014 7:21 PM | |
Posts: 60 Rating:
|
this is the error from the buffer |
5/31/2014 10:42 PM | |
Posts: 5225 Rating:
|
Hi, The designer's intent is important to understand any coding. Sorry.. I should probably left the bit out where "count" is used. Please see the code below that has more detail filled in. This is also attached to this post. Hopefully the code will speak for itself. If still not.. please ask. Knowing my skills as programmer, I would advise you to check and test the code. My first versions of anything I design is full of bugs. [code]FUNCTION_BLOCK "MovingAverage" TITLE = Calculating average over a sample { S7_Optimized_Access := 'TRUE' } AUTHOR : WilliamB FAMILY : TOOLS VERSION : 0.01 VAR_INPUT RESTART { S7_HMI_Accessible := 'False'; S7_HMI_Visible := 'False'} : Bool; // Restart data recording and zero values TRIGGER { S7_HMI_Accessible := 'False'; S7_HMI_Visible := 'False'} : Bool; // Trigger next recording and calculate next average READING { S7_HMI_Accessible := 'False'; S7_HMI_Visible := 'False'} : DInt; // Reading needing to be averaged END_VAR VAR_OUTPUT FILTERED { S7_HMI_Accessible := 'False'; S7_HMI_Visible := 'False'} : Bool; // Filtered reading value END_VAR VAR count { S7_HMI_Accessible := 'False'; S7_HMI_Visible := 'False'} : USInt; // Counter keeps track of values recorded.. useful when less than maximum index { S7_HMI_Accessible := 'False'; S7_HMI_Visible := 'False'} : USInt; // Next read/write position in the buffer total { S7_HMI_Accessible := 'False'; S7_HMI_Visible := 'False'} : DInt; // Sum of the recorded values average { S7_HMI_Accessible := 'False'; S7_HMI_Visible := 'False'} : DInt; // Running average output buffer { S7_HMI_Accessible := 'False'; S7_HMI_Visible := 'False'} : Array[1..10] of DInt; // Buffered values; Circular buffer END_VAR VAR CONSTANT index_max : USInt := 10; END_VAR BEGIN // CLIENT...Siemens A&D Forum, User: Control Boy // DATE.....31 MAY 2014 // PURPOSE..USE CIRCULAR BUFFER TO CALCULATE A MOVING AVERAGE. // AUTHOR...William B. // Clear all relevant values. IF #RESTART OR #index < 1 THEN #count := 0; #index := 1; //..buffer is from 1 to 10 #total := 0; #average := 0; FILL_BLK(IN := 0, //clear all buffer values COUNT := #index_max, OUT => #buffer[1]); END_IF; // cancel further actions if next recording not triggered. IF NOT #TRIGGER THEN #FILTERED := #average; //ensure there is an output RETURN; END_IF; // Track values already recoded, if less than array maximum after RESTART IF #count < #index_max THEN #count := #count + 1; // ..always: count <= index_max END_IF; // Calculate the new average. // Remember buffer and total was seeded to zero values at RESTART #total := #total - #buffer[#index] + #READING; // ..oldest buffered value now removed #average := #total / #count; // Absorb a new reading into the circular buffer #buffer[#index] := #READING; //ensure circular action #index := #index + 1; IF #index > #index_max THEN #index := 1; END_IF; #FILTERED := #average; END_FUNCTION_BLOCK [/code] Is this of value to anyone? AttachmentFB1050_MovingAverage 2.zip (469 Downloads) |
Last edited by: William B. at: 5/31/2014 11:21 PMversion 2 of code..previously the output did not receive a value. |
|
This contribution was helpful to
3 thankful Users |
Follow us on