This repository has been archived by the owner on May 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisc.py
executable file
·91 lines (76 loc) · 3 KB
/
misc.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
"""
Miscellaneous devices
"""
import serial
import numpy as np
def sr570_write(cmd, port='COM5'):
"""
A standalone function writing to SR570 low noise preamplifier via RS232 link
RS232 communication interface, listen-only
"""
ser = serial.Serial(port=port, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_TWO, bytesize=serial.EIGHTBITS)
if not ser.isOpen():
ser.open()
else:
ser.write(cmd + '\r\n')
ser.close()
class SR570():
"""
SR570 low-noise current amplifier
"""
def __init__(self, port='COM5'):
self.port = port
def write(self, cmd):
ser = serial.Serial(port=self.port, baudrate=9600, parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_TWO, bytesize=serial.EIGHTBITS)
if not ser.isOpen():
ser.open()
else:
ser.write(cmd + '\r\n')
ser.close()
def sensitivity_mapping(self, sens, direction='n2c'):
"""
Map the numerical sensitivity to the commmand line argument
Return an int for 'n2c', or a float for 'c2n'
Input arg:
sens: sensitivity
* when in 'n2c'(numerical to command line) mode, unit A/V
* when in 'c2n' mode: unitless, just an int
"""
if direction == 'n2c': # Convert from numerical to command line argument
# sens*1.01 to avoid the precision problem during the float point operation
# e.g., 1/1e-5 results in 99999.99999999999, which makes exponent == 4 and fsdigit == 9 below
# Now use an alogrithm to get the command line argument
# Decompose the number val to fsdigit * 10** exponent
# Normalize the value to 1pA
val = int(sens * 1.01 / 1e-12)
exponent = int(np.floor(np.log10(val)))
fsdigit = int(val / 10 ** exponent) # the first significant digit
if fsdigit * 10 ** exponent > 10 ** 9 or fsdigit * 10 ** exponent < 1:
raise RuntimeError('Sensitivity out of range')
if fsdigit == 2:
a = 1
elif fsdigit == 5:
a = 2
elif fsdigit == 1:
a = 0
else:
raise RuntimeError('Unrecognized sensitivity')
i = a + exponent * 3
return i
elif direction == 'c2n': # Convert from command line argument to numerical (unit: A or V)
sens = int(sens)
if sens < 0 or sens > 27:
raise RuntimeError('Sensitivity out of range')
# Do the reverse of "n2c" algorithm
exponent = sens / 3
a = sens % 3
if a == 0:
fsdigit = 1
elif a == 1:
fsdigit = 2
elif a == 2:
fsdigit = 5
val = fsdigit * 10 ** exponent
val = val / 1e12
return val