0% found this document useful (0 votes)
61 views12 pages

Time Domain Based Active Power Calculation

This application note describes the active energy calculation method used for single phase power meter based on the 8-bit microcontroller. Disclaimer: I do not have any of the rights of the paper and it may not be use for private profit activities, only educational.

Uploaded by

Frank Cifuentes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views12 pages

Time Domain Based Active Power Calculation

This application note describes the active energy calculation method used for single phase power meter based on the 8-bit microcontroller. Disclaimer: I do not have any of the rights of the paper and it may not be use for private profit activities, only educational.

Uploaded by

Frank Cifuentes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Freescale Semiconductor Document Number:AN4504

Application Note Rev. 0, 09/2012

Time Domain Based Active Power


Calculation
by: Radomir Kozub
Roznov pod Radhostem
Czech Republic

Contents

1 Introduction 1 Introduction................................................................1

This application note describes the active energy calculation 2 Basic Math................................................................1
method used for single phase power meter based on the 8-bit 2.1 Active, reactive, apparent, and total
microcontroller. It will discuss: apparent power...............................................1
• The mathematical background for energy calculation in 2.2 Basic math – discrete form............................4
the time domain
• Different phenomena affecting calculation accuracy and 3 Active Power Accuracy Affecting
how to deal with them Phenomena.................................................................5
• The calculation algorithm used in the power meter by 3.1 Sampling frequency.......................................5
LH60 Single Phase Power Meter Reference Design
(DRM133) 3.2 Zero cross detection.......................................5
3.3 Variable type used.........................................5
The described calculation method is suitable for active energy
calculation for power meters compliant with EN50470. 3.4 Calculation granularity..................................6

Active energy calculation method is suitable for power meters 3.5 Offset removal...............................................7
which are suitable to measure (5)60A current range with B 3.5.1 High pass filter.................................7
class accuracy.
3.5.2 Fixed value subtraction. . .................7
3.5.3 Signal mean value
calculation and removal..................7
2 Basic Math 3.6 Phase error correction.....................................8
4 Calculation Flow Diagram.........................................9

© 2012 Freescale Semiconductor, Inc.


Basic Math

2.1 Active, reactive, apparent, and total apparent power


Active power calculation in electric systems are easy to implement, but there are some topics worth discussing, starting with
some background math.
Active power [W] is equal to voltage multiplied by the working component of the current:

Figure 1. Equation 1
Reactive power [VAR] is equal to the voltage multiplied by the magnetizing (reactive) current:

Figure 2. Equation 2
Apparent power [VA] is equal to the vector sum of active and reactive power, otherwise known as the triangle method, as
shown in Figure 4.

Figure 3. Equation 3

S
Q

φ
P

Figure 4. Apparent power calculation – triangle method


Total apparent power is the product of the RMS voltage and RMS current:

Figure 5. Total apparent power


In a pure sinusoidal system with no higher harmonics, the apparent power is equal to total apparent power. After harmonics
are encountered in the system, then vector sum loose accuracy.
Active energy is the sum of active power in time:

Figure 6. Equation 4

Figure 7. Equation 5
Reactive energy is the sum of reactive power in time:

Time Domain Based Active Power Calculation, Rev. 0, 09/2012


2 Freescale Semiconductor, Inc.
Basic Math

Figure 8. Equation 6

Figure 9. Equation 7
While the theory is now clear, there are some differences in real system situations. Due to various factors, we are not working
with accurate voltage and current signals u(t) and i(t).

N
Cc Vref/2

Rdiv
voltage signal u

Rdiv Vref/2 ADC


M
L_OUT U
current signal i X
Rshunt OA trigger

L_INP
I load

Figure 10. Voltage and current conditional circuits and connection to the MCU
The signal measured by the ADC is influenced by many errors. For simplicity, we omit error variation during temperature,
aging, and so on here. The dominant errors in power meter signal condition circuits are signal offset, signal gain, and phase
shift. Equations for real life voltage and current signals can be stated as follows:

