]> Pileus Git - ~andy/csm213a-hw/commitdiff
Debugging..
authorAndy Spencer <andy753421@gmail.com>
Wed, 12 Feb 2014 21:38:19 +0000 (21:38 +0000)
committerAndy Spencer <andy753421@gmail.com>
Wed, 12 Feb 2014 21:38:19 +0000 (21:38 +0000)
vis/config.py
vis/device.py
vis/logger.py
vis/visual.py
yue/main.cpp

index 07151fe383b06a41d5581fefead5c6821a1d3ee0..a41cf643c3e261747a4e0dc63040eb2902346f7c 100644 (file)
@@ -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])))
index d1f96e4d59448167f6fb42daed79d95bdcc9b902..3d19ab24f2af66245f85ac57b17b09ed249af1f0 100644 (file)
@@ -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)))
index f7fce7baa69fd37f7b01fe533108b48957b7553b..bd394a1fdef2b0f14ec6bed489f7c51c063d5231 100644 (file)
@@ -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()
index 2ad6db69324ab31f6ea82686d9089a9592f31b19..f2deed65d24ffe7b16e87525ef3d4d0ebdac8e94 100644 (file)
@@ -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
index ce909d4eeff3eb9d95e69f8d6825b7c1f76209ea..218b27a9bbf8e96336ff8667e510d8c93f493063 100644 (file)
@@ -15,7 +15,7 @@
          (x) > (max) ? (max) : (x))\r
 \r
 #define MMA8451_I2C_ADDRESS (0x1d<<1)   // acc sensor address\r
-#define TIME_ACCURACY     0.001\r
+#define TIME_ACCURACY     0.0005\r
 \r
 #define ACC_SNS_DEFAULT   0.01 // default collecting interval in seconds\r
 #define MAG_SNS_DEFAULT   0.1\r
@@ -148,7 +148,8 @@ int main(void) {
     // Interruption Declarations\r
     //clock1.attach(&clock1_interrupt, TIME_ACCURACY);    // maximun accuracy be 0.1s\r
     serial.attach(&serialRx_interrupt, Serial::RxIrq);  // receive interrupt for serialS\r
-    serial.baud(115200);\r
+    //serial.baud(115200);\r
+    serial.baud(230400);\r
     magSensor.begin();\r
 \r
     sdma_setup(UART0, 0);\r
@@ -157,7 +158,7 @@ int main(void) {
 \r
     int ticks = 0, tocks = 0;\r
     while(1){\r
-       ticks = us_ticker_read() * TIME_ACCURACY;\r
+       ticks = (us_ticker_read()*1E-6) / TIME_ACCURACY;\r
        if (tocks < ticks) {\r
                clock1_interrupt();\r
                tocks++;\r
@@ -218,44 +219,44 @@ void clock1_interrupt(void){
     static int a2dCnt;\r
     static int ledCnt;\r
 \r
-    accCnt++;\r
-    magCnt++;\r
-    lgtCnt++;\r
-    tchCnt++;\r
-    a2dCnt++;\r
-    ledCnt++;\r
-\r
     // Write A2D output sine wave\r
     a2dOutput.write(sin(a2dCnt * TIME_ACCURACY * (2*PI) * 0.1));\r
 \r
     // Send data through Serial\r
-    if (accEnable && (accCnt<0 || accCnt>=accTmr/TIME_ACCURACY)){\r
+    if (accEnable && accCnt >= (int)(accTmr/TIME_ACCURACY+0.5)){\r
         sendAccInfo();\r
         accCnt = 0;\r
     }\r
-    if (magEnable && (magCnt<0 || magCnt>=magTmr/TIME_ACCURACY)){\r
+    if (magEnable && magCnt >= (int)(magTmr/TIME_ACCURACY+0.5)){\r
         sendMagInfo();\r
         magCnt = 0;\r
     }\r
-    if (lgtEnable && (lgtCnt<0 || lgtCnt>=lgtTmr/TIME_ACCURACY)){\r
+    if (lgtEnable && lgtCnt >= (int)(lgtTmr/TIME_ACCURACY+0.5)){\r
         sendLgtInfo();\r
         lgtCnt = 0;\r
     }\r
-    if (tchEnable && (tchCnt<0 || tchCnt>=tchTmr/TIME_ACCURACY)){\r
+    if (tchEnable && tchCnt >= (int)(tchTmr/TIME_ACCURACY+0.5)){\r
         sendTchInfo();\r
         tchCnt = 0;\r
     }\r
-    if (a2dEnable && (a2dCnt<0 || a2dCnt>=a2dTmr/TIME_ACCURACY)){\r
+    if (a2dEnable && a2dCnt >= (int)(a2dTmr/TIME_ACCURACY+0.5)){\r
         sendA2dInfo();\r
         a2dCnt = 0;\r
     }\r
 \r
     // Toggel LED for debugging\r
-    if (ledEnable && (ledCnt<0 || ledCnt>=ledTmr/TIME_ACCURACY)){\r
+    if (ledEnable && ledCnt>=ledTmr/TIME_ACCURACY){\r
         led1   = !led1;\r
        ledCnt = 0;\r
     }\r
 \r
+    accCnt++;\r
+    magCnt++;\r
+    lgtCnt++;\r
+    tchCnt++;\r
+    a2dCnt++;\r
+    ledCnt++;\r
+\r
     sdma_flush();\r
 }\r
 \r