Author Topic: RMS Calculation  (Read 7848 times)

montyk

  • Newbie
  • Posts: 11
    • View Profile
RMS Calculation
« 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.

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:RMS Calculation
« Reply #1 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:
  • How quickly does your ADC input change?
  • What is the "shape" of the input waveform?  Sine, square, triangle, random?
  • Is there you input a pure AC waveform? Is there a DC component?

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

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3171
    • View Profile
    • Internet Programmable PLCs
Re:RMS Calculation
« Reply #2 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.
« Last Edit: April 23, 2010, 03:35:34 PM by support »
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

montyk

  • Newbie
  • Posts: 11
    • View Profile
Re:RMS Calculation
« Reply #3 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.

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:RMS Calculation
« Reply #4 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:
  • Convert the AC waveform to DC with a diode.  Filter the resultant pulsing DC to remove the 63Hz components.  Scale this voltage to within the range of the MD888 ADC inputs. Use the MD888 ADC to convert the filtered DC to a digital value.
  • Add circuitry that converts the AC wave form to a 0-24V square wave.  Use the 0-24V signal to trigger an interrupt route that synchronously samples the peak AC voltage.


Gary D.


montyk

  • Newbie
  • Posts: 11
    • View Profile
Re:RMS Calculation
« Reply #5 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.