Need Urgent help on Tweet-A-Watt project

Xbee projects like the adapter, xbee tutorials, tweetawatt/wattcher, etc. purchased at Adafruit

Moderators: adafruit_support_bill, adafruit

Need Urgent help on Tweet-A-Watt project

Postby ForeverSNSD » Tue Nov 08, 2011 10:12 pm

I am on the part of the Twee-a-Watt project where Limor parses her data and normalizes the ADC reads she gets for her voltage and current values. This is the code she uses.

[Edit - moderator - use code block]
Code: Select all
#!/usr/bin/env python
import serial
from xbee import xbee

SERIALPORT = "COM4"    # the com/serial port the XBee is connected to
BAUDRATE = 9600      # the baud rate we talk to the xbee
CURRENTSENSE = 4       # which XBee ADC has current draw data
VOLTSENSE = 0          # which XBee ADC has mains voltage data

# open up the FTDI serial port to get data transmitted to xbee
ser = serial.Serial(SERIALPORT, BAUDRATE)
ser.open()

while True:
    # grab one packet from the xbee, or timeout
    packet = xbee.find_packet(ser)
    if packet:
        xb = xbee(packet)

        #print xb
        # we'll only store n-1 samples since the first one is usually messed up
        voltagedata = [-1] * (len(xb.analog_samples) - 1)
        ampdata = [-1] * (len(xb.analog_samples ) -1)
        # grab 1 thru n of the ADC readings, referencing the ADC constants
        # and store them in nice little arrays
        for i in range(len(voltagedata)):
            voltagedata[i] = xb.analog_samples[i+1][VOLTSENSE]
            ampdata[i] = xb.analog_samples[i+1][CURRENTSENSE]
           
        # get max and min voltage and normalize the curve to '0'
        # to make the graph 'AC coupled' / signed
        min_v = 1024     # XBee ADC is 10 bits, so max value is 1023
        max_v = 0
        for i in range(len(voltagedata)):
            if (min_v > voltagedata[i]):
                min_v = voltagedata[i]
            if (max_v < voltagedata[i]):
                max_v = voltagedata[i]

        # figure out the 'average' of the max and min readings
        avgv = (max_v + min_v) / 2
        # also calculate the peak to peak measurements
        vpp =  max_v-min_v

        for i in range(len(voltagedata)):
            #remove 'dc bias', which we call the average read
            voltagedata[i] -= avgv
            # We know that the mains voltage is 120Vrms = +-170Vpp
            voltagedata[i] = (voltagedata[i] * MAINSVPP) / vpp

        # normalize current readings to amperes
        for i in range(len(ampdata)):
            # VREF is the hardcoded 'DC bias' value, its
            # about 492 but would be nice if we could somehow
            # get this data once in a while maybe using xbeeAPI
            ampdata[i] -= VREF
            # the CURRENTNORM is our normalizing constant
            # that converts the ADC reading to Amperes
            ampdata[i] /= CURRENTNORM

        print "Voltage, in volts: ", voltagedata
        print "Current, in amps:  ", ampdata

I have some questions, for example, this part

for i in range(len(voltagedata)):
            if (min_v > voltagedata[i]):
                min_v = voltagedata[i]
            if (max_v < voltagedata[i]):
                max_v = voltagedata[i]

It seems like it is just looking at one ADC voltage reading at a time, and in that case, wouldn't that number be both the min and the max according to that code. Or is it taking the first reading, then comparing it to the 2nd reading, then comparing the 2nd to the 3rd and on. This part confuses me because I am not sure what the functions "range" and "len" do.

Another question is the MAINSVPP, is that just the wall outlets VPP 340V?

And then the VREF, am I just using 492 for this?
And my last question is the CURRENTNORM which it says it is the normalizing constant, but how do I get the value for this constant.
If someone can explain the process this whole code goes through, it would be great!! Thank you.
ForeverSNSD
 
Posts: 15
Joined: Tue Nov 01, 2011 1:10 am

Re: Need Urgent help on Tweet-A-Watt project

Postby BruceF » Tue Nov 08, 2011 10:49 pm

Firstly, you should put that big chunk of code in (code) (/code) blocks when you post it here. (but with square brackets, not round ones. Just use the buttons above the comment edit window until you get the hang of it.)

Secondly:
ForeverSNSD wrote:This part confuses me because I am not sure what the functions "range" and "len" do.


See range() and len().

So if voltagedata is an array of 10 values, for instance, len(voltagedata) will return 10, and range(10) will return the set of numbers between 0 and 9. The 'for' loop will then be executed 10 times, with 'i' being 0, 1, 2, ... to 9, and max_v and min_v will pick up the max and min values out of the array, just like it says on the label.

