Yet another energy logger (with a division problem)

wez

New Member
Hello!

I am trying out the Picaxe for a energy meter project that I have been working on for a couple of years. My last version
was based on an Atmel Mega32, but I wanted to test the Picaxe to see if I could speed up my very limited development time.
So far I am pretty impressed with the Picaxe and I found it extremely easy to get started. So, for simple, small microcontroller projects I
will absolutely use Picaxe in the future, but for my energy meter project I am a bit uncertain if I have to roll back to my Atmel since I am
planning to do a lot of additional features to the logger.

I am now using a 28x2 and counting pulses with a hardware interrupt which is working great up to at least 50Hz (I tested it by pulsing thousands of series of
100 pulses @ 50Hz and it worked for hours without missing a pulse). The only thing to look out for is not to use any commands that takes longer
than 1/frequency seconds, so for example, I have to send serial data one byte at a time.

I am now displaying total logged Watt*hour and instant power consumption in Watt.
The instant power consumption I calculate by counting pulses for 10 seconds and multiplying it with 36 (my meter gives 10000 pulses / kWh).
It is OK, I tested it by shutting down all electricity in my house and turned 60W lightbulbs on to check. It seems like it is accurate.
This method gives me +-36watt accuracy (depending on missed pulses within the 10 second window) and it is quite slow so
I tested to measure the distance between pulses with pulsin.
This worked very well and I am able to get a instant power consumption reading for every pulse.
However, I did not get the Picaxe to calculate the result so I had to use a PC to calculate and display the Watt-reading.

My question is:
Is there any way of calculating 1/x*3600 where x is the measured pulselength (0-65535) * 10us (without using a math processor).

Best Regards,
Andreas
 

nick12ab

Senior Member
Is there any way of calculating 1/x*3600 where x is the measured pulselength (0-65535) * 10us (without using a math processor).
The PICAXE cannot do decimal math, only integer math so you can't directly calculate the reciprocal like that as you'll either get a 1 or a 0 depending on the value of x. However, instead you CAN do this: 100/x*36. It still won't give you decimals, but it is much better than a simple 1 or 0.
 

Armp

Senior Member
My question is:
Is there any way of calculating 1/x*3600 where x is the measured pulselength (0-65535) * 10us (without using a math processor).
As nick indicated there is no native PICAXE operator that will do what you (and I) want. There are however a number of algorithms that can be implemented to give the desired result.

I tried to implement Golovchenko's method http://www.piclist.com/techref/microchip/math/div/24by16.htm but never quite got there.

Hippy suggested in http://www.picaxeforum.co.uk/showthread.php?19833-Expressing-X-Y-as-a-Percentage post #3
I think you can do a division of 32-bit by 16-bit in just 16 loops maximum and that's probably what I'd use if no "maths wizard" stepped forward with an answer. There should be a number of examples on the web and it should just be a case of converting to PICAXE basic, but that may be easier said than done.
It was, and unfortunately no such 'wizard' stepped forward. I was able to get 'good enough' results by careful scaling and multiplying. Don't know if a similar approach would work for you though?

I'll watch with interest to see if any solutions surface this time around.
 
Last edited:

Armp

Senior Member
PICAXE Division with Scaling

If I understand your meter correctly the instantaneous power is given by

Pwatts = 360 / PulseWidth(secs)

Pulsin gives time in units of 10^-5 secs. 65535 or 655mS corresponds to ~550W, and 1800 (1.8mS)corresponds to 20Kw.

So Pwatts = (360 / PulWid) * 10^5

The trick is to scale the numerator to <= 16bits, and the denominator about 8bits. Then we'll get about 8 bits in the answer.

Step 1: Pwatts = ( 36000/PulWid ) *1000​

Step 2: While PulWid > 256*Sqrt(2) Divide PulWid by 2 and keep track of what we've done​

Step 3: Calculate Pwr = 36000/NewPW * (1000/Mult)​


Fine tune to reduce round off and we get:

Pwatts = ( 35157/PulWid ) *(1024/Scale) ' 35157 = 36000*1000/1024; scale is 2^N

Here's the code:

Code:
Symbol  PulseWidth = w10  ' This would be from Pulsin
Symbol  Scale = w11 
Symbol  Power = w12

