Commit 94b10aee authored by James Pallister's avatar James Pallister

Added average voltage and current, and fixed overflow issue

parent b375f1b2
......@@ -109,6 +109,8 @@ typedef struct {
unsigned peak_voltage;
unsigned peak_current;
unsigned n_samples;
uint64_t avg_current;
uint64_t avg_voltage;
} accumulated_data;
//uint64_t energy_accum=123;
......@@ -191,8 +193,10 @@ void start_measurement()
a_data.peak_voltage = 0;
a_data.peak_current = 0;
a_data.n_samples = 0;
a_data.avg_voltage = 0;
a_data.avg_current = 0;
tperiod = 5000;
tperiod =500;
adc_power_on(ADC1);
......@@ -536,7 +540,7 @@ void process_buffer(power_data *pd)
{
unsigned short c = pd->data[i+1];
unsigned short v = pd->data[i];
unsigned short p = c*v;
unsigned p = c*v;
a_data.energy_accum += p;
pp_tot += p;
......@@ -545,6 +549,8 @@ void process_buffer(power_data *pd)
a_data.n_samples += 1;
a_data.elapsed_time += tperiod;
a_data.avg_voltage += v;
a_data.avg_current += c;
}
pp_tot /= n_samples/2;
......
......@@ -13,8 +13,8 @@ class Measurement(object):
pass
class EnergyMonitor(object):
MeasurementData = namedtuple('MeasurementData', 'energy_accum elapsed_time peak_power peak_voltage peak_current n_samples')
MeasurementData_packing = "=QQLLLL"
MeasurementData = namedtuple('MeasurementData', 'energy_accum elapsed_time peak_power peak_voltage peak_current n_samples avg_current avg_voltage')
MeasurementData_packing = "=QQLLLLQQ"
def __init__(self, serial="EE00"):
# Find the usb device that corresponds to the serial number
......@@ -33,7 +33,7 @@ class EnergyMonitor(object):
defaultparams = {'resistor':1, 'gain':50, 'vref':3}
self.measurement_params = {i : copy(defaultparams) for i in range(1,4)}
self.samplePeriod = 200
self.samplePeriod = 500
def connect(self):
self.dev.set_configuration()
......@@ -41,12 +41,20 @@ class EnergyMonitor(object):
def toggleLEDs(self):
self.dev.ctrl_transfer(0x41, 0, 0, 0, None)
def start(self):
self.dev.ctrl_transfer(0x41, 1, 0, 0, None)
def stop(self):
self.dev.ctrl_transfer(0x41, 2, 0, 0, None)
def convertData(self, md, resistor, gain, vref, samplePeriod):
en = float(vref)**2 / gain / resistor / 4096**2 * 2 * samplePeriod * 2 / 168000000. * md.energy_accum
el = md.elapsed_time * 2. / 168000000
en = float(vref)**2 / gain / resistor / 4096**2 * 2 * samplePeriod * 2 / 168000000. * md.energy_accum * 2
el = md.elapsed_time * 2. / 168000000 * 2
pp = float(vref)**2 / gain / resistor / 4096**2 * md.peak_power * 2
pv = float(vref) / 4096. * md.peak_voltage * 2
pi = float(vref) / gain / resistor / 4096. * md.peak_current
av = float(vref) / 4096. * md.avg_voltage / md.n_samples * 2
ai = float(vref) / gain / resistor / 4096. * md.avg_current / md.n_samples
print "Peak power (W):", pp
print "Peak voltage (v):", pv
......@@ -54,10 +62,12 @@ class EnergyMonitor(object):
print "Energy (J):", en
print "Time (s):", el
print "Avg Power (W):", en/el
print "Avg voltage (V):", av
print "Avg current (A):", ai
print md
def getMeasurement(self, m_point=1):
b = self.dev.ctrl_transfer(0xc1, 6, 0, 0, 32)
b = self.dev.ctrl_transfer(0xc1, 6, 0, 0, 48)
u = EnergyMonitor.MeasurementData._make(unpack(EnergyMonitor.MeasurementData_packing, b))
self.convertData(u, samplePeriod=self.samplePeriod, **self.measurement_params[m_point])
......@@ -69,8 +79,16 @@ class EnergyMonitor(object):
if __name__ == "__main__":
from time import sleep
em = EnergyMonitor("EE00")
em.connect()
em.toggleLEDs()
em.measurement_params[1]['resistor'] = 1
em.start()
sleep(1)
em.stop()
sleep(0.1)
em.getMeasurement()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment