From fc3721b2f7f7acd9dae5340615b434f68b811b28 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Wed, 12 Feb 2014 21:38:19 +0000 Subject: [PATCH] Debugging.. --- vis/config.py | 24 +++++++++++----------- vis/device.py | 1 + vis/logger.py | 4 ++-- vis/visual.py | 56 +++++++++++++++++++++++++++++++++++++-------------- yue/main.cpp | 33 +++++++++++++++--------------- 5 files changed, 73 insertions(+), 45 deletions(-) diff --git a/vis/config.py b/vis/config.py index 07151fe..a41cf64 100644 --- a/vis/config.py +++ b/vis/config.py @@ -61,15 +61,15 @@ class Config: self.parser.read(self.filename) - self.device = get('serial', 'device') - self.baudrate = int(get('serial', 'baudrate')) - self.parity = get('serial', 'parity') - self.databits = int(get('serial', 'databits')) - self.stopbits = int(get('serial', 'stopbits')) - - self.feedid = get('xively', 'feedid') - self.apikey = get('xively', 'apikey') - self.maxrate = int(get('xively', 'maxrate')) + self.device = get('serial', 'device') + self.baudrate = int(get('serial', 'baudrate')) + self.parity = get('serial', 'parity') + self.databits = int(get('serial', 'databits')) + self.stopbits = int(get('serial', 'stopbits')) + + self.feedid = get('xively', 'feedid') + self.apikey = get('xively', 'apikey') + self.maxrate = float(get('xively', 'maxrate')) for key in list(self.defaults['enable'].keys()): self.enable[key] = bin(get('enable', key)) @@ -82,9 +82,9 @@ class Config: self.parser.set('serial', 'databits', str(int(self.databits))) self.parser.set('serial', 'stopbits', str(int(self.stopbits))) - self.parser.set('xively', 'feedid', self.feedid) - self.parser.set('xively', 'apikey', self.apikey) - self.parser.set('xively', 'maxrate', str(int(self.maxrate))) + self.parser.set('xively', 'feedid', self.feedid) + self.parser.set('xively', 'apikey', self.apikey) + self.parser.set('xively', 'maxrate', str(float(self.maxrate))) for key in self.defaults['enable'].keys(): self.parser.set('enable', key, str( bool(self.enable[key]))) diff --git a/vis/device.py b/vis/device.py index d1f96e4..3d19ab2 100644 --- a/vis/device.py +++ b/vis/device.py @@ -162,6 +162,7 @@ class Frame: # Print debug output self.sampleNum[self.bits_sns] += 1 + #if self.sampleNum[self.bits_sns] == 1000: if self.sampleNum[self.bits_sns] == 1000: print('convert: %3s = \'%3s\'%%[%s] -> [%s]' % (sns, fmt, hexDump(self.binary), fltDump(self.values))) diff --git a/vis/logger.py b/vis/logger.py index f7fce7b..bd394a1 100644 --- a/vis/logger.py +++ b/vis/logger.py @@ -57,8 +57,8 @@ class Logger: self.feed.datastreams = [ get(self.queue, 'acc'), get(self.queue, 'mag'), - get(self.queue, 'touch'), - get(self.queue, 'light'), + get(self.queue, 'tch'), + get(self.queue, 'lgt'), get(self.queue, 'a2d'), ] self.feed.update() diff --git a/vis/visual.py b/vis/visual.py index 2ad6db6..f2deed6 100644 --- a/vis/visual.py +++ b/vis/visual.py @@ -16,6 +16,9 @@ else: ICON_SIZE_BUTTON = Gtk.IconSize.BUTTON class Visual: + FRAMES_PER_SEC = 60 # hz + RATES_PER_SEC = 10 # hz + def __init__(self, config, device, logger): def get_objects(names): return list(map(lambda x: @@ -27,6 +30,13 @@ class Visual: self.builder = Gtk.Builder() self.history = [0.5]*1000 + self.rate_due = 0 + self.rate_cnt = {'acc': 0, + 'mag': 0, + 'lgt': 0, + 'tch': 0, + 'a2d': 0} + self.builder.add_from_file('visual.ui') self.builder.connect_signals(self) self.window = self.builder.get_object("window") @@ -37,7 +47,6 @@ class Visual: self.lgts = get_objects(['lgt']) self.tchs = get_objects(['tchp', 'tchd']) self.a2ds = get_objects(['a2d0', 'a2d1', 'a2d2', 'a2d3', 'a2d4', 'a2d5']) - self.smpl = get_objects(['sampling']) # Signal handlers def on_hide(self, win, *args): @@ -97,11 +106,11 @@ class Visual: cairo = obj.window.cairo_create() count = len(self.history) extents = cairo.clip_extents() - width = extents[2] - extents[0] - height = extents[3] - extents[1] + width = extents[2] - extents[0] + height = extents[3] - extents[1] cairo.set_line_width(4) - for i in range(0,len(self.history)): + for i in range(count): x = i*width/(count-1) y = ((1-self.history[i]) * 0.8 + 0.1) * height if i==0: @@ -143,26 +152,29 @@ class Visual: set_act(key+'_btn', self.config.enable[key]) set_val(key+'_spin', self.config.rate[key]) - def update(self, state): def setval(objs, vals, lbls): + found = 0 for i in range(0,len(objs)): if vals[i] == None: continue + found = 1 if lbls and lbls[i]: objs[i].set_text('%s: %f' % (lbls[i], vals[i])) else: objs[i].set_text('%f' % vals[i]) + return found - setval(self.accs, state.acc, ['X', 'Y', 'Z']) - setval(self.mags, state.mag, ['X', 'Y', 'Z']) - setval(self.lgts, state.lgt, []) - setval(self.tchs, state.tch, ['P', 'D']) - setval(self.a2ds, state.a2d, []) - #setval(self.smpl, state.) + # Dispaly values and sum up rates + self.rate_cnt['acc'] += setval(self.accs, state.acc, ['X', 'Y', 'Z']) + self.rate_cnt['mag'] += setval(self.mags, state.mag, ['X', 'Y', 'Z']) + self.rate_cnt['lgt'] += setval(self.lgts, state.lgt, []) + self.rate_cnt['tch'] += setval(self.tchs, state.tch, ['P', 'D']) + self.rate_cnt['a2d'] += setval(self.a2ds, state.a2d, []) - self.history = self.history[1:] + [state.a2d[0]] - self.window.queue_draw() + # Save history + if state.a2d[0] != None: + self.history = self.history[1:] + [state.a2d[0]] def debug(self, state): print('update: ' + str(state)) @@ -172,7 +184,7 @@ class Visual: print('\ttch - ' + str(state.touch)) print('\ta2d - ' + str(state.a2d)) - def timer(self): + def read_timer(self): def check(state, name): btn = self.builder.get_object(name + '_btn') con = self.builder.get_object(name + '_con') @@ -180,20 +192,34 @@ class Visual: btn.set_active(state) con.set_from_stock(img, ICON_SIZE_BUTTON) + # Update status icons check(self.device.running(), "serial") check(self.logger.running(), "xively") + # Read data and update data display for item in self.device.process(): self.update(item) self.logger.append(item) #self.debug(item) + # Refresh window + self.window.queue_draw() + + return True + + def rate_timer(self): + for sns in self.rate_cnt: + rate = self.rate_cnt[sns] * Visual.RATES_PER_SEC + obj = self.builder.get_object(sns + '_rate') + obj.set_text('%d' % rate) + self.rate_cnt[sns] = 0 return True def run(self): self.load_config() self.window.show() - GLib.timeout_add(1000/60, self.timer) + GLib.timeout_add(1000/Visual.FRAMES_PER_SEC, self.read_timer) + GLib.timeout_add(1000/Visual.RATES_PER_SEC, self.rate_timer) Gtk.main() # Private methods diff --git a/yue/main.cpp b/yue/main.cpp index ce909d4..218b27a 100644 --- a/yue/main.cpp +++ b/yue/main.cpp @@ -15,7 +15,7 @@ (x) > (max) ? (max) : (x)) #define MMA8451_I2C_ADDRESS (0x1d<<1) // acc sensor address -#define TIME_ACCURACY 0.001 +#define TIME_ACCURACY 0.0005 #define ACC_SNS_DEFAULT 0.01 // default collecting interval in seconds #define MAG_SNS_DEFAULT 0.1 @@ -148,7 +148,8 @@ int main(void) { // Interruption Declarations //clock1.attach(&clock1_interrupt, TIME_ACCURACY); // maximun accuracy be 0.1s serial.attach(&serialRx_interrupt, Serial::RxIrq); // receive interrupt for serialS - serial.baud(115200); + //serial.baud(115200); + serial.baud(230400); magSensor.begin(); sdma_setup(UART0, 0); @@ -157,7 +158,7 @@ int main(void) { int ticks = 0, tocks = 0; while(1){ - ticks = us_ticker_read() * TIME_ACCURACY; + ticks = (us_ticker_read()*1E-6) / TIME_ACCURACY; if (tocks < ticks) { clock1_interrupt(); tocks++; @@ -218,44 +219,44 @@ void clock1_interrupt(void){ static int a2dCnt; static int ledCnt; - accCnt++; - magCnt++; - lgtCnt++; - tchCnt++; - a2dCnt++; - ledCnt++; - // Write A2D output sine wave a2dOutput.write(sin(a2dCnt * TIME_ACCURACY * (2*PI) * 0.1)); // Send data through Serial - if (accEnable && (accCnt<0 || accCnt>=accTmr/TIME_ACCURACY)){ + if (accEnable && accCnt >= (int)(accTmr/TIME_ACCURACY+0.5)){ sendAccInfo(); accCnt = 0; } - if (magEnable && (magCnt<0 || magCnt>=magTmr/TIME_ACCURACY)){ + if (magEnable && magCnt >= (int)(magTmr/TIME_ACCURACY+0.5)){ sendMagInfo(); magCnt = 0; } - if (lgtEnable && (lgtCnt<0 || lgtCnt>=lgtTmr/TIME_ACCURACY)){ + if (lgtEnable && lgtCnt >= (int)(lgtTmr/TIME_ACCURACY+0.5)){ sendLgtInfo(); lgtCnt = 0; } - if (tchEnable && (tchCnt<0 || tchCnt>=tchTmr/TIME_ACCURACY)){ + if (tchEnable && tchCnt >= (int)(tchTmr/TIME_ACCURACY+0.5)){ sendTchInfo(); tchCnt = 0; } - if (a2dEnable && (a2dCnt<0 || a2dCnt>=a2dTmr/TIME_ACCURACY)){ + if (a2dEnable && a2dCnt >= (int)(a2dTmr/TIME_ACCURACY+0.5)){ sendA2dInfo(); a2dCnt = 0; } // Toggel LED for debugging - if (ledEnable && (ledCnt<0 || ledCnt>=ledTmr/TIME_ACCURACY)){ + if (ledEnable && ledCnt>=ledTmr/TIME_ACCURACY){ led1 = !led1; ledCnt = 0; } + accCnt++; + magCnt++; + lgtCnt++; + tchCnt++; + a2dCnt++; + ledCnt++; + sdma_flush(); } -- 2.43.2