Commit f04b693f authored by James Pallister's avatar James Pallister

Added ability to retreive instantaneous value from board

parent d2dfedce
......@@ -114,6 +114,12 @@ typedef struct {
uint64_t avg_voltage;
} accumulated_data;
typedef struct {
unsigned voltage;
unsigned current;
unsigned current_time;
} instant_data;
typedef struct {
unsigned short data[PWR_SAMPLES];
unsigned short idx;
......@@ -123,6 +129,7 @@ int tperiod=500;
typedef struct {
accumulated_data accum_data;
instant_data id;
power_data data_bufs[NUM_BUFFERS];
int running; // Are we collecting measurements
......@@ -134,6 +141,8 @@ typedef struct {
int assigned_adc;
unsigned short lastI, lastV;
unsigned char chans[2];
} measurement_point;
......@@ -363,6 +372,18 @@ static int usbdev_control_request(usbd_device *usbd_dev, struct usb_setup_data *
m_points[req->wValue-1].number_of_runs = 0;
break;
}
case 11: // Get instantaneous
{
int m_point = req->wValue - 1;
measurement_point *mp = &m_points[m_point];
mp->id.current = mp->lastI;
mp->id.voltage = mp->lastV;
mp->id.current_time = mp->accum_data.elapsed_time;
*len = sizeof(instant_data);
*buf = &m_points[req->wValue-1].id;
}
default:
return 0;
}
......@@ -738,14 +759,28 @@ void adc_isr()
if(adc_eoc(adcs[i]))
{
unsigned short val;
m_point = adc_to_mpoint[i];
if(m_point == -1)
error_condition();
mp = &m_points[m_point];
// Get measurement point & buffer
mp = &m_points[m_point];
power_data *pd = &mp->data_bufs[mp->tail_ptr];
pd->data[pd->idx++] = ADC_DR(adcs[i]);
// Read ADC
val = ADC_DR(adcs[i]);
pd->data[pd->idx] = val;
// Save last value
if(pd->idx&1)
mp->lastI = val;
else
mp->lastV = val;
pd->idx++;
// Move to next buffer?
if(pd->idx >= PWR_SAMPLES)
{
mp->tail_ptr++;
......
#!/usr/bin/python
# Output continuous debug data
import pyenergy
from time import sleep
em = pyenergy.EnergyMonitor("EE00")
em.connect()
em.enableMeasurementPoint(1)
em.start()
while True:
m = em.getInstantaneous()
em.debugInstantaneous(m)
sleep(0.1)
......@@ -16,9 +16,14 @@ class EnergyMonitor(object):
MeasurementData = namedtuple('MeasurementData', 'energy_accum elapsed_time peak_power peak_voltage peak_current n_samples avg_current avg_voltage')
MeasurementData_packing = "=QQLLLLQQ"
InstantaneousData = namedtuple('InstantaneousData', 'voltage current current_time')
InstantaneousData_packing = "=LLL"
ADC1 = 0
ADC2 = 0
ADC3 = 0
ADC2 = 1
ADC3 = 2
port_mappings = {1: ["PA2", "PC2"], 2: ["PA3", "PA1"], 3: ["PB1", "PC5"], 4:["PB0", "PC4"]}
def __init__(self, serial="EE00"):
# Find the usb device that corresponds to the serial number
......@@ -152,6 +157,28 @@ class EnergyMonitor(object):
return self.convertData(u, samplePeriod=self.samplePeriod, **self.measurement_params[m_point])
# get an instantaneous measurement of voltage and current (debugging)
def getInstantaneous(self, m_point=1):
b = self.dev.ctrl_transfer(0xc1, 11, int(m_point), 0, 12)
args = unpack(EnergyMonitor.InstantaneousData_packing, b)
args.append(m_point)
return args
# Convert and display instantaneous measurement
def debugInstantaneous(self, v):
resistor = self.measurement_params[v[3]]['resistor']
gain = self.measurement_params[v[3]]['gain']
vref = self.measurement_params[v[3]]['vref']
print "Timestamp:", v[2] * 2. / 168000000 * 2
print "Current: Raw={} Voltage@{}={} Current={}".format(v[1],
EnergyMonitor.port_mappings[v[3]][1],
v[1]/4096.*vref,
float(vref) / gain / resistor / 4096. * v[1])
print "Voltage: Raw={} Voltage@{}={} Current={}".format(v[0],
EnergyMonitor.port_mappings[v[3]][0],
v[0]/4096.*vref,
float(vref) / 4096. * v[0] * 2)
def disconnect(self):
pass
......
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