Subversion Repositories Projects

Rev

Rev 564 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 564 Rev 565
1
#! /usr/bin/env python
1
#! /usr/bin/env python
2
 
2
 
3
#
3
#
4
# Mikrokopter Vibration Test
4
# Mikrokopter Vibration Test
5
#
5
#
6
# Author: FredericG
6
# Author: FredericG
7
# 
7
# 
8
 
8
 
9
 
9
 
10
import mkProto
10
import mkProto
11
import time
11
import time
12
import traceback
12
import traceback
13
import getopt
13
import getopt
14
import sys
14
import sys
15
 
-
 
-
 
15
 
16
 
16
CHECK_VERSION = False
17
 
17
 
18
CHANNEL_NAMES = ["GyroYaw", "GyroRoll", "GyroNick", "Pressure", "Batt", "AccTop", "AccRoll", "AccNick"]
18
CHANNEL_NAMES = ["GyroYaw", "GyroRoll", "GyroNick", "Pressure", "Batt", "AccTop", "AccRoll", "AccNick"]
19
 
19
 
20
def usage():
20
def usage():
21
    print
21
    print
22
    print "VibrationTest.py COMPORT MOTORS SPEEDS CHANNELS [-m MINSPEED] [-s NBSAMPLES] [-n NAME] [-d FILENAME] [-v]"
22
    print "VibrationTest.py COMPORT MOTORS SPEEDS CHANNELS [-m MINSPEED] [-s NBSAMPLES] [-n NAME] [-d FILENAME] [-v]"
23
    print " COMPORT         Serial port to use. e.g. COM4"
23
    print " COMPORT         Serial port to use. e.g. COM4"
24
    print " MOTORS          Motors to activate during test. Multiple motors can be used at the same time. e.g. 1,2,3,4"
24
    print " MOTORS          Motors to activate during test. Multiple motors can be used at the same time. e.g. 1,2,3,4"
25
    print " SPEEDS          Indicates at what speeds the motors need to be tested. "
25
    print " SPEEDS          Indicates at what speeds the motors need to be tested. "
26
    print "                   Format 1:  e.g. 50,110,140            Tests at speeds 50, 110 and 140"
26
    print "                   Format 1:  e.g. 50,110,140            Tests at speeds 50, 110 and 140"
27
    print "                   Format 2:  e.g. 100-200:50            Tests at speeds 100, 150 and 200"
27
    print "                   Format 2:  e.g. 100-200:50            Tests at speeds 100, 150 and 200"
28
    print " CHANNELS        Channels to monitor. e.g. 5,6,7"
28
    print " CHANNELS        Channels to monitor. e.g. 5,6,7"
29
    for i,channelName in enumerate(CHANNEL_NAMES):
29
    for i,channelName in enumerate(CHANNEL_NAMES):
30
      print "                   Channel %d = %s" % (i, channelName)
30
      print "                   Channel %d = %s" % (i, channelName)
31
    print " -m MINSPEED     Minimum speed of the motor(s)"
31
    print " -m MINSPEED     Minimum speed of the motor(s)"
32
    print " -s NBSAMPLES    Number of samples"
32
    print " -s NBSAMPLES    Number of samples"
33
    print " -n NAME         Name of the test"
33
    print " -n NAME         Name of the test"
34
    print " -d FILENAME     File to which the measured values will be logged in"
34
    print " -d FILENAME     File to which the measured values will be logged in"
35
    print " -v              Verbose"          
35
    print " -v              Verbose"          
36
    print
36
    print
37
   
37
   
38
def exit_usage():
38
def exit_usage():
39
    usage()
39
    usage()
40
    sys.exit(2)
40
    sys.exit(2)
41
 
41
 
42
if __name__ == '__main__':
42
if __name__ == '__main__':
43
 
43
 
44
    #print sys.argv
44
    #print sys.argv
45
    if len(sys.argv)<5:
45
    if len(sys.argv)<5:
46
        exit_usage()
46
        exit_usage()
47
 
47
 
48
    parComPort = sys.argv[1]
48
    parComPort = sys.argv[1]
49
    parMotors = sys.argv[2].split(',')
49
    parMotors = sys.argv[2].split(',')
50
    parChannels = sys.argv[4].split(',')
