-
Notifications
You must be signed in to change notification settings - Fork 0
/
TDaemon.py
executable file
·307 lines (267 loc) · 7.91 KB
/
TDaemon.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Sub programs for operation of the PicoWatt and Leiden TCS to control temperature
author : Eoin O'Farrell
email : phyoec@nus.edu.sg
last edited : August 2013
The daemon listens for commands to change the control loop or setpoint
The daemon broadcasts the current temperature
ToDo:
Listen
Broadcast
Initialize
ReadPico
CalcPID
SetTCS
"""
import SocketUtils as SocketUtils
import logging
import visa as visa
import VisaSubs as VisaSubs
import string as string
import re as res
import time
import numpy as np
import asyncore
import PIDControl
class TControl():
# Initialization call, initialize visas for the TCS, Picowatt and the
# Server, server always runs at 18871
def __init__(self):
self.PicoVisa = visa.instrument("GPIB0::20::INSTR",delay=0.04)
self.PicoVisa.write("HDR0")
self.PicoVisa.write("ARN 1")
self.PicoVisa.write("REM 1")
self.TCSVisa = VisaSubs.InitializeSerial("ASRL5",idn="ID?",term_chars="\\n")
address = ('localhost',18871)
self.Server = SocketUtils.SockServer(address)
self.ResThermometer = 1
self.Temperature = 0.0
self.PicoChannel = 0
self.PicoRange = 0
self.SetTemp = -1
self.Status = -1
self.TCSHeater = [0,0,0]
self.TCSRange = [1,1,1]
self.TCSCurrent = [0,0,0]
self.ConstCurrent = False
self.CurrentConst = 0
self.DeltaTemp = 0
self.MaxTemp = 5000
self.MaxCurrent = 25000
self.ErrorTemp = 10 # The acceptable error in temperature
self.ErrorDeltaTemp = 10 # The acceptable stability
self.Sweep = False
self.SweepStart = 0
self.SweepFinish = 0
self.SweepRate = 0 # rate in mK/s
self.SweepTime = 0
self.SweepDirection = 1.0
return
def SetTCS(self,Source,Current):
if Current < 0:
Current = 0
elif Current > self.MaxCurrent:
Current = self.MaxCurrent
# Current in microAmp
# print Current
Source = Source + 1
command = " ".join(("SETDAC","%d" % Source,"0","%d" % Current))
NEWPID = pid.update(control.Temperature)
NEWPID = int(NEWPID)
self.TCSVisa.ask(command)
return
def ReadPico(self):
# Get the resistance of the current channel of the picowatt
self.PicoVisa.write("ADC")
time.sleep(0.4)
Answer = self.PicoVisa.ask("RES ?")
Answer = Answer.strip()
try:
self.ResThermometer = float(Answer)
except:
self.ResThermometer = self.ResThermometer
pass
return
def ReadPicoRange(self):
Answer = self.PicoVisa.ask("RAN ?")
Answer = Answer.strip()
self.PicoRange = int(Answer)
return
def SetPicoChannel(self,Channel):
self.PicoVisa.write("INP 0")
Command = "".join(("MUX ","%d" % Channel))
self.PicoVisa.write(Command)
time.sleep(3)
self.PicoVisa.write("INP 1")
time.sleep(10)
return
def ReadTCS(self):
Answer = self.TCSVisa.ask("STATUS?")
Reply = Answer.split("\t")[1]
Reply = Reply.split(",")
Range = Reply[1::4]
Current = Reply[2::4]
Heaters = Reply[3::4]
TMP = [1,10,100,1000]
for i in range(3):
self.TCSHeater[i] = int(Heaters[i])
for i in range(3):
self.TCSCurrent[i] = int(Current[i])*TMP[int(Range[i])-1]
return
def CalcTemperature(self,Calibration,factor=0):
logR = np.log10(self.ResThermometer)-factor
RPoly = np.ones((len(Calibration),))
OldT = self.Temperature
for i in range(1,len(RPoly)):
RPoly[i] = logR * RPoly[i-1]
self.Temperature = np.power(10,(np.sum(np.multiply(RPoly,Calibration))))
self.DeltaTemp = abs(self.Temperature - OldT)
return
def UpdateStatus(self):
# TDaemon has modes:
# -2 = No set heater off
# -1 = constant current
# 0 = Going to set
# 1 = At set point
# 2 = Sweeping
OldStatus = self.Status
if self.ConstCurrent:
self.Status = -1
elif self.Sweep:
self.Status = 2
elif self.SetTemp <= 0:
# There is no set point
self.Status = -1
elif (abs(self.SetTemp - self.Temperature) < self.ErrorTemp) and abs(self.DeltaTemp) <= self.ErrorDeltaTemp:
# We are at the set point
self.Status = 1
else:
# Going to Set
self.Status = 0
return
# Interpret a message from the socket
def ReadMsg(self,Msg):
Msg = Msg.split(" ")
GotSet = False
if Msg[0] == "SET":
try:
NewSet = float(Msg[1])
if abs(self.SetTemp-NewSet) > 5:
if NewSet <= self.MaxTemp:
self.SetTemp = NewSet
GotSet = True
else:
self.SetTemp = self.MaxTemp
GotSet = True
print "Got set point from socket %.2f" % self.SetTemp
self.ConstCurrent = False
self.Sweep = False
except:
pass
if Msg[0] == "SWP":
try:
self.SweepStart = float(Msg[1])
self.SweepFinish = float(Msg[2])
self.SweepRate = abs(float(Msg[3]))
self.Sweep = True
self.SweepTime = time.time()
self.ConstCurrent = False
print "Got temperature sweep from %.2f mK to %.2f mK at %.2f mK/s" % (self.SweepStart, self.SweepFinish, self.SweepRate)
if self.SweepFinish >= self.SweepStart:
self.SweepDirection = 1.0
else:
self.SweepDirection = -1.0
except:
pass
if Msg[0] == "CST":
try:
self.CurrentConst = float(Msg[1])
self.ConstCurrent = True
self.Sweep = False
print "Got constant current point from socket %.2f micro amps" % self.CurrentConst
except:
pass
if Msg[0] == "T_ERROR":
try:
self.ErrorTemp = float(Msg[1])
except:
pass
if Msg[0] == "DT_ERROR":
try:
self.ErrorDeltaTemp = float(Msg[1])
except:
pass
return GotSet
def TCSSwitchHeater(self,Heater):
CommandVec = np.zeros((12,))
CommandVec[2+Heater*4] = 1
CommandStr = ""
print "Heater %d Switched" % Heater
for i in CommandVec:
CommandStr = "".join((CommandStr, "%d," % i))
CommandStr = CommandStr[:-1]
self.TCSVisa.ask(" ".join(("SETUP",CommandStr)))
return
##################### Calibrations
SO703 = [7318.782092,-13274.53584,10276.68481,-4398.202411,1123.561007,-171.3095557,14.43456504,-0.518534965]
SO914 = [5795.148097375,-11068.032226486,9072.821104899,-4133.466851312,1129.955799406,-185.318021359,16.881907269,-0.658939155]
MATS56 = [19.68045382,-20.19660902,10.13318296,-2.742724207,0.385556989,-0.022178276]
if __name__ == '__main__':
# Initialize a PID controller
pid = PIDControl.PID(P=10,I=1,D=0,Derivator=0,Integrator=0,Integrator_max=15000,Integrator_min=-2000)
control = TControl()
control.SetPicoChannel(3)
# Main loop
control.ReadTCS()
while 1:
# Read the picowatt and calculate the temperature
control.ReadPico()
control.CalcTemperature(SO703)
# Push the reading to clients
for j in control.Server.handlers:
j.to_send = ",%.3f %d" % (control.Temperature, control.Status)
SocketMsg = j.received_data
if SocketMsg:
GotSet = control.ReadMsg(SocketMsg)
if GotSet:
pid.setPoint(control.SetTemp)
asyncore.loop(count=1,timeout=0.001)
control.UpdateStatus()
# Now we should do some PID stuff
if control.Sweep:
deltaTime = time.time() - control.SweepTime
control.setTemp = deltaTime * control.SweepRate * control.SweepDirection + control.SweepStart
pid.setPoint(control.setTemp)
if (control.setTemp - control.SweepFinish)*control.SweepDirection >= 0:
# Sweep has finished
control.Sweep = False
elif not control.Server.handlers:
control.Sweep = False
if control.Status >= 0:
NEWPID = pid.update(control.Temperature)
NEWPID = int(NEWPID)
if NEWPID < 0:
NEWPID = 0
if NEWPID > control.MaxCurrent:
NEWPID = control.MaxCurrent
if control.Status == -1 and control.TCSHeater[2] == 1:
# status is unset and the heater is on turn it off
control.TCSSwitchHeater(2)
control.ReadTCS()
elif control.Status == -2:
if control.TCSHeater[2] == 0:
# the status is constant current --> Turn heater on to con
control.TCSSwitchHeater(2)
control.SetTCS(2,control.CurrentConst)
control.ReadTCS()
elif control.Status >= 0 and control.TCSHeater[2] == 0:
# status is go to set and heater is off --> turn it on
control.TCSSwitchHeater(2)
control.ReadTCS()
elif control.Status >= 0 and control.TCSHeater[2] == 1:
control.SetTCS(2,NEWPID)
control.TCSCurrent[2] = NEWPID
time.sleep(0.4)
control.TCSVisa.close()