Subversion Repositories Projects

Rev

Rev 627 | Rev 629 | 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
623 FredericG 15
import wx.lib.agw.speedmeter as speedmeter
584 FredericG 16
 
613 FredericG 17
import mkProto
18
 
19
 
20
 
21
CHANNEL_NAMES = ["GyroYaw", "GyroRoll", "GyroNick", "Pressure", "Batt", "AccTop", "AccRoll", "AccNick"]
615 FredericG 22
FS = 11111
623 FredericG 23
pi = 3.14
613 FredericG 24
 
626 FredericG 25
rootPath = ""
26
 
585 FredericG 27
# Needs Numeric or numarray or NumPy
28
try:
29
    import numpy.oldnumeric as _Numeric
30
except:
31
    try:
32
        import numarray as _Numeric  #if numarray is used it is renamed Numeric
33
    except:
34
        try:
35
            import Numeric as _Numeric
36
        except:
37
            msg= """
38
            This module requires the Numeric/numarray or NumPy module,
39
            which could not be imported.  It probably is not installed
40
            (it's not part of the standard Python distribution). See the
41
            Numeric Python site (http://numpy.scipy.org) for information on
42
            downloading source or binaries."""
43
            raise ImportError, "Numeric,numarray or NumPy not found. \n" + msg
44
 
584 FredericG 45
# begin wxGlade: extracode
46
# end wxGlade
47
 
48
 
612 FredericG 49
# This creates a new Event class and a EVT binder function
613 FredericG 50
(MeasStatusUpdateEvent, EVT_MEAS_STATUS_UPDATE) = wx.lib.newevent.NewEvent()  
51
(MeasDataEvent, EVT_MEAS_DATA) = wx.lib.newevent.NewEvent()  
584 FredericG 52
 
612 FredericG 53
class MeasureDialog(wx.Dialog):
54
    def __init__(self, *args, **kwds):
55
        # begin wxGlade: MeasureDialog.__init__
626 FredericG 56
        kwds["style"] = wx.CAPTION|wx.RESIZE_BORDER|wx.THICK_FRAME
612 FredericG 57
        wx.Dialog.__init__(self, *args, **kwds)
58
        self.text_ctrl_1 = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
59
        self.button = wx.Button(self, -1, "STOP")
623 FredericG 60
        self.voltageCtrl = speedmeter.SpeedMeter(self, extrastyle=speedmeter.SM_DRAW_HAND | speedmeter.SM_DRAW_PARTIAL_SECTORS |  speedmeter.SM_DRAW_MIDDLE_ICON )
61
        self.speedCtrl = speedmeter.SpeedMeter(self, extrastyle=speedmeter.SM_DRAW_HAND | speedmeter.SM_DRAW_PARTIAL_SECTORS | speedmeter.SM_DRAW_MIDDLE_TEXT | speedmeter.SM_DRAW_SECONDARY_TICKS)
626 FredericG 62
 
612 FredericG 63
        self.__set_properties()
64
        self.__do_layout()
65
 
66
        self.Bind(wx.EVT_BUTTON, self.onButton, self.button)
67
        # end wxGlade
68
 
626 FredericG 69
        self.button.SetFocus()
70
 
623 FredericG 71
        YELLOW = wx.Colour(255, 240, 0)
72
        BG = wx.Colour(0x80, 0x80, 0x80)
73
        # Configure Voltage Ctrl
74
        self.voltageCtrl.SetAngleRange(0,pi)
75
        intervals = range(0, 5)
76
        self.voltageCtrl.SetIntervals(intervals)
77
        colours = [wx.RED, wx.GREEN, wx.GREEN, YELLOW]
78
        self.voltageCtrl.SetIntervalColours(colours)
79
        ticks = ["", "", "", "", ""]
80
        self.voltageCtrl.SetTicks(ticks)
81
        self.voltageCtrl.SetTicksColour(wx.WHITE)
82
        self.voltageCtrl.SetHandColour(YELLOW)
83
 
626 FredericG 84
        icon = wx.Icon("%s/Resources/fuel.ico" % rootPath, wx.BITMAP_TYPE_ICO)
623 FredericG 85
        icon.SetWidth(24)
86
        icon.SetHeight(24)
87
 
88
        self.voltageCtrl.SetMiddleIcon(icon)        
89
        self.voltageCtrl.SetSpeedBackground(BG)        
90
        self.voltageCtrl.SetArcColour(wx.WHITE)
91
        self.voltageCtrl.SetSpeedValue(2)
92
 
93
 
94
        # Configure Speed Ctr;
95
        self.speedCtrl.SetAngleRange(0,pi)        
96
        intervals = range(0, 261, 20)
97
        self.speedCtrl.SetIntervals(intervals)
98
 
99
        colours = [BG]*(len(intervals)-1)
100
        for i in range(5,10):
101
          colours[i] = wx.GREEN
102
        self.speedCtrl.SetIntervalColours(colours)
103
        ticks = [str(interval) for interval in intervals]
104
        self.speedCtrl.SetTicks(ticks)
105
        self.speedCtrl.SetTicksColour(wx.WHITE)
106
        self.speedCtrl.SetNumberOfSecondaryTicks(1)
107
        self.speedCtrl.SetTicksFont(wx.Font(7, wx.SWISS, wx.NORMAL, wx.NORMAL))
108
        self.speedCtrl.SetMiddleText("Speed")
109
        self.speedCtrl.SetMiddleTextColour(wx.WHITE)
110
        self.speedCtrl.SetMiddleTextFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.BOLD))
111
        self.speedCtrl.SetHandColour(YELLOW)
112
        self.speedCtrl.SetSpeedBackground(BG)  
113
        self.speedCtrl.SetArcColour(wx.WHITE)        
114
        self.speedCtrl.SetSpeedValue(0)
115
 
116
 
612 FredericG 117
        self.running = True
118
        self.Bind(EVT_MEAS_STATUS_UPDATE, self.OnUpdate)
613 FredericG 119
        self.Bind(EVT_MEAS_DATA, self.OnData)
120
        # The first argument that is passed to the constructor is the parent
121
        self.app = args[0].app
122
        self.error = False
623 FredericG 123
        self.firstVoltage = True
612 FredericG 124
 
623 FredericG 125
 
612 FredericG 126
    def __set_properties(self):
127
        # begin wxGlade: MeasureDialog.__set_properties
128
        self.SetTitle("Measuring Status")
129
        self.text_ctrl_1.SetMinSize((400,300))
623 FredericG 130
        self.voltageCtrl.SetMinSize((50,-1))
131
        self.speedCtrl.SetMinSize((50,-1))
612 FredericG 132
        # end wxGlade
133
 
134
    def __do_layout(self):
135
        # begin wxGlade: MeasureDialog.__do_layout
136
        sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
623 FredericG 137
        sizer_4 = wx.BoxSizer(wx.VERTICAL)
612 FredericG 138
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
139
        sizer_1.Add((20, 20), 0, 0, 0)
140
        sizer_2.Add((20, 20), 0, 0, 0)
141
        sizer_2.Add(self.text_ctrl_1, 1, wx.EXPAND, 0)
142
        sizer_2.Add((20, 20), 0, 0, 0)
143
        sizer_2.Add(self.button, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
144
        sizer_2.Add((20, 20), 0, 0, 0)
145
        sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
146
        sizer_1.Add((20, 20), 0, 0, 0)
623 FredericG 147
        sizer_4.Add(self.voltageCtrl, 1, wx.EXPAND, 0)
148
        sizer_4.Add(self.speedCtrl, 1, wx.EXPAND, 0)
149
        sizer_1.Add(sizer_4, 1, wx.EXPAND, 0)
150
        sizer_1.Add((20, 20), 0, 0, 0)
612 FredericG 151
        self.SetSizer(sizer_1)
152
        sizer_1.Fit(self)
153
        self.Layout()
154
        # end wxGlade
613 FredericG 155
 
156
    def OnData(self, evt):
157
        print "Received Data"
158
        self.app.AddTest2(evt.vibTest)
623 FredericG 159
 
612 FredericG 160
    def OnUpdate(self, evt):
613 FredericG 161
        print "Status update"
612 FredericG 162
        self.running = evt.running
613 FredericG 163
        if evt.error:
164
            self.error = True;
165
            self.text_ctrl_1.WriteText("ERROR: ")
166
            self.text_ctrl_1.SetBackgroundColour("Red")  
612 FredericG 167
        self.text_ctrl_1.WriteText("%s\n"%evt.msg)
168
        if (not self.running):
613 FredericG 169
            if (not self.error):
170
                self.text_ctrl_1.SetBackgroundColour("Green")
171
                self.text_ctrl_1.write(" ") # so that the background is redrawn
623 FredericG 172
            self.button.SetLabel("Close")
613 FredericG 173
 
623 FredericG 174
        if evt.speed != None:
175
            self.speedCtrl.SetSpeedValue(evt.speed)
176
 
177
        if evt.voltage != None:
178
            vmin,vmax,v = evt.voltage
179
            if self.firstVoltage:
180
                ticks = ["", "%.1f V"%vmin, "", "%.1f V"%vmax, ""]
181
                self.voltageCtrl.SetTicks(ticks)
182
                self.firstVoltage = False
183
            i = (v-vmin)/(vmax-vmin)  # 0..1
184
            i *= 2
185
            i = i+1
186
            i = min(max(i,0),5)
187
            self.voltageCtrl.SetSpeedValue(i)
188
 
189
 
612 FredericG 190
 
191
    def onButton(self, event): # wxGlade: MeasureDialog.<event_handler>
192
        if (not self.running):
193
            self.Destroy()
613 FredericG 194
        else:
195
            self.app.cancelMeasurement()
612 FredericG 196
 
197
# end of class MeasureDialog
198
 
199
 
607 FredericG 200
class SettingsDialog(wx.Dialog):
601 FredericG 201
    def __init__(self, *args, **kwds):
607 FredericG 202
        # begin wxGlade: SettingsDialog.__init__
203
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
204
        wx.Dialog.__init__(self, *args, **kwds)
608 FredericG 205
        self.button_5 = wx.Button(self, wx.ID_CANCEL, "")
206
        self.button_6 = wx.Button(self, wx.ID_OK, "")
601 FredericG 207
 
208
        self.__set_properties()
209
        self.__do_layout()
611 FredericG 210
 
211
        self.Bind(wx.EVT_BUTTON, self.onOK, self.button_6)
601 FredericG 212
        # end wxGlade
213
 
608 FredericG 214
        # The first argument that is passed to the constructor is the parent
611 FredericG 215
        self.settings = args[0].app.settings
608 FredericG 216
        # Add text-boxes for all settings
611 FredericG 217
        self.tb = []
218
        self.grid_sizer_2.SetRows(len(self.settings))
219
        for setting in self.settings.iteritems():
608 FredericG 220
            lb = wx.StaticText(self, -1, setting[1].descr, style=wx.ALIGN_RIGHT)
221
            tb = wx.TextCtrl(self, -1, str(setting[1].value))
222
            self.grid_sizer_2.Add(lb, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
223
            self.grid_sizer_2.Add(tb, 0, 0, 0)
611 FredericG 224
            self.tb.append(tb)
608 FredericG 225
        self.sizer_5.Fit(self)
226
        self.Layout()
227
 
601 FredericG 228
    def __set_properties(self):
607 FredericG 229
        # begin wxGlade: SettingsDialog.__set_properties
601 FredericG 230
        self.SetTitle("Settings")
231
        # end wxGlade
232
 
233
    def __do_layout(self):
607 FredericG 234
        # begin wxGlade: SettingsDialog.__do_layout
608 FredericG 235
        sizer_5 = wx.BoxSizer(wx.VERTICAL)
236
        grid_sizer_3 = wx.GridSizer(1, 2, 0, 0)
237
        sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
238
        grid_sizer_2 = wx.GridSizer(1, 2, 4, 4)
239
        sizer_5.Add((20, 20), 0, 0, 0)
240
        sizer_6.Add((20, 20), 0, 0, 0)
241
        sizer_6.Add(grid_sizer_2, 0, 0, 0)
242
        sizer_6.Add((20, 20), 0, 0, 0)
243
        sizer_5.Add(sizer_6, 1, wx.EXPAND, 0)
244
        sizer_5.Add((20, 20), 0, 0, 0)
245
        grid_sizer_3.Add(self.button_5, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
246
        grid_sizer_3.Add(self.button_6, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
247
        sizer_5.Add(grid_sizer_3, 0, wx.EXPAND, 0)
248
        sizer_5.Add((20, 20), 0, 0, 0)
249
        self.SetSizer(sizer_5)
250
        sizer_5.Fit(self)
601 FredericG 251
        self.Layout()
252
        # end wxGlade
253
 
608 FredericG 254
        # Store some of the items, we will need them later
255
        self.grid_sizer_2 = grid_sizer_2  
256
        self.sizer_5 = sizer_5
257
 
258
 
611 FredericG 259
    def onOK(self, event): # wxGlade: SettingsDialog.<event_handler>
260
        print "Updating parameters"
261
        try:
262
            i=0
263
            for setting in self.settings.iteritems():
264
              print setting[0], self.tb[i].GetValue()
265
              setting[1].set(self.tb[i].GetValue())
266
              i += 1
267
            event.Skip()
268
        except:
269
            wx.MessageBox("Invalid format for \"%s\" setting." % setting[1].descr)
270
 
607 FredericG 271
# end of class SettingsDialog
272
 
273
 
274
 
584 FredericG 275
class MainFrame(wx.Frame):
276
    def __init__(self, *args, **kwds):
277
        # begin wxGlade: MainFrame.__init__
278
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
279
        wx.Frame.__init__(self, *args, **kwds)
587 FredericG 280
 
281
        # Menu Bar
282
        self.frame_1_menubar = wx.MenuBar()
283
        wxglade_tmp_menu = wx.Menu()
607 FredericG 284
        wxglade_tmp_menu.Append(101, "Settings", "", wx.ITEM_NORMAL)
616 FredericG 285
        wxglade_tmp_menu.AppendSeparator()
607 FredericG 286
        wxglade_tmp_menu.Append(150, "Exit", "", wx.ITEM_NORMAL)
587 FredericG 287
        self.frame_1_menubar.Append(wxglade_tmp_menu, "File")
288
        wxglade_tmp_menu = wx.Menu()
616 FredericG 289
        wxglade_tmp_menu.Append(301, "Clear All", "", wx.ITEM_NORMAL)
290
        wxglade_tmp_menu.Append(302, "Clear Selected", "", wx.ITEM_NORMAL)
291
        wxglade_tmp_menu.AppendSeparator()
627 FredericG 292
        wxglade_tmp_menu.Append(310, "Select All", "", wx.ITEM_NORMAL)
293
        wxglade_tmp_menu.AppendSeparator()
616 FredericG 294
        wxglade_tmp_menu.Append(303, "Load", "", wx.ITEM_NORMAL)
295
        wxglade_tmp_menu.Append(304, "Save", "", wx.ITEM_NORMAL)
587 FredericG 296
        self.frame_1_menubar.Append(wxglade_tmp_menu, "TestSet")
297
        wxglade_tmp_menu = wx.Menu()
626 FredericG 298
        wxglade_tmp_menu.Append(401, "Flash VibTest FC software", "", wx.ITEM_NORMAL)
299
        wxglade_tmp_menu.Append(402, "Restore original FC software", "", wx.ITEM_NORMAL)
587 FredericG 300
        self.frame_1_menubar.Append(wxglade_tmp_menu, "MK")
301
        wxglade_tmp_menu = wx.Menu()
626 FredericG 302
        wxglade_tmp_menu.Append(1099, "About", "", wx.ITEM_NORMAL)
587 FredericG 303
        self.frame_1_menubar.Append(wxglade_tmp_menu, "Help")
304
        self.SetMenuBar(self.frame_1_menubar)
305
        # Menu Bar end
600 FredericG 306
        self.Description = wx.StaticText(self, -1, "Description")
613 FredericG 307
        self.tcDescr = wx.TextCtrl(self, -1, "Test")
600 FredericG 308
        self.label_37 = wx.StaticText(self, -1, "Speed(s)")
613 FredericG 309
        self.tcSpeeds = wx.TextCtrl(self, -1, "100-200:10")
600 FredericG 310
        self.label_35 = wx.StaticText(self, -1, "Motor(s)")
613 FredericG 311
        self.tcMotors = wx.TextCtrl(self, -1, "1")
600 FredericG 312
        self.label_38 = wx.StaticText(self, -1, "")
626 FredericG 313
        self.panel_2 = wx.Panel(self, -1)
613 FredericG 314
        self.label_36 = wx.StaticText(self, -1, "Channel(s)")
315
        self.tcChannels = wx.TextCtrl(self, -1, "6")
600 FredericG 316
        self.label_39 = wx.StaticText(self, -1, "")
626 FredericG 317
        self.panel_3 = wx.Panel(self, -1)
318
        self.button_4 = wx.Button(self, -1, "Measure")
319
        self.graphCtrl = wx.lib.plot.PlotCanvas(self, size=(800,300))
600 FredericG 320
        self.label_40 = wx.StaticText(self, -1, "Graph Type ")
321
        self.graphTypeChoice = wx.Choice(self, -1, choices=["Raw Signal", "Filtered Signal", "Spectrum"])
322
        self.label_41 = wx.StaticText(self, -1, "Y Axis Range ")
323
        self.yAxisChoice = wx.Choice(self, -1, choices=["25", "50", "100", "200"])
627 FredericG 324
        self.copyGraphButton = wx.Button(self, -1, "Copy Graph Data")
586 FredericG 325
        self.TestListCtrl = wx.ListCtrl(self, -1, style=wx.LC_REPORT|wx.SUNKEN_BORDER)
584 FredericG 326
 
327
        self.__set_properties()
328
        self.__do_layout()
587 FredericG 329
 
607 FredericG 330
        self.Bind(wx.EVT_MENU, self.OnSettings, id=101)
626 FredericG 331
        self.Bind(wx.EVT_MENU, self.onExit, id=150)
607 FredericG 332
        self.Bind(wx.EVT_MENU, self.onClear, id=301)
616 FredericG 333
        self.Bind(wx.EVT_MENU, self.onClearSelected, id=302)
627 FredericG 334
        self.Bind(wx.EVT_MENU, self.onSelectAll, id=310)
616 FredericG 335
        self.Bind(wx.EVT_MENU, self.OnImport, id=303)
336
        self.Bind(wx.EVT_MENU, self.onExport, id=304)
626 FredericG 337
        self.Bind(wx.EVT_MENU, self.onAbout, id=1099)
612 FredericG 338
        self.Bind(wx.EVT_BUTTON, self.onStartMeasure, self.button_4)
600 FredericG 339
        self.Bind(wx.EVT_CHOICE, self.onGraphTypeChange, self.graphTypeChoice)
340
        self.Bind(wx.EVT_CHOICE, self.onYAxisChange, self.yAxisChoice)
627 FredericG 341
        self.Bind(wx.EVT_BUTTON, self.onCopyGraphData, self.copyGraphButton)
584 FredericG 342
        # end wxGlade
626 FredericG 343
        favicon = wx.Icon('%s/Resources/60px-Procman.ico' % rootPath, wx.BITMAP_TYPE_ICO, 32, 32)
344
        wx.Frame.SetIcon(self, favicon)
584 FredericG 345
 
587 FredericG 346
    def setApp(self, app):
347
        self.app = app
585 FredericG 348
 
584 FredericG 349
    def __set_properties(self):
350
        # begin wxGlade: MainFrame.__set_properties
587 FredericG 351
        self.SetTitle("VibrationTest")
600 FredericG 352
        self.SetSize((850, 700))
353
        self.Description.SetMinSize((53, 13))
601 FredericG 354
        self.button_4.SetMinSize((80, 80))
626 FredericG 355
        self.graphCtrl.SetMinSize((800,300))
600 FredericG 356
        self.graphTypeChoice.SetSelection(0)
357
        self.yAxisChoice.SetSelection(1)
358
        self.TestListCtrl.SetMinSize((800,300))
584 FredericG 359
        # end wxGlade
360
 
361
    def __do_layout(self):
362
        # begin wxGlade: MainFrame.__do_layout
600 FredericG 363
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
364
        sizer_8 = wx.BoxSizer(wx.VERTICAL)
365
        sizer_11 = wx.BoxSizer(wx.VERTICAL)
366
        sizer_12 = wx.BoxSizer(wx.HORIZONTAL)
367
        sizer_9 = wx.BoxSizer(wx.HORIZONTAL)
368
        sizer_10 = wx.BoxSizer(wx.HORIZONTAL)
369
        grid_sizer_1 = wx.GridSizer(3, 4, 4, 5)
370
        sizer_3.Add((20, 20), 0, 0, 0)
371
        sizer_8.Add((20, 20), 0, 0, 0)
372
        grid_sizer_1.Add(self.Description, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
613 FredericG 373
        grid_sizer_1.Add(self.tcDescr, 0, 0, 0)
600 FredericG 374
        grid_sizer_1.Add(self.label_37, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
613 FredericG 375
        grid_sizer_1.Add(self.tcSpeeds, 0, 0, 0)
600 FredericG 376
        grid_sizer_1.Add(self.label_35, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
613 FredericG 377
        grid_sizer_1.Add(self.tcMotors, 0, 0, 0)
600 FredericG 378
        grid_sizer_1.Add(self.label_38, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
626 FredericG 379
        grid_sizer_1.Add(self.panel_2, 1, wx.EXPAND, 0)
600 FredericG 380
        grid_sizer_1.Add(self.label_36, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
613 FredericG 381
        grid_sizer_1.Add(self.tcChannels, 0, 0, 0)
600 FredericG 382
        grid_sizer_1.Add(self.label_39, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL, 0)
626 FredericG 383
        grid_sizer_1.Add(self.panel_3, 1, wx.EXPAND, 0)
600 FredericG 384
        sizer_9.Add(grid_sizer_1, 0, 0, 0)
385
        sizer_10.Add((50, 20), 0, 0, 0)
386
        sizer_10.Add(self.button_4, 0, wx.ALIGN_CENTER_VERTICAL, 0)
387
        sizer_9.Add(sizer_10, 1, wx.EXPAND, 0)
388
        sizer_8.Add(sizer_9, 0, 0, 0)
389
        sizer_8.Add((20, 30), 0, 0, 0)
626 FredericG 390
        sizer_11.Add(self.graphCtrl, 1, wx.EXPAND, 0)
600 FredericG 391
        sizer_11.Add((20, 5), 0, 0, 0)
392
        sizer_12.Add(self.label_40, 0, wx.ALIGN_CENTER_VERTICAL, 0)
393
        sizer_12.Add(self.graphTypeChoice, 0, 0, 0)
394
        sizer_12.Add((40, 20), 0, 0, 0)
395
        sizer_12.Add(self.label_41, 0, wx.ALIGN_CENTER_VERTICAL, 0)
396
        sizer_12.Add(self.yAxisChoice, 0, 0, 0)
627 FredericG 397
        sizer_12.Add((80, 20), 0, 0, 0)
398
        sizer_12.Add(self.copyGraphButton, 0, 0, 0)
600 FredericG 399
        sizer_11.Add(sizer_12, 0, 0, 0)
400
        sizer_8.Add(sizer_11, 0, 0, 0)
401
        sizer_8.Add((20, 30), 0, 0, 0)
402
        sizer_8.Add(self.TestListCtrl, 1, 0, 0)
403
        sizer_8.Add((20, 20), 0, 0, 0)
404
        sizer_3.Add(sizer_8, 1, wx.EXPAND, 0)
405
        self.SetSizer(sizer_3)
584 FredericG 406
        self.Layout()
600 FredericG 407
        self.SetSize((850, 700))
584 FredericG 408
        # end wxGlade
409
 
590 FredericG 410
        # List events
411
        self.TestListCtrl.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnTestSelected, self.TestListCtrl)
412
 
586 FredericG 413
        # Configure Graph
626 FredericG 414
        #self.graphCtrl = wx.lib.plot.PlotCanvas(self.GraphPanel, size=(800,300))
593 FredericG 415
 
626 FredericG 416
        self.graphCtrl.SetPointLabelFunc(self.DrawPointLabel)
585 FredericG 417
 
626 FredericG 418
        self.graphCtrl.SetFont(wx.Font(10,wx.SWISS,wx.NORMAL,wx.NORMAL))
419
        self.graphCtrl.SetFontSizeAxis(10)
420
        self.graphCtrl.SetFontSizeLegend(7)
421
        self.graphCtrl.setLogScale((False,False))
585 FredericG 422
 
586 FredericG 423
 
424
        # Configure TestListCtrl
594 FredericG 425
        self.TestListCtrl.InsertColumn(0, "Description")
619 FredericG 426
        self.TestListCtrl.InsertColumn(1, "Voltage")
427
        self.TestListCtrl.InsertColumn(2, "Speed")
428
        self.TestListCtrl.InsertColumn(3, "Channel")
429
        self.TestListCtrl.InsertColumn(4, "Vibration Value")
430
        self.TestListCtrl.SetColumnWidth(4, 500)
586 FredericG 431
 
585 FredericG 432
    def DrawPointLabel(self, dc, mDataDict):
433
        """This is the fuction that defines how the pointLabels are plotted
434
            dc - DC that will be passed
435
            mDataDict - Dictionary of data that you want to use for the pointLabel
436
 
437
            As an example I have decided I want a box at the curve point
438
            with some text information about the curve plotted below.
439
            Any wxDC method can be used.
440
        """
441
        # ----------
442
        dc.SetPen(wx.Pen(wx.BLACK))
443
        dc.SetBrush(wx.Brush( wx.BLACK, wx.SOLID ) )
444
 
445
        sx, sy = mDataDict["scaledXY"] #scaled x,y of closest point
446
        dc.DrawRectangle( sx-5,sy-5, 10, 10)  #10by10 square centered on point
447
        px,py = mDataDict["pointXY"]
448
        cNum = mDataDict["curveNum"]
449
        pntIn = mDataDict["pIndex"]
450
        legend = mDataDict["legend"]
451
        #make a string to display
452
        s = "Crv# %i, '%s', Pt. (%.2f,%.2f), PtInd %i" %(cNum, legend, px, py, pntIn)
453
        dc.DrawText(s, sx , sy+1)
454
        # -----------
455
 
596 FredericG 456
 
457
    def onNewTest(self, test):
458
        index = self.TestListCtrl.InsertStringItem(sys.maxint, test.descr)
619 FredericG 459
        self.TestListCtrl.SetStringItem(index, 1, "%.1f V" %test.voltage)
460
        self.TestListCtrl.SetStringItem(index, 2, str(test.speed))
461
        self.TestListCtrl.SetStringItem(index, 3, test.channel)
607 FredericG 462
 
615 FredericG 463
        vv = int(test.getVibValue(self.app.settings["hpf"].value, self.app.settings["lpf"].value))
607 FredericG 464
        vvs = "|%s| (%d)" % ("----------------------------------------------------------------------------------------------------"[0:min(vv,100)], vv)
619 FredericG 465
        self.TestListCtrl.SetStringItem(index, 4, vvs)
613 FredericG 466
        self.TestListCtrl.Select(index)
596 FredericG 467
 
468
 
590 FredericG 469
    def OnTestSelected(self, event):
470
         testId = event.m_itemIndex
471
         print "Test Selected id=%d" % (testId)
592 FredericG 472
         self.activeTestId = testId
473
         self.drawGraph()
590 FredericG 474
 
592 FredericG 475
    def drawGraph(self):
607 FredericG 476
 
600 FredericG 477
         y = int(self.yAxisChoice.GetStringSelection())
590 FredericG 478
 
607 FredericG 479
         nbSelected = self.TestListCtrl.SelectedItemCount
596 FredericG 480
 
616 FredericG 481
         if nbSelected == 0:
626 FredericG 482
              self.graphCtrl.Clear()
616 FredericG 483
 
484
         elif nbSelected > 1:
607 FredericG 485
             self.graphTypeChoice.Disable()
627 FredericG 486
             self.copyGraphButton.Enable()
607 FredericG 487
             x = 1
488
             data = []
489
             idx = self.TestListCtrl.GetFirstSelected()
490
             while idx != -1:
615 FredericG 491
                 data.append([x,self.app.getTest(idx).getVibValue(self.app.settings["hpf"].value, self.app.settings["lpf"].value)])
607 FredericG 492
                 x += 1
493
                 idx = self.TestListCtrl.GetNextSelected(idx)
494
             line = wx.lib.plot.PolyLine(data, legend= 'Vibrations', colour='red', width=2)
495
             markers = wx.lib.plot.PolyMarker(data, legend= '', colour='red', marker='circle',size=2)
496
             title = "Comparing tests"
626 FredericG 497
             self.graphCtrl.Draw(wx.lib.plot.PlotGraphics([line, markers], title, "Test", "Vibration Value"), xAxis=(1,max(x,10)), yAxis=(0,y))
498
             self.graphCtrl.SetEnableGrid('Horizontal')
600 FredericG 499
 
607 FredericG 500
         else:
501
             self.graphTypeChoice.Enable()
627 FredericG 502
             self.copyGraphButton.Disable()
607 FredericG 503
             vibTest = self.app.getTest(self.activeTestId)
504
             nb = vibTest.getDataLen()
590 FredericG 505
 
607 FredericG 506
             if self.graphTypeChoice.GetSelection() == 0:
507
                 xydata = _Numeric.linspace(0,0.09*nb,2*nb)
508
                 xydata.shape = (nb, 2)
509
                 xydata[:,1] = vibTest.getRawData()
510
                 line = wx.lib.plot.PolyLine(xydata, legend= 'Raw Data', colour='red', width=2)
511
 
512
                 title = "Raw Signal: %s %s %d" %(vibTest.descr, vibTest.channel, vibTest.speed)
626 FredericG 513
                 self.graphCtrl.Draw(wx.lib.plot.PlotGraphics([line], title, "Time (ms)", "Acc"), yAxis= (-y,y))
514
                 self.graphCtrl.SetEnableGrid('Horizontal')
599 FredericG 515
 
607 FredericG 516
             if self.graphTypeChoice.GetSelection() == 1:
517
                 xydata = _Numeric.linspace(0,0.09*nb,2*nb)
518
                 xydata.shape = (nb, 2)
615 FredericG 519
                 xydata[:,1] = vibTest.getFilteredData(self.app.settings["hpf"].value, self.app.settings["lpf"].value)
607 FredericG 520
                 line = wx.lib.plot.PolyLine(xydata, legend= 'Raw Data', colour='red', width=2)
521
 
522
                 title = "Filtered Signal: %s %s %d" %(vibTest.descr, vibTest.channel, vibTest.speed)
626 FredericG 523
                 self.graphCtrl.Draw(wx.lib.plot.PlotGraphics([line], title, "Time (ms)", "Acc"), yAxis= (-y,y))
524
                 self.graphCtrl.SetEnableGrid('Horizontal')
607 FredericG 525
 
526
             elif self.graphTypeChoice.GetSelection() == 2:
615 FredericG 527
                 xydata = _Numeric.linspace(0,FS/2,nb)
607 FredericG 528
                 xydata.shape = (nb/2, 2)
529
 
530
                 xydata[:,1] = vibTest.getSpectrum()
531
 
532
                 line = wx.lib.plot.PolyLine(xydata, legend= 'Spectrum', colour='red')
533
                 markers = wx.lib.plot.PolyMarker(xydata, legend= '', colour='red', marker='circle',size=2)
615 FredericG 534
 
535
                 fc = self.app.settings["hpf"].value
536
                 filterLine1 = wx.lib.plot.PolyLine(((fc,0),(fc,y)), legend='HP Filter', colour='Black', width=4)
537
                 fc = self.app.settings["lpf"].value
538
                 filterLine2 = wx.lib.plot.PolyLine(((fc,0),(fc,y)), legend='HP Filter', colour='Black', width=4)
607 FredericG 539
 
540
                 title = "Spectrum: %s %s %d" %(vibTest.descr, vibTest.channel, vibTest.speed)
626 FredericG 541
                 self.graphCtrl.Draw(wx.lib.plot.PlotGraphics([line,markers, filterLine1, filterLine2], title, "Freq (Hz)", "Acc"), xAxis=(0,200), yAxis= (-0,y))
542
                 self.graphCtrl.SetEnableGrid(True)
590 FredericG 543
 
599 FredericG 544
 
587 FredericG 545
    def OnImport(self, event): # wxGlade: MainFrame.<event_handler>
606 FredericG 546
        dlg = wx.FileDialog(
547
            self, message="Choose a file",
619 FredericG 548
            defaultDir="%s/Data/" % os.getcwd(),
606 FredericG 549
            defaultFile="*.txt",
619 FredericG 550
            wildcard="*.txt",
606 FredericG 551
            style=wx.OPEN | wx.CHANGE_DIR
552
            )
553
        if dlg.ShowModal() == wx.ID_OK:
554
            paths = dlg.GetPaths();
619 FredericG 555
            self.app.loadTests(paths[0])
606 FredericG 556
        dlg.Destroy()
587 FredericG 557
 
619 FredericG 558
    def onExport(self, event): # wxGlade: MainFrame.<event_handler>
559
        dlg = wx.FileDialog(
560
              self, message="Save file as ...",
561
              defaultDir="%s/Data/" % os.getcwd(),
562
              defaultFile="*.txt",
563
              wildcard="",
564
              style=wx.SAVE
565
              )
566
        if dlg.ShowModal() == wx.ID_OK:
567
            paths = dlg.GetPaths();
568
            self.app.saveTests(paths[0])
569
        dlg.Destroy()
570
 
599 FredericG 571
    def onYAxisChange(self, event): # wxGlade: MainFrame.<event_handler>
592 FredericG 572
        self.drawGraph()
573
 
600 FredericG 574
    def onGraphTypeChange(self, event): # wxGlade: MainFrame.<event_handler>
599 FredericG 575
        self.drawGraph()
576
 
607 FredericG 577
    def OnSettings(self, event): # wxGlade: MainFrame.<event_handler>
578
        dlg = SettingsDialog(self, -1, "Sample Dialog", size=(350, 200),
579
                         #style=wx.CAPTION | wx.SYSTEM_MENU | wx.THICK_FRAME,
580
                         style=wx.DEFAULT_DIALOG_STYLE, # & ~wx.CLOSE_BOX
581
                         )
582
        dlg.CenterOnScreen()
611 FredericG 583
        val = dlg.ShowModal()  # this does not return until the dialog is closed.
607 FredericG 584
        dlg.Destroy()
611 FredericG 585
        self.app.onSettingsChanged()
607 FredericG 586
 
612 FredericG 587
    def onStartMeasure(self, event): # wxGlade: MainFrame.<event_handler>
613 FredericG 588
        # Collect measure parameters
589
        mp = MeasureParameters()
590
        mp.descr = self.tcDescr.GetValue()
591
        mp.motors = map(int,self.tcMotors.GetValue().split(','))
592
        mp.channels = map(int,self.tcChannels.GetValue().split(','))
593
        s = self.tcSpeeds.GetValue()
619 FredericG 594
        if s=="test":
595
          mp.speeds = (100,100,100,100,100, 150,150,150,150,150, 200,200,200,200,200, 100,150,200, 100,150,200, 100,150,200, 100,150,200)
596
        elif s.count("-") == 1:
613 FredericG 597
            # assume from-to:step format
598
            s = s.split("-")
599
            if len(s) != 2: raise Exception("Invalid format")
600
            s[1] = s[1].split(":")
601
            if len(s[1]) != 2: raise Exception("Invalid format")
602
            mp.speeds = range(int(s[0]),int(s[1][0])+int(s[1][1]),int(s[1][1]))
603
        else:
614 FredericG 604
            mp.speeds = map(int,s.split(','))
605
 
613 FredericG 606
 
612 FredericG 607
        # create the dialog that will show the satus
608
        dlg = MeasureDialog(self)
609
        dlg.CenterOnScreen()
613 FredericG 610
 
611
        # Signal the application
612
        self.app.startMeasure(mp, dlg)
613
 
612 FredericG 614
        # Show the dialog
615
        val = dlg.ShowModal()  # this does not return until the dialog is closed.
616
        dlg.Destroy()
617
 
616 FredericG 618
    def _removeTest(self, idx):
619
        print "Deleting test %d" % idx
620
        self.app.removeTest(idx)
621
        self.TestListCtrl.DeleteItem(idx)
622
 
623
 
624
    def onClear(self, event): # wxGlade: MainFrame.<event_handler>
625
        print "Clear all tests"
626
        for i in range(len(self.app.VibTests)-1, -1, -1):
627
            self._removeTest(i)
628
        self.drawGraph()
629
 
630
 
631
    def onClearSelected(self, event): # wxGlade: MainFrame.<event_handler>
632
        while True:
633
            idx = self.TestListCtrl.GetFirstSelected()
634
            if idx == -1: break
635
            self._removeTest(idx)
636
 
627 FredericG 637
 
626 FredericG 638
 
639
    def onAbout(self, event): # wxGlade: MainFrame.<event_handler>
640
         # First we create and fill the info object
641
        print "about"
642
        info = wx.AboutDialogInfo()
643
        info.Name = "MK Vibration Test"
644
        info.Version = "0.2"
645
        info.Copyright = ""
646
        info.Developers=["Frederic Goddeeris"]
647
        info.Description = "Please consult the WIKI page for a complete description of the tool:"
648
        info.WebSite = ("http://www.mikrokopter.de/ucwiki/en/VibrationTest", "VibrationTest WIKI page")
649
        wx.AboutBox(info)
650
 
651
 
652
    def onExit(self, event): # wxGlade: MainFrame.<event_handler>
653
        self.Close(True)
654
 
655
 
627 FredericG 656
    def onSelectAll(self, event): # wxGlade: MainFrame.<event_handler>
657
        for i in xrange(self.TestListCtrl.GetItemCount()):
658
          self.TestListCtrl.Select(i)
659
 
660
    def onCopyGraphData(self, event): # wxGlade: MainFrame.<event_handler>
661
        clipdata = wx.TextDataObject()
662
        txt = ""
663
        idx = self.TestListCtrl.GetFirstSelected()
664
        while idx != -1:
665
             txt += ("%d\n" % self.app.getTest(idx).getVibValue(self.app.settings["hpf"].value, self.app.settings["lpf"].value))
666
             idx = self.TestListCtrl.GetNextSelected(idx)
667
        clipdata.SetText(txt)
668
        wx.TheClipboard.Open()
669
        wx.TheClipboard.SetData(clipdata)
670
        wx.TheClipboard.Close()
671
 
584 FredericG 672
# end of class MainFrame
673
 
607 FredericG 674
class Setting:
675
    def __init__(self, descr, defaultValue):
676
        self.descr = descr
677
        self.value = defaultValue
611 FredericG 678
 
679
    def set(self, newValue):
680
        if isinstance(self.value, int):
681
            self.value = int(newValue)
682
        else:
683
            self.value = str(newValue)
584 FredericG 684
 
613 FredericG 685
class MeasureParameters:
686
      pass
612 FredericG 687
 
688
class MeasureThread:
613 FredericG 689
    def __init__(self, measureParameters, evtConsumer):
690
        self.mk = mkProto.MkComm()
691
        self.param = measureParameters
612 FredericG 692
        self.evtConsumer = evtConsumer
613 FredericG 693
        self.cancel = False
694
        self.running = False
612 FredericG 695
 
696
    def start(self):
697
        thread.start_new_thread(self._run, ())
698
 
613 FredericG 699
    def stop(self):
700
        self.cancel = True
701
 
702
    def _testCancel(self):
703
        if self.cancel:
704
            raise Exception("Operation cancelled")
705
 
623 FredericG 706
    def _sendEvent(self, msg=None, error=False, parVoltage=None, speed=None):
707
        evt = MeasStatusUpdateEvent(running=self.running, msg=msg, error=error, voltage=parVoltage, speed=speed)
613 FredericG 708
        wx.PostEvent(self.evtConsumer, evt)
709
 
710
    def _setMotorSpeed(self, speed, settlingTime):
711
        speeds = [0,0,0,0]
712
        for motor in self.param.motors:
713
            speeds[motor-1] = speed
714
        for i in range(int(settlingTime*10)):
715
          self._testCancel()
716
          self.mk.setMotorTest(speeds)
717
          time.sleep(.1)
718
 
719
 
612 FredericG 720
    def _run(self):
613 FredericG 721
        self.running = True
722
        self._sendEvent("Starting test \"%s\"" % self.param.descr)    
723
 
724
        try:
725
            self._sendEvent("Opening SerialPort \"%s\"" % self.param.serialPort)
726
            self.mk.open(comPort=self.param.serialPort)
612 FredericG 727
 
613 FredericG 728
            msg = self.mk.getVersionMsg()
729
            version = msg.getVersion()
730
            self._sendEvent("Version: %d.%d" % version)
731
 
732
            msg = self.mk.getDebugMsg()
733
            voltage = msg.getVoltage()
734
            if (voltage == 0):
628 FredericG 735
              # Board is probably fed by USB
613 FredericG 736
              minVoltage = 0
623 FredericG 737
              maxVoltage = 1
613 FredericG 738
            else:
628 FredericG 739
              # Determine the n umber of cells
613 FredericG 740
              if (voltage > 4.2*3):
623 FredericG 741
                nbCells = 4  
613 FredericG 742
              else:
623 FredericG 743
                nbCells = 3
628 FredericG 744
              # Set minimum and maximum voltages
745
              if self.param.minVoltage > 0:
746
                minVoltage =  self.param.minVoltage
747
              else:
748
                minVoltage = nbCells*3.5  # auto
749
              if self.param.maxVoltage > 0:
750
                maxVoltage =  self.param.maxVoltage
751
              else:
752
                maxVoltage = nbCells*3.9  
753
 
613 FredericG 754
            self._sendEvent("Voltage: %2.1fV" % voltage)
623 FredericG 755
            self._sendEvent("Min/Max Voltage: %2.1fV-%2.1fV" % (minVoltage, maxVoltage), parVoltage=(minVoltage, maxVoltage, voltage))
756
 
757
            self._sendEvent("Starting motor(s) (speed=%d)... " % self.param.motorStartupSpeed, speed=self.param.motorStartupSpeed)
613 FredericG 758
            self._setMotorSpeed(self.param.motorStartupSpeed, self.param.motorStartupSettlingTime)
759
 
760
            for speed in self.param.speeds:
623 FredericG 761
                self._sendEvent("Changing motor speed to %d... " % speed, speed=speed)
613 FredericG 762
                self._setMotorSpeed(speed, 1)
763
 
764
                for channel in self.param.channels:
765
                    self._setMotorSpeed(speed, .1)
619 FredericG 766
                    msg = self.mk.getDebugMsg()
767
                    voltage = msg.getVoltage()
768
 
623 FredericG 769
                    self._sendEvent("Getting data from channel %s" % CHANNEL_NAMES[channel], parVoltage=(minVoltage, maxVoltage, voltage))
613 FredericG 770
                    data = self.mk.doVibrationTest(1000, channel)
771
 
619 FredericG 772
                    vt = VibTest(self.param.descr, voltage, self.param.motors, speed, CHANNEL_NAMES[channel], data)
613 FredericG 773
                    evt = MeasDataEvent(vibTest = vt)
774
                    wx.PostEvent(self.evtConsumer, evt)
775
 
619 FredericG 776
                    if voltage<minVoltage:
777
                        raise Exception("Voltage too low")
778
 
613 FredericG 779
            self._sendEvent("Done !")            
612 FredericG 780
 
613 FredericG 781
        except Exception, e:
782
            self._sendEvent("Exception \"%s\"" % e, error=True)  
783
 
784
        self.running = False
785
        self._sendEvent("")    
786
 
612 FredericG 787
 
586 FredericG 788
class VibTest:
619 FredericG 789
    def __init__(self, descr, voltage, motor, speed, channel, rawData):
594 FredericG 790
        self.descr = descr
619 FredericG 791
        self.voltage = voltage
594 FredericG 792
        self.motor = motor
793
        self.speed = speed
596 FredericG 794
        self.channel = channel
599 FredericG 795
 
796
        self.dataLen = len(rawData)
797
 
590 FredericG 798
        self.rawData = _Numeric.array(rawData)
799
        self.dc = self.rawData.mean()
800
        self.rawData -= self.dc
586 FredericG 801
 
600 FredericG 802
        self.fft = _Numeric.fft.rfft(self.rawData)
803
 
804
        self.spectrum = None
805
        self.filteredData = None
615 FredericG 806
        self.fc1 = None
807
        self.fc2 = None
599 FredericG 808
 
602 FredericG 809
        self.vibValue = None
810
 
594 FredericG 811
    def getDescr(self):
812
        return self.Descr
587 FredericG 813
 
590 FredericG 814
    def getRawData(self):
815
        return self.rawData
816
 
817
    def getDataLen(self):
818
        return self.dataLen
819
 
599 FredericG 820
    def getSpectrum(self):
600 FredericG 821
        if self.spectrum == None:
822
            self.spectrum = _Numeric.absolute(self.fft[1:self.dataLen/2+1]) / (self.dataLen/2)
823
        return self.spectrum
590 FredericG 824
 
615 FredericG 825
    def getFilteredData(self, fc1, fc2):
826
        if self.fc1 != fc1 or self.fc2 != fc2:
827
            self.filteredData = None  
828
 
600 FredericG 829
        if self.filteredData == None:
830
            tmpfft = self.fft.copy()
615 FredericG 831
            fc = (float(fc1))/(FS/2)*len(tmpfft)
832
            print "fc1=%d => fc=%d" % (fc1,fc)
833
            for i in range(0,int(fc)):
600 FredericG 834
                tmpfft[i] = 0
615 FredericG 835
            fc = (float(fc2))/(FS/2)*len(tmpfft)
836
            print "fc2=%d => fc=%d" % (fc2,fc)
837
            for i in range(int(fc), len(tmpfft)):
600 FredericG 838
                tmpfft[i] = 0
839
            self.filteredData = _Numeric.fft.irfft(tmpfft)
615 FredericG 840
            self.fc1 = fc1
841
            self.fc2 = fc2
842
 
600 FredericG 843
        return self.filteredData
602 FredericG 844
 
615 FredericG 845
    def getVibValue(self, fc1, fc2):
846
      if self.fc1 != fc1 or self.fc2 != fc2:
847
        self.vibValue = None  
602 FredericG 848
      if self.vibValue == None:
615 FredericG 849
        fd = self.getFilteredData(fc1, fc2)[100:-100];
602 FredericG 850
        self.vibValue = max(fd)-min(fd)
851
      return self.vibValue
599 FredericG 852
 
607 FredericG 853
 
854
 
584 FredericG 855
class App(wx.App):
607 FredericG 856
 
857
    SETTINGSFILE = "settings.cfg"
858
 
586 FredericG 859
    def __init__(self, par):
860
        self.VibTests = []
861
        wx.App.__init__(self, par)
862
 
607 FredericG 863
        # Init settings
864
        self.settings={}
611 FredericG 865
        self.settings["serialport"] = Setting("Serial Port", "COM1")
613 FredericG 866
        self.settings["startupspeed"] = Setting("Motor Startup Speed", 25)
867
        self.settings["startupsettling"] = Setting("Motor Startup Setting time (s)", 3)
868
        self.settings["serialport"] = Setting("Serial Port", "COM1")
607 FredericG 869
        self.settings["hpf"] = Setting("HP Filter cutoff (Hz)", 50)
615 FredericG 870
        self.settings["lpf"] = Setting("LP Filter cutoff (Hz)", 180)
628 FredericG 871
        self.settings["minvoltage"] = Setting("Minimum Bettery Voltage (0=Automatic) (V) ", 0)
872
        self.settings["maxvoltage"] = Setting("Maximum Bettery Voltage (0=Automatic) (V) ", 0)
607 FredericG 873
 
628 FredericG 874
 
607 FredericG 875
        self.readSettings()
876
 
606 FredericG 877
        if len(sys.argv)>1:
619 FredericG 878
            self.loadTests(sys.argv[1])
586 FredericG 879
 
606 FredericG 880
 
607 FredericG 881
    def readSettings(self):
882
        print "Reading settings"
883
        cp = ConfigParser.ConfigParser()
884
 
885
        try:
886
            cp.read(App.SETTINGSFILE)
887
            for setting in cp.items("DEFAULT"):
888
                print " ",setting
889
                try:
613 FredericG 890
                    self.settings[setting[0]].set(setting[1])
607 FredericG 891
                except:
892
                    print "WARNING, unknown setting"
893
        except:
894
            print "ERROR reading settingsfile"
895
 
896
 
897
    def storeSettings(self):
898
        print "Storing settings"
899
 
900
        cp = ConfigParser.ConfigParser()
901
        for setting in self.settings.iteritems():
902
            cp.set("", setting[0], setting[1].value)
903
 
904
        file = open(App.SETTINGSFILE, "w")
905
        cp.write(file)
906
        file.close()
907
 
611 FredericG 908
 
909
    def onSettingsChanged(self):
910
        self.storeSettings()
613 FredericG 911
 
912
    def AddTest2(self, vibTest):
913
        self.VibTests.append(vibTest)
914
        self.frame_1.onNewTest(vibTest)
611 FredericG 915
 
619 FredericG 916
    def AddTest(self, descr, voltage, motor, speed, channel, rawData):
917
        test = VibTest(descr, voltage, motor, speed, channel, rawData)
613 FredericG 918
        self.AddTest2(test)
586 FredericG 919
 
616 FredericG 920
    def removeTest(self, idx):
921
        del(self.VibTests[idx])
922
 
590 FredericG 923
    def getTest(self, testId):
924
        return self.VibTests[testId]
925
 
584 FredericG 926
    def OnInit(self):
927
        wx.InitAllImageHandlers()
586 FredericG 928
        self.frame_1 = MainFrame(None, -1, "")
587 FredericG 929
        self.frame_1.setApp(self);
586 FredericG 930
        self.SetTopWindow(self.frame_1)
931
 
607 FredericG 932
        self.frame_1.CenterOnScreen()
587 FredericG 933
        self.frame_1.Show()
934
        return 1
935
 
619 FredericG 936
    def saveTests(self, filePath):
937
        try:
938
          logfile = open(filePath, "r")
939
          newFile = False
940
          logfile.close()
941
        except:
942
          newFile = True
943
 
944
        for test in self.VibTests:
945
          if newFile:
946
            logfile = open(filePath, "w")
947
            print "Writing result to %s ..." % filePath,
948
            logfile.write("%s %d %s\n" % (test.descr, test.speed, test.channel))
949
            for value in test.rawData:
950
              logfile.write("%d\n" % value)
951
            logfile.close()  
952
            print "OK"
953
          else:
954
            print "Appending result to %s ..." % filePath,
955
            logfile = open(filePath, "r")
956
            prevData = []
957
            for line in logfile:
958
              prevData.append(line[:-1])
959
            logfile.close()
960
            logfile = open(filePath, "w")
961
            logfile.write("%s,%s %d %s\n" % (prevData[0], test.descr, test.speed, test.channel))
962
            i = 1
963
            for value in test.rawData:
964
              logfile.write("%s,%d\n" % (prevData[i], value))
965
              i += 1
966
            logfile.close()
967
            print "OK"
968
          newFile = False
969
 
970
 
971
    def loadTests(self, filePath):
596 FredericG 972
 
606 FredericG 973
        print "Importing file \"%s\"" % filePath
596 FredericG 974
 
975
        logfile = open(filePath, "r")
590 FredericG 976
        data = None
596 FredericG 977
 
619 FredericG 978
        headers = (logfile.readline()[:-1]).split(',')
596 FredericG 979
        nbCols = len(headers)
980
        print "NbCols =", nbCols
981
 
982
        data = []
983
        descr = []
984
        speed = []
985
        channel = []
986
        for c in range(nbCols):
987
            data.append([])
988
            h = headers[c].split(' ')
989
            descr.append(h[0]);
990
            speed.append(h[1]);
991
            channel.append(h[2]);
992
 
590 FredericG 993
        for line in logfile:
994
            values = line.split(',')
596 FredericG 995
            for i in range(nbCols):
996
                data[i].append(int(values[i]))
590 FredericG 997
        logfile.close()
596 FredericG 998
 
999
        for c in range(nbCols):
599 FredericG 1000
            if (len(data[c]) % 2) != 0:
1001
                data[c].append(data[c][-1])
619 FredericG 1002
            self.AddTest(descr[c], 0, 0, int(speed[c]), channel[c], data[c])
612 FredericG 1003
 
613 FredericG 1004
    def startMeasure(self, measureParams, dialog):
612 FredericG 1005
        print "Start measuring"
590 FredericG 1006
 
613 FredericG 1007
        measureParams.serialPort = self.settings["serialport"].value
1008
        measureParams.motorStartupSpeed = self.settings["startupspeed"].value
1009
        measureParams.motorStartupSettlingTime = self.settings["startupsettling"].value
628 FredericG 1010
        measureParams.minVoltage = self.settings["minvoltage"].value
1011
        measureParams.maxVoltage = self.settings["maxvoltage"].value        
613 FredericG 1012
 
1013
        self.measureThread = MeasureThread(measureParams, dialog)
612 FredericG 1014
        self.measureThread.start()
590 FredericG 1015
 
613 FredericG 1016
    def cancelMeasurement(self):
1017
        print "Measuring CANCEL"
1018
 
1019
        self.measureThread.stop()
1020
 
612 FredericG 1021
 
1022
 
1023
 
1024
 
590 FredericG 1025
 
586 FredericG 1026
 
590 FredericG 1027
 
584 FredericG 1028
# end of class App
1029
 
1030
if __name__ == "__main__":
626 FredericG 1031
 
1032
    rootPath = os.path.abspath(os.path.dirname(sys.argv[0]))
1033
 
1034
    print rootPath
1035
 
584 FredericG 1036
    VibrationTestGui = App(0)
1037
    VibrationTestGui.MainLoop()