I can't answer all your tweet-a-watt questions, but I thought I'd try to get you started since you said it's urgent.
- Bruce
BruceF
 
Posts: 185
Joined: Tue May 03, 2011 3:51 pm

Re: Need Urgent help on Tweet-A-Watt project

Postby adafruit_support_bill » Wed Nov 09, 2011 5:54 am

The voltage and current constants are for scaling the ADC readings. The circuitry scales the measured voltage to 0-VREF (the voltage reference of the ADC) A 10-bit ADC will give you a value between 0 and 1023. The other constants are used to scale that 0-1023 value back into actual voltage and current units.
User avatar
adafruit_support_bill
 
Posts: 16607
Joined: Sat Feb 07, 2009 9:11 am

Re: Need Urgent help on Tweet-A-Watt project

Postby ForeverSNSD » Thu Nov 10, 2011 5:12 pm

Thanks for the responses. I actually have a few other questions about the data after it has been "normalized". After parsing the data and normalizing the voltage and current on the tweet-a-watt project, I was wondering if I took all the samples of voltages and currents, and found the RMS value of each, it would be the same value I would be seeing on the Kill-a-watt's LCD of the appliance that is plugged in. So for example, if I had nothing plugged in to the Kill-a-watt and it is just simply plugged into the Kill-a-watt, using the normalized current and voltage data, if I find the RMS values of each, it should come out to 120V and 0 A, which is what the LCD displays with nothing plugged in, correct?
ForeverSNSD
 
Posts: 15
Joined: Tue Nov 01, 2011 1:10 am

Re: Need Urgent help on Tweet-A-Watt project

Postby adafruit_support_bill » Thu Nov 10, 2011 9:40 pm

Ideally, yes. Some constants may need tweaking in order to more precisely calibrate to your Kill-a-watt unit.
User avatar
adafruit_support_bill
 
Posts: 16607
Joined: Sat Feb 07, 2009 9:11 am

Re: Need Urgent help on Tweet-A-Watt project

Postby ForeverSNSD » Mon Nov 14, 2011 1:11 am

I pretty much got everything working, the voltages seem to be right, when I take the RMS value of the voltage samples, i get around 120. But my current RMS is a little off, it is not as close to 0 as I would want. And I think this is a calibration problem and not making 498 my 0 current point. I am looking at Limor's instructions and I am lost at this point.

Code: Select all
See how the Amp line (green) is steady but its not at zero, its at 0.4 amps? There is a 'DC offset' of 0.4 amps

OK, open up the wattcher.py script in a text editor.

    vrefcalibration = [492,  # Calibration for sensor #0
            492,  # Calibration for sensor #1
            489,  # Calibration for sensor #2
            492,  # Calibration for sensor #3
            501,  # Calibration for sensor #4
            493]  # etc... approx ((2.4v * (10Ko/14.7Ko)) / 3

See the line that says # Calibration for sensor #1? Change that to 498

    vrefcalibration = [492,  # Calibration for sensor #0
            498,  # Calibration for sensor #1
            489,  # Calibration for sensor #2
            492,  # Calibration for sensor #3
            501,  # Calibration for sensor #4
            493]  # etc... approx ((2.4v * (10Ko/14.7Ko)) / 3

Save the file and start up the script again, this time without the -d


I don't see any of this in the watcher.py code at all, How am I supposed to change the Vref, or calibrate the current so it knows 498 is the 0 current.
Also, what does this equation mean 2.4v * (10Ko/14.7Ko)) / 3 in the code, where does the 3 come from. One last question is the 15.5 for currentnorm constant, if I wanted to get this value myself through tests, how would I go about doing so, I know this is what Limor got, but is it okay to just use that value or should I calibrate my own value.
ForeverSNSD
 
Posts: 15
Joined: Tue Nov 01, 2011 1:10 am

Re: Need Urgent help on Tweet-A-Watt project

Postby adafruit_support_bill » Mon Nov 14, 2011 6:52 am

How am I supposed to change the Vref, or calibrate the current so it knows 498 is the 0 current.

498 was what Limor used to correct for the 0.4A offset. You need to calculate the correction for the offset you are seeing. How far off is it?
User avatar
adafruit_support_bill
 
Posts: 16607
Joined: Sat Feb 07, 2009 9:11 am

Re: Need Urgent help on Tweet-A-Watt project

Postby ForeverSNSD » Mon Nov 14, 2011 4:49 pm

