Go to most recent revision | 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" |
||
554 | FredericG | 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" |
||
27 | print " Format 2: e.g. 100-200:50 Tests at speeds 100, 150 and 200" |
||
552 | fredericg | 28 | print " CHANNELS Channels to monitor. e.g. 5,6,7" |
554 | FredericG | 29 | for i,channelName in enumerate(CHANNEL_NAMES): |
552 | fredericg | 30 | print " Channel %d = %s" % (i, channelName) |
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 |
||
554 | FredericG | 37 | |
38 | def exit_usage(): |
||
39 | usage() |
||
40 | sys.exit(2) |
||
552 | fredericg | 41 | |
42 | if __name__ == '__main__': |
||
43 | |||
44 | #print sys.argv |
||
45 | if len(sys.argv)<5: |
||
554 | FredericG | 46 | exit_usage() |
552 | fredericg | 47 | |
48 | parComPort = sys.argv[1] |
||
49 | parMotors = sys.argv[2].split(',') |
||
50 | parChannels = sys.argv[4].split(',') |
||
554 | FredericG | 51 | |
52 | par = sys.argv[3] |
||
53 | if par.count("-") == 1: |
||
54 | # assume from-to:step format |
||
55 | par = par.split("-") |
||
56 | if len(par) != 2: |
||
57 | exit_usage() |
||
58 | |||
59 | par[1] = par[1].split(":") |
||
60 | if len(par[1]) != 2: |
||
61 | exit_usage() |
||
552 | fredericg | 62 | |
554 | FredericG | 63 | parSpeeds = range(int(par[0]),int(par[1][0])+int(par[1][1]),int(par[1][1])) |
64 | |||
65 | else: |
||
66 | parSpeeds = par.split(',') |
||
67 | |||
552 | fredericg | 68 | try: |
69 | opts, args = getopt.getopt(sys.argv[5:], "s:n:d:m:v") |
||
70 | except Exception, err: |
||
71 | print str(err) # will print something like "option -a not recognized" |
||
72 | usage() |
||
73 | sys.exit(2) |
||
74 | |||
75 | parVerbose = False |
||
76 | parFileName = None |
||
77 | parNbSamples = 400 |
||
78 | parMinSpeed = 25 |
||
79 | parTestName = "" |
||
80 | |||
81 | for o, a in opts: |
||
82 | if o == "-v": |
||
83 | parVerbose = True |
||
84 | elif o == "-d": |
||
85 | parFileName = a |
||
86 | elif o == "-s": |
||
87 | parNbSamples = int(a) |
||
88 | elif o == "-m": |
||
89 | parMinSpeed = int(a) |
||
90 | elif o == "-n": |
||
91 | parTestName = a |
||
92 | |||
93 | else: |
||
94 | assert False, "unhandled option %s" % (o) |
||
95 | |||
96 | if parVerbose: |
||
97 | print "comPort =", parComPort |
||
98 | print "motors =", parMotors |
||
99 | print "minSpeed =", parMinSpeed |
||
100 | print "speeds =", parSpeeds |
||
101 | print "channels =", parChannels |
||
102 | print "nbSamples=", parNbSamples |
||
103 | print "fileName =", parFileName |
||
104 | print "testName =", parTestName |
||
105 | |||
106 | |||
107 | try: |
||
108 | parMotors = map(int, parMotors) |
||
109 | parSpeeds = map(int, parSpeeds) |
||
110 | parChannels = map(int, parChannels) |
||
111 | |||
112 | |||
113 | if parVerbose: |
||
114 | print "Opening comPort... " |
||
115 | mk = mkProto.MkComm() |
||
116 | mk.open(comPort=parComPort) |
||
117 | |||
118 | msg = mk.getVersionMsg() |
||
556 | FredericG | 119 | version = msg.getVersion() |
552 | fredericg | 120 | if parVerbose: |
556 | FredericG | 121 | print "Version: %d.%d" % version |
122 | if version != (0,74): |
||
123 | print "INVALID VERSION", version |
||
124 | sys.exit(2) |
||
552 | fredericg | 125 | |
556 | FredericG | 126 | |
552 | fredericg | 127 | msg = mk.getDebugMsg() |
556 | FredericG | 128 | voltage = msg.getVoltage() |
552 | fredericg | 129 | if (voltage > 4.2*3): |
130 | minVoltage = 4*3.5 |
||
131 | else: |
||
132 | minVoltage = 3*3.5 |
||
133 | |||
134 | if parVerbose: |
||
135 | print "Voltage: %2.1fV" % msg.getVoltage() |
||
136 | print "Minimum Voltage: %2.1fV" % minVoltage |
||
137 | |||
138 | motorStartSpeeds = [0,0,0,0] |
||
139 | |||
140 | if parVerbose: |
||
141 | print "Starting motor(s) (speed=%d)... " % (parMinSpeed), |
||
142 | for i in range(0,parMinSpeed): |
||
143 | time.sleep(.2) |
||
144 | for motor in parMotors: |
||
145 | motorStartSpeeds[motor-1] = i |
||
146 | mk.setMotorTest(motorStartSpeeds) |
||
147 | |||
148 | if parVerbose: |
||
149 | print "OK" |
||
150 | |||
151 | for speed in parSpeeds: |
||
152 | if len(parChannels)>1: |
||
153 | print |
||
154 | |||
155 | if parVerbose: |
||
156 | print "Setting speed to %3d ..." % speed |
||
157 | motorSpeeds = [0,0,0,0] |
||
158 | for motor in parMotors: |
||
159 | motorSpeeds[motor-1] = speed |
||
160 | for i in range(0,10): |
||
161 | time.sleep(.1) |
||
162 | mk.setMotorTest(motorSpeeds) |
||
163 | |||
164 | for channel in parChannels: |
||
165 | channelName = CHANNEL_NAMES[channel] |
||
166 | if parVerbose: |
||
167 | print "Getting data... " , |
||
168 | data = mk.doVibrationTest(motorSpeeds, parNbSamples, channel) |
||
556 | FredericG | 169 | #data = (0,1,2) |
553 | FredericG | 170 | minval = min(data[1:]) |
171 | maxval = max(data[1:]) |
||
556 | FredericG | 172 | #if (maxval-minval)>100: print data |
173 | time.sleep(.1) |
||
552 | fredericg | 174 | msg = mk.getDebugMsg() |
175 | voltage = msg.getVoltage() |
||
176 | |||
177 | if voltage<minVoltage: |
||
178 | print "VOLTAGE TOO LOW, TEST ABORTED" |
||
179 | sys.exit(2) |
||
556 | FredericG | 180 | |
181 | pp = maxval-minval; |
||
182 | print "%10s Speed=%3d U=%2.1fV Channel=%-10s Min=%3d Max=%3d pp=%3d" % (parTestName, speed, voltage, channelName, minval, maxval, pp), |
||
183 | print "*"*(min(pp,200)/5) |
||
552 | fredericg | 184 | |
185 | if parFileName != None: |
||
186 | try: |
||
187 | logfile = open(parFileName, "r") |
||
188 | newFile = False |
||
189 | except: |
||
190 | newFile = True |
||
191 | |||
192 | if newFile: |
||
193 | logfile = open(parFileName, "w") |
||
194 | if parVerbose: |
||
195 | print "Writing result to %s ..." % parFileName, |
||
196 | logfile.write("%s %d %s\n" % (parTestName, speed, channelName)) |
||
197 | for value in data[1:]: |
||
198 | logfile.write("%d\n" % value) |
||
199 | logfile.close() |
||
200 | if parVerbose: |
||
201 | print "OK" |
||
202 | else: |
||
203 | if parVerbose: |
||
204 | print "Appending result to %s ..." % parFileName, |
||
205 | prevData = [] |
||
206 | for line in logfile: |
||
207 | prevData.append(line[:-1]) |
||
208 | logfile.close() |
||
209 | logfile = open(parFileName, "w") |
||
210 | logfile.write("%s,%s %d %s\n" % (prevData[0], parTestName, speed, channelName)) |
||
211 | i = 1 |
||
212 | for value in data[1:]: |
||
213 | logfile.write("%s,%d\n" % (prevData[i], value)) |
||
214 | i += 1 |
||
215 | logfile.close() |
||
216 | if parVerbose: |
||
217 | print "OK" |
||
218 | |||
219 | except Exception,e: |
||
220 | print |
||
221 | print "== ERROR ==: \"%s\"" % e |
||
222 | if parVerbose: |
||
223 | print |
||
224 | print "Traceback:" |
||
225 | traceback.print_exc() |
||
226 | print |
||
227 | raw_input("Press ENTER, the application will close") |
||
228 | print |