Internet PLC Forum

General => Technical support => Topic started by: montyk on April 23, 2010, 12:42:01 PM

Title: RMS Calculation
Post by: montyk on April 23, 2010, 12:42:01 PM
I need to do the RMS calculation on the ADC inputs but I can not figure out the best way to calculate a square root.
Title: Re:RMS Calculation
Post by: garysdickinson on April 23, 2010, 02:37:34 PM
Scroll down to the end for a brute force integer square root algorithm.

I have a couple of questions that you need to answer when considering RMS calculations use a TriLog PLC:

The reason I ask the first question is that if you are trying to look at an input that changes quickly (50/60Hz) the PLC may not be fast enough or able to sample consistently enough to get data that you can use for your computations.

The reason I ask the second question is simple: if the input waveform has a predictable "shape" you don't need to do a formal RMS calculation, just a simple multiply of a constant.

The third question is a test.  If you don't know why I asked this question, you don't need to compute RMS values nor do you need a square root algorithm.


OK, as promised a brute force algorithm





' Integer Square Root algorithm
'
' X is argument
'
' Temp variables used:
'   T temporary root
'   A temporary add
'   B temporary result
'
' On Exit
'   R is calculated square root
'
R = 0
A = 32768

WHILE (A)
    T = R + A
    B = T * T
    IF (B <= X)
        R = T
    ENDIF
    A = A/2
ENDWHILE


[/color]

Good luck,

Gary
Title: Re:RMS Calculation
Post by: support on April 23, 2010, 03:29:53 PM
since the M and Nano PLC do not support floating point, the result can only be approximated using an algorithm.

A sample .PC6 program that can compute the square root of a number with 2 decimal places and return the square root in one decimal place can be downloaded from following URL:

http://www.tri-plc.com/trilogi/SquareRoot.zip

It is a dumb method but will work if you don't need to do intensive square root computation at every scan. It is presented as a function that you can call from your program.
Title: Re:RMS Calculation
Post by: montyk on April 27, 2010, 05:49:50 AM
I am using the MD888 as a controller for a PWM power supply with voltage, current and feedback from a sensor to control the output.
1. Frequencey is 63 Hz
2. Square Wave
3. DC Output.
Title: Re:RMS Calculation
Post by: garysdickinson on April 27, 2010, 11:07:10 AM
What are you trying to measure with the MD888?  I'm confused.


I'm assuming that you are trying to measure some analog signal from a sensor.

If the frequency of this signal is "63 Hz" and does not vary.  Then the frequency carries no information.

If the signal is always a square wave then the duty cycle is constant and carries no information.

If the signal has a "DC output", does this mean that the amplitude of the 63 Hz square wave is what you want to measure?

If the signal amplitude is of interest I'd suggest one of two approaches:


Gary D.

Title: Re:RMS Calculation
Post by: montyk on April 27, 2010, 01:44:09 PM
I am using the MD888 to control the output by varying the duty cycle. I am monitouring the voltage and the current (using a 5A @ 100mV shunt) to maintain a constant current output but also to make sure that the voltage does not exceed a set limit.