50
    parChannels = sys.argv[4].split(',')
51
 
51
 
52
    par = sys.argv[3]
52
    par = sys.argv[3]
53
    if par.count("-") == 1:
53
    if par.count("-") == 1:
54
        # assume from-to:step format
54
        # assume from-to:step format
55
        par = par.split("-")
55
        par = par.split("-")
56
        if len(par) != 2:
56
        if len(par) != 2:
57
            exit_usage()
57
            exit_usage()
58
           
58
           
59
        par[1] = par[1].split(":")
59
        par[1] = par[1].split(":")
60
        if len(par[1]) != 2:
60
        if len(par[1]) != 2:
61
            exit_usage()
61
            exit_usage()
62
   
62
   
63
        parSpeeds = range(int(par[0]),int(par[1][0])+int(par[1][1]),int(par[1][1]))
63
        parSpeeds = range(int(par[0]),int(par[1][0])+int(par[1][1]),int(par[1][1]))
64
       
64
       
65
    else:
65
    else:
66
        parSpeeds = par.split(',')
66
        parSpeeds = par.split(',')
67
   
67
   
68
    try:
68
    try:
69
        opts, args = getopt.getopt(sys.argv[5:], "s:n:d:m:v")
69
        opts, args = getopt.getopt(sys.argv[5:], "s:n:d:m:v")
70
    except Exception, err:
70
    except Exception, err:
71
        print str(err) # will print something like "option -a not recognized"
71
        print str(err) # will print something like "option -a not recognized"
72
        usage()
72
        usage()
73
        sys.exit(2)
73
        sys.exit(2)
74
 
74
 
75
    parVerbose = False
75
    parVerbose = False
76
    parFileName = None
76
    parFileName = None
77
    parNbSamples = 400
77
    parNbSamples = 400
78
    parMinSpeed = 25
78
    parMinSpeed = 25
79
    parTestName = ""
79
    parTestName = ""
80
 
80
 
81
    for o, a in opts:
81
    for o, a in opts:
82
        if o == "-v":
82
        if o == "-v":
83
            parVerbose = True
83
            parVerbose = True
84
        elif o == "-d":
84
        elif o == "-d":
85
            parFileName = a
85
            parFileName = a
86
        elif o == "-s":
86
        elif o == "-s":
87
            parNbSamples = int(a)
87
            parNbSamples = int(a)
88
        elif o == "-m":
88
        elif o == "-m":
89
            parMinSpeed = int(a)
89
            parMinSpeed = int(a)
90
        elif o == "-n":
90
        elif o == "-n":
91
            parTestName = a
91
            parTestName = a
92
           
92
           
93
        else:
93
        else:
94
            assert False, "unhandled option %s" % (o)
94
            assert False, "unhandled option %s" % (o)
95
 
95
 
96
    if parVerbose:
96
    if parVerbose:
97
        print "comPort  =", parComPort
97
        print "comPort  =", parComPort
98
        print "motors   =", parMotors
98
        print "motors   =", parMotors
99
        print "minSpeed =", parMinSpeed
99
        print "minSpeed =", parMinSpeed
100
        print "speeds   =", parSpeeds
100
        print "speeds   =", parSpeeds
101
        print "channels =", parChannels
101
        print "channels =", parChannels
102
        print "nbSamples=", parNbSamples
102
        print "nbSamples=", parNbSamples
103
        print "fileName =", parFileName
103
        print "fileName =", parFileName
104
        print "testName =", parTestName
104
        print "testName =", parTestName
105
 
105
 
106
 
106
 
107
    try:
107
    try:
108
        parMotors = map(int, parMotors)
108
        parMotors = map(int, parMotors)
109
        parSpeeds = map(int, parSpeeds)
109
        parSpeeds = map(int, parSpeeds)
110
        parChannels = map(int, parChannels)
110
        parChannels = map(int, parChannels)
111
       
111
       
112
       
112
       
113
        if parVerbose:
113
        if parVerbose:
114
          print "Opening comPort... "
114
          print "Opening comPort... "
115
        mk = mkProto.MkComm()
115
        mk = mkProto.MkComm()
116
        mk.open(comPort=parComPort)
116
        mk.open(comPort=parComPort)
