Power monitor

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
prinonis
Date:
Sun Mar 29 07:06:22 2015 +0000
Parent:
2:2c163adb4304
Commit message:
added PowerWatch.m code

Changed in this revision

PowerWatch.m.txt Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PowerWatch.m.txt	Sun Mar 29 07:06:22 2015 +0000
@@ -0,0 +1,123 @@
+%%  Setup Section
+
+    try
+        clc;clear;
+        delete( instrfindall );
+        fprintf( 'Starting\n' );
+
+        com_prt = 'COM9';
+
+        scr_wdt = 768;
+        scr_hgt = 512;
+
+        blz_sze = 1000;
+
+        fig_wdt = ( scr_wdt -  32 );
+        fig_hgt = ( scr_hgt - 128 );
+        plt_wdt = ( fig_wdt -  96 );
+        plt_hgt = ( fig_hgt -  20 ) / 3;
+
+        fig = figure( 'Color', [.73 .83 .96], 'Position', [16,16,fig_wdt, fig_hgt], 'Name', 'Power Watch', 'NumberTitle', 'off', 'Visible', 'off', 'MenuBar', 'none', 'ToolBar', 'none' );
+
+        datBV = zeros(1,blz_sze) * 0.0;
+        datSV = zeros(1,blz_sze) * 1.0;
+        datSI = zeros(1,blz_sze) * 1.0;
+
+        axeBV = axes( 'Unit', 'Pixel', 'Position', [64 32+plt_hgt*2 plt_wdt plt_hgt-20] ); pltBV = plot( axeBV, datBV, 'LineSmoothing', 'on', 'LineWidth', 2.0 );
+        axeSV = axes( 'Unit', 'Pixel', 'Position', [64 32+plt_hgt*1 plt_wdt plt_hgt-20] ); pltSV = plot( axeSV, datSV, 'LineSmoothing', 'on', 'LineWidth', 2.0 );
+        axeSI = axes( 'Unit', 'Pixel', 'Position', [64 32+plt_hgt*0 plt_wdt plt_hgt-20] ); pltSI = plot( axeSI, datSI, 'LineSmoothing', 'on', 'LineWidth', 2.0 );
+        
+        ylabel( axeBV,   'Bus Voltage' );
+        ylabel( axeSV, 'Shunt Voltage' );
+        ylabel( axeSI, 'Shunt Current' );
+
+
+    catch
+        fprintf( 'Setup Error\n' );
+    end
+    
+%%  Wait for Device Section
+
+    try
+        connected = false;
+        
+        h = waitbar( 0, 'Waiting for Measurement Device, Please Wait...' );
+        s = serial( com_prt, 'BaudRate', 230400, 'ByteOrder', 'littleEndian' ); fopen(s); connected = false;
+        i = 1;
+
+        while ishandle( h )
+            if  s.BytesAvailable > 0
+                connected = true;
+                break
+            else
+                fprintf( 'Waiting for Measurement Device, Please Wait...\n' );
+                waitbar( i / 360 ); i = i + 1;
+                pause( 1 );                    
+                if i > 360; break; end
+            end
+        end
+        close( h );
+    catch
+        fprintf( 'Waiting for Device Error\n' );
+    end
+
+%%  Data Aquisition Section
+
+    try
+        if  connected
+            
+            fprintf( 'Acquiring Data\n' );
+            set( fig, 'Visible', 'on' );
+            fread(s, s.BytesAvailable ); % Clear Backlog
+
+            while ishandle( fig )
+                if  s.BytesAvailable > 0
+                    [m,k] = fread( s, 1, 'uchar' );
+
+                    if  m(1) == hex2dec('de')
+                        [m,k] = fread( s, 1, 'uchar' );
+
+                        if  m(1) == hex2dec('ad')
+                            [x,k] = fread( s, 1, 'uint32'  );
+                            [y,k] = fread( s, 3, 'float32' );
+
+                            datBV = [ y(1), datBV(1:end-1) ]; set( pltBV, 'YData', datBV );
+                            datSV = [ y(2), datSV(1:end-1) ]; set( pltSV, 'YData', datSV );
+                            datSI = [ y(3), datSI(1:end-1) ]; set( pltSI, 'YData', datSI );
+
+                            fprintf( '     TimeStamp = %6d  \n', x(1) );
+                            fprintf( '   Bus Voltage = %6.3f\n', y(1) );
+                            fprintf( ' Shunt Voltage = %6.3f\n', y(2) );
+                            fprintf( ' Shunt Current = %6.3f\n\n', y(3) );
+
+                        else fprintf( 'Bad Marker\n' );
+                        end
+                        
+                    else fprintf( 'Bad Marker\n' );
+                    end
+                    
+                else pause( 0.01 );
+                end
+            end
+        end
+    catch
+        fprintf( 'Data Acqusition Error\n' );
+    end
+
+%%  Cleanup Section
+
+    try
+        fprintf( 'Cleaning Up\b' );
+
+        if  ishandle( fig ) 
+            close( fig );
+        end
+        
+        fclose( 'all' );
+        delete( s );
+        clear s;
+        close;
+
+    catch
+        fprintf( 'Cleanup Error\n' );
+    end
\ No newline at end of file