With no appliance plugged in, mine doesn't really stay exactly at 498 like Limor's data. It jumps from like 495- 505 which is still decent, but when I normalize the data to get the instantaneous current of each sample, it is probably like 0.5-0.6A off 0, some even higher. How do I get this number to not jump so much? So how do I set a certain number like 498 or the average of my samples and make that number the Vref (or 0 current value).

Limor says to change the code for this part.
Code: Select all
vrefcalibration = [492,  # Calibration for sensor #0
            492,  # Calibration for sensor #1
            489,  # Calibration for sensor #2
            492,  # Calibration for sensor #3
            501,  # Calibration for sensor #4
            493]  # etc... approx ((2.4v * (10Ko/14.7Ko)) / 3


But I don't really understand this code or see it in her watcher.py anywhere. Is this all I have to do to calibrate the offset? Why is there 4 sensors, I don't get this part.
Also this equation, 2.4v * (10Ko/14.7Ko)) / 3, I don't get what it is for, where does the 3 come from too.
ForeverSNSD
 
Posts: 15
Joined: Tue Nov 01, 2011 1:10 am

Re: Need Urgent help on Tweet-A-Watt project

Postby adafruit_support_bill » Mon Nov 14, 2011 8:22 pm

If the number is not stable, you probably want to take an average of some samples over time.

But I don't really understand this code or see it in her watcher.py anywhere. Why is there 4 sensors,

It starts on line 25 of watcher.py. You can edit this to have as many (or as few) entries as you have xbee-equipped sensors.

2.4v * (10Ko/14.7Ko)) / 3

That is an approximate conversion between volts and raw-counts from the ADC. If your no-load average is 500, use 500.
User avatar
adafruit_support_bill
 
Posts: 16607
Joined: Sat Feb 07, 2009 9:11 am

Re: Need Urgent help on Tweet-A-Watt project

Postby ForeverSNSD » Tue Nov 15, 2011 3:30 pm

K everything seems to look good now. One last question, the 15.5 currentnorm constant, I know it is the conversion factor from ADC to amperes but how exactly did Limor get this number. If I wanted to get this constant myself through calibration, how would I go about doing so?
ForeverSNSD
 
Posts: 15
Joined: Tue Nov 01, 2011 1:10 am

Re: Need Urgent help on Tweet-A-Watt project

Postby adafruit_support_bill » Tue Nov 15, 2011 4:51 pm

I don't know how Limor arrived at that number exactly, but a lot of reverse engineering is experimentation and observation. One way is to apply a known load to the system and observe the change in raw counts. From that you can derive a scaling factor.
User avatar
adafruit_support_bill
 
Posts: 16607
Joined: Sat Feb 07, 2009 9:11 am

Re: Need Urgent help on Tweet-A-Watt project

Postby ForeverSNSD » Tue Nov 15, 2011 11:32 pm

I understand. For some of the samples for instantaneous voltages and currents, sometimes one is negative and the other isn't, there can't be a negative power draw, so can I just multiply those ones by -1 to turn them into a positive number.

Another question I have is that my current ADC values seem to jump a bit, from 480-505, with no appliance plugged into the KAW, is there any other ways I can eliminate the noise that is causing this, or I am not sure why it is not constant at 498 like Limor's data.
ForeverSNSD
 
Posts: 15
Joined: Tue Nov 01, 2011 1:10 am

Re: Need Urgent help on Tweet-A-Watt project

Postby adafruit_support_bill » Wed Nov 16, 2011 6:31 am

so can I just multiply those ones by -1 to turn them into a positive number.

yes.

is there any other ways I can eliminate the noise that is causing this

Noise can be difficult to diagnose. Looking at it with an oscilloscope sometimes can give you clues as to the source.
User avatar
adafruit_support_bill
 
Posts: 16607
Joined: Sat Feb 07, 2009 9:11 am


Return to XBee products from Adafruit

Who is online

Users browsing this forum: No registered users and 1 guest

Stuff to buy from the Adafruit store and links to product documentation!


New Products [113]

Raspberry Pi[81]
 
FLORA[24]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[12]
Arduino[60]
 
NETduino[14]
 
BeagleBone[23]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[39]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[9]


 
Breakout Boards[35]
LCDs & Displays[49]
Components & Parts[70]
Batteries & Power[54]
EL Wire/Tape/Panel[52]
LEDs[112]
 
Wireless[16]
Cables[66]
 
Lasers[6]
Sensors/Parts[147]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[41]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[25]


 
Stickers[41]
 
Skill badges[55]
 
Books[26]
 
Circuit Playground[7]
 
Gift Certificates[4]