Subversion Repositories Projects

Rev

Rev 611 | Rev 613 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
584 FredericG 1
#!/usr/bin/env python
2
# -*- coding: iso-8859-15 -*-
3
# generated by wxGlade 0.6.3 on Thu Sep 24 15:46:36 2009
4
 
586 FredericG 5
import sys
606 FredericG 6
import os
612 FredericG 7
import time
8
import thread
9
import ConfigParser
10
 
584 FredericG 11
import wx
585 FredericG 12
import wx.lib
13
import wx.lib.plot
612 FredericG 14
import wx.lib.newevent
584 FredericG 15
 
585 FredericG 16
# Needs Numeric or numarray or NumPy
17
try:
18
    import numpy.oldnumeric as _Numeric
19
except:
20
    try:
21
        import numarray as _Numeric  #if numarray is used it is renamed Numeric
22
    except:
23
        try:
24
            import Numeric as _Numeric
25
        except:
26
            msg= """
27
            This module requires the Numeric/numarray or NumPy module,
28
            which could not be imported.  It probably is not installed
29
            (it's not part of the standard Python distribution). See the
30
            Numeric Python site (http://numpy.scipy.org) for information on
31
            downloading source or binaries."""
32
            raise ImportError, "Numeric,numarray or NumPy not found. \n" + msg
33
 
584 FredericG 34
# begin wxGlade: extracode
35
# end wxGlade
36
 
37
 
612 FredericG 38
# This creates a new Event class and a EVT binder function
39
(MeasStatusUpdateEvent, EVT_MEAS_STATUS_UPDATE) = wx.lib.newevent.NewEvent()    
584 FredericG 40
 
612 FredericG 41
class MeasureDialog(wx.Dialog):
42
    def __init__(self, *args, **kwds):
43
        # begin wxGlade: MeasureDialog.__init__
44
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.THICK_FRAME
45
        wx.Dialog.__init__(self, *args, **kwds)
46
        self.text_ctrl_1 = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
47
        self.button = wx.Button(self, -1, "STOP")
48
 
49
        self.__set_properties()
50
        self.__do_layout()
51
 
52
        self.Bind(wx.EVT_BUTTON, self.onButton, self.button)
53
        # end wxGlade
54
 
55
        self.running = True
56
        self.Bind(EVT_MEAS_STATUS_UPDATE, self.OnUpdate)
57
 
58
    def __set_properties(self):
59
        # begin wxGlade: MeasureDialog.__set_properties
60
        self.SetTitle("Measuring Status")
61
        self.text_ctrl_1.SetMinSize((400,300))
62
        # end wxGlade
63
 
64
    def __do_layout(self):
65
        # begin wxGlade: MeasureDialog.__do_layout
66
        sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
67
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
68
        sizer_1.Add((20, 20), 0, 0, 0)
69
        sizer_2.Add((20, 20), 0, 0, 0)
70
        sizer_2.Add(self.text_ctrl_1, 1, wx.EXPAND, 0)
71
        sizer_2.Add((20, 20), 0, 0, 0)