Figure 11. Equation 8

Figure 12. Equation 9


Where:
• i(t), u(t) - signal equivalent to current and voltage which is sampled by ADC
• I0, U0 - offset error , typically caused on signal biasing error (Vref/2), thermal offset of operational amplifier (OA), AD
converter offset, resistors thermal shift
• Gi, Gu – gain error, usually due to OA gain error due to component variation and thermal shift, AD converter gain
error, reference error. For our purposes, the gain error also includes signal “scaling factor”.
• φCc – phase error introduced on Compensation capacitor (Cc)
• φie , φue – phase error due to shunt resistor impedance, time lag in OA, parasitic impedances on the resistors used

If we put real voltage and current signals to Figure 1 and derive it, the result is much more complex and dependent on other
parasitic parameters, like offset and phase error.

Time Domain Based Active Power Calculation, Rev. 0, 09/2012


Freescale Semiconductor, Inc. 3
Basic Math

Figure 13. Equation 10

Figure 14. Equation 11


As shown in Figure 13, offset and phase error must be removed to get a correct power value. The following assumptions can
be made:
1. Mains voltage has umean = 0 by nature, therefore we can determine and remove voltage signal offset U0 , so that the
resulting signal has U0=0. Signal offset is equal to signal mean value.
2. Actual active power is calculated on sine-wave borders where the following, Figure 15, is true:

Figure 15. Equation 12


Using previous assumptions, we can simplify Figure 14 to the following form:

Figure 16. Equation 13


We can start the calculation on the signal phase of our choice, therefore we can do the following:
• Subtract φie from both signals, the frequency is the same
• Use φe = φue + φCc - φie
• Substitute the gain factor G = GI*GU

Figure 17. Equation 14


In the calculation method proposed above, we must make the following assumptions to reach an accurate power and
consecutively energy value:
• The voltage signal offset U0 must be determined and then removed from signal samples to have umean = 0
• Phase error φe must be compensated
• Instantaneous power oscillates around its accurate value, and provides accurate value only on waveform border:

• Gain and scaling factors G must be set during calibration

2.2 Basic math – discrete form


In discrete form, we can rewrite Figure 17 using the same assumptions as in Active, reactive, apparent, and total apparent
power. Power is the sum of products of voltage and current samples scaled to accurate value using gain factor G.

Figure 18. Equation 15

Time Domain Based Active Power Calculation, Rev. 0, 09/2012


4 Freescale Semiconductor, Inc.
Active Power Accuracy Affecting Phenomena

Figure 19. Equation 16


Samples uk, ik must have offset and phase error removed. Energy is then the endless sum of power.

3 Active Power Accuracy Affecting Phenomena

3.1 Sampling frequency


Figure 15 shows that power calculation is accurate only on the waveform border. Otherwise, instantaneous active power
value oscillates around its real value. In the discrete calculation, the sampling window must be aligned to the mains sine
wave. This leads the calculation algorithm to use variable sampling rate to have sampling frequency as multiples of the actual
mains frequency. In our design, we have 64 samples per one mains sine wave. Therefore, the sampling frequency is 3200
samples/sec for 50 Hz mains frequency and changes as mains vary around 50 Hz.
We also synchronize sampling window to the mains sine wave phase, so the sampling window starts on 90° or 270°. Then we
can halve the sine wave into two intervals which also fulfills the assumption of Figure 15.

Figure 20. Equation 17

Figure 21. Equation 18

3.2 Zero cross detection


Sampling frequency must be synchronized with frequency of the mains as treated in the previous section. The mains voltage
signal is compared against a certain level on the comparator. The comparator output provides a signal whose edges are
measured, and time information is used for ASC sampling frequency control. This ensures that the sampling window is
aligned with the mains voltage signal.

3.3 Variable type used


