Square rooting a signal

thech0sen

Member
Join Date
Oct 2009
Location
Europe
Posts
2
Hello

I have a flow control loop, using flow-orifice and dp-cell for measure the flow. The cell is very old and dont have squaring.

So i wanted to ask how should i do the math in the PLC program to square the signal?

Im using an analog input card where i take the signal in as an integer, and then using a scaling block to scale it to 0-100 REAL

And from there im stuck, how do i get the squaring correct?
 
Hello

I have a flow control loop, using flow-orifice and dp-cell for measure the flow. The cell is very old and dont have squaring.

So i wanted to ask how should i do the math in the PLC program to square the signal?

Im using an analog input card where i take the signal in as an integer, and then using a scaling block to scale it to 0-100 REAL

And from there im stuck, how do i get the squaring correct?

The first obvious question would be which type of PLC?
Obvious answer to square question, SQUARE = INPUT_REAL * INPUT_REAL, assuming your PLC has floating point math capabilities
or are you asking about square root?

Perhaps look at this
 
Last edited:
n=0
while N>0
N=N-2*n+1
N=N+1 <- was this supposed to be lowercase 'n'?
wend

How can you have 18? Shouldn't 2*n+1 always be odd?

I like it, very easy for a limited processor. It only has one multiply and no divides.
What does it do with non-perfect squares? It can't come up with a fractional part. Though that may not be relevant in this case.

The Newton method would return a fractional part but needs REAL math and has divides so much slower with the same processor.
 
Last edited:
Wow, did I ever screw that last post up!!!!!

n=0
N=N+1 <- was this supposed to be lowercase 'n'?
Yes, I made a mistake. Good catch
it should be n=n+1 and the number should be 17 instead of 18. I was posting too quickly.

What does it do with non-perfect squares? It can't come up with a fractional part. Though that may not be relevant in this case.
Not a problem. The square root of twenty is done like this
N=20*100
The answer is
r=sqrt(20)*sqrt(100) or sqrt(20)*10
So
N=2000
the square root of 2000 is 45 but now this must be divided by 10 to get the answer 4.5

I can also multiply by 10000 before the square root and divide by 100 after wards.

One our second generation, integer only, motion controller I multiplied the the integer by 65536 and took the square root. The square root of a 32 bit number is a 16 bit number but since I multiplied by 65536 before the square root, I must divide by 256 after the square root. Actually I simply used the integer in the high byte and the fraction of 256 in the lower byte.

I didn't use the 1,3,5,7 approach. I used a look up table to calculate the most significant byte of the square root. I then did one iteration using Newton's method. Since Newton's methods doubles the number if accurate bits only one iteration is required to get a 16 bit result.

I wouldn't use Newton's method on a integer only PLC. If one needs the answer that quickly then get a PLC with the square root built in.

The Newton method would return a fractional part but needs REAL math and has divides so much slower with the same processor.
You can't believe how much time I have spent optimizing the real sqrt(). No division is necessary. I have done the same for cbrt() or cube root too but that requires a divide.

BTW, the 1,3,5,7,.... technique is how the HP35 did square roots.
 
Straying off topic ... Peter - with massive memory becoming so cheap, small and less power hungry the processing by lookup table (like Gray -> binary decoding) becomes a lot faster for very little cost. Do you see many functions passing out of the computation area and into simple lookup?
 
Straying off topic ... Peter - with massive memory becoming so cheap, small and less power hungry the processing by lookup table (like Gray -> binary decoding) becomes a lot faster for very little cost. Do you see many functions passing out of the computation area and into simple lookup?
Yes, our second generation controller had a sine table that had 256 entries.
DSPs for cell phones have compression and decompression tables. Even back in the 80's we programmed tables into proms. Bit count tables, bit index tables, I think I had tables for just about anything that required bits.
 
This is off topic (pardon me), and just for curiosity.

Peter, given your low level number crunching expertise, do you know an effective way of multiplying two variables ?

I aks, because in the ancient times I had a control loop in an S5 PLC that should follow a 3d pattern. The 3d pattern gave a setpoint for an actuator. The setpoint was calculated by multiplying constants and variables (not variable * variable, only constant * variable). This was done by shifting the bitpattern and adding only. In this way the cycle time was kept reasonably low.
Siemens had library blocks for multiplying and dividing variables, but the execution time was exorbitant (20-40 milliseconds I think).
Luckily we only had to multiply constants with variable, not variable with variable.
 
This is off topic (pardon me), and just for curiosity.

Peter, given your low level number crunching expertise, do you know an effective way of multiplying two variables ?
Just shift and conditionally add, that is fairly standard for CPUs without a multipy.

Siemens had library blocks for multiplying and dividing variables, but the execution time was exorbitant (20-40 milliseconds I think).
Ouch!!!!
I know nothing about programming a S5 except that it was a not fun to program. I could write the code in S7 and you can translate the STL to S5 STL. I know that even the S7 STL code would not be efficietnt. The S7 needs more registers. Fortunately the S7 can easily shift numbers and test the carry bit.

A lot of old tricks and tips for programmng mathematically challenged CPU can be found here.
http://www.piclist.com/techref/method/math/muldiv.htm
 
The first obvious question would be which type of PLC?
Obvious answer to square question, SQUARE = INPUT_REAL * INPUT_REAL, assuming your PLC has floating point math capabilities
or are you asking about square root?

Perhaps look at this

Im using a simatic s7 313c cpu. Im talking about square rooting a signal and then scaling it
 
Just shift and conditionally add, that is fairly standard for CPUs without a multipy.
I was thinking along these lines already, but thought there was a more smarterer way.

The link is interesting. I think the "Kenyan algorithm" is what I could have used. Bit it would have taken some effort to dissect the 8080 assembler code in order to convert to S7 S5 code.
 
Last edited:
I think the "Kenyan algorithm" is what I could have used.
This makes me laugh. I don't know if this person's name is Kenyan or he is Kenyan but someone has been claiming a lot of algorithms as "Kenyan". I would like to make it clear that some of us programmed 8080s 30+ years ago and there was no mention of a "Kenyan" algorithm for multiplies and divides. I wonder where this person was 30+ years ago.

There is a CPU called the Propellor chip that has 8 cores but has no multiply and divide so it must be done with shifts and adds or shifts and subtracts. Someone on the Parallax Propeller forum is claiming the obvious as the "Kenyan" algorithm. What a joke, but it is all new to most there.

It is amazing how old stuff is rediscovered and claimed as new.

Something that is rarely mentioned on this forum is how to use the carry bit to do extended precision math. The use of the carry bit is also important for testing bits being on. It is much faster than ANDing with 1 or some bit pattern.
 

Similar Topics

here's another doozy for every one! we have a DP-cell style flow transmitter (orifice Plate). Its scaled 0-150"WC/4-20 mA. I need this to be...
Replies
4
Views
1,843
Square-D Symax SFI-324 I have used the SFI-510 card for many projects, but I came across a SFI-324. Does anyone have ant tech info on it?
Replies
0
Views
78
hi everybody i want to make a backup (upload) from a plc square d micro 1 ready i have the software WINLDR i am looking the cable to Conect to a...
Replies
1
Views
517
Hello guys, I need to understand how instructions work in Square D Symax PLC. I need to upgrade old code which is in Square D symax SCP401...
Replies
7
Views
2,362
Hello guys, I need to understand how instructions work in Square D Symax PLC. I need to upgrade old code which is in Square D symax SCP401...
Replies
0
Views
831
Back
Top Bottom