72
        sizer_2.Add(self.button, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
73
        sizer_2.Add((20, 20), 0, 0, 0)
74
        sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
75
        sizer_1.Add((20, 20), 0, 0, 0)
76
        self.SetSizer(sizer_1)
77
        sizer_1.Fit(self)
78
        self.Layout()
79
        # end wxGlade
80
 
81
    def OnUpdate(self, evt):
82
        print "Event received"
83
        self.running = evt.running
84
        self.text_ctrl_1.WriteText("%s\n"%evt.msg)
85
        if (not self.running):
86
            self.button.SetLabel("Close")  
87
 
88
    def onButton(self, event): # wxGlade: MeasureDialog.<event_handler>
89
        if (not self.running):
90
            self.Destroy()
91
 
92
# end of class MeasureDialog
93
 
94
 
607 FredericG 95
class SettingsDialog(wx.Dialog):
601 FredericG 96
    def __init__(self, *args, **kwds):
607 FredericG 97
        # begin wxGlade: SettingsDialog.__init__
98
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
99
        wx.Dialog.__init__(self, *args, **kwds)
608 FredericG 100
        self.button_5 = wx.Button(self, wx.ID_CANCEL, "")
101
        self.button_6 = wx.Button(self, wx.ID_OK, "")
601 FredericG 102
 
103
        self.__set_properties()
104
        self.__do_layout()
611 FredericG 105
 
106
        self.Bind(wx.EVT_BUTTON, self.onOK, self.button_6)
601 FredericG 107
        # end wxGlade
108
 
608 FredericG 109
        # The first argument that is passed to the constructor is the parent
611 FredericG 110
        self.settings = args[0].app.settings
608 FredericG 111
        # Add text-boxes for all settings
611 FredericG 112
        self.tb = []
113
        self.grid_sizer_2.SetRows(len(self.settings))
114
        for setting in self.settings.iteritems():
608 FredericG 115
            lb = wx.StaticText(self, -1, setting[1].descr, style=wx.ALIGN_RIGHT)
116
            tb = wx.TextCtrl(self, -1, str(setting[1].value))
117
            self.grid_sizer_2.Add(lb, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
118
            self.grid_sizer_2.Add(tb, 0, 0, 0)
611 FredericG 119
            self.tb.append(tb)
608 FredericG 120
        self.sizer_5.Fit(self)
121
        self.Layout()
122
 
601 FredericG 123
    def __set_properties(self):
607 FredericG 124
        # begin wxGlade: SettingsDialog.__set_properties
601 FredericG 125
        self.SetTitle("Settings")
126
        # end wxGlade
127
 
128
    def __do_layout(self):
607 FredericG 129
        # begin wxGlade: SettingsDialog.__do_layout
608 FredericG 130
        sizer_5 = wx.BoxSizer(wx.VERTICAL)
131
        grid_sizer_3 = wx.GridSizer(1, 2, 0, 0)
132
        sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
133
        grid_sizer_2 = wx.GridSizer(1, 2, 4, 4)
134
        sizer_5.Add((20, 20), 0, 0, 0)
135
        sizer_6.Add((20, 20), 0, 0, 0)
136
        sizer_6.Add(grid_sizer_2, 0, 0, 0)
137
        sizer_6.Add((20, 20), 0, 0, 0)
138
        sizer_5.Add(sizer_6, 1, wx.EXPAND, 0)
139
        sizer_5.Add((20, 20), 0, 0, 0)
140
        grid_sizer_3.Add(self.button_5, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
141
        grid_sizer_3.Add(self.button_6, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
142
        sizer_5.Add(grid_sizer_3, 0, wx.EXPAND, 0)
143
        sizer_5.Add((20, 20), 0, 0, 0)
144
        self.SetSizer(sizer_5)
145
        sizer_5.Fit(self)
601 FredericG 146
        self.Layout()
147
        # end wxGlade
148
 
608 FredericG 149
        # Store some of the items, we will need them later
150
        self.grid_sizer_2 = grid_sizer_2  
151
        self.sizer_5 = sizer_5
152
 
153
 
611 FredericG 154
    def onOK(self, event): # wxGlade: SettingsDialog.<event_handler>
155
        print "Updating parameters"
156
        try:
157
            i=0
158
            for setting in self.settings.iteritems():
159
              print setting[0], self.tb[i].GetValue()
160
              setting[1].set(self.tb[i].GetValue())
161
              i += 1
162
            event.Skip()
163
        except:
164
            wx.MessageBox("Invalid format for \"%s\" setting." % setting[1].descr)
165
 
607 FredericG 166
# end of class SettingsDialog
167
 
168
 
169
 
584 FredericG 170
class MainFrame(wx.Frame):
171
    def __init__(self, *args, **kwds):
172
        # begin wxGlade: MainFrame.__init__
173
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
174
        wx.Frame.__init__(self, *args, **kwds)
587 FredericG 175
 
176
        # Menu Bar
177
        self.frame_1_menubar = wx.MenuBar()
178
        wxglade_tmp_menu = wx.Menu()
607 FredericG 179
        wxglade_tmp_menu.Append(101, "Settings", "", wx.ITEM_NORMAL)
180
        wxglade_tmp_menu.Append(150, "Exit", "", wx.ITEM_NORMAL)
587 FredericG 181
        self.frame_1_menubar.Append(wxglade_tmp_menu, "File")
182
        wxglade_tmp_menu = wx.Menu()
607 FredericG 183
        wxglade_tmp_menu.Append(301, "Clear", "", wx.ITEM_NORMAL)
184
        wxglade_tmp_menu.Append(302, "Import", "", wx.ITEM_NORMAL)
587 FredericG 185
        self.frame_1_menubar.Append(wxglade_tmp_menu, "TestSet")
186
        wxglade_tmp_menu = wx.Menu()
187
        self.frame_1_menubar.Append(wxglade_tmp_menu, "MK")
188
        wxglade_tmp_menu = wx.Menu()
189
        self.frame_1_menubar.Append(wxglade_tmp_menu, "Help")
190
        self.SetMenuBar(self.frame_1_menubar)
191
        # Menu Bar end
600 FredericG 192
        self.Description = wx.StaticText(self, -1, "Description")
193
        self.text_ctrl_6 = wx.TextCtrl(self, -1, "Test")
194
        self.label_37 = wx.StaticText(self, -1, "Speed(s)")
195
        self.text_ctrl_9 = wx.TextCtrl(self, -1, "100-200:10")
196
        self.label_35 = wx.StaticText(self, -1, "Motor(s)")
197
        self.text_ctrl_7 = wx.TextCtrl(self, -1, "1")
198
        self.label_38 = wx.StaticText(self, -1, "")
199
        self.text_ctrl_10 = wx.TextCtrl(self, -1, "")
200
        self.label_36 = wx.StaticText(self, -1, "Channel")
201
        self.text_ctrl_8 = wx.TextCtrl(self, -1, "6")
202
        self.label_39 = wx.StaticText(self, -1, "")
203
        self.text_ctrl_11 = wx.TextCtrl(self, -1, "")
204
        self.button_4 = wx.Button(self, -1, "Start")
205
        self.GraphPanel = wx.Panel(self, -1)
206
        self.label_40 = wx.StaticText(self, -1, "Graph Type ")
207
        self.graphTypeChoice = wx.Choice(self, -1, choices=["Raw Signal", "Filtered Signal", "Spectrum"])
208
        self.label_41 = wx.StaticText(self, -1, "Y Axis Range ")
209
        self.yAxisChoice = wx.Choice(self, -1, choices=["25", "50", "100", "200"])
586 FredericG 210
        self.TestListCtrl = wx.ListCtrl(self, -1, style=wx.LC_REPORT|wx.SUNKEN_BORDER)
584 FredericG 211
 
212
        self.__set_properties()
213
        self.__do_layout()
587 FredericG 214
 
607 FredericG 215
        self.Bind(wx.EVT_MENU, self.OnSettings, id=101)
216
        self.Bind(wx.EVT_MENU, self.onClear, id=301)
217
        self.Bind(wx.EVT_MENU, self.OnImport, id=302)
612 FredericG 218
        self.Bind(wx.EVT_BUTTON, self.onStartMeasure, self.button_4)
600 FredericG 219
        self.Bind(wx.EVT_CHOICE, self.onGraphTypeChange, self.graphTypeChoice)
220
        self.Bind(wx.EVT_CHOICE, self.onYAxisChange, self.yAxisChoice)
584 FredericG 221
        # end wxGlade
222
 
587 FredericG 223
    def setApp(self, app):
224
        self.app = app
585 FredericG 225
 
584 FredericG 226
    def __set_properties(self):
227
        # begin wxGlade: MainFrame.__set_properties
587 FredericG 228
        self.SetTitle("VibrationTest")
600 FredericG 229
        self.SetSize((850, 700))
230
        self.Description.SetMinSize((53, 13))
601 FredericG 231
        self.button_4.SetMinSize((80, 80))
600 FredericG 232
        self.GraphPanel.SetMinSize((800,300))
233
        self.graphTypeChoice.SetSelection(0)
234
        self.yAxisChoice.SetSelection(1)
235
        self.TestListCtrl.SetMinSize((800,300))
584 FredericG 236
        # end wxGlade
237
 
238
    def __do_layout(self):
239
        # begin wxGlade: MainFrame.__do_layout
600 FredericG 240
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
241
        sizer_8 = wx.BoxSizer(wx.VERTICAL)
242
        sizer_11 = wx.BoxSizer(wx.VERTICAL)
243
        sizer_12 = wx.BoxSizer(wx.HORIZONTAL)
244
        sizer_9 = wx.BoxSizer(wx.HORIZONTAL)
245
        sizer_10 = wx.BoxSizer(wx.HORIZONTAL)
246
        grid_sizer_1 = wx.GridSizer(3, 4, 4, 5)
247
        sizer_3.Add((20, 20), 0, 0, 0)
248
        sizer_8.Add((20, 20), 0, 0, 0)
249
        grid_sizer_1.Add(self.Description, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
584 FredericG 250
        grid_sizer_1.Add(self.text_ctrl_6, 0, 0, 0)
600 FredericG 251
        grid_sizer_1.Add(self.label_37, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
252
        grid_sizer_1.Add(self.text_ctrl_9, 0, 0, 0)
253
        grid_sizer_1.Add(self.label_35, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
584 FredericG 254
        grid_sizer_1.Add(self.text_ctrl_7, 0, 0, 0)
600 FredericG 255
        grid_sizer_1.Add(self.label_38, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
256
        grid_sizer_1.Add(self.text_ctrl_10, 0, 0, 0)
257
        grid_sizer_1.Add(self.label_36, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
584 FredericG 258
        grid_sizer_1.Add(self.text_ctrl_8, 0, 0, 0)
600 FredericG 259
        grid_sizer_1.Add(self.label_39, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
260
        grid_sizer_1.Add(self.text_ctrl_11, 0, 0, 0)
261
        sizer_9.Add(grid_sizer_1, 0, 0, 0)
262
        sizer_10.Add((50, 20), 0, 0, 0)
263
        sizer_10.Add(self.button_4, 0, wx.ALIGN_CENTER_VERTICAL, 0)
264
        sizer_9.Add(sizer_10, 1, wx.EXPAND, 0)
265
        sizer_8.Add(sizer_9, 0, 0, 0)
266
        sizer_8.Add((20, 30), 0, 0, 0)
267
        sizer_11.Add(self.GraphPanel, 1, wx.EXPAND, 0)
268
        sizer_11.Add((20, 5), 0, 0, 0)
269
        sizer_12.Add(self.label_40, 0, wx.ALIGN_CENTER_VERTICAL, 0)
270
        sizer_12.Add(self.graphTypeChoice, 0, 0, 0)
271
        sizer_12.Add((40, 20), 0, 0, 0)
272
        sizer_12.Add(self.label_41, 0, wx.ALIGN_CENTER_VERTICAL, 0)
273
        sizer_12.Add(self.yAxisChoice, 0, 0, 0)
274
        sizer_11.Add(sizer_12, 0, 0, 0)
275
        sizer_8.Add(sizer_11, 0, 0, 0)
276
        sizer_8.Add((20, 30), 0, 0, 0)
277
        sizer_8.Add(self.TestListCtrl, 1, 0, 0)
278
        sizer_8.Add((20, 20), 0, 0, 0)
279
        sizer_3.Add(sizer_8, 1, wx.EXPAND, 0)
280
        self.SetSizer(sizer_3)
584 FredericG 281
        self.Layout()
600 FredericG 282
        self.SetSize((850, 700))
584 FredericG 283
        # end wxGlade
284
 
590 FredericG 285
        # List events
286
        self.TestListCtrl.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnTestSelected, self.TestListCtrl)
287
 
586 FredericG 288
        # Configure Graph
593 FredericG 289
        self.client = wx.lib.plot.PlotCanvas(self.GraphPanel, size=(800,300))
290
 
585 FredericG 291
        self.client.SetPointLabelFunc(self.DrawPointLabel)
292
 
293
        self.client.SetFont(wx.Font(10,wx.SWISS,wx.NORMAL,wx.NORMAL))
294
        self.client.SetFontSizeAxis(10)
295
        self.client.SetFontSizeLegend(7)
296
        self.client.setLogScale((False,False))
297
 
586 FredericG 298
 
299
        # Configure TestListCtrl
594 FredericG 300
        self.TestListCtrl.InsertColumn(0, "Description")
301
        self.TestListCtrl.InsertColumn(1, "Speed")
302
        self.TestListCtrl.InsertColumn(2, "Channel")
607 FredericG 303
        self.TestListCtrl.InsertColumn(3, "Vibration Value")
304
        self.TestListCtrl.SetColumnWidth(3, 500)
586 FredericG 305
 
585 FredericG 306
    def DrawPointLabel(self, dc, mDataDict):
307
        """This is the fuction that defines how the pointLabels are plotted
308
            dc - DC that will be passed
309
            mDataDict - Dictionary of data that you want to use for the pointLabel
310
 
311
            As an example I have decided I want a box at the curve point
312
            with some text information about the curve plotted below.
313
            Any wxDC method can be used.
314
        """
315
        # ----------
316
        dc.SetPen(wx.Pen(wx.BLACK))
317
        dc.SetBrush(wx.Brush( wx.BLACK, wx.SOLID ) )
318
 
319
        sx, sy = mDataDict["scaledXY"] #scaled x,y of closest point
320
        dc.DrawRectangle( sx-5,sy-5, 10, 10)  #10by10 square centered on point
321
        px,py = mDataDict["pointXY"]
322
        cNum = mDataDict["curveNum"]
323
        pntIn = mDataDict["pIndex"]
324
        legend = mDataDict["legend"]
325
        #make a string to display
326
        s = "Crv# %i, '%s', Pt. (%.2f,%.2f), PtInd %i" %(cNum, legend, px, py, pntIn)
327
        dc.DrawText(s, sx , sy+1)
328
        # -----------
329
 
596 FredericG 330
 
331
    def onNewTest(self, test):
332
        index = self.TestListCtrl.InsertStringItem(sys.maxint, test.descr)
333
        self.TestListCtrl.SetStringItem(index, 1, str(test.speed))
334
        self.TestListCtrl.SetStringItem(index, 2, test.channel)
607 FredericG 335
 
336
        vv = int(test.getVibValue())
337
        vvs = "|%s| (%d)" % ("----------------------------------------------------------------------------------------------------"[0:min(vv,100)], vv)
338
        self.TestListCtrl.SetStringItem(index, 3, vvs)
606 FredericG 339
        if (index == 0):
340
            self.TestListCtrl.Select(index)
596 FredericG 341
 
342
 
590 FredericG 343
    def OnTestSelected(self, event):
344
         testId = event.m_itemIndex
345
         print "Test Selected id=%d" % (testId)
592 FredericG 346
         self.activeTestId = testId
347
         self.drawGraph()
590 FredericG 348
 
592 FredericG 349
    def drawGraph(self):
607 FredericG 350
 
600 FredericG 351
         y = int(self.yAxisChoice.GetStringSelection())
590 FredericG 352
 
607 FredericG 353
         nbSelected = self.TestListCtrl.SelectedItemCount
596 FredericG 354
 
607 FredericG 355
         if nbSelected > 1:
356
             self.graphTypeChoice.Disable()
357
             x = 1
358
             data = []
359
             idx = self.TestListCtrl.GetFirstSelected()
360
             while idx != -1:
361
                 data.append([x,self.app.getTest(idx).getVibValue()])
362
                 x += 1
363
                 idx = self.TestListCtrl.GetNextSelected(idx)
364
             line = wx.lib.plot.PolyLine(data, legend= 'Vibrations', colour='red', width=2)
365
             markers = wx.lib.plot.PolyMarker(data, legend= '', colour='red', marker='circle',size=2)
366
             title = "Comparing tests"
367
             self.client.Draw(wx.lib.plot.PlotGraphics([line, markers], title, "Test", "Vibration Value"), xAxis=(1,max(x,10)), yAxis=(0,y))
600 FredericG 368
             self.client.SetEnableGrid('Horizontal')
369
 
607 FredericG 370
         else:
371
             self.graphTypeChoice.Enable()
372
             vibTest = self.app.getTest(self.activeTestId)
373
             nb = vibTest.getDataLen()
590 FredericG 374
 
607 FredericG 375
             if self.graphTypeChoice.GetSelection() == 0:
376
                 xydata = _Numeric.linspace(0,0.09*nb,2*nb)
377
                 xydata.shape = (nb, 2)
378
                 xydata[:,1] = vibTest.getRawData()
379
                 line = wx.lib.plot.PolyLine(xydata, legend= 'Raw Data', colour='red', width=2)
380
 
381
                 title = "Raw Signal: %s %s %d" %(vibTest.descr, vibTest.channel, vibTest.speed)
382
                 self.client.Draw(wx.lib.plot.PlotGraphics([line], title, "Time (ms)", "Acc"), yAxis= (-y,y))
383
                 self.client.SetEnableGrid('Horizontal')
599 FredericG 384
 
607 FredericG 385
             if self.graphTypeChoice.GetSelection() == 1:
386
                 xydata = _Numeric.linspace(0,0.09*nb,2*nb)
387
                 xydata.shape = (nb, 2)
388
                 xydata[:,1] = vibTest.getFilteredData()
389
                 line = wx.lib.plot.PolyLine(xydata, legend= 'Raw Data', colour='red', width=2)
390
 
391
                 title = "Filtered Signal: %s %s %d" %(vibTest.descr, vibTest.channel, vibTest.speed)
392
                 self.client.Draw(wx.lib.plot.PlotGraphics([line], title, "Time (ms)", "Acc"), yAxis= (-y,y))
393
                 self.client.SetEnableGrid('Horizontal')
394
 
395
             elif self.graphTypeChoice.GetSelection() == 2:
396
                 xydata = _Numeric.linspace(0,5555,nb)
397
                 xydata.shape = (nb/2, 2)
398
 
399
                 xydata[:,1] = vibTest.getSpectrum()
400
 
401
                 line = wx.lib.plot.PolyLine(xydata, legend= 'Spectrum', colour='red')
402
                 markers = wx.lib.plot.PolyMarker(xydata, legend= '', colour='red', marker='circle',size=2)
403
 
404
                 title = "Spectrum: %s %s %d" %(vibTest.descr, vibTest.channel, vibTest.speed)
405
                 self.client.Draw(wx.lib.plot.PlotGraphics([line,markers], title, "Freq (Hz)", "Acc"), xAxis=(0,200), yAxis= (-0,y))
406
                 self.client.SetEnableGrid(True)
590 FredericG 407
 
599 FredericG 408
 
587 FredericG 409
    def OnImport(self, event): # wxGlade: MainFrame.<event_handler>
606 FredericG 410
        dlg = wx.FileDialog(
411
            self, message="Choose a file",
412
            defaultDir=os.getcwd(),
413
            defaultFile="*.txt",
414
            wildcard="",
415
            style=wx.OPEN | wx.CHANGE_DIR
416
            )
417
        if dlg.ShowModal() == wx.ID_OK:
418
            paths = dlg.GetPaths();
419
            self.app.Import(paths[0])
420
        dlg.Destroy()
587 FredericG 421
 
599 FredericG 422
    def onYAxisChange(self, event): # wxGlade: MainFrame.<event_handler>
592 FredericG 423
        self.drawGraph()
424
 
600 FredericG 425
    def onGraphTypeChange(self, event): # wxGlade: MainFrame.<event_handler>
599 FredericG 426
        self.drawGraph()
427
 
601 FredericG 428
    def onClear(self, event): # wxGlade: MainFrame.<event_handler>
429
        print "Event handler `onClear' not implemented"
430
        event.Skip()
431
 
607 FredericG 432
    def OnSettings(self, event): # wxGlade: MainFrame.<event_handler>
433
        dlg = SettingsDialog(self, -1, "Sample Dialog", size=(350, 200),
434
                         #style=wx.CAPTION | wx.SYSTEM_MENU | wx.THICK_FRAME,
435
                         style=wx.DEFAULT_DIALOG_STYLE, # & ~wx.CLOSE_BOX
436
                         )
437
        dlg.CenterOnScreen()
611 FredericG 438
        val = dlg.ShowModal()  # this does not return until the dialog is closed.
607 FredericG 439
        dlg.Destroy()
611 FredericG 440
        self.app.onSettingsChanged()
607 FredericG 441
 
612 FredericG 442
    def onStartMeasure(self, event): # wxGlade: MainFrame.<event_handler>
443
 
444
        # create the dialog that will show the satus
445
        dlg = MeasureDialog(self)
446
        dlg.CenterOnScreen()
447
        # Signal that application
448
        self.app.startMeasure(dlg)
449
        # Show the dialog
450
        val = dlg.ShowModal()  # this does not return until the dialog is closed.
451
        dlg.Destroy()
452
 
584 FredericG 453
# end of class MainFrame
454
 
607 FredericG 455
class Setting:
456
    def __init__(self, descr, defaultValue):
457
        self.descr = descr
458
        self.value = defaultValue
611 FredericG 459
 
460
    def set(self, newValue):
461
        if isinstance(self.value, int):
462
            self.value = int(newValue)
463
        else:
464
            self.value = str(newValue)
584 FredericG 465
 
612 FredericG 466
 
467
class MeasureThread:
468
    def __init__(self, evtConsumer):
469
        self.evtConsumer = evtConsumer
470
 
471
    def start(self):
472
        thread.start_new_thread(self._run, ())
473
 
474
    def _run(self):
475
        i=0
476
        for i in range(20):
477
            time.sleep(0.5)
478
            evt = MeasStatusUpdateEvent(running=True, msg="Hello %d" % i)
479
            wx.PostEvent(self.evtConsumer, evt)
480
        evt = MeasStatusUpdateEvent(running=False, msg="Done")
481
        wx.PostEvent(self.evtConsumer, evt)
482
 
483
 
484
 
586 FredericG 485
class VibTest:
596 FredericG 486
    def __init__(self, descr, motor, speed, channel, rawData):
594 FredericG 487
        self.descr = descr
488
        self.motor = motor
489
        self.speed = speed
596 FredericG 490
        self.channel = channel
599 FredericG 491
 
492
        self.dataLen = len(rawData)
493
 
590 FredericG 494
        self.rawData = _Numeric.array(rawData)
495
        self.dc = self.rawData.mean()
496
        self.rawData -= self.dc
586 FredericG 497
 
600 FredericG 498
        self.fft = _Numeric.fft.rfft(self.rawData)
499
 
500
        self.spectrum = None
501
        self.filteredData = None
599 FredericG 502
 
602 FredericG 503
        self.vibValue = None
504
 
594 FredericG 505
    def getDescr(self):
506
        return self.Descr
587 FredericG 507
 
590 FredericG 508
    def getRawData(self):
509
        return self.rawData
510
 
511
    def getDataLen(self):
512
        return self.dataLen
513
 
599 FredericG 514
    def getSpectrum(self):
600 FredericG 515
        if self.spectrum == None:
516
            self.spectrum = _Numeric.absolute(self.fft[1:self.dataLen/2+1]) / (self.dataLen/2)
517
        return self.spectrum
590 FredericG 518
 
600 FredericG 519
    def getFilteredData(self):
520
        if self.filteredData == None:
521
            tmpfft = self.fft.copy()
602 FredericG 522
            for i in range(0,5):
600 FredericG 523
                tmpfft[i] = 0
524
            for i in range(30, len(tmpfft)):
525
                tmpfft[i] = 0
526
            self.filteredData = _Numeric.fft.irfft(tmpfft)
527
        return self.filteredData
602 FredericG 528
 
529
    def getVibValue(self):
530
      if self.vibValue == None:
531
        fd = self.getFilteredData();
532
        self.vibValue = max(fd)-min(fd)
533
      return self.vibValue
599 FredericG 534
 
607 FredericG 535
 
536
 
584 FredericG 537
class App(wx.App):
607 FredericG 538
 
539
    SETTINGSFILE = "settings.cfg"
540
 
586 FredericG 541
    def __init__(self, par):
542
        self.VibTests = []
543
        wx.App.__init__(self, par)
544
 
607 FredericG 545
        # Init settings
546
        self.settings={}
611 FredericG 547
        self.settings["serialport"] = Setting("Serial Port", "COM1")
607 FredericG 548
        self.settings["hpf"] = Setting("HP Filter cutoff (Hz)", 50)
549
        self.settings["lpf"] = Setting("LP Filter cutoff (Hz)", 400)
550
 
551
        self.readSettings()
552
 
606 FredericG 553
        if len(sys.argv)>1:
554
            self.Import(sys.argv[1])
586 FredericG 555
 
606 FredericG 556
 
607 FredericG 557
    def readSettings(self):
558
        print "Reading settings"
559
        cp = ConfigParser.ConfigParser()
560
 
561
        try:
562
            cp.read(App.SETTINGSFILE)
563
            for setting in cp.items("DEFAULT"):
564
                print " ",setting
565
                try:
566
                    self.settings[setting[0]].value = setting[1]
567
                except:
568
                    print "WARNING, unknown setting"
569
        except:
570
            print "ERROR reading settingsfile"
571
 
572
 
573
    def storeSettings(self):
574
        print "Storing settings"
575
 
576
        cp = ConfigParser.ConfigParser()
577
        for setting in self.settings.iteritems():
578
            cp.set("", setting[0], setting[1].value)
579
 
580
        file = open(App.SETTINGSFILE, "w")
581
        cp.write(file)
582
        file.close()
583
 
611 FredericG 584
 
585
    def onSettingsChanged(self):
586
        self.storeSettings()
587
 
588
 
596 FredericG 589
    def AddTest(self, descr, motor, speed, channel, rawData):
590
        test = VibTest(descr, motor, speed, channel, rawData)
586 FredericG 591
        self.VibTests.append(test)
596 FredericG 592
        self.frame_1.onNewTest(test)
586 FredericG 593
 
590 FredericG 594
    def getTest(self, testId):
595
        return self.VibTests[testId]
596
 
584 FredericG 597
    def OnInit(self):
598
        wx.InitAllImageHandlers()
586 FredericG 599
        self.frame_1 = MainFrame(None, -1, "")
587 FredericG 600
        self.frame_1.setApp(self);
586 FredericG 601
        self.SetTopWindow(self.frame_1)
602
 
607 FredericG 603
        self.frame_1.CenterOnScreen()
587 FredericG 604
        self.frame_1.Show()
605
        return 1
606
 
606 FredericG 607
    def Import(self, filePath):
596 FredericG 608
 
606 FredericG 609
        print "Importing file \"%s\"" % filePath
596 FredericG 610
 
611
        logfile = open(filePath, "r")
590 FredericG 612
        data = None
596 FredericG 613
 
614
        headers = (logfile.readline()).split(',')
615
        nbCols = len(headers)
616
        print "NbCols =", nbCols
617
 
618
        data = []
619
        descr = []
620
        speed = []
621
        channel = []
622
        for c in range(nbCols):
623
            data.append([])
624
            h = headers[c].split(' ')
625
            descr.append(h[0]);
626
            speed.append(h[1]);
627
            channel.append(h[2]);
628
 
590 FredericG 629
        for line in logfile:
630
            values = line.split(',')
596 FredericG 631
            for i in range(nbCols):
632
                data[i].append(int(values[i]))
590 FredericG 633
        logfile.close()
596 FredericG 634
 
635
        for c in range(nbCols):
599 FredericG 636
            if (len(data[c]) % 2) != 0:
637
                data[c].append(data[c][-1])
596 FredericG 638
            self.AddTest(descr[c], 0, int(speed[c]), channel[c], data[c])
612 FredericG 639
 
640
    def startMeasure(self, dialog):
641
        print "Start measuring"
590 FredericG 642
 
612 FredericG 643
        self.measureThread = MeasureThread(dialog)
644
        self.measureThread.start()
590 FredericG 645
 
612 FredericG 646
 
647
 
648
 
649
 
590 FredericG 650
 
586 FredericG 651
 
590 FredericG 652
 
584 FredericG 653
# end of class App
654
 
655
if __name__ == "__main__":
656
    VibrationTestGui = App(0)
657
    VibrationTestGui.MainLoop()