The platform (core) used must also be considered based on:
• Its computational power
• Influence to the choose algorithm for active power calculation
• How many bits must be used in calculation to maintain accuracy over the required dynamic range

This application note describes active power calculation on an 8-bit platform, so the native variable length is 8 bits. Using
wider variables increases the computationL power needed for arithmetic operations, which results in a higher CPU load,
therefore using adequate variable length is essential. The AD converter provides 16-bit long result, so by multiplying current
and voltage, we have a 32-bit wide result. If we multiply the 32-bit result with the 16-bit wide gain factor G, the result will be
48 bits, a length which is not supported in the C compiler. We must find a scale factor and keep the variables used for
calculation in a reasonable format.

Time Domain Based Active Power Calculation, Rev. 0, 09/2012


Freescale Semiconductor, Inc. 5
Active Power Accuracy Affecting Phenomena

Here we are referring to 230 V at 5(60)B power meter and from the following table we see the current signal dynamic range
where we have to provide accurate power calculation:
Table 1. Power meter dynamic range
Istart Imin Itransition Ireference Imax
Current [A] 0.02 0.25 0.5 5 60
Accuracy [%] One pulse in 30 1.5 1 1 1
min

The dynamic range needed with 1.5% accuracy for a worst case scenario is 60:0.25 = 240:1. But if we want to have, for
example, approximately 2% accuracy for Istart, where dynamic range is 60:0.02 -> 3000:1, which is not mandatory by
regulations, we need at least 6 bits for beginning -> 32*3000 = 96000 -> approximately 17 bits. This leads to a 24-bit wide
variable to work with for active power calculation.
Although C compilers for an 8-bit core support only 16- or 32-bit long variables, it can be useful to build your own 24-bit
long math libraries to lower the CPU load. Optimized ASM mathematic routines may also be more effective. For the
algorithm, we must consider that if we use a high sampling frequency, calculation may eat significant CPU power only for
energy calculation.
Table 2 shows the CPU cycles needed for 16- and 32-bit standard C library multiplication, 32*32 to 64 bit assembly-
optimized function, and CPU load related for 3200 multiplications per second, which is actual power meter ADC sampling
rate. CPU is running 19.2 MHz bus clock.
Table 2. CPU load for various mathematic routines
19.92 MHz CPU @ 3200 Cycles / multiplication Cycles / sec @ 3200 CPU load [%]
samples/s
16*16 -> 16 @ C lib 220 704000 ~3.5
32*32 -> 32 @ C lib 620 1984000 ~10
32*32->64 @ASM optimized 635 2032000 ~10.2
32+32-> @ 32 C lib 229 732800 ~3.6

Table 2 shows that just one simple multiplication may take around 10% of CPU power, therefore we have to write code
cautiously. A twice as wide calculation by optimized ASM routine may take a similar length of time as 32*32->32.

3.4 Calculation granularity


Computational power can also be saved by higher power calculation granularity, which is how often the active power is
calculated and energy counters / LED pulse output are updated.
1. Figure 18 shows that gain factor G is multiplied with sum each n samples. We can determine optimal value n when the
sum of product of samples of voltage and current is multiplied with gain factor G. We can theoretically choose a value
from interval <1, ∞>. In this case, a larger n value leads to a lower CPU load. Choosing n=1 will lead to high CPU
load, while energy counters will not be updated when n = ∞ .

2. Figure 15 shows that a suitable value of n may be on the borders of the sine wave to fulfill the assumption. The
sampling rate used in LH60 Single Phase Power Meter Reference Design (DRM133) gives us 64 samples per sine
wave, which makes n = 64 a good option.

Time Domain Based Active Power Calculation, Rev. 0, 09/2012


6 Freescale Semiconductor, Inc.
Active Power Accuracy Affecting Phenomena

