How to do complex math in S7

douyi

Member
Join Date
Aug 2005
Location
Toronto
Posts
123
I have a very complex math need to finish in S7 like:

(X1-A)*Y1+(X2-A)*Y2+(X3-A)*Y3 ... +(X20-A)*Y20

Is there some good way to do that?
Or I have to write about 20 rungs with 20 temp variables?

Thanks.
 
Douyi, if you are doing a lot of math, you should consider writing it in STL. That way, you can reduce the code by about 75% and it's much easier to follow. For instance, in STL your function would look like this (I'll assume your variables are 32 bit, but you can freely mix the data types):

L X1
L A
-D
L Y1
*D
T tempValue

L X2
L A
-D
L Y2
*D
L tempValue
+D
T tempValue

L X3
L A
-D
L Y3
*D
L tempValue
+D
T tempValue

etc, etc
By adding some comments at the end of some of the lines, you can make it so anyone can see what's going on.

Another thing to think about is maybe creating a canned function that you feed variables to. Sometimes that makes sense, but I don't really know what you are trying to do.
 
You can do it in SCL or Structured Control Language- its an add on to Step7. It comes with Step7 Professional too.

Also- your right, my Timer.zip file is corrupt. I'll send an e-mail to Phil and replace it with one that works.
 
I would go with JRW on this one (sorry, S7Guy!). This seems to be crying out for SCL.

Douyi, SCL would allow you to write your code almost exactly as a mathematical equation -
Code:
Z := (X1-A)*Y1+(X2-A)*Y2+(X3-A)*Y3 ... +(X20-A)*Y20;

Get a copy of the SCL manual from the Siemens web-site and have a look at what commands and structures are available - you'll never write maths in ladder or STL again!

Regards

Ken
 
Ken M said:
I would go with JRW on this one (sorry, S7Guy!). This seems to be crying out for SCL.

Douyi, SCL would allow you to write your code almost exactly as a mathematical equation -
Code:
Z := (X1-A)*Y1+(X2-A)*Y2+(X3-A)*Y3 ... +(X20-A)*Y20;

Get a copy of the SCL manual from the Siemens web-site and have a look at what commands and structures are available - you'll never write maths in ladder or STL again!

Regards

Ken

Since SCL costs money, this tight Scottish git doesn't have it! I've heard that the SCL compiler in its latest version is very much improved over the older versions and produces much more efficient code. If you've got a copy of SCL Ken, I'd be interested in seeing what the compiled version of the SCL code looks like compared to S7Guys version.
 
This particular example also could also use arrays, another benefit of using SCL. Please see below my implementation:

FUNCTION_BLOCK FB699

VAR_INPUT
A:REAL;
X:ARRAY[1..20] OF REAL;
Y:ARRAY[1..20] OF REAL;
END_VAR

VAR_IN_OUT
Z:REAL;
END_VAR

VAR_TEMP
i:INT;
END_VAR
Z:=0.0;
FOR i:=1 TO 20 DO
Z:=Z+((X-A)*Y);
END_FOR;
END_FUNCTION_BLOCK
 
Thanks for the PM with the STL code Simon. That certainly looks pretty clean - only 52 lines (if I've counted correctly). Surprisingly enough, it wasn't even too difficult to follow. Certainly doesn't resemble the horror stories I used to hear about SCL a few years back!
 
I would go with JRW on this one (sorry, S7Guy!). This seems to be crying out for SCL.
No no, if you have tried "reverse polish notation" on HP calculators, then it is quite easy to get into STL math.

Ok, people that havent been exposed to HP calculators are handicapped.
I still have my HP15C in my desk drawer. I would use it more if it didnt eat the batteries so quickly.
Sidenote: Does anyone know of an alternative to the Windows calculator that does RPN ?
 
Roy

I haven't seen Simon's STL code, nor do I want to. The SCL, excluding the declarations etc, comes to 4 lines of perfectly comprehensible code. I can understand it, I would know what the end purpose was, even if I hadn't seen the preceding messages. If I write a piece of code in any language, I want to be able to monitor, debug and correct it in that language, using the same thinking that created it in the first place. Sure you can look at the resulting compiled code decompiled back in to STL but what does that tell you that the SCL doesn't?

Ken
 
Thanks guys.

For the math, I don't think I have the power to convict my boss to buy a SCL now (Maybe in the furture), so I don't have a choice except STL or LAD.

Last night I wrote the whole math in LAD, I only did from 1 - 15 (which is the smallest set) and I used 43 temp variables, far more than what I thought.

Considering about STL :

L X1
L A
-D
L Y1
*D
T tempValue

L X2
L A
-D
L Y2
*D
L tempValue
+D
T tempValue

...
...
...

Here is a question:

Actually, because PLC do the math in every scan, so if I use only one temp variable as shown above, it could accumulate again and again until up to the limit, is that right?

Above all, it simplyfied at least more than 50 percent, that means I have to learn it. Do I have any shortcut in learning STL?

Thanks for help.
 
Just set your tempValue to zero first. From your code, the variables you are using are 32bit dints. Can you be sure the numbers will not overflow when all the arithmetic is done ?
 
Another good way to learn STL (IMHO) is to write the code you want in ladder and then convert it to STL (View-STL). Then read and digest.

By just converting networks of ladder into small chunks of STL it gives you a good idea of how its written.

Also bear in mind that in S7 you can highlight any command in STL and press F1 and the help file will pop up with detailed info on what that command does.

Just highlight it as though you were copying and pasteing it and press F1.

Cheers

JT
 
Originally posted by Ken M:

Sure you can look at the resulting compiled code decompiled back in to STL but what does that tell you that the SCL doesn't?

Nothing specifically about the function. However, it will give you a good idea of relative performance. And, as was already mentioned, not everyone has the SCL package. So you may be forced to look at an SCL implementation in STL.

Keith
 

Similar Topics

RSLogix 5000 V24 1756-L72 Trying to figure out the best way to solve this problem. TCF = e ˄ 1100(1/298 - 1/T) Where T = °C + 273.15 TCF...
Replies
16
Views
3,886
Hello. I'm still on my first major project in S7 and have a rather simple problem that I can't quite put into S7 terms. The problem is simple...
Replies
8
Views
2,481
Yes it's very legacy.. but sometimes it's necessary to use old stuff for fun.. and because it's so much cheaper. Crimson 3.0 had the ability to...
Replies
4
Views
1,620
Hello all, I am fairly new to programming HMI's and am working with Crimson 3.1 for the first time. I am trying to recreate an annunciator of...
Replies
12
Views
6,375
What application in your experience has demanded the most complex PLC program? I suppose I should clarify what manner "complex" I'm asking about...
Replies
37
Views
12,510
Back
Top Bottom