Followers 0

# Calculate expression for a balloon

## 23 posts in this topic

Hi all, I am looking to do the following calculation, it harks back to the problem I was having with trying to compensate the bouncing in the Analogue signal coming back from my Airanger unit. Anyway I have never done a calculation expression like this before, so any help would be most grateful. V=Pi/6*H(3(r2-(H-6.605)2 +3*b2 + H2)) With the following meaning V= Volume Pi = Pi i.e. 3.14159 Anywhere I have a 2 this means I am trying to square this, so r2 = r squared, b2 is b squared, (H - 6.605)2 is this squared and H2 is H squared. I have never done a calculation of squaring or used Pi either. I can clarify anything if needed. I have been given this formula by an Engineer and asked to write it into my PLC. This project is on a PLC5. Thanks, Conor

##### Share on other sites
I have not used PLC5, but can you move the value 3.14159 to a register and use a reference to this in your calculations? For squaring, you can you multiply the value by itself? (3 x 3 = 3 squared) I would try and split the equation into smaller sections...

##### Share on other sites
Let V = F8:0 , PI = F8:1 , H= F8:2 ; r = F8:3 and b = F8:4 and in a PLC 5 the following CPT will work CPT F8:0 ((F8:1 | 6.0) * F8:2) * (3.0 * ((((F8:3 * F8:3) - ((F8:2 - 6.605) * 2.0)) + ((3.0 * F8:4) * F8:4)) + (F8:2 * F8:2))) this was in a PLC 5/40 enhanced series A. NOTE : CAVEAT - I wrote the code and there were no errors - I not sure the PLC order of operations will give you the correct results without some more parentheses. USE WITH CAUTION.

##### Share on other sites
Just to add to this, there is an operator to do an exponential expression (x**y) see pictures below

##### Share on other sites
Applying Mickey's Post you get CPT F8:0 ((F8:1 | 6.0) * F8:2) * (3.0 * ((((F8:3 **2 ) - ((F8:2 - 6.605) * 2.0)) + (3.0 * F8:4**2) + (F8:2 **2))) this was in a PLC 5/40 enhanced series A. NOTE : CAVEAT - I wrote the code and there were no errors - I not sure the PLC order of operations will give you the correct results without some more parentheses. USE WITH CAUTION.

##### Share on other sites
Hi guys, Thank you all for your replys. I will try out your code on Monday, as it is late on Friday (after 8pm) here, and I have left for the weekend (hopefully). I will let you all know on Mon how it goes.

##### Share on other sites
X * X will execute considerably faster (as much as 20x faster) than X**2.

##### Share on other sites
From watching the forums I have come to the conclusion that it is impossible to train everybody. What we did is check the exponent. If the exponent is a constant integer then we translate x**2 to x*x. x**3 gets translated to x*x*x. x^4 gets translated to (x*x)^2 which gets translated to xx*xx which takes only 2 multiplies. A smart compiler should do this. It doesn't take much effort. Now the question is what did Rockwell do? You should always avoid divides by a constant. It is much faster to multiply by a constant. On our processors it is about 4 to 5 times faster to multiply than divide. We got smart here to and translate dividing by a constant to multiplying by (1/constant). Edited by Peter Nachtwey

##### Share on other sites
If I read your expression correctly, it should be possible to reduce it to: 1.570795 * H * ( 3 * b * b + r * r - 13.21 * H + 43.626) where 1.570795 is (Pi / 6 ) * 3 or Pi / 2 13.21 is 2 * 6.605 43.626 is 6.605 ** 2 which would involve only 6 multipies and 3 adds, no great challenge for a PLC-5 Of course, if any of H, b or r is a constant value, you could also calculate them by hand as well.

##### Share on other sites
You forgot a H^2 term but otherwise that is why the engineer owes someone lunch for doing his job.

##### Share on other sites
Hi guys, I am going to try this out now, but I can clarify, that all values except H are constant. As you all have stated, I will try and reduce this calculation down as much as possible, to help my PLC. Conor

##### Share on other sites
Hi guys, I have written this into code and it is working. As stated before, I did some of the calculations and put them into spare F8's that I had. It does simplify the calculation. I have it added to our Scada now, so I can trend one against the other (original vs new volume) Conor

##### Share on other sites
Unless I've blown it somewhere, I think the H2 term cancels out: -(H - 6.605)**2 + H**2 = - (H**2 - 2 * 6.605 + 6.605**2) + H**2 = -H**2 + 2 * 6.605 - 6.605 **2 + H**2 If r and b are both constants, it should be possible to reduce this whole mess to the form A * H * (1 + B * H) One needs to know the values of r and b to calculate A and B of course. Edited by DwalterE

##### Share on other sites
Hi Dwalter, Can you explain a little bit please. A= ? B= ?

##### Share on other sites
I believe he's replacing r*r with A and b*b with B.

##### Share on other sites
Get wxMaxima from sourceforge. It can simply very complicated expressions. The equation above is simple but the engineer that came up with that formula needs it. http://sourceforge.net/projects/maxima/files/

##### Share on other sites
How does multiplying X by 1/N stack up against dividing by N when both X and N are integers since 1/N forces a floating point calculation? I'm not a guru on the inner tickings of the processor (I promptly forgot most of the processor design lectures because I was never the least bit interested in that aspect of EE) however I suspect some FP processors might do it pretty dang fast. But would the FP operation be faster than integer division?

##### Share on other sites
FP division is often just as fast or faster than integer division. Integer division requires a cycle for each shift and subtract. FP divide often calculated using multiplies and Newtons method. Our DSP doesn't have a floating point divide. It simulates a divide using a multiplies.

##### Share on other sites
/* [wxMaxima: input start ] */ (%pi/6)*H*(3*(r*r-(H-6.605)*(H-6.605) +3*b*b + H*H)); float(%); expand(%); collectterms(%,H); grind(%); /* [wxMaxima: input end ] */ 20.75021947696058*H^2+(1.570796326794897*r^2+4.71238898038469*b^2-68.52759982266234)*H\$

##### Share on other sites
1. The PLC-5 is an interpreted system. There is no compiler. Everything is tokenized. So your statement that "a smart compiler should do this" doesn't apply with an interpeter. Or at least not with a PLC-5. They just took the brute force tokenized language approach. There is little optimization at all. 2. As far as the MUL/DIV/ADD (as in doubling can be done with an ADD) normally I'd agree, partly because I know exactly how it has to be implemented in the ALU. However with a PLC-5, again, this is not the case. MUL/DIV are the same speed. It seems ludicrous in my mind that this is the case since a Booth multiplier takes up a lot of silicon real estate and is far slower than an ADD while a DIV is best implemented with an inversion which takes a long time (O(N log N) or there abouts vs. O(1)!) but hey, it is what it is.

##### Share on other sites
The PLC 5 uses a 68000 and I don't think is has a math coprocessor so there is no booth algorithm. The overhead of the interpreter probably dwarfs the difference in execution time difference between a floating point MUL and DIV.