117
       
117
       
118
        time.sleep(.1)
118
        time.sleep(.1)
119
        msg = mk.getVersionMsg()
119
        msg = mk.getVersionMsg()
120
        version = msg.getVersion()
120
        version = msg.getVersion()
121
        if parVerbose:
121
        if parVerbose:
122
          print "Version: %d.%d" % version
122
          print "Version: %d.%d" % version
123
        if version != (0,74):
123
        if CHECK_VERSION and version != (0,74):
124
          print "INVALID VERSION", version
124
          print "INVALID VERSION", version
125
          sys.exit(2)
125
          sys.exit(2)
126
         
126
         
127
         
127
         
128
        msg = mk.getDebugMsg()
128
        msg = mk.getDebugMsg()
129
        voltage = msg.getVoltage()
129
        voltage = msg.getVoltage()
130
        if (voltage == 0):
130
        if (voltage == 0):
131
          minVoltage = 0
131
          minVoltage = 0
132
        else:
132
        else:
133
          if (voltage > 4.2*3):
133
          if (voltage > 4.2*3):
134
            minVoltage = 4*3.5
134
            minVoltage = 4*3.5
135
          else:
135
          else:
136
            minVoltage = 3*3.5
136
            minVoltage = 3*3.5
137
 
137
 
138
        if parVerbose:
138
        if parVerbose:
139
          print "Voltage: %2.1fV" % msg.getVoltage()
139
          print "Voltage: %2.1fV" % msg.getVoltage()
140
          print "Minimum Voltage: %2.1fV" % minVoltage
140
          print "Minimum Voltage: %2.1fV" % minVoltage
141
       
141
       
142
        motorStartSpeeds = [0,0,0,0]
142
        motorStartSpeeds = [0,0,0,0]
143
       
143
       
144
        if parVerbose:
144
        if parVerbose:
145
          print "Starting motor(s) (speed=%d)... " % (parMinSpeed),
145
          print "Starting motor(s) (speed=%d)... " % (parMinSpeed),
146
        for i in range(0,parMinSpeed):
146
        for i in range(0,parMinSpeed):
147
          time.sleep(.2)
147
          time.sleep(.2)
148
          for motor in parMotors:
148
          for motor in parMotors:
149
            motorStartSpeeds[motor-1] = i
149
            motorStartSpeeds[motor-1] = i
150
          mk.setMotorTest(motorStartSpeeds)
150
          mk.setMotorTest(motorStartSpeeds)
151
         
151
         
152
        if parVerbose:
152
        if parVerbose:
153
          print "OK"
153
          print "OK"
154
   
154
   
155
        for speed in parSpeeds:
155
        for speed in parSpeeds:
156
          if len(parChannels)>1:
156
          if len(parChannels)>1:
157
            print
157
            print
158
           
158
           
159
          if parVerbose:
159
          if parVerbose:
160
            print "Setting speed to %3d ..." % speed
160
            print "Setting speed to %3d ..." % speed
161
          motorSpeeds = [0,0,0,0]  
161
          motorSpeeds = [0,0,0,0]  
162
          for motor in parMotors:
162
          for motor in parMotors:
163
            motorSpeeds[motor-1] = speed
163
            motorSpeeds[motor-1] = speed
164
          for i in range(0,10):
164
          for i in range(0,10):
165
            time.sleep(.1)
165
            time.sleep(.1)
166
            mk.setMotorTest(motorSpeeds)
166
            mk.setMotorTest(motorSpeeds)
167
           
167
           
168
          for channel in parChannels:
168
          for channel in parChannels:
169
            channelName = CHANNEL_NAMES[channel]
169
            channelName = CHANNEL_NAMES[channel]
170
            if parVerbose:
170
            if parVerbose:
171
              print "Getting data... " ,
171
              print "Getting data... " ,
172
            data = mk.doVibrationTest(motorSpeeds, parNbSamples, channel)
172
            data = mk.doVibrationTest(motorSpeeds, parNbSamples, channel)
173
            #data = (0,1,2)
173
            #data = (0,1,2)
174
            minval = min(data[1:])
174
            minval = min(data[1:])
175
            maxval = max(data[1:])
175
            maxval = max(data[1:])
