@@ -0,0 +1,73 | |||
|
1 | #!/usr/bin/env python | |
|
2 | #-*- coding: utf-8 -*- | |
|
3 | """Simple python library to communicate with Prologix USB GPIB module. | |
|
4 | """ | |
|
5 | import time | |
|
6 | import sys | |
|
7 | import os | |
|
8 | import matplotlib.pyplot as plt | |
|
9 | import numpy as np | |
|
10 | import serial | |
|
11 | ||
|
12 | __author__ = "Alexis Jeandet" | |
|
13 | __copyright__ = "Copyright 2015, Laboratory of Plasma Physics" | |
|
14 | __credits__ = [] | |
|
15 | __license__ = "GPLv2" | |
|
16 | __version__ = "1.0.0" | |
|
17 | __maintainer__ = "Alexis Jeandet" | |
|
18 | __email__ = "alexis.jeandet@member.fsf.org" | |
|
19 | __status__ = "Development" | |
|
20 | ||
|
21 | class UsbGpib(object): | |
|
22 | modedic = {0:"DEVICE" , | |
|
23 | 1:"CONTROLLER" ,} | |
|
24 | revmodedic = {"DEVICE":"0" , | |
|
25 | "CONTROLLER":"1" ,} | |
|
26 | def __init__(self,port,address=0): | |
|
27 | self._port=serial.Serial(port,timeout=0.1) | |
|
28 | self._address=address | |
|
29 | self._mode=1 | |
|
30 | self.write("++ver") | |
|
31 | self.version=self.read() | |
|
32 | self.write("++auto 1") | |
|
33 | ||
|
34 | def set_as_device(self): | |
|
35 | self.write("++mode 0") | |
|
36 | def set_as_controller(self): | |
|
37 | self.write("++mode 1") | |
|
38 | ||
|
39 | @property | |
|
40 | def mode(self): | |
|
41 | self.write("++mode") | |
|
42 | self._mode= self.modedic[int(self.read())] | |
|
43 | return self._mode | |
|
44 | ||
|
45 | @mode.setter | |
|
46 | def mode(self,new_mode): | |
|
47 | self._mode=self.revmodedic[new_mode] | |
|
48 | self.write("++mode %d" % self._mode) | |
|
49 | ||
|
50 | @property | |
|
51 | def address(self): | |
|
52 | self._address=int(self.read("++addr")) | |
|
53 | return self._address | |
|
54 | ||
|
55 | @address.setter | |
|
56 | def address(self,value): | |
|
57 | self._address=int(value) | |
|
58 | self.write("++addr %d" % self._address) | |
|
59 | ||
|
60 | def write(self,command): | |
|
61 | self._port.write(b"%s\n\r" % command) | |
|
62 | self._port.flush() | |
|
63 | ||
|
64 | ||
|
65 | def read(self,command="",GPIB=False): | |
|
66 | if not command=="": | |
|
67 | self.write(command) | |
|
68 | if GPIB: | |
|
69 | self.write("++read") | |
|
70 | return self._port.readall() | |
|
71 | ||
|
72 | def idn(self): | |
|
73 | return self.read("*IDN?") |
@@ -1,333 +1,344 | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | #-*- coding: utf-8 -*- |
|
3 | 3 | """Simple python library to drive the analog discovery module from www.digilentinc.com |
|
4 | 4 | """ |
|
5 | 5 | |
|
6 | 6 | from ctypes import * |
|
7 | 7 | import time |
|
8 | 8 | import sys |
|
9 | 9 | import os |
|
10 | 10 | import matplotlib.pyplot as plt |
|
11 | 11 | import numpy as np |
|
12 | 12 | |
|
13 | 13 | __author__ = "Alexis Jeandet" |
|
14 | 14 | __copyright__ = "Copyright 2015, Laboratory of Plasma Physics" |
|
15 | 15 | __credits__ = [] |
|
16 | 16 | __license__ = "GPLv2" |
|
17 | 17 | __version__ = "1.0.0" |
|
18 | 18 | __maintainer__ = "Alexis Jeandet" |
|
19 | 19 | __email__ = "alexis.jeandet@member.fsf.org" |
|
20 | 20 | __status__ = "Production" |
|
21 | 21 | |
|
22 | 22 | |
|
23 | 23 | nodev = c_int(0) |
|
24 | 24 | DwfStateDone = c_int(2) |
|
25 | 25 | |
|
26 | 26 | DECIAnalogInChannelCount = c_int(1) |
|
27 | 27 | DECIAnalogOutChannelCount = c_int(2) |
|
28 | 28 | DECIAnalogIOChannelCount = c_int(3) |
|
29 | 29 | DECIDigitalInChannelCount = c_int(4) |
|
30 | 30 | DECIDigitalOutChannelCount = c_int(5) |
|
31 | 31 | DECIDigitalIOChannelCount = c_int(6) |
|
32 | 32 | DECIAnalogInBufferSize = c_int(7) |
|
33 | 33 | DECIAnalogOutBufferSize = c_int(8) |
|
34 | 34 | DECIDigitalInBufferSize = c_int(9) |
|
35 | 35 | DECIDigitalOutBufferSize = c_int(10) |
|
36 | 36 | |
|
37 | 37 | trigsrcNone = c_byte(0) |
|
38 | 38 | trigsrcPC = c_byte(1) |
|
39 | 39 | trigsrcDetectorAnalogIn = c_byte(2) |
|
40 | 40 | trigsrcDetectorDigitalIn = c_byte(3) |
|
41 | 41 | trigsrcAnalogIn = c_byte(4) |
|
42 | 42 | trigsrcDigitalIn = c_byte(5) |
|
43 | 43 | trigsrcDigitalOut = c_byte(6) |
|
44 | 44 | trigsrcAnalogOut1 = c_byte(7) |
|
45 | 45 | trigsrcAnalogOut2 = c_byte(8) |
|
46 | 46 | trigsrcAnalogOut3 = c_byte(9) |
|
47 | 47 | trigsrcAnalogOut4 = c_byte(10) |
|
48 | 48 | trigsrcExternal1 = c_byte(11) |
|
49 | 49 | trigsrcExternal2 = c_byte(12) |
|
50 | 50 | trigsrcExternal3 = c_byte(13) |
|
51 | 51 | trigsrcExternal4 = c_byte(14) |
|
52 | 52 | trigAuto = c_byte(254) |
|
53 | 53 | trigNormal = c_byte(255) |
|
54 | 54 | |
|
55 | 55 | AnalogOutNodeCarrier = c_int(0) |
|
56 | 56 | AnalogOutNodeFM = c_int(1) |
|
57 | 57 | AnalogOutNodeAM = c_int(2) |
|
58 | 58 | |
|
59 | 59 | |
|
60 | 60 | shapes = {'DC' : 0, |
|
61 | 61 | 'Sine' : 1, |
|
62 | 62 | 'Square' : 2, |
|
63 | 63 | 'Triangle' : 3, |
|
64 | 64 | 'RampUp' : 4, |
|
65 | 65 | 'RampDown' : 5, |
|
66 | 66 | 'Noise' : 6, |
|
67 | 67 | 'Custom' : 30, |
|
68 | 68 | 'Play' :31, } |
|
69 | 69 | |
|
70 | 70 | closed=False |
|
71 | 71 | opened=True |
|
72 | 72 | |
|
73 | 73 | |
|
74 | class DiscoveryLimits(): | |
|
75 | class limitRange(): | |
|
74 | class DiscoveryLimits(object): | |
|
75 | class limitRange(object): | |
|
76 | 76 | def __init__(self,Min,Max,name="Unknow",unit=""): |
|
77 | 77 | self.Min = Min |
|
78 | 78 | self.Max = Max |
|
79 | 79 | self.name = name |
|
80 | 80 | self.unit = unit |
|
81 | 81 | |
|
82 | 82 | def conform(self,value): |
|
83 | 83 | if value<self.Min: |
|
84 | 84 | raise UserWarning("Parameter "+self.name+" out of bound\nValue="+str(value)+"\nForce to "+str(self.Min)) |
|
85 | 85 | return self.Min |
|
86 | 86 | if value>self.Max: |
|
87 | 87 | raise UserWarning("Parameter "+self.name+" out of bound\nValue="+str(value)+"\nForce to "+str(self.Max)) |
|
88 | 88 | return self.Max |
|
89 | 89 | return value |
|
90 | 90 | |
|
91 | 91 | def __str__(self): |
|
92 | 92 | return self.name + ":\n Min="+str(self.Min)+" "+self.unit+",Max="+str(self.Max)+" "+self.unit |
|
93 | 93 | |
|
94 | 94 | errors = {0: RuntimeError("No card opened"), |
|
95 | 95 | 1: UserWarning("Parameter out of bound"), |
|
96 | 96 | } |
|
97 | 97 | def __init__(self,libdwf,hdwf): |
|
98 | 98 | self.limits=[] |
|
99 | 99 | self.ACQ_IN_RANGES=[0.0] |
|
100 | 100 | if hdwf.value == nodev.value: |
|
101 | 101 | return |
|
102 | 102 | self.__hdwf=hdwf |
|
103 | 103 | self.__libdwf=libdwf |
|
104 | 104 | Mind=c_double() |
|
105 | 105 | Maxd=c_double() |
|
106 | 106 | Mini=c_int() |
|
107 | 107 | Maxi=c_int() |
|
108 | 108 | StepsCount=c_int() |
|
109 | 109 | Steps=(c_double*32)() |
|
110 | 110 | self.__libdwf.FDwfAnalogInBufferSizeInfo(self.__hdwf, byref(Mini), byref(Maxi)) |
|
111 | 111 | self.ACQ_BUF=self.limitRange(Mini.value,Maxi.value,"ACQ Buffer Size","Sps") |
|
112 | 112 | self.limits.append(self.ACQ_BUF) |
|
113 | 113 | self.__libdwf.FDwfAnalogInFrequencyInfo(self.__hdwf, byref(Mind), byref(Maxd)) |
|
114 | 114 | self.ACQ_FREQ=self.limitRange(Mind.value,Maxd.value,"ACQ Frequency","Hz") |
|
115 | 115 | self.limits.append(self.ACQ_FREQ) |
|
116 | 116 | self.__libdwf.FDwfAnalogInChannelRangeSteps(self.__hdwf, byref(Steps), byref(StepsCount)) |
|
117 | 117 | self.ACQ_IN_RANGES=Steps[0:StepsCount.value] |
|
118 | 118 | self.__libdwf.FDwfAnalogOutNodeAmplitudeInfo(self.__hdwf,c_int(0), AnalogOutNodeCarrier, |
|
119 | 119 | byref(Mind), byref(Maxd)) |
|
120 | 120 | self.GEN_AMPL=self.limitRange(Mind.value,Maxd.value,"GEN Amplitude","V") |
|
121 | 121 | self.limits.append(self.GEN_AMPL) |
|
122 | 122 | self.__libdwf.FDwfAnalogOutNodeFrequencyInfo(self.__hdwf,c_int(0), AnalogOutNodeCarrier, |
|
123 | 123 | byref(Mind), byref(Maxd)) |
|
124 | 124 | self.GEN_FREQ=self.limitRange(Mind.value,Maxd.value,"GEN Frequency","Hz") |
|
125 | 125 | self.limits.append(self.GEN_FREQ) |
|
126 | 126 | self.__libdwf.FDwfAnalogOutNodeOffsetInfo(self.__hdwf,c_int(0), AnalogOutNodeCarrier, |
|
127 | 127 | byref(Mind), byref(Maxd)) |
|
128 | 128 | self.GEN_OFFSET=self.limitRange(Mind.value,Maxd.value,"GEN Offset","V") |
|
129 | 129 | self.limits.append(self.GEN_OFFSET) |
|
130 | 130 | self.__libdwf.FDwfAnalogOutNodeDataInfo(self.__hdwf,c_int(0), AnalogOutNodeCarrier, |
|
131 | 131 | byref(Mini), byref(Maxi)) |
|
132 | 132 | self.GEN_BUFF=self.limitRange(Mini.value,Maxi.value,"GEN Buffer size","Sps") |
|
133 | 133 | self.limits.append(self.GEN_BUFF) |
|
134 | 134 | |
|
135 | 135 | |
|
136 | 136 | def __conformParam(self,minVal,maxVal,val): |
|
137 | 137 | if val<minVal: |
|
138 | 138 | raise self.errors.get(1) |
|
139 | 139 | print("Force to "+str(minVal)) |
|
140 | 140 | return minVal |
|
141 | 141 | if val>maxVal: |
|
142 | 142 | raise self.errors.get(1) |
|
143 | 143 | print("Force to "+str(maxVal)) |
|
144 | 144 | return maxVal |
|
145 | 145 | return val |
|
146 | 146 | |
|
147 | 147 | def acqFreq(self, value): |
|
148 | 148 | return self.ACQ_FREQ.conform(value) |
|
149 | 149 | |
|
150 | 150 | def acqBufSize(self, value): |
|
151 | 151 | return self.ACQ_BUF.conform(value) |
|
152 | 152 | |
|
153 | 153 | def genFreq(self, value): |
|
154 | 154 | return self.GEN_FREQ.conform(value) |
|
155 | 155 | |
|
156 | 156 | def genAmplitude(self, value): |
|
157 | 157 | return self.GEN_AMPL.conform(value) |
|
158 | 158 | |
|
159 | 159 | def genOffset(self, value): |
|
160 | 160 | return self.GEN_OFFSET.conform(value) |
|
161 | 161 | |
|
162 | 162 | def genBuffSize(self, value): |
|
163 | 163 | return self.GEN_BUFF.conform(value) |
|
164 | 164 | |
|
165 | 165 | def __str__(self): |
|
166 | 166 | res=str() |
|
167 | 167 | for i in self.limits: |
|
168 | 168 | res+=i.__str__()+"\n" |
|
169 | 169 | res+="ACQ Input ranes: "+str(self.ACQ_IN_RANGES) |
|
170 | 170 | return res |
|
171 | 171 | |
|
172 | 172 | |
|
173 | class Discovery(): | |
|
173 | class Discovery(object): | |
|
174 | 174 | |
|
175 | 175 | errors = {0: RuntimeError("No card opened"), |
|
176 | 176 | 1: UserWarning("Parameter out of bound"), |
|
177 | 177 | } |
|
178 | 178 | def __init__(self,card=-1): |
|
179 | 179 | if sys.platform.startswith("win"): |
|
180 | 180 | self.__libdwf = cdll.dwf |
|
181 | 181 | elif sys.platform.startswith("darwin"): |
|
182 | 182 | self.__libdwf = cdll.LoadLibrary("libdwf.dylib") |
|
183 | 183 | else: |
|
184 | 184 | self.__libdwf = cdll.LoadLibrary("libdwf.so") |
|
185 | 185 | self.__opened = True |
|
186 | 186 | self.__hdwf = c_int() |
|
187 | 187 | self.__libdwf.FDwfDeviceOpen(c_int(card), byref(self.__hdwf)) |
|
188 | 188 | if self.__hdwf.value == nodev.value: |
|
189 | 189 | szerr = create_string_buffer(512) |
|
190 | 190 | self.__libdwf.FDwfGetLastErrorMsg(szerr) |
|
191 | 191 | print(szerr.value) |
|
192 | 192 | print("failed to open device") |
|
193 | 193 | self.__opened=False |
|
194 | 194 | self.__limits=DiscoveryLimits(self.__libdwf,self.__hdwf) |
|
195 | 195 | print(self.__limits) |
|
196 | 196 | |
|
197 | 197 | @property |
|
198 | 198 | def opened(self): |
|
199 | 199 | return self.__opened |
|
200 | 200 | |
|
201 | @property | |
|
202 | def max_sampling_freq(self): | |
|
203 | return self.__limits.ACQ_FREQ.Max | |
|
204 | ||
|
205 | @property | |
|
206 | def min_sampling_freq(self): | |
|
207 | return self.__limits.ACQ_FREQ.Min | |
|
208 | ||
|
209 | @property | |
|
210 | def max_sampling_buffer(self): | |
|
211 | return self.__limits.ACQ_BUF.Max | |
|
201 | 212 | |
|
202 | 213 | ############################################################# |
|
203 | 214 | # Power Supply |
|
204 | 215 | ############################################################# |
|
205 | 216 | def set_power(self,fiveVolt=1,minusFiveVolt=1,master=True): |
|
206 | 217 | if not self.__opened: |
|
207 | 218 | raise self.errors.get(0) |
|
208 | 219 | # enable positive supply |
|
209 | 220 | self.__libdwf.FDwfAnalogIOChannelNodeSet(self.__hdwf, 0, 0, c_double(fiveVolt)) |
|
210 | 221 | # enable negative supply |
|
211 | 222 | self.__libdwf.FDwfAnalogIOChannelNodeSet(self.__hdwf, 1, 0, c_double(minusFiveVolt)) |
|
212 | 223 | # master enable |
|
213 | 224 | return self.__libdwf.FDwfAnalogIOEnableSet(self.__hdwf, master) |
|
214 | 225 | |
|
215 | 226 | def get_power(self): |
|
216 | 227 | if not self.__opened: |
|
217 | 228 | raise self.errors.get(0) |
|
218 | 229 | supplyVoltage = c_double() |
|
219 | 230 | supplyCurrent = c_double() |
|
220 | 231 | IsEnabled = c_bool() |
|
221 | 232 | self.__libdwf.FDwfAnalogIOStatus(self.__hdwf) |
|
222 | 233 | self.__libdwf.FDwfAnalogIOChannelNodeStatus(self.__hdwf, c_int(3), c_int(0), byref(supplyVoltage)) |
|
223 | 234 | self.__libdwf.FDwfAnalogIOChannelNodeStatus(self.__hdwf, c_int(3), c_int(1), byref(supplyCurrent)) |
|
224 | 235 | self.__libdwf.FDwfAnalogIOEnableStatus(self.__hdwf, byref(IsEnabled)) |
|
225 | 236 | return [IsEnabled.value,supplyVoltage.value,supplyCurrent.value] |
|
226 | 237 | |
|
227 | 238 | ############################################################# |
|
228 | 239 | # AnalogIn |
|
229 | 240 | ############################################################# |
|
230 | 241 | def analog_in_read(self,ch1=True,ch2=True,frequency=100000000,samplesCount=100,ch1range=5.0,ch2range=5.0,trigger=trigsrcNone): |
|
231 | 242 | if not self.__opened: |
|
232 | 243 | raise self.errors.get(0) |
|
233 | 244 | cnt=self.__limits.acqBufSize(samplesCount) |
|
234 | 245 | self.__libdwf.FDwfAnalogInFrequencySet(self.__hdwf, c_double(self.__limits.acqFreq(frequency))) |
|
235 | 246 | f=c_double() |
|
236 | 247 | self.__libdwf.FDwfAnalogInFrequencyGet(self.__hdwf, byref(f)) |
|
237 | 248 | frequency=f.value |
|
238 | 249 | self.__libdwf.FDwfAnalogInBufferSizeSet(self.__hdwf, c_int(cnt)) |
|
239 | 250 | self.__libdwf.FDwfAnalogInChannelEnableSet(self.__hdwf, c_int(0), c_bool(ch1)) |
|
240 | 251 | self.__libdwf.FDwfAnalogInChannelRangeSet(self.__hdwf, c_int(0), c_double(ch1range)) |
|
241 | 252 | self.__libdwf.FDwfAnalogInChannelEnableSet(self.__hdwf, c_int(1), c_bool(ch2)) |
|
242 | 253 | self.__libdwf.FDwfAnalogInChannelRangeSet(self.__hdwf, c_int(1), c_double(ch2range)) |
|
243 | 254 | self.set_analog_in_trigger(trigger) |
|
244 | 255 | self.__libdwf.FDwfAnalogInConfigure(self.__hdwf, c_bool(False), c_bool(True)) |
|
245 | 256 | status = c_byte() |
|
246 | 257 | while True: |
|
247 | 258 | self.__libdwf.FDwfAnalogInStatus(self.__hdwf, c_int(1), byref(status)) |
|
248 | 259 | if status.value == DwfStateDone.value : |
|
249 | 260 | break |
|
250 | 261 | time.sleep(0.1) |
|
251 | 262 | if ch1: |
|
252 | 263 | ch1data = (c_double*cnt)() |
|
253 | 264 | self.__libdwf.FDwfAnalogInStatusData(self.__hdwf, 0, ch1data, cnt) |
|
254 | 265 | if ch2: |
|
255 | 266 | ch2data = (c_double*cnt)() |
|
256 | 267 | self.__libdwf.FDwfAnalogInStatusData(self.__hdwf, 1, ch2data, cnt) |
|
257 | 268 | return [np.array([ch1data,ch2data]),frequency] |
|
258 | 269 | else: |
|
259 | 270 | return [np.array([ch1data]),frequency] |
|
260 | 271 | if ch2: |
|
261 | 272 | ch2data = (c_double*cnt)() |
|
262 | 273 | self.__libdwf.FDwfAnalogInStatusData(self.__hdwf, 1, ch2data, cnt) |
|
263 | 274 | return [np.array([ch2data]),frequency] |
|
264 | 275 | |
|
265 | 276 | |
|
266 | 277 | def set_analog_in_trigger(self,trigger=trigAuto,autoTimeout=0.0): |
|
267 | 278 | if not self.__opened: |
|
268 | 279 | raise self.errors.get(0) |
|
269 | 280 | if trigger == trigAuto: |
|
270 | 281 | self.__libdwf.FDwfAnalogInTriggerSourceSet(self.__hdwf,trigsrcDetectorAnalogIn) |
|
271 | 282 | self.__libdwf.FDwfAnalogInTriggerAutoTimeoutSet(self.__hdwf,c_double(autoTimeout)) |
|
272 | 283 | return |
|
273 | 284 | if trigger == trigNormal: |
|
274 | 285 | self.__libdwf.FDwfAnalogInTriggerSourceSet(self.__hdwf,trigsrcDetectorAnalogIn) |
|
275 | 286 | self.__libdwf.FDwfAnalogInTriggerAutoTimeoutSet(self.__hdwf,c_double(0.0)) |
|
276 | 287 | return |
|
277 | 288 | self.__libdwf.FDwfAnalogInTriggerSourceSet(self.__hdwf,trigger) |
|
278 | 289 | |
|
279 | 290 | ############################################################# |
|
280 | 291 | # AnalogOut |
|
281 | 292 | ############################################################# |
|
282 | 293 | def analog_out_gen(self,frequency=1000, shape='Sine', channel=0, amplitude=1.0, offset=0.0): |
|
283 | 294 | self.__libdwf.FDwfAnalogOutConfigure(self.__hdwf, c_int(channel), c_bool(False)) |
|
284 | 295 | self.__libdwf.FDwfAnalogOutNodeEnableSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_bool(True)) |
|
285 | 296 | self.__libdwf.FDwfAnalogOutNodeFunctionSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_int(shapes.get(shape))) |
|
286 | 297 | self.__libdwf.FDwfAnalogOutNodeFrequencySet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_double(self.__limits.genFreq(frequency))) |
|
287 | 298 | self.__libdwf.FDwfAnalogOutNodeAmplitudeSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_double(self.__limits.genAmplitude(amplitude))) |
|
288 | 299 | self.__libdwf.FDwfAnalogOutNodeOffsetSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_double(self.__limits.genOffset(offset))) |
|
289 | 300 | self.__libdwf.FDwfAnalogOutConfigure(self.__hdwf, c_int(channel), c_bool(True)) |
|
290 | 301 | |
|
291 | 302 | def analog_out_gen_arbit(self,samplesBuffer ,repeatingFrequency=100, channel=0, amplitude=1.0, offset=0.0): |
|
292 | 303 | self.__libdwf.FDwfAnalogOutConfigure(self.__hdwf, c_int(channel), c_bool(False)) |
|
293 | 304 | cnt=self.__limits.genBuffSize(len(samplesBuffer)) |
|
294 | 305 | buf=(c_double*cnt)() |
|
295 | 306 | buf[:]=samplesBuffer[0:cnt] |
|
296 | 307 | repeatingFrequency = self.__limits.genFreq(repeatingFrequency*cnt)/cnt |
|
297 | 308 | self.__libdwf.FDwfAnalogOutNodeEnableSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_bool(True)) |
|
298 | 309 | self.__libdwf.FDwfAnalogOutNodeFunctionSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_int(shapes.get("Custom"))) |
|
299 | 310 | self.__libdwf.FDwfAnalogOutNodeFrequencySet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_double(repeatingFrequency)) |
|
300 | 311 | self.__libdwf.FDwfAnalogOutNodeAmplitudeSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_double(self.__limits.genAmplitude(amplitude))) |
|
301 | 312 | self.__libdwf.FDwfAnalogOutNodeOffsetSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_double(self.__limits.genOffset(offset))) |
|
302 | 313 | self.__libdwf.FDwfAnalogOutNodeDataSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, buf, c_int(cnt)) |
|
303 | 314 | self.__libdwf.FDwfAnalogOutConfigure(self.__hdwf, c_int(channel), c_bool(True)) |
|
304 | 315 | |
|
305 | 316 | |
|
306 | 317 | def __del__(self): |
|
307 | 318 | if self.__opened: |
|
308 | 319 | self.__libdwf.FDwfDeviceClose(self.__hdwf) |
|
309 | 320 | |
|
310 | 321 | |
|
311 | 322 | |
|
312 | 323 | |
|
313 | 324 | if __name__ == '__main__': |
|
314 | 325 | print("open first dev") |
|
315 | 326 | test = Discovery() |
|
316 | 327 | test.set_power() |
|
317 | 328 | for i in range(2): |
|
318 | 329 | time.sleep(0.2) |
|
319 | 330 | print(test.get_power()) |
|
320 | 331 | test.analog_out_gen() |
|
321 | 332 | res=test.analog_in_read(frequency=1000000,samplesCount=1000) |
|
322 | 333 | print(res) |
|
323 | 334 | plt.plot(range(len(res[0][0])),res[0][0]) |
|
324 | 335 | plt.plot(range(len(res[0][0])),res[0][1]) |
|
325 | 336 | plt.show() |
|
326 | 337 | test.temp() |
|
327 | 338 | # del test |
|
328 | 339 | quit() |
|
329 | 340 | |
|
330 | 341 | |
|
331 | 342 | |
|
332 | 343 | |
|
333 | 344 |
@@ -1,44 +1,127 | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | #-*- coding: utf-8 -*- |
|
3 | 3 | """Simple python library to drive DLP-TEMP module from www.dlpdesign.com |
|
4 | 4 | """ |
|
5 | 5 | import time |
|
6 | 6 | import sys |
|
7 | 7 | import os |
|
8 | 8 | import matplotlib.pyplot as plt |
|
9 | 9 | import numpy as np |
|
10 | 10 | import serial |
|
11 | 11 | |
|
12 | 12 | __author__ = "Alexis Jeandet" |
|
13 | 13 | __copyright__ = "Copyright 2015, Laboratory of Plasma Physics" |
|
14 | 14 | __credits__ = [] |
|
15 | 15 | __license__ = "GPLv2" |
|
16 | 16 | __version__ = "1.0.0" |
|
17 | 17 | __maintainer__ = "Alexis Jeandet" |
|
18 | 18 | __email__ = "alexis.jeandet@member.fsf.org" |
|
19 | 19 | __status__ = "Production" |
|
20 | 20 | |
|
21 | class dlp_temp(): | |
|
21 | class dlp_temp(object): | |
|
22 | 22 | sensors = {0 : b'S', |
|
23 | 23 | 1 : b'T', |
|
24 | 24 | 2 : b'U', |
|
25 | 25 | } |
|
26 | aninputs = {0 : b'A', | |
|
27 | 1 : b'B', | |
|
28 | 2 : b'C', | |
|
29 | } | |
|
30 | digitin= {0 : b'M', | |
|
31 | 1 : b'N', | |
|
32 | 2 : b'O', | |
|
33 | } | |
|
34 | digithigh= {0 : b'J', | |
|
35 | 1 : b'K', | |
|
36 | 2 : b'L', | |
|
37 | } | |
|
38 | digitlow= {0 : b'G', | |
|
39 | 1 : b'H', | |
|
40 | 2 : b'I', | |
|
41 | } | |
|
26 | 42 | def __init__(self,port): |
|
27 | 43 | self.i=0 |
|
28 | 44 | self.__port=serial.Serial(port,timeout=0.5) |
|
29 | 45 | |
|
30 | 46 | def ping(self): |
|
31 | 47 | self.__port.write(b"P") |
|
32 | 48 | return b'Q' == self.__port.read(1) |
|
33 | 49 | |
|
34 |
def read |
|
|
50 | def read_sensor(self,index): | |
|
35 | 51 | if index < 3: |
|
36 | 52 | self.__port.write(self.sensors.get(index)) |
|
37 | 53 | dat=self.__port.read(9) |
|
38 | 54 | test=( int(ord(dat[0])) + (int(ord(dat[1]))*256) ) |
|
39 | 55 | temp=float(test)*0.0625 |
|
40 | 56 | return temp #(temp-32.0)/1.8 |
|
41 | 57 | raise UserWarning("Parameter out of bound") |
|
42 | 58 | |
|
59 | def read_analog_in(self,index): | |
|
60 | if index < 3: | |
|
61 | self.__port.write(self.aninputs.get(index)) | |
|
62 | dat=self.__port.read(2) | |
|
63 | test=( int(ord(dat[0])) + (int(ord(dat[1]))*256) ) | |
|
64 | val=float(test)/512.0 | |
|
65 | return val | |
|
66 | raise UserWarning("Parameter out of bound") | |
|
67 | ||
|
68 | def digit_in(self,index): | |
|
69 | if index < 3: | |
|
70 | self.__port.write(self.digitin.get(index)) | |
|
71 | dat=self.__port.read(1) | |
|
72 | return dat | |
|
73 | raise UserWarning("Parameter out of bound") | |
|
74 | ||
|
75 | def digit_out(self,index,val): | |
|
76 | if index < 3: | |
|
77 | if val: | |
|
78 | self.__port.write(self.digithigh.get(index)) | |
|
79 | else: | |
|
80 | self.__port.write(self.digitlow.get(index)) | |
|
81 | raise UserWarning("Parameter out of bound") | |
|
82 | ||
|
83 | @Property | |
|
84 | def sensor1(self): | |
|
85 | return read_sensor(0) | |
|
86 | ||
|
87 | @Property | |
|
88 | def sensor2(self): | |
|
89 | return read_sensor(1) | |
|
90 | ||
|
91 | @Property | |
|
92 | def sensor3(self): | |
|
93 | return read_sensor(2) | |
|
94 | ||
|
95 | @Property | |
|
96 | def AN1(self): | |
|
97 | return read_analog_in(0) | |
|
98 | @Property | |
|
99 | def AN2(self): | |
|
100 | return read_analog_in(1) | |
|
101 | @Property | |
|
102 | def AN3(self): | |
|
103 | return read_analog_in(2) | |
|
104 | ||
|
105 | @Property | |
|
106 | def GP2(self): | |
|
107 | return self.digit_in(0) | |
|
108 | @GP2.setter | |
|
109 | def GP2(self,value): | |
|
110 | return self.digit_out(0,val) | |
|
111 | ||
|
112 | @Property | |
|
113 | def GP0(self): | |
|
114 | return self.digit_in(1) | |
|
115 | @GP0.setter | |
|
116 | def GP0(self,value): | |
|
117 | return self.digit_out(1,val) | |
|
118 | ||
|
119 | @Property | |
|
120 | def GP4(self): | |
|
121 | return self.digit_in(2) | |
|
122 | @GP4.setter | |
|
123 | def GP4(self,value): | |
|
124 | return self.digit_out(2,val) | |
|
125 | ||
|
43 | 126 | if __name__ == '__main__': |
|
44 | 127 | print("") |
@@ -1,126 +1,126 | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | #-*- coding: utf-8 -*- |
|
3 | 3 | """Simple python library to communicate with GW Instek GPD-Series power supplies. |
|
4 | 4 | """ |
|
5 | 5 | import time |
|
6 | 6 | import sys |
|
7 | 7 | import os |
|
8 | 8 | import matplotlib.pyplot as plt |
|
9 | 9 | import numpy as np |
|
10 | 10 | import serial |
|
11 | 11 | |
|
12 | 12 | __author__ = "Alexis Jeandet" |
|
13 | 13 | __copyright__ = "Copyright 2015, Laboratory of Plasma Physics" |
|
14 | 14 | __credits__ = [] |
|
15 | 15 | __license__ = "GPLv2" |
|
16 | 16 | __version__ = "1.0.0" |
|
17 | 17 | __maintainer__ = "Alexis Jeandet" |
|
18 | 18 | __email__ = "alexis.jeandet@member.fsf.org" |
|
19 | 19 | __status__ = "Development" |
|
20 | 20 | |
|
21 | 21 | |
|
22 | class gpd_xxx(): | |
|
22 | class gpd_xxx(object): | |
|
23 | 23 | conf = {"GPD-3303S" : (2,0.0,30.0), |
|
24 | 24 | "GPD-3303" : (), |
|
25 | 25 | } |
|
26 | 26 | trackingMode = {"Independent" : '0', |
|
27 | 27 | "Series" : '1', |
|
28 | 28 | "Parallel" : '2', |
|
29 | 29 | } |
|
30 | 30 | trackingModeStat = {"01": "Independent", |
|
31 | 31 | "11" : "Series", |
|
32 | 32 | "10" : "Parallel", |
|
33 | 33 | } |
|
34 | 34 | def __init__(self,port): |
|
35 | 35 | self.i=0 |
|
36 | 36 | self.__port=serial.Serial(port,timeout=0.5) |
|
37 | 37 | |
|
38 | 38 | def idn(self): |
|
39 | 39 | self.__port.setTimeout(0.1) |
|
40 | 40 | self.__port.write(b"*IDN?\n") |
|
41 | 41 | return self.__port.readall() |
|
42 | 42 | |
|
43 | 43 | def setVoltage(self,index,tension): |
|
44 | 44 | if index < 2: |
|
45 | 45 | self.__port.write("VSET"+str(index+1)+":"+str(tension)+"\n") |
|
46 | 46 | else: |
|
47 | 47 | raise UserWarning("Parameter out of bound") |
|
48 | 48 | |
|
49 | 49 | def voltageSet(self,index): |
|
50 | 50 | if index < 2: |
|
51 | 51 | self.__port.setTimeout(0.1) |
|
52 | 52 | self.__port.write("VSET"+str(index+1)+"?\n") |
|
53 | 53 | V=self.__port.readall() |
|
54 | 54 | return float(V.split("V")[0]) |
|
55 | 55 | else: |
|
56 | 56 | raise UserWarning("Parameter out of bound") |
|
57 | 57 | |
|
58 | 58 | def voltage(self,index): |
|
59 | 59 | if index < 2: |
|
60 | 60 | self.__port.setTimeout(0.1) |
|
61 | 61 | self.__port.write("VOUT"+str(index+1)+"?\n") |
|
62 | 62 | V=self.__port.readall() |
|
63 | 63 | return float(V.split("V")[0]) |
|
64 | 64 | else: |
|
65 | 65 | raise UserWarning("Parameter out of bound") |
|
66 | 66 | |
|
67 | 67 | def setCurrentLimit(self,index,limit): |
|
68 | 68 | if index < 2: |
|
69 | 69 | self.__port.write("ISET"+str(index+1)+":"+str(limit)+"\n") |
|
70 | 70 | else: |
|
71 | 71 | raise UserWarning("Parameter out of bound") |
|
72 | 72 | |
|
73 | 73 | def currentLimit(self,index): |
|
74 | 74 | if index < 2: |
|
75 | 75 | self.__port.setTimeout(0.1) |
|
76 | 76 | self.__port.write("ISET"+str(index+1)+"?\n") |
|
77 | 77 | I = self.__port.readall() |
|
78 | 78 | return float(I.split("A")[0]) |
|
79 | 79 | else: |
|
80 | 80 | raise UserWarning("Parameter out of bound") |
|
81 | 81 | |
|
82 | 82 | def current(self,index): |
|
83 | 83 | if index < 2: |
|
84 | 84 | self.__port.setTimeout(0.1) |
|
85 | 85 | self.__port.write("IOUT"+str(index+1)+"?\n") |
|
86 | 86 | I = self.__port.readall() |
|
87 | 87 | return float(I.split("A")[0]) |
|
88 | 88 | else: |
|
89 | 89 | raise UserWarning("Parameter out of bound") |
|
90 | 90 | |
|
91 | 91 | def turnOn(self,on=True): |
|
92 | 92 | if on: |
|
93 | 93 | self.__port.write("OUT1\n") |
|
94 | 94 | else: |
|
95 | 95 | self.__port.write("OUT0\n") |
|
96 | 96 | |
|
97 | 97 | def setTracking(self,mode="Independent"): |
|
98 | 98 | self.__port.write("TRACK"+self.trackingMode.get(mode)+"\n") |
|
99 | 99 | |
|
100 | 100 | def setBeep(self,on=True): |
|
101 | 101 | if on: |
|
102 | 102 | self.__port.write("BEEP1\n") |
|
103 | 103 | else: |
|
104 | 104 | self.__port.write("BEEP0\n") |
|
105 | 105 | |
|
106 | 106 | def tracking(self): |
|
107 | 107 | self.__port.write("STATUS?\n") |
|
108 | 108 | self.__port.setTimeout(0.1) |
|
109 | 109 | STAT = self.__port.readall() |
|
110 | 110 | BITS = STAT.split(" ") |
|
111 | 111 | return self.trackingModeStat.get(''.join(BITS[2:4])) |
|
112 | 112 | |
|
113 | 113 | def save(self,mem=1): |
|
114 | 114 | if mem>=1 and mem<=4: |
|
115 | 115 | self.__port.write("SAV"+str(mem)+"\n") |
|
116 | 116 | else: |
|
117 | 117 | raise UserWarning("Parameter mem of bound 1-4") |
|
118 | 118 | |
|
119 | 119 | def recal(self,mem=1): |
|
120 | 120 | if mem>=1 and mem<=4: |
|
121 | 121 | self.__port.write("RCL"+str(mem)+"\n") |
|
122 | 122 | else: |
|
123 | 123 | raise UserWarning("Parameter mem of bound 1-4") |
|
124 | 124 | |
|
125 | 125 | if __name__ == '__main__': |
|
126 | 126 | print("") |
@@ -1,71 +1,79 | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | #-*- coding: utf-8 -*- |
|
3 | 3 | """Simple python library to compute transfert functions |
|
4 | 4 | """ |
|
5 | 5 | import time |
|
6 | 6 | import sys |
|
7 | 7 | import os |
|
8 | 8 | import matplotlib.pyplot as plt |
|
9 | 9 | import numpy as np |
|
10 | 10 | from scipy import fftpack |
|
11 | 11 | |
|
12 | 12 | __author__ = "Alexis Jeandet" |
|
13 | 13 | __copyright__ = "Copyright 2015, Laboratory of Plasma Physics" |
|
14 | 14 | __credits__ = [] |
|
15 | 15 | __license__ = "GPLv2" |
|
16 | 16 | __version__ = "1.0.0" |
|
17 | 17 | __maintainer__ = "Alexis Jeandet" |
|
18 | 18 | __email__ = "alexis.jeandet@member.fsf.org" |
|
19 | 19 | __status__ = "Development" |
|
20 | 20 | |
|
21 | 21 | |
|
22 | ||
|
23 | 22 | def __parseFFT(FFTi,FFTo,signalFreq,samplingFreq): |
|
24 | 23 | index=signalFreq*len(FFTi)/samplingFreq |
|
25 |
powI=np.abs(FFTi[index- |
|
|
26 |
i=np.argmax(powI)+index- |
|
|
24 | powI=np.abs(FFTi[int(index-1):int(index+1)]) | |
|
25 | i=int(np.argmax(powI)+index-1) | |
|
27 | 26 | mod=np.abs(FFTo[i])/np.abs(FFTi[i]) |
|
28 | 27 | arg=np.angle(FFTo[i])-np.angle(FFTi[i]) |
|
29 | 28 | if arg<-np.pi: |
|
30 | 29 | arg = (np.pi*2)+arg |
|
31 | 30 | if arg>np.pi: |
|
32 | 31 | arg = (-np.pi*2)+arg |
|
33 | 32 | return [signalFreq,mod,arg] |
|
34 | 33 | |
|
34 | def __compute_params(device,freq): | |
|
35 | Periods=5 | |
|
36 | Fs=(freq*device.max_sampling_buffer)/Periods | |
|
37 | if Fs < device.min_sampling_freq : | |
|
38 | return [device.min_sampling_freq,device.max_sampling_buffer] | |
|
39 | while Fs > (0.98*device.max_sampling_freq) : | |
|
40 | Periods=Periods+1 | |
|
41 | Fs=(freq*device.max_sampling_buffer)/Periods | |
|
42 | return [Fs,device.max_sampling_buffer] | |
|
43 | ||
|
35 | 44 | def __step(device,freq,offset=0.0,maxAmp=5.0,lastAmp=1.0): |
|
36 | 45 | device.analog_out_gen(freq, shape='Sine', channel=0, amplitude=lastAmp, offset=offset) |
|
37 | samplesCount=8192 | |
|
38 | if freq > 500000: | |
|
39 | FS=freq*samplesCount/500.0 | |
|
40 | elif freq > 100000: | |
|
41 | FS=freq*samplesCount/50.0 | |
|
42 | else: | |
|
43 | FS=freq*samplesCount/10.0 | |
|
44 | res=device.analog_in_read(ch1=True,ch2=True,frequency=FS,samplesCount=samplesCount,ch1range=5.0,ch2range=5.0) | |
|
45 | FFTi=fftpack.fft(res[0][0]) | |
|
46 | FFTo=fftpack.fft(res[0][1]) | |
|
46 | params=__compute_params(device,freq) | |
|
47 | res=device.analog_in_read(ch1=True,ch2=True,frequency=params[0],samplesCount=params[1],ch1range=5.0,ch2range=5.0) | |
|
48 | meanI=np.mean(res[0][0]) | |
|
49 | meanO=np.mean(res[0][1]) | |
|
50 | FFTi=fftpack.fft((res[0][0]-meanI)*np.hamming(len(res[0][0]))) | |
|
51 | FFTo=fftpack.fft((res[0][1]-meanO)*np.hamming(len(res[0][1]))) | |
|
47 | 52 | return __parseFFT(FFTi,FFTo,freq,res[1]) |
|
48 | 53 | |
|
54 | def generateLogFreq(startFreq=1.0,stopFreq=100.0,nstep=100): | |
|
55 | freq=np.zeros(nstep) | |
|
56 | for i in range(int(nstep)) : | |
|
57 | freq[i]=startFreq*np.power(10,((np.log10(stopFreq/startFreq))*i/(nstep-1))) | |
|
58 | return freq | |
|
49 | 59 | |
|
50 | 60 | def computeTF(device,startFreq=1.0,stopFreq=100.0,offset=0.0,maxAmp=5.0,nstep=100): |
|
51 | freq=np.zeros(nstep) | |
|
61 | freqs=generateLogFreq(startFreq,stopFreq,nstep) | |
|
52 | 62 | f=[] |
|
53 | 63 | mod=[] |
|
54 | 64 | arg=[] |
|
55 | for i in range(int(nstep)) : | |
|
56 | freq[i]=startFreq*np.power(10,((np.log10(stopFreq/startFreq))*i/(nstep-1))) | |
|
57 | 65 | lastAmp=0.1 |
|
58 |
for |
|
|
59 |
step=__step(device,freq |
|
|
66 | for freq in freqs: | |
|
67 | step=__step(device,freq,offset=offset,maxAmp=maxAmp,lastAmp=lastAmp) | |
|
60 | 68 | f.append(step[0]) |
|
61 | 69 | mod.append(step[1]) |
|
62 | 70 | arg.append(step[2]) |
|
63 | 71 | return [f,mod,arg] |
|
64 | 72 | |
|
65 | 73 | |
|
66 | 74 | if __name__ == '__main__': |
|
67 | 75 | print("") |
|
68 | 76 | |
|
69 | 77 | |
|
70 | 78 | |
|
71 | 79 |
@@ -1,55 +1,55 | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | #-*- coding: utf-8 -*- |
|
3 | 3 | """Simple python library to communicate over USB-TMC protocol with linux's |
|
4 | 4 | usbtmc module. |
|
5 | 5 | """ |
|
6 | 6 | import time |
|
7 | 7 | import sys |
|
8 | 8 | import os |
|
9 | 9 | import glob |
|
10 | 10 | import re |
|
11 | 11 | |
|
12 | 12 | __author__ = "Alexis Jeandet" |
|
13 | 13 | __copyright__ = "Copyright 2015, Laboratory of Plasma Physics" |
|
14 | 14 | __credits__ = [] |
|
15 | 15 | __license__ = "GPLv2" |
|
16 | 16 | __version__ = "1.0.0" |
|
17 | 17 | __maintainer__ = "Alexis Jeandet" |
|
18 | 18 | __email__ = "alexis.jeandet@member.fsf.org" |
|
19 | 19 | __status__ = "Development" |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | def find_instrument(ref,serial=""): |
|
23 | 23 | instruments=glob.glob("/dev/usbtmc[0-9]") |
|
24 | 24 | p = re.compile(ref) |
|
25 | 25 | for instrument in instruments: |
|
26 | 26 | dev=UsbTmc(instrument) |
|
27 | 27 | idn=dev.idn().split(",") |
|
28 | 28 | if p.match(idn[1]): |
|
29 | 29 | if serial=="" or serial == idn[2]: |
|
30 | 30 | return instrument |
|
31 | 31 | return "" |
|
32 | 32 | |
|
33 | class UsbTmc(): | |
|
33 | class UsbTmc(object): | |
|
34 | 34 | def __init__(self,dev): |
|
35 | 35 | self.__PATH__=dev |
|
36 | 36 | self.__FILE__ = os.open(dev, os.O_RDWR) |
|
37 | 37 | if self.__FILE__==-1: |
|
38 | 38 | raise UserWarning("can't open "+dev) |
|
39 | 39 | self.Manufacturer="" |
|
40 | 40 | self.Reference="" |
|
41 | 41 | self.Serial="" |
|
42 | 42 | self.Version="" |
|
43 | 43 | |
|
44 | 44 | def write(self, command): |
|
45 | 45 | os.write(self.__FILE__, command); |
|
46 | 46 | |
|
47 | 47 | def read(self, length = 4000): |
|
48 | 48 | return os.read(self.__FILE__, length) |
|
49 | 49 | |
|
50 | 50 | def idn(self): |
|
51 | 51 | self.write("*IDN?") |
|
52 | 52 | return self.read(100) |
|
53 | 53 | |
|
54 | 54 | def __str__(self): |
|
55 | 55 | return self.idn() +"\n"+ self.__PATH__ |
General Comments 0
You need to be logged in to leave comments.
Login now