3. Calculation granularity also affects the time of periodic energy counter update and related power LED pulse jitter. The
LED pulse output was used for accuracy testing previously. The n=64 interval means that the LED output is updated
every 20 mS in the worst case signal jitter scenario. If the desired power meter accuracy step is 0.1%, we must measure
for at least 20 seconds to negate jitter error. Finally, we proposed n = 32 with related half wave measure interval
wherethe assumptions of Figure 20 and Figure 21 are fulfilled as well.

Half sine wave granularity n=32 for power and energy calculation is the optimal compromise of CPU computational power
demand and power pulse LED signal jitter.

3.5 Offset removal


To remove signal offset, there are many aspects we must consider. Offset may be removed using special hardware to have a
signal without offset error. A software-based solution such as applying a high-pass filter, or simply subtracting fixed offset
Umean from the signal can also be used. Umean can also be determined from voltage signal samples.
Because of the cost of hardware solutions, we will deal only with software methods of removing offset.

3.5.1 High pass filter


IIR high pass filter is one option for removing offset from the voltage signal. Let’s try to implement a simple first order filter
with a -3 dB cutoff frequency of 0.15 Hz and sampling frequency 3200 s/sec. Such a filter has the following parameters:

For the filter to work without significant errors for small signals, coefficients (a, b) and result y must be in 32-bit arithmetic.
This leads to the two multiplications x*b and y*a [32*32->32] and two sum operations [32+32->32] for each sample. This
leads to 1698 cycles per sample, which is the same as 30% of CPU computational power. We must filter both current and
voltage samples to make the total CPU load 60% for offset removal only.
Using ASM and making some changes, we can get lower CPU load, but still it is not an acceptable CPU load. Therefore,
using an FIR filter on 8-bit core is not optimal for 3200 samples/sec.

3.5.2 Fixed value subtraction


We can also subtract a fixed value from each sample. The fixed value can be determined during the calibration process. This
method is sensitive to the thermal input offset drift on operational amplifier and causes significant errors.

3.5.3 Signal mean value calculation and removal


Determining signal mean value Umean and subtracting this value from the incoming samples is the proposed for removing
offset in the actual design. For the harmonic voltage signal, we know from Figure 15 that we expect mean value to be zero on
the waveform border.

Time Domain Based Active Power Calculation, Rev. 0, 09/2012


Freescale Semiconductor, Inc. 7
Active Power Accuracy Affecting Phenomena

On real samples, we get a non-zero value over sine wave period, which is equivalent to mean value. In design, we typically
average this value over several seconds. As a result, we have the voltage mean value, which is subtracted from incoming
samples.
This method provides a precise, adaptive method for removing offset together with light CPU load.

3.6 Phase error correction


For Figure 17, we must have voltage and current signals phase-aligned. This means that for pure active load, both the voltage
and current signal and its samples must have zero phase shift. In real application, both signals have phase errors as shown in
Figure 11 and Figure 12.
However, the LH64 only has asingle AD converter, therefore we cannot sample voltage and current signal simultaneously.
See Figure 22 to see the time lag (tl) between the voltage and current sample.
The tl between two samples of current and voltage in time domain may be transformed to signal phase shift in frequency
domain. For example, 1.8 degree equals approximately 100 us for a 50 Hz signal.
The tl between voltage and current signal sample may be used to remove phase error φue, φie in Figure 16.

voltage sample current sample voltage sample


tcu tci t

tv
ts

Figure 22. ADC current and voltage samples


The ADC sampling time is approximately 100 uS for voltage sample “tcu” which equals approximately 1.8 degree at 50 Hz.
Therefore, the current signal has to be shifted by at least 1.8 degrees to be sampled in correct time/phase. Signal errors φue,
φie are much smaller than 1.8 degree, therefore we can add an additional phase shift on capacitor C in the voltage signal to
increase the time between the sample of voltage and current to the level acceptable by the AD converter. For the path, see
Figure 23.

DC BIAS 0.6V

I
R1
C
R2
N VOLTAGE

Un Uo

GNDA GNDA

Figure 23. Voltage conditional circuit schematic