176
            #if (maxval-minval)>100: print data
176
            #if (maxval-minval)>100: print data
177
            time.sleep(.1)
177
            time.sleep(.1)
178
            msg = mk.getDebugMsg()
178
            msg = mk.getDebugMsg()
179
            voltage = msg.getVoltage()
179
            voltage = msg.getVoltage()
180
       
180
       
181
            if voltage<minVoltage:
181
            if voltage<minVoltage:
182
              print "VOLTAGE TOO LOW, TEST ABORTED"
182
              print "VOLTAGE TOO LOW, TEST ABORTED"
183
              sys.exit(2)
183
              sys.exit(2)
184
             
184
             
185
            pp = maxval-minval;  
185
            pp = maxval-minval;  
186
            print "%10s Speed=%3d U=%2.1fV Channel=%-10s Min=%3d Max=%3d pp=%3d" % (parTestName,  speed, voltage, channelName, minval, maxval, pp),
186
            print "%10s Speed=%3d U=%2.1fV Channel=%-10s Min=%3d Max=%3d pp=%3d" % (parTestName,  speed, voltage, channelName, minval, maxval, pp),
187
            print "*"*(min(pp,200)/5)
187
            print "*"*(min(pp,200)/5)
188
             
188
             
189
            if parFileName != None:
189
            if parFileName != None:
190
              try:
190
              try:
191
                logfile = open(parFileName, "r")
191
                logfile = open(parFileName, "r")
192
                newFile = False
192
                newFile = False
193
              except:
193
              except:
194
                newFile = True
194
                newFile = True
195
               
195
               
196
              if newFile:
196
              if newFile:
197
                logfile = open(parFileName, "w")
197
                logfile = open(parFileName, "w")
198
                if parVerbose:
198
                if parVerbose:
199
                  print "Writing result to %s ..." % parFileName,
199
                  print "Writing result to %s ..." % parFileName,
200
                logfile.write("%s %d %s\n" % (parTestName, speed, channelName))
200
                logfile.write("%s %d %s\n" % (parTestName, speed, channelName))
201
                for value in data[1:]:
201
                for value in data[1:]:
202
                  logfile.write("%d\n" % value)
202
                  logfile.write("%d\n" % value)
203
                logfile.close()
203
                logfile.close()
204
                if parVerbose:  
204
                if parVerbose:  
205
                  print "OK"
205
                  print "OK"
206
              else:
206
              else:
207
                if parVerbose:
207
                if parVerbose:
208
                  print "Appending result to %s ..." % parFileName,
208
                  print "Appending result to %s ..." % parFileName,
209
                prevData = []
209
                prevData = []
210
                for line in logfile:
210
                for line in logfile:
211
                  prevData.append(line[:-1])
211
                  prevData.append(line[:-1])
212
                logfile.close()
212
                logfile.close()
213
                logfile = open(parFileName, "w")
213
                logfile = open(parFileName, "w")
214
                logfile.write("%s,%s %d %s\n" % (prevData[0], parTestName, speed, channelName))
214
                logfile.write("%s,%s %d %s\n" % (prevData[0], parTestName, speed, channelName))
215
                i = 1
215
                i = 1
216
                for value in data[1:]:
216
                for value in data[1:]:
217
                  logfile.write("%s,%d\n" % (prevData[i], value))
217
                  logfile.write("%s,%d\n" % (prevData[i], value))
218
                  i += 1
218
                  i += 1
219
                logfile.close()
219
                logfile.close()
220
                if parVerbose:
220
                if parVerbose:
221
                  print "OK"
221
                  print "OK"
222
                 
222
                 
223
        mk.close()
223
        mk.close()
224
             
224
             
225
    except Exception,e:
225
    except Exception,e:
226
      print
226
      print
227
      print "== ERROR ==: \"%s\"" % e
227
      print "== ERROR ==: \"%s\"" % e
228
      if parVerbose:
228
      if parVerbose:
229
        print
229
        print
230
        print "Traceback:"
230
        print "Traceback:"
231
        traceback.print_exc()
231
        traceback.print_exc()
232
        print
232
        print
233
      raw_input("Press ENTER, the application will close")
233
      raw_input("Press ENTER, the application will close")
234
      print
234
      print
235
     
235