S7-300 Average Array Values inside of instance fb

minerman

Member
Join Date
Nov 2012
Location
Columbus, OH
Posts
82
Hello,

Using LAD, how can I generate an array of 50 numbers and then calculate an average of them?


I don't know how to define an array as a local stat variable. Also, is there an fb that I can use to calculate an average of all the elements in an array?

Edit: I guess I should be more specific with my problem. I am having trouble indirectly accessing an array element. I want to fill the array with numbers, but I can't figure out in LAD, how to go to ARRAY[my_array_element_variable].


Thanks!
 
Last edited:
Indirect access using LAD not possible - you will have to call an FC (written in STL) that will read/write your array data.
 
Thanks. I have since searched the internet and discovered that this is the only possibility.

Unfortunately I have no experience in STL, so I am trying to learn.

There is one basic thing about STL that I do not understand.

Do you have to use jumps to group commands? For example, if I want to check a bool value and if its true, set two ints to 0, is the example below valid or do I have to use a jump?

A #my_bool_value
L 0
L 0
T #my_first_int
T #my_second_int
 
Indirect access using LAD not possible - you will have to call an FC (written in STL) that will read/write your array data.
Yes, but I have a library of FCs that do simple indirect addressing.
http://deltamotion.com/peter/S7/S7ug.zip
I wrote these because a lot of my customers don't like to program in STL.

These FCs are not as efficient as doing the whole AVG FC in in STL but they are simple.
 
Thanks.

I figure I'm doing myself a favor if I learn the STL. But, I'm definitely struggling.

I don't understand why the T statement is invalid (I have already defined the ints and array of ints):
Code:
      A     #SPM_Timer_Done
      JCB   L33A
      JU    L33B

L33A: L     #S_Counter
      T     #S_Count_Array[#S_Count_Array_Pos]

L33B: NOP   0
 
Last edited:
There is no array indexing unless you use SCL

Now you are getting into the nitty gritty of assembly like programming.
Look at the STL code in my FC examples.

Edit, you should say what you are really trying to do. Filter an input? Do you get one data points to average one at a time?
You are telling me you want to climb a mountain. I can tell you how to climb the mountain but if all you are trying to do is get to the other side I may be able to show you the easy pass around the mountain.
 
Last edited:
I'm looking at your examples but I guess they're a little over my head.

Here are some questions that I have:

Code:
      L     P##paDWord  [I][COLOR="Red"]You made this an input of datatype "Pointer".  I'm trying to generate my code inside of an FB.  Datatype pointer is not an option.  Is this necessary?[/COLOR][/I]
      LAR1  
      L     W [AR1,P#0.0]  [COLOR="red"][I]This statement is kind of strange to me.  Can you explain this?  What is the purpose of the [X,Y]?  I understand that you're loading a Word, but where from?  Address Register 1 is equal to the pointer input.  Is the second Y parameter like a data offset?  Maybe not necessary if your offset is 0?  Would this statement be equivalent to L W AR1?[/I][/COLOR]
      T     #dbTemp
      OPN   DB [#dbTemp]   [COLOR="red"][I]Looks like you're modifying DB info.  This is most of the examples that I've discovered.  I'm actually trying to modify an array that is local to my FB.  Is this possible?[/I][/COLOR]
      L     #Index
      SLD   5    [COLOR="red"][I]Why are you shifting the data in your index input? [/I][/COLOR]                       // Convert index to 32 bit index
      L     D [AR1,P#2.0]
      +D    
      LAR1  
      L     #dwData
      T     DBD [AR1,P#0.0]

If I understand the code above right... The purpose of this FC is to take dwData and place it inside of a specified DB Word, at the specified index location? I still am not sure why my statement is invalid? I see the difference in your transfer code, where you specify the data type that you must transfer. I tried "T I #S_Count_Array[#S_Count_Array_Pos] and it doesn't like that either. :(

Now you are getting into the nitty gritty of assembly like programming.
Look at the STL code in my FC examples.

Edit, you should say what you are really trying to do. Filter an input? Do you get one data points to average one at a time?
You are telling me you want to climb a mountain. I can tell you how to climb the mountain but if all you are trying to do is get to the other side I may be able to show you the easy pass around the mountain.
 
OK. I think I understand my problem now.

STL will not allow you to have a variable index specified within an array. Are creating databases the only way around this? Is there a simpler solution? ;)
 
Try seance for a library from oscat.de i Think thats the name..
The library contains hundreds of pre-programmed functions like the one You search for.

Maybe i Can mail You the library?
 
Edit, you should say what you are really trying to do. Filter an input? Do you get one data points to average one at a time?
You are telling me you want to climb a mountain. I can tell you how to climb the mountain but if all you are trying to do is get to the other side I may be able to show you the easy pass around the mountain.

Peter, I just noticed your edit.

The purpose of the STL that I want to generate is the following:

Take an int every minute, and store it in an array of ints. Obtain the average values of ints.

I'm sure someone can generate an FB or FC to do that in no time. But, its important to me to understand how it is done.

I have been trying to understand for the past two days, how to move a variable into an array's index/address field. I have been told and understood that it cannot be accomplished directly, and must be done using a pointer method. But the examples I've been shown, do not make sense to me. Maybe I need an example to be fully commented to understand?

I have noticed that in your FC example, you can define a pointer data type. But, I am programming in an FB and I don't have pointer datatype as an option. I've tried storing a pointer in some other type of variable and using it:

T #My_Array[#Pointer_stored_here_in_int_form]

I have tried putting a pointer directly in the statement:

T #My_Array[P#272.0]

The only time I can get the statement to work, is with a static value.

T #My_Array[0]


I am really at a loss on why I can't seem to accomplish this. Why can't I figure out any method that makes sense to me, to have a variable index, on an array that I created in the local instance database?

I have seen examples where people have created external databases for this purpose, and I don't understand why this is necessary or how it works.

Thanks.
 

Similar Topics

Hello, I'd like to obtain average of my analog input value which will be displayed on SCADA. For examplle, analog value should be sampled avery...
Replies
2
Views
2,113
Does anyone know if there is a standard block to perform Moving Average in S7-300? I can't see one in the library, even though Siemens suggest...
Replies
13
Views
8,402
Hi everyone. I'm relative inexperienced with plc's but very curious. I'm starting to use an s7-300 siemens plc, and I'm starting to program with...
Replies
7
Views
14,179
Have a system that has been running for over a year and all of a sudden getting a ExcessiveVelocityFault on one of the drives when the MSO command...
Replies
2
Views
134
Hello PLCS.Net Forum, First time posting. Let's assume I am a novice. BASIC PROBLEM: My servo/linear piston is no longer zeroed to the...
Replies
9
Views
196
Back
Top Bottom