Time Domain Based Active Power Calculation, Rev. 0, 09/2012


8 Freescale Semiconductor, Inc.
Calculation Flow Diagram

UR1

Un
UR2
φCc

UC

Figure 24. Voltage conditional circuit phasor


CTRL+U
By controlling tl time via software, we can then a potom
compensate unicode
phase error 03b1
between pozor
signals of voltage and current.
numericka klavesnice nefunguje

4 Calculation Flow Diagram


Now we can see the actual active power calculation diagram used in the LH60 Single Phase Power Meter Reference Design
(DRM133). In the following diagram, we calculate more parameters, such as RMS voltage, RMS current, and reactive power.
However, those parameters calculation are out of scope of this document.
Steps 1 and 2 on the calculation diagram in Figure 25 are performed with each incoming sample, so each is approximately
312 µs. At the beginning, we have two 16-bit samples, unsigned voltage sample and signed current sample:
• We subtract offsets from both voltage and current samples. Voltage sample is transformed to signed.
• Voltage and current samples are summed 64 times – one complete sine wave for following offsets calculation.
• Urms value calculation is done on the upper 8 bits of the voltage sample. We throw away the lower 8 bits to lower the
CPU load, square it, and accumulate for 32 samples – half sine wave.
• Voltage and current samples are multiplied and accumulated 32 times – therefore our calculation granularity is ½ of the
sine wave, approximately 10 ms at 50 Hz.
• Voltage samples are derivative and the result is multiplied with current signal sample and summed 32 times to get
reactive power. Reactive power is used only for informative readings and does not need to be accurate.

Steps 3-6 are performed over 32 samples, so every 10 mS.


• Voltage and current offset value is either increased or decreased depending on its polarity. This controls offset value on
the level, ensuring that the correct offset is removed from the voltage and current sample.
• For active/reactive power calculation, we subtract and save the lowest 16 bits to lower the actual value range. Then,
depending on the psFiltOn variable state, 32-taps average filter is applied to smooth instantaneous power variation.
• Step calibration is done in the following step. We typically apply gain G on the signal. Gain is set during calibration
process. We can also apply offset value to power, if needed.
• The signal out of the filter is adjusted by the factor of actual sampling rate, to compensate for sampling rate variation,
and added to the energy counter and energy pulse generator.
• Power samples are summed approximately a hundred times a second for energy showed on the display.
• Urms value is summed over one second.

Steps 7-10 are performed once per second and are referred to values showed on the display:
• Depending on the pdFiltOn variable, active and reactive power samples are averaged on the four taps average filter.
• Active and reactive power values are updated on the display.
• Apparent power is calculated using the triangle method from active and reactive power.

Time Domain Based Active Power Calculation, Rev. 0, 09/2012


Freescale Semiconductor, Inc. 9
Calculation Flow Diagram

• Urms value is also calibrated and updated on the display.


• Irms value is determined based on apparent power and Urms, and updated on the display.

Time Domain Based Active Power Calculation, Rev. 0, 09/2012


10 Freescale Semiconductor, Inc.
Calculation Flow Diagram

current sample i mains voltage sample u

smp.i smp.uu
16b, s 16b, u

i.offset u.offset
1. - -

smp.i smp.u
16b, s 16b, s

2. Σ (i)
64
Σ
32
u*i DERIV. Σ
32
u*i Σ (u)
64
Σ (u/2^3)2
32
smp.q smp.p
40b, s 40b, s

+ +
Σi Σu
positiv negativ reminder reminder positiv negativ

16b, s 16b, s
3. s.qRem s.pRem
s.qRem % 2^16 s.pRem % 2^16
i.offset -- i.offset ++ s.qRem / 2^16 s.pRem / 2^16 u.offset -- u.offset ++
s.q s.p
32b, s 32b, s

psFiltOn psFiltOn smp.uRms


4. AVERAGE AVERAGE

s.p

5. CALIB. CALIB.

s.q 32b, s
32b, s s.p