' Test Cases:
'PulseWidth = 2345  ' gives 15232 watts.  15351 with calculator
PulseWidth = 65535 ' gives   548 watts.    550 with calculator
'PulseWidth = 3600 '  gives  9984 watts.  10000 with calculator
'PulseWidth = 1800 '  gives 19968 watts.  20000 with calculator

Scale = 1024 

Do while PulseWidth > 362  ' 256 * Sqrt(2)
PulseWidth = PulseWidth/2
Scale = Scale /2
Loop

Power = 35157/PulseWidth * Scale  ' In Watts
I hope this helps and is not too confusing - even if my assumptions are incorrect....
 

wez

New Member
Hi Armp!

Thank you very much, your assumptions were correct and I just tested it on my system and it works as it should!
Now I am going to set up a real time clock and the V-drive 2 and start logging. After christmas I will get a couple
of Zigbee modules so I can set up the link to my computer to start drawing some graphs..

I will post some updates when I have gotten the system up and running!

Best Regards,
Andreas
 

wez

New Member
Aargh... I toasted the DS1307 yesterday evening/night... (somehow I read that it was a 1302 and got the wiring all mixed up...)
now I have to wait for another one.
In the meantime I will try to get the Vdrive2 working...
It seems like I will be using 2 or 3 processors in this project since I have to have a reliable pulse counter and a separate processor to measure
the temperatures (I read that the 1-wire routines messes with the hardware serial communication due to the fact that the processor slows down to 4MHz during 1-wire comm.)
 

wez

New Member
Now I have a beta version of the new system up and running. I´m using two picaxe shields with 28x2 processors, a VDrive2 and a AXE033 display/clock.
I am using one shield as a i2c slave and it is basically working as a pulse counter and in the future it will recieve messages from a PC/Mac dashboard program.
The master shield is doing all the work and I am using the algorithm provided by Armp to display the instant power. It is working great and updates very quickly (<0.5 seconds).

I´m having some problems with the i2c communication to the display though. It sometimes mixes up the cursor position and it is quite annoying.
The VDrive2 communication also seems to be quite unstable at the moment. However, I installed a 8MHz (28x2 running at 32MHz) resonator and I am hoping that it will help.

I'm also planning to test the pulse counter by bursting pulses half the length of the energy meter pulses and at double speed of the maximum pulse interval to see if it misses any pulses.
The most important test of the system will be to have it count kWh for one month and compare it to the electricity bill.

Last night I logged the energy consumption and plotted a graph in excel, but I had to do quite a bit of cleaning up before I could use the data due to the problems with the VDrive2 communication.
But I got a nice graph with a log entry every 10 seconds. I could clearly see my heat pump defrost periods and the breakfast preparation... it would be really nice to have this system logging all year
around, it would be nice to be able to analyze and improve the energy efficiency of the house.
I am thinking of analyzing the usage of my fireplace, total power consumption with light bulbs on (since our central heating system recycles heat produced inside the house it might not make any difference
if I´m using a lot of lightbulbs in the winter), the total consumption if I would have the house a few degrees colder when we are not at home etc.

I´ll post some updates when I have done further testing.

Andreas
 

westaust55

Moderator
It seems like I will be using 2 or 3 processors in this project since I have to have a reliable pulse counter and a separate processor to measure
the temperatures (I read that the 1-wire routines messes with the hardware serial communication due to the fact that the processor slows down to 4MHz during 1-wire comm.)
Yes the PICAXE does reduce speed while performing 1-Wire commands including the READTEMP commands.

How many temperature sensors do you need to read?

If just a few then a smaller PICAXE and using the READTEMP/READTEMP12 commands may suffice. the PICAXE cannot perform other commands for the 750 ms while the READTEMP command is being performed.

If there are many temp sensors or you wish to have them all on 1 PICAXE (input + output capable) IO pin then a 1-Wire network can be used with an X1 and X2 PICAXE part.
The 1-Wire commands do give you the opportunity to perform other commands during the period (up to 750ms) while the DS18B20 temp sensor is performing the temperature conversion.
 

wez

New Member
Hi again,

Thanks westaust55 for the input, I will probably try to get the master 28x2 to perform readings from 3 temp sensors. I also need to slow down the processor my self to
measure a pulse width so the poor processor will be jumping between frequencies quite frequent...
 

wez

New Member
After a lot of fighting with the VDrive2 (it had an old firmware) I got the first energy log last night.

Here is what it looks like:

PowerConsumption.jpg
 
Top