The while loop

Why is the calcVI function always fed with 20 and 2000? In other words what does int crossings and int timeout signify in the while loop shown below? And what will be the value of int numberOfSamples? 

 

while ((crossCount < crossings) && ((millis()-start)<timeout))
  {
    numberOfSamples++;

.

.

.

}

calypso_rae's picture

Re: The while loop

The '20' is the number of half-cycles that are measured, so that equates to 10 whole cycles of the AC mains supply.

The '2000' is the timeout period in milliseconds.

So this routine normally runs for 10 whole cycles of the AC voltage signal plus some time at the start for initialisation.  If it fails to terminate for any reason (maybe there is no AC signal), the routine will abort after 2 seconds.

yhabib's picture

Re: The while loop

Its still so confusing. I am printing a value every 5 seconds as shown below, am i still taking 20 half cycles (meaning at the end of the while loop as shown above, int numberOfSamples=20)??

void loop()
{
  unsigned long time = millis();

  if (time - lasttime >= 5000)
  {
    lasttime = time;

  
    port.calcVI(20,2000);

Serial.print(port.Real_Power)

}

Robert Wall's picture

Re: The while loop

Yes, you sample 20 half-cycles every 5 seconds. We assume nothing big will change in that time, or if it does - say a big load comes on - then the error will tend to average out when the load goes off.

The name of the demonstration sketch that uses this method is "emonTxV3_RFM12B_DiscreteSampling.ino"  The adjective 'discrete' means

1.
apart or detached from others; separate; distinct: "six discrete parts".
2.
consisting of or characterized by distinct or individual parts; discontinuous.

and that accurately describes how the section of code you quoted works.

yhabib's picture

Re: The while loop

So the sampling frequency in this case is 4 Hz since i am taking 20 samples over a period of 5 seconds or 4 samples in 1 second??

Robert Wall's picture

Re: The while loop

No. You are taking about 50 sample pairs per cycle over 10 cycles - about 500 sample pairs - and calculating the average and printing it. You then go to sleep and wake up 5 seconds later to do it all over again.

(By 'sample pairs' I mean a voltage and a current.)

yhabib's picture

Re: The while loop

Let me put it this way for my understanding.

The while loop runs 500 times, each time taking instantaneous value of current and voltage and then calculate their average by dividing their total with 500 (since int numberOfSamples = 500) ? Please correct me if i am wrong.

If thats the case then what is my sampling frequency? I was asked by someone and i had no answer to it. I really need to understand the code.

Robert Wall's picture

Re: The while loop

"The while loop runs 500 times, each time taking instantaneous value of current and voltage and then calculate their average by dividing their total with 500 (since int numberOfSamples = 500) ? Please correct me if i am wrong."

You are right, but where I think you are confused is here: there is not one sampling frequency, there are two.

The 'fast' sampling frequency is within emonLib in calcVI( ) and that is the one that runs for 20 half-cycles. It goes fast enough to be able to accurately measure all the important harmonics in the voltage and current waves and accurately calculates the average power during those 20 half-cycles. That is the one you are describing.

The 'slow' sampling frequency - one sample every 5 seconds - is controlled by the main loop of your sketch. That runs the fast samples in calcVI( ), which samples the average power for 200 ms (in the 50 Hz world), every 5 seconds.

The code is two nested loops. The outer loop (stripped down to the minimum, and not strictly accurate) is

loop( )
{
    every 5 s
        calcVI( )
        print power
}

Similarly, the inner loop is

calcVI( )
{
    while (< 20 half-cycles)
         measure V & I
    calculate average power
}

If you wish to measure the power continuously, look at one of the 'continuous' sketches. But I warn you, they are very complicated.

 

calypso_rae's picture

Re: The while loop

If you wish to measure the power continuously, look at one of the 'continuous' sketches. But I warn you, they are very complicated.

Nah, there are plenty of experts now in this field Robert. 

IMHO, "continuous" monitoring is the only way to go :D

To understand how it works, I would recommend studying  Mk2_PV_Router_mini_3.ino,  this code is available at http://openenergymonitor.org/emon/sites/default/files/Mk2_PV_Router_mini_3.ino.zip

No library files are used, and it's all floating point maths.  Voltage x Current gives the instantaneous power (instP).  These instP values are summed during each mains cycle to give average power.  To make the Router work, these power values are converted to energy and accumulated, but that's not part of the measurement side.

yhabib's picture

Re: The while loop

So here in UK, we get 50 cycles of voltage and current every second. The while loop only considers 10 cycles from these 50 cycles each second. And from these 10 cycles, it samples a total of 500 samples of instantaneous voltage and current each. It calculates instantaneous power each time, doing it 500 times and then calculates the real power by dividing their total with 500. So practically we are taking 500 samples each second which sets our fast sampling frequency at 500 Hz. 

calypso_rae's picture

Re: The while loop

So here in UK, we get 50 cycles of voltage and current every second.  Yes 

The while loop only considers 10 cycles from these 50 cycles each second.  No.  The while loop runs for ten mains cycles and then stops until it is next called upon to run

And from these 10 cycles, it samples a total of 500 samples of instantaneous voltage and current each. It calculates instantaneous power each time, doing it 500 times and then calculates the real power by dividing their total with 500.  Yes, the sampling period lasts for approx 0.2 seconds whenever it is called upon to run.

So practically we are taking 500 samples each second which sets our fast sampling frequency at 500 Hz.  Not quite.  During the first 0.2 seconds, we take enough samples to calculate the average power.  Then, the system waits until it is called upon to take the next set of measurements.

Hope this helps.

yhabib's picture

Re: The while loop

The while loop only considers 10 cycles from these 50 cycles each second.  NoThe while loop runs for ten mains cycles and then stops until it is next called upon to run. Isn't my statement same as yours? All i am saying is that each time the while loop is called, it runs for 10 mains cycles out of the 50 mains cycles available each second. 

So practically we are taking 500 samples each second which sets our fast sampling frequency at 500 HzNot quite.  During the first 0.2 seconds, we take enough samples to calculate the average power.  Then, the system waits until it is called upon to take the next set of measurements. How many are these 'enough' samples? My supervisor asked me the sampling frequency and i had absolutely no idea. Please tell me what my sampling frequency is? In other words, how many samples i am taking each second?

Sorry for bothering you guys again and again :(

Robert Wall's picture

Re: The while loop

"Isn't my statement same as yours?" No, because your statement to me implies that you will sample 10 cycles out of the first 50, then 10 out of the next fifty, and so on, which is not the case in your sketch.

"My supervisor asked me the sampling frequency and i had absolutely no idea. Please tell me what my sampling frequency is? In other words, how many samples i am taking each second?"   There is no simple answer to that. You must explain that there are two sampling rates, as I tried to explain to you several posts above: You sample voltage and current at around 2500 times per second to calculate power, but you only do that for 200 ms in every 5, or 10 seconds, or however often you want, depending on what your needs are.

But if you are not happy with sampling power every few seconds, change to one of the "continuous" sketches, which does exactly as it says. It measures power continuously and reports the average over the preceding interval every second, sampling at around 2500 sample pairs per second.

calypso_rae's picture

Re: The while loop

The "continuous code" that I have posted was developed for my Mk2 PV Router project.  This application needs to have a rapid response time, so it's no good having to wait until a lengthy measurement window has run its course.  When  someone turns the kettle on, the control system needs to stop diverting surplus power very quickly, otherwise the user will be charged by their meter for the energy that is consumed from the grid.

To provide a rapid response time, the measurement window in my Router code has been shortened to the duration of a single mains cycle.  The average power is calculated every mains cycle, and this knowledge is used to updated an energy bucket.  The objective is to match how the meter works, and this simple approach seems to work well.

With the "continuous code" that has been posted, the average power is only transmitted occasionally.  So although the results are accurate (because the 2500 Hz sampling rate is maintained throughout), the response time is poor because the results are only displayed or transmitted every few seconds.

Power meters and energy meters are like the speedo and mile-ometer in a car.  The speedo shows the speed of motion at all times whereas the mile-ometer shows the total distance travelled.  Each meter is useful for its own purpose.  The questions "how fast am I going", and "how far have I travelled", can only be answered easily by consulting the relevant meter. 

If you can explain exactly what you are trying to achieve, we may be able to help you better.  What hardware are you using, or is this just a theoretical investigation?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.