-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.py
254 lines (221 loc) · 8.06 KB
/
cmd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/python
"""
written by Jake Pring from CircuitSpecialists.com
licensed as GPLv3
"""
import time
import threading
import sys
import os
from lib import keyboard
# Path to CSV Files
sys.path.insert(0, './Example CSV')
# Path to devices
import ElectronicLoads as electronicload
import PowerSupplies as powersupply
class CMD:
def __init__(self):
# if in test mode, else run normally
if(len(sys.argv) == 2 and sys.argv[1] == 'dev'):
self.dev_getDevice()
elif(len(sys.argv) == 3 and sys.argv[1] == 'dev'):
self.dev_Device(sys.argv[2])
else:
self.normalRun()
def normalRun(self):
self.threads = []
self.getDevice()
self.device_output = 0
self.run_type = self.getRunType(prompt=True)
self.keys = keyboard.KEYBOARD()
self.addThread(self.keys.inputHandler)
if (self.run_type == 'a'):
if (self.device.type == 'electronicload'):
self.setLogFile('electronicload_log')
self.loadCSVFile()
self.run_log = True
self.addThread(self.logfileThread)
self.addThread(self.outputThread)
self.addThread(self.runCSV)
elif (self.run_type == 'm'):
self.getParameters(prompt=True)
self.addThread(self.runManual)
else:
self.quit()
self.runThreads()
def getRunType(self, prompt):
if (prompt):
print("Open CSV file to run auto loop press:'a'")
print("For Manual Control press:'m'")
print("Quit press:'q'")
return self.getInput()
def getInput(self):
if (sys.version_info.major >= 3):
return str(input()) # python 3
else:
return str(raw_input()) # python 2
def getDevice(self):
try:
self.device = electronicload.BUS_INIT().device
except:
try:
self.device = powersupply.BUS_INIT().device
except:
print("No Supported Devices connected to computer bus")
sys.exit()
print(self.device.name)
def loadCSVFile(self):
files = os.listdir('./Example CSV')
count = 1
for filenames in files:
print("%d: %s" % (count, filenames))
count += 1
file_selection = int(self.getInput())
print('Running... ./Example CSV/%s' % (files[file_selection - 1]))
file = open('./Example CSV/%s' % (files[file_selection - 1]), "r")
self.file_lines = file.readlines()
self.file_lines = self.file_lines[1:]
def setLogFile(self, filename):
print(filename)
if (filename != "" or filename != None):
self.log_file = open("%s.csv" % filename, "w")
else:
self.log_file = open("auto_log_el.csv", "w")
self.log_file.writelines(str("timestamp,voltage,current,power\n"))
print("Input Log-Time interval. Default is 1s")
self.write_interval = self.getInput()
if (self.write_interval == None or self.write_interval == ""):
self.write_interval = 1.0
else:
self.write_interval = float(self.write_interval)
def runCSV(self):
while(self.run_log):
# input handler
input_temp = self.keys.getInput()
if (input_temp == 'q'):
self.quit()
print("Finished auto run mode")
self.quit()
def outputThread(self):
for line in self.file_lines:
try:
# csv file loop
if (self.device.channels > 1):
self.device.setVoltage(line.split(',')[1], line)
self.device.setAmperage(line.split(',')[2], line)
self.device.setOutput(int(line.split(',')[3]), line)
else:
if (self.device.type == "powersupply"):
self.device.setVoltage(line.split(',')[1])
self.device.setAmperage(line.split(',')[2])
self.device.setOutput(int(line.split(',')[3]))
time.sleep(float(line.split(',')[0]))
elif (self.device.type == "electronicload"):
self.device.setMode(line.split(',')[1])
self.device.setCurrent(line.split(',')[2])
self.device.setOutput(int(line.split(',')[3]))
time.sleep(float(line.split(',')[0]))
except:
pass
self.run_log = False
def logfileThread(self):
start_time = time.time()
while (self.run_log):
try:
try:
currentVolts = self.device.getVoltage()[:-1]
except:
currentVolts = "N/A"
try:
currentAmps = self.device.getCurrent()[:-1]
except:
currentAmps = "N/A"
try:
currentPower = self.device.getPower()[:-1]
except:
currentPower = "N/A"
self.log_file.writelines(
str(time.time() - start_time) + "," +
str(currentVolts) + "," +
str(currentAmps) + "," +
str(currentPower) + "\n")
except:
pass
time.sleep(self.write_interval)
def runThreads(self):
for th in self.threads:
if(not th.is_alive()):
th.start()
def quitThreads(self):
for th in self.threads:
try:
print(len(self.threads))
th.join()
except:
self.threads.pop()
def addThread(self, function):
self.threads.append(threading.Thread(target=function))
def quit(self):
print("exiting...")
self.keys.quit()
try:
self.device.quit()
except:
pass
# self.quitThreads()
exit()
def getParameters(self, prompt):
if (self.device.channels > 1):
for i in range(1, self.device.channels):
print("Setting Channel: %s" % str(i))
self.run_time = self.getLength(prompt)
if (self.device.type == "powersupply"):
self.getVoltage(prompt)
print('Voltage: %s' % self.device.voltage)
self.getCurrent(prompt)
print('Amps: %s' % self.device.amperage)
def getLength(self, prompt):
if (prompt):
print("Input Time to run in (s)")
return float(self.getInput())
def getVoltage(self, prompt):
if (prompt):
print("Input Volts in Volts.hectoVolts")
self.device.setVoltage(self.getInput())
def getCurrent(self, prompt):
if (prompt):
print("Input Amps in Amps.milliAmps")
if (self.device.type == "powersupply"):
self.device.setAmperage(self.getInput())
elif (self.device.type == "electronicload"):
self.device.setCurrent = self.getInput()
def flipOutput(self):
if (self.device.output):
self.device.setOutput(0)
else:
self.device.setOutput(1)
def runManual(self):
start_time = time.time()
self.device.setOutput(1)
while time.time() <= start_time + self.run_time:
# input handler
input_temp = self.keys.getInput()
if (input_temp == 'q'):
break
elif (input_temp == 'v'):
self.getVoltage(prompt=False)
elif (input_temp == 'a'):
self.getCurrent(prompt=False)
elif (input_temp == 'o'):
self.flipOutput()
self.quit()
def dev_getDevice(self):
print("Enter Device name")
device_name = self.getInput()
self.dev_Device(device_name)
def dev_Device(self, device):
self.device = powersupply.BUS_INIT(device.upper()).device
self.device.turnON()
self.device.quit()
if __name__ == "__main__":
cmd = CMD()