Pardon my ignorance, but why go through the trouble of performing zero crossing detection and counting cycles in the EmonTx firmware ?
I've been playing around and my setup "seems" to be giving me accurate RMS current readings by simply grabbing 4 or 5 cycles with roughly 25 samples per cycle and computing the RMS current without regards for a specific start point.
Am I missing something here ?
I'm sure there is a very good reason for doing it this way in the EmonTx firmware but I just can't figure out why.
Is my sloppy method doomed to fail somehow ?
Re: Why perform zero crossing detection and count cycles ?
The standard library has two approaches, chosen depending on whether voltage is being monitored or not. If you are measuring current only, then it's not possible to guarantee that there will be only two zero crossings, one positive-going and one negative-going, per cycle, or even that you will have any reliably detectable zero crossings at all. In this case, we average over a fixed number of samples that, taking account of the different mains frequencies and the sample rate, is a 'best guess' at a whole number of cycles. There are sufficient samples and cycles so that the overall error will be small, but not so many that a small frequency change will itself add a significant error.
In the case where the voltage is being monitored, the voltage is, as you say, checked to detect the zero crossing and then the number of crossings is counted, and an attempt is made again to end up with a whole number of complete cycles. Because the sampling rate is not absolutely fixed and not synchronised to the mains, there is inevitably still some "end effect" and the intention is for this to happen where the error is smallest, which of course is near the zero crossing.
MartinR's PLL synchronises the samples to fixed points in the cycle exactly, so an exact number of whole cycles can be measured and there is no error from 'end effect'.
Re: Why perform zero crossing detection and count cycles ?
That is a good question, and one that has been asked many times in the past. As an engineer, I can agree that in theory by measuring the signal with enough samples, the starting and ending point are not critical as long as you try to measure close to an integral number of cycles. What I have found in my testing of the various algorithms though, is that the phase locked loops of the PV router code (and various variants) and the EmonLibPro code from chaveiro, led to more accurate measurements with much less noise. The code in both of these examples tries to constantly adjust to start exactly at the zero crossing. I found that at lower ranges, the measurements bounced around a lot less, and gave more consistent results. For example, my inverter consumes a small amount of power (70W or so) all night simply powering itself. So, this reads as a negative generation on my setup (measuring current and voltage). The standard library that measured a set number of cycles would bounce between 50W and -150W, averaging to the -70W that was being consumed. The PLL code locked on and only varied +- 20W or so, instead of the +-80W or so the standard code did.
So, from my experience, the locking of the measurements to the cycles improved measurement noise levels. I have not looked back from implementing it for my setup.
Dan
Re: Why perform zero crossing detection and count cycles ?
What I didn't mention, Dan, is the library is also intended for use when the transmitter is battery-powered, and then of course power consumption has to be kept to a minimum and so the smallest number of samples consistent with adequate accuracy is taken, and the processor sleeps between measurements. If you have mains power, you can afford to monitor continuously, which is what both Martin's and Robin's sketches do; and so not only is the error when spread over a measurement period of 5 s much less than over a period of 0.2 s, any 'end effect' also carries over into the next measurement period and is incorporated into that, thus compensation is automatic. But note, Dan, Robin's "interrupt" code is not phase-locked to the mains, it free-runs but continuously and, for the non-timed versions, at maximum speed, that's around 55 sample pairs per 20 ms cycle.
Re: Why perform zero crossing detection and count cycles ?
Thanks for the clarification, I appreciate the responses.