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