New power calculation for pulse meters.

Hello!

Been using the Flukso for a couple of weeks now and it works really well. I've started to experiment with the local REST API to get measurements out with 1 second resolution.
It works ok, but i think the accuracy could be improved. I'm using a 2000 pulses/kWh electricity meter and during some earlier experiments (before using flukso), I got really good results by measuring the number of milliseconds between pulses and use it as a base for power calculations.
Been reading up on the source code for the AVR controller and the flukso lua daemon, but can't really say I got it all nailed down yet. (Doesn't help that I've never written any lua scripts before.)

I was hoping to add a command to the AVR controller, sort of similar to 'gd' which would allow me to read out each pulse with a delta time in milliseconds since the last read. This would require an increase of the sample rate to at least 5Hz, since S0 pulses can come up to 5 times a second (depending on max range of meter etc)
The idea is to poll the AVR and when a new pulse is detected, the new power value is calculated in the lua daemon. A good conversation from pulse timing to a 1Hz power value takes some math but should be doable in lua. You have any pointers how one could start changing the lua code to get this done? and also any ideas on a good extension format for the SPI protocol to handle this?
The other option is to let the AVR handle the power calculation and introduce a command for reading the power values once a second. Back side of this is that the calculation might get a bit heavy for the AVR, even after converting to fixed point integers. I'm not sure how much processing power is still available without affecting interrupt timing e.t.c.
Also, a minor issue would be that the AVR clock and the OpenWRT aren't perfectly synced so some values might be dropped/duplicated once and a while.

Any ideas how to tackle this little extension to flukso?

cheers,

--- Mikael R

ragtime's picture

Hmm, been loocking a bit more on the code. Seems everything I need is already transfered to the OpenWRT and all I need to do is to change the power calculation in the lanBuffer function. I'll keep on digging...

icarus75's picture

Hello Mikael,

First of all, kudos for ploughing through the code in search of the power calculation code. Your analysis is quite correct. Here's the full story:
1/ For analog (clamp) inputs, we sample each input at 667Hz. Each second we calculate power for the analog inputs in the AVR using fixed point arithmetic. See the main.c function

  1. void calculate_power(volatile struct state_struct *pstate)

2/ For pulse inputs however, we timestamp each pulse with a millisec clock local in the AVR. We then transfer the timestamps along with the counter value to the MIPS processor. The fluksod.lua daemon processes each of these pulses and converts them to power readings in the

  1. function lan_buffer(child)

co-routine.

I could not find a good way to perform these calculations efficiently in the AVR itself. At first sight they would involve floating point maths. There probably _is_ a way to do this with fixed point arithmetic, but I haven't bothered looking very deep into it. Letting the MIPS processor do some limited (soft) floating point arithmetic seemed like the most straightforward option.

What I couldn't grasp yet from your forum post is the statement about the lack of accuracy wrt power measurements on the pulse ports. What makes you think they are inaccurate?

Cheers,
Bart.

ragtime's picture

The total amount of energy seems to be correctly measured and calculated by the system. There are 2 points I want to address:
- The AVR reports changes in Wh, so for my 2000 pulses/kWh meter, the OpenWRT will only be notified of a change every second pulse.
- The other is pure speculation, but during my own experimentation with graphs from delta times, I think I got more defined transitions in the graphs when the power load changed. Also I see some ripples in the flukso graphs during steady state that I don't recognize.

The second point could be a result from the first.
I want to be able to see a power change as soon as possible in the data. So that's why I'm trying to work on the power calculation.

I'm gonna try to add some extra code where:
1. Add a new SPI command similar to GET_DELTA but with an added field for each meter. For pulse meters, this will contain a value 0-999 with unit mWh. The values will be updated for each pulse counter tick.
2. The OpenWRT wil now see changes sooner and hopefully generate smoother graphs.

This is some work in preparation for my other project. I'm writing a small daemon in python that polls the REST interface and store values with 1 second resolution. Flukso has been the best system so far that allow me to do this at a reasonable cost and without to much hardware hacking. I plan to release it when I got something that is actually useful.

cheer,

--- Mikael R

icarus75's picture

You might consider committing your code to Github. That way people can track your progress, perhaps even help with the coding part. Just my 2 cents.