frequency
Σ Σ s.p Σ
update
6. s.q calibration pulses s.uRms
100 100 100
s.uRms
s.qdisp s.pdisp
32b, s 32b, s

update
7. AVERAGE AVERAGE
counters

s.qdispf s.pdispf

pdFiltOn pdFiltOn
CALIB.

8.
S = P^2 * Q^2
32b, s 32b, s 32b, s 32b, s

Irms = S / Urms
9.
d.q d.s d.p cnt.ep d.iRms d.uRms

10. Q S P Ep Irms Urms

Figure 25. Active power calculation

Time Domain Based Active Power Calculation, Rev. 0, 09/2012


Freescale Semiconductor, Inc. 11
How to Reach Us: Information in this document is provided solely to enable system and software
implementers to use Freescale Semiconductors products. There are no express or implied
Home Page: copyright licenses granted hereunder to design or fabricate any integrated circuits or
www.freescale.com integrated circuits based on the information in this document.
Freescale Semiconductor reserves the right to make changes without further notice to any
Web Support: products herein. Freescale Semiconductor makes no warranty, representation, or
http://www.freescale.com/support guarantee regarding the suitability of its products for any particular purpose, nor does
Freescale Semiconductor assume any liability arising out of the application or use of any
USA/Europe or Locations Not Listed: product or circuit, and specifically disclaims any liability, including without limitation
Freescale Semiconductor consequential or incidental damages. "Typical" parameters that may be provided in
Technical Information Center, EL516 Freescale Semiconductor data sheets and/or specifications can and do vary in different
applications and actual performance may vary over time. All operating parameters,
2100 East Elliot Road
including "Typicals", must be validated for each customer application by customer's
Tempe, Arizona 85284 technical experts. Freescale Semiconductor does not convey any license under its patent
+1-800-521-6274 or +1-480-768-2130 rights nor the rights of others. Freescale Semiconductor products are not designed,
www.freescale.com/support intended, or authorized for use as components in systems intended for surgical implant
into the body, or other applications intended to support or sustain life, or for any other
Europe, Middle East, and Africa: application in which failure of the Freescale Semiconductor product could create a
Freescale Halbleiter Deutschland GmbH situation where personal injury or death may occur. Should Buyer purchase or use
Technical Information Center Freescale Semiconductor products for any such unintended or unauthorized application,
Buyer shall indemnify Freescale Semiconductor and its officers, employees, subsidiaries,
Schatzbogen 7 affiliates, and distributors harmless against all claims, costs, damages, and expenses, and
81829 Muenchen, Germany reasonable attorney fees arising out of, directly or indirectly, any claim of personal injury
+44 1296 380 456 (English) or death associated with such unintended or unauthorized use, even if such claims alleges
+46 8 52200080 (English) that Freescale Semiconductor was negligent regarding the design or manufacture of
the part.
+49 89 92103 559 (German)
+33 1 69 35 48 48 (French) RoHS-compliant and/or Pb-free versions of Freescale products have the functionality and
electrical characteristics as their non-RoHS-complaint and/or non-Pb-free counterparts.
www.freescale.com/support
For further information, see http://www.freescale.com or contact your Freescale
Japan: sales representative.
Freescale Semiconductor Japan Ltd. For information on Freescale's Environmental Products program, go to
Headquarters http://www.freescale.com/epp.
ARCO Tower 15F Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc.
1-8-1, Shimo-Meguro, Meguro-ku, All other product or service names are the property of their respective owners.
Tokyo 153-0064 © 2012 Freescale Semiconductor, Inc.
Japan
0120 191014 or +81 3 5437 9125
[email protected]

Asia/Pacific:
Freescale Semiconductor China Ltd.
Exchange Building 23F
No. 118 Jianguo Road
Chaoyang District
Beijing 100022
China
+86 10 5879 8000
[email protected]

Document Number: AN4504


Rev. 0, 09/2012

You might also like