Author Topic: Volume calculation Integer to Dec?  (Read 14047 times)

Petty_boy

  • Jr. Member
  • **
  • Posts: 53
  • I'm a llama!
    • View Profile
Volume calculation Integer to Dec?
« on: May 08, 2009, 08:39:09 AM »
Hi,

I'm trying to run some simple calculations through the PLC using the memory space and I'm not getting the correct values back.

Calc:-

DM[20] = ((DM[18]*DM[18])*(3142/1000)) * DM[19]
DM[21] = DM[20] / 1000

'  DM[18] = RADIUS IN CM                     (4)
'  DM[19] = HEIGHT IN CM                     (10)
'  DM[21] = VOLUME IN LITRES
 
' VOLUME = pi * (r * r) * h / 1000 = LITRES

The answer on my calculator is:-

(4 X 4) * 3.142 * 10 = 502.72

/ 1000 = 0.50727 L

I know the DM[XX] are Integers that can only display whole numbers.

I guess my question would be how can I convert the above into Decimal and can I display this in the data memory?

Many thanks

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:Volume calculation Integer to Dec?
« Reply #1 on: May 08, 2009, 01:58:06 PM »
You can use "Fixed point" representation. E.g. 1 digit = 0.001

So 12345 means 1234.5

  D = (DM[18]*DM[18])*3142* DM[19]

DM[] are 16-bit variables so only can represent -32768 to +32767. It will not be able to display 50272. These must be contained in a 32-bit variable such as A to Z or EMLIN[1] to EMLINT[16]

If you want to use DM[], the use 1 digit = 0.01 so the expression will be:

  D = (DM[18]*DM[18])*314* DM[19]

The result would be 5027 which represents 502.7
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

Petty_boy

  • Jr. Member
  • **
  • Posts: 53
  • I'm a llama!
    • View Profile
Re:Volume calculation Integer to Dec?
« Reply #2 on: May 13, 2009, 03:19:32 PM »
Hi,

still struggling with these calcs!

I'm trying to calculate a pump actual and total flow in litres / second.

first I have to convert the pump data from m3/h to L/s,  to do this you divide the m3/h by 3.6.  Then its just a mater of  multiplying the answer by the pump operation time.

I then need to add the calculated running flows together to a total flow in litres.

my code below.

DM[23]= DM[22] / (36/10) ' PUMP FLOW RATE IN M3/H / 3.6

DM[24] = DM[23] + DM[24] ' = TOTALISER UPDATED EVERY 1 SECOND.

i.e 3 (m3/h) / 3.6 = 0.83 L/S

X 10 seconds = 8.3 litres total.

The trouble is due to the fact 0.83 is not an Integer and is rounded to 1,  now my pump flow is 1 L/s.

an you provide an example using the "Fixed point" representation described above.





support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:Volume calculation Integer to Dec?
« Reply #3 on: May 13, 2009, 09:23:18 PM »
If you use 1 digit = 0.01, then data in DM[22] needs to multiply by 100 (provided it does not exceed 32767) to represent the fixed point value.  So if instead of DM[22]=3, put DM[22]= 300.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

Petty_boy

  • Jr. Member
  • **
  • Posts: 53
  • I'm a llama!
    • View Profile
Re:Volume calculation Integer to Dec?
« Reply #4 on: May 14, 2009, 02:30:42 AM »
OK,

Thanks.

cdenk

  • Full Member
  • Posts: 161
  • newbie
    • View Profile
Re:Volume calculation Integer to Dec?
« Reply #5 on: May 20, 2009, 12:31:22 PM »
To put in a little different words. One needs to calculate and store all numbers as whole integers, including the early arithmetic operation results (division in the middle of the equation) as multiples of some constant like 10, 100, etc. over the full range of expected values. Then when displaying, divide by the constant. When used in formulas, and boolean, one needs to remember that the number stored is multiplied by some constant.  Here are some copy/paste from my code:

 Note the value of "G" the fuel level is multiplied by 10.

IF (TESTIO(GAS_PUMP) = 0) and (G <= 106) and (TESTIO(HIGH_LIMT) = 1)
   SETIO GAS_PUMP
ENDIF

--I really want to turn on the pump when "G" is less than 10.6
--and when displaying the value of "G" to 1/10 gallon, the code looks like:

SETLCD 2,1, "GASOLINE " + STR$(G/10,2) + "." + STR$(G MOD 10,1) +" GALLON"

Petty_boy

  • Jr. Member
  • **
  • Posts: 53
  • I'm a llama!
    • View Profile
Re:Volume calculation Integer to Dec?
« Reply #6 on: May 20, 2009, 03:42:45 PM »
Thanks for the extra information.

I come from a control background "after the PLC" so getting to grips with the basics.

Cheers