-
Notifications
You must be signed in to change notification settings - Fork 0
/
FiberPIDControl.py
142 lines (130 loc) · 3.62 KB
/
FiberPIDControl.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
#notes:
# include GUI
# shows voltage, time, snapshot of maybe past minute,
# and current input, with a line showing desired point
# develop better PID
# modulize better so it's easier to change
import u6
import time
import math
from visa import *
from pyvisa.visa import *
import pyvisa.vpp43 as vpp43
import matplotlib.pyplot as plt
import numpy as np
from PID import PID
current_milli_time = lambda: int(round(time.time() * 1000))
def getResult(res):
a = res.split(',')
return float(a[0].strip('+'))
def setPSU(vi, voltage, current):
buf = ":volt " + str(voltage) + ";:curr " + str(current) + "\n"
vpp43.write(vi, buf)
buf = ":apply?\n"
vpp43.write(vi, buf)
result = vpp43.read(vi, 256)
return result
def readValue(d):
POSITIVE_CHANNEL = 2
GAIN_INDEX = 1
RESOLUTION_INDEX = 12
AIN_FEEDBACK_COMMAND = u6.AIN24(POSITIVE_CHANNEL, GainIndex = GAIN_INDEX, ResolutionIndex = RESOLUTION_INDEX, Differential = 1)
bits, = d.getFeedback(AIN_FEEDBACK_COMMAND)
volts = d.binaryToCalibratedAnalogVoltage(GAIN_INDEX, bits)
return volts
def initPSU():
IPaddr = "192.168.6.6"
defaultRm = vpp43.open_default_resource_manager()
if defaultRm < VI_SUCCESS:
print "status < VI_SUCCESS, quitting"
quit()
rsc = []
rsc.extend([] * 256)
rsc = "TCPIP::192.168.6.6::2268::SOCKET"
accessMode = VI_NO_LOCK
timeout = 0
vi = vpp43.open(defaultRm, rsc, accessMode, timeout)
vpp43.set_attribute(vi, VI_ATTR_TMO_VALUE, 10000)
vpp43.set_attribute(vi, VI_ATTR_TERMCHAR_EN, VI_TRUE)
setPSU(vi, 0, 0)
return vi
def setSetpoint():
setpoint = raw_input("Enter desired setpoint: ")
setpoint = float(setpoint)
return setpoint
def setDuration():
duration = raw_input("Input time in minutes: ")
duration = float(duration)
return duration * 60
def initLabJack():
d = u6.U6()
d.configU6()
d.getCalibrationData()
return d
def writeToFile(data):
f = open("PIDTest%s.csv" % time.strftime("%c"), 'w')
for point in data:
f.write(str(point[0]))
f.write(",")
f.write(str(point[1]))
f.write(",")
f.write(str(point[2]))
f.write("\n")
f.close()
def makePlot(data):
table = np.array(data).T
plt.ion()
plt.figure(1)
plt.plot(table[1], table[0])
plt.figure(2)
plt.plot(table[1], table[2])
plt.show(block=True)
def getNewOutput(setpoint, output, reading):
print setpoint
print reading
if setpoint > reading:
print "setpoint > reading"
returnValue = 0
else:
returnValue = 80
return returnValue
vpp43.visa_library.load_library("")
d = initLabJack()
vi = initPSU()
setpoint = setSetpoint()
p = PID(800000, 0, 0)
p.setPoint(setpoint)
startTime = int(time.time())
count = 0
table = []
currentRead = []
timePassed = 0
lastTime = 0
data = []
pidread = []
volts = 0
cur = 15
current = readValue(d)
duration = setDuration()
startmilli = current_milli_time()
while timePassed < duration:
print "Time Passed: ", timePassed
currentRead = []
timePassed = int(time.time() - startTime)
vRead = readValue(d)
currentRead.append(vRead)
print "Current reading: ", vRead
currentRead.append(current_milli_time() - startmilli)
#volts = getNewOutput(setpoint, volts, vRead)
volts = 80 - p.update(vRead)
print "PID value: ", volts
currentRead.append(volts)
table.append(currentRead)
pidread.append(volts)
result = setPSU(vi, volts, cur)
print "Voltage setting: ", result
result = setPSU(vi, 0, 0)
writeToFile(table)
makePlot(table)
vpp43.close(vi)
d.close();