@@ -0,0 +1,24 | |||
|
1 | #!/usr/bin/env python | |
|
2 | #-*- coding: utf-8 -*- | |
|
3 | """Simple python library to communicate with Agilent 34410A over USB-TMC. | |
|
4 | """ | |
|
5 | import time | |
|
6 | import sys | |
|
7 | import os | |
|
8 | import agilenttmc | |
|
9 | ||
|
10 | __author__ = "Alexis Jeandet" | |
|
11 | __copyright__ = "Copyright 2015, Laboratory of Plasma Physics" | |
|
12 | __credits__ = [] | |
|
13 | __license__ = "GPLv2" | |
|
14 | __version__ = "1.0.0" | |
|
15 | __maintainer__ = "Alexis Jeandet" | |
|
16 | __email__ = "alexis.jeandet@member.fsf.org" | |
|
17 | __status__ = "Development" | |
|
18 | ||
|
19 | class Agilent3441xA(usbtmc.UsbTmc): | |
|
20 | def __init__(self,ref,serial=""): | |
|
21 | isnt=usbtmc.findInstrument(ref,serial) | |
|
22 | if inst=="": | |
|
23 | raise UserWarning("Can't find instrument "+ref) | |
|
24 | self.UsbTmc.__init__(isnt) |
@@ -0,0 +1,26 | |||
|
1 | #!/usr/bin/env python | |
|
2 | #-*- coding: utf-8 -*- | |
|
3 | """Simple python library to communicate with Agilent 34410A over USB-TMC. | |
|
4 | """ | |
|
5 | import time | |
|
6 | import sys | |
|
7 | import os | |
|
8 | import agilenttmc | |
|
9 | ||
|
10 | __author__ = "Alexis Jeandet" | |
|
11 | __copyright__ = "Copyright 2015, Laboratory of Plasma Physics" | |
|
12 | __credits__ = [] | |
|
13 | __license__ = "GPLv2" | |
|
14 | __version__ = "1.0.0" | |
|
15 | __maintainer__ = "Alexis Jeandet" | |
|
16 | __email__ = "alexis.jeandet@member.fsf.org" | |
|
17 | __status__ = "Development" | |
|
18 | ||
|
19 | "34410A" | |
|
20 | ||
|
21 | class Agilent3441xA(agilentusbtmc.AgilentUsbTmc): | |
|
22 | def __init__(self,serial=""): | |
|
23 | isnt=usbtmc.findInstrument("3441[0-1]A",serial) | |
|
24 | if inst=="": | |
|
25 | raise UserWarning("Can't find instrument "+ref) | |
|
26 | self.UsbTmc.__init__(isnt) |
@@ -0,0 +1,24 | |||
|
1 | #!/usr/bin/env python | |
|
2 | #-*- coding: utf-8 -*- | |
|
3 | """Common Agilent USB-TMC/SCPI traits. | |
|
4 | """ | |
|
5 | import time | |
|
6 | import sys | |
|
7 | import os | |
|
8 | import usbtmc | |
|
9 | ||
|
10 | __author__ = "Alexis Jeandet" | |
|
11 | __copyright__ = "Copyright 2015, Laboratory of Plasma Physics" | |
|
12 | __credits__ = [] | |
|
13 | __license__ = "GPLv2" | |
|
14 | __version__ = "1.0.0" | |
|
15 | __maintainer__ = "Alexis Jeandet" | |
|
16 | __email__ = "alexis.jeandet@member.fsf.org" | |
|
17 | __status__ = "Development" | |
|
18 | ||
|
19 | class AgilentUsbTmc(usbtmc.UsbTmc): | |
|
20 | def __init__(self,ref,serial=""): | |
|
21 | isnt=usbtmc.findInstrument(ref,serial) | |
|
22 | if inst=="": | |
|
23 | raise UserWarning("Can't find instrument "+ref) | |
|
24 | self.UsbTmc.__init__(isnt) |
@@ -0,0 +1,55 | |||
|
1 | #!/usr/bin/env python | |
|
2 | #-*- coding: utf-8 -*- | |
|
3 | """Simple python library to communicate over USB-TMC protocol with linux's | |
|
4 | usbtmc module. | |
|
5 | """ | |
|
6 | import time | |
|
7 | import sys | |
|
8 | import os | |
|
9 | import glob | |
|
10 | import re | |
|
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 | ||
|
22 | def find_instrument(ref,serial=""): | |
|
23 | instruments=glob.glob("/dev/usbtmc[0-9]") | |
|
24 | p = re.compile(ref) | |
|
25 | for instrument in instruments: | |
|
26 | dev=UsbTmc(instrument) | |
|
27 | idn=dev.idn().split(",") | |
|
28 | if p.match(idn[1]): | |
|
29 | if serial=="" or serial == idn[2]: | |
|
30 | return instrument | |
|
31 | return "" | |
|
32 | ||
|
33 | class UsbTmc(): | |
|
34 | def __init__(self,dev): | |
|
35 | self.__PATH__=dev | |
|
36 | self.__FILE__ = os.open(dev, os.O_RDWR) | |
|
37 | if self.__FILE__==-1: | |
|
38 | raise UserWarning("can't open "+dev) | |
|
39 | self.Manufacturer="" | |
|
40 | self.Reference="" | |
|
41 | self.Serial="" | |
|
42 | self.Version="" | |
|
43 | ||
|
44 | def write(self, command): | |
|
45 | os.write(self.__FILE__, command); | |
|
46 | ||
|
47 | def read(self, length = 4000): | |
|
48 | return os.read(self.__FILE__, length) | |
|
49 | ||
|
50 | def idn(self): | |
|
51 | self.write("*IDN?") | |
|
52 | return self.read(100) | |
|
53 | ||
|
54 | def __str__(self): | |
|
55 | return self.idn() +"\n"+ self.__PATH__ |
@@ -7,4 +7,4 | |||
|
7 | 7 | __version__ = "1.0.0" |
|
8 | 8 | __maintainer__ = "Alexis Jeandet" |
|
9 | 9 | __email__ = "alexis.jeandet@member.fsf.org" |
|
10 |
__status__ = " |
|
|
10 | __status__ = "Development" |
This diff has been collapsed as it changes many lines, (664 lines changed) Show them Hide them | |||
@@ -1,331 +1,333 | |||
|
1 | #!/usr/bin/env python | |
|
2 | #-*- coding: utf-8 -*- | |
|
3 | """Simple python library to drive the analog discovery module from www.digilentinc.com | |
|
4 | """ | |
|
5 | ||
|
6 | from ctypes import * | |
|
7 | import time | |
|
8 | import sys | |
|
9 | import os | |
|
10 | import matplotlib.pyplot as plt | |
|
11 | import numpy as np | |
|
12 | ||
|
13 | __author__ = "Alexis Jeandet" | |
|
14 | __copyright__ = "Copyright 2015, Laboratory of Plasma Physics" | |
|
15 | __credits__ = [] | |
|
16 | __license__ = "GPLv2" | |
|
17 | __version__ = "1.0.0" | |
|
18 | __maintainer__ = "Alexis Jeandet" | |
|
19 | __email__ = "alexis.jeandet@member.fsf.org" | |
|
20 | __status__ = "Production" | |
|
21 | ||
|
22 | ||
|
23 | nodev = c_int(0) | |
|
24 | DwfStateDone = c_int(2) | |
|
25 | ||
|
26 | DECIAnalogInChannelCount = c_int(1) | |
|
27 | DECIAnalogOutChannelCount = c_int(2) | |
|
28 | DECIAnalogIOChannelCount = c_int(3) | |
|
29 | DECIDigitalInChannelCount = c_int(4) | |
|
30 | DECIDigitalOutChannelCount = c_int(5) | |
|
31 | DECIDigitalIOChannelCount = c_int(6) | |
|
32 | DECIAnalogInBufferSize = c_int(7) | |
|
33 | DECIAnalogOutBufferSize = c_int(8) | |
|
34 | DECIDigitalInBufferSize = c_int(9) | |
|
35 | DECIDigitalOutBufferSize = c_int(10) | |
|
36 | ||
|
37 | trigsrcNone = c_byte(0) | |
|
38 | trigsrcPC = c_byte(1) | |
|
39 | trigsrcDetectorAnalogIn = c_byte(2) | |
|
40 | trigsrcDetectorDigitalIn = c_byte(3) | |
|
41 | trigsrcAnalogIn = c_byte(4) | |
|
42 | trigsrcDigitalIn = c_byte(5) | |
|
43 | trigsrcDigitalOut = c_byte(6) | |
|
44 | trigsrcAnalogOut1 = c_byte(7) | |
|
45 | trigsrcAnalogOut2 = c_byte(8) | |
|
46 | trigsrcAnalogOut3 = c_byte(9) | |
|
47 | trigsrcAnalogOut4 = c_byte(10) | |
|
48 | trigsrcExternal1 = c_byte(11) | |
|
49 | trigsrcExternal2 = c_byte(12) | |
|
50 | trigsrcExternal3 = c_byte(13) | |
|
51 | trigsrcExternal4 = c_byte(14) | |
|
52 | trigAuto = c_byte(254) | |
|
53 | trigNormal = c_byte(255) | |
|
54 | ||
|
55 | AnalogOutNodeCarrier = c_int(0) | |
|
56 | AnalogOutNodeFM = c_int(1) | |
|
57 | AnalogOutNodeAM = c_int(2) | |
|
58 | ||
|
59 | ||
|
60 | shapes = {'DC' : 0, | |
|
61 | 'Sine' : 1, | |
|
62 | 'Square' : 2, | |
|
63 | 'Triangle' : 3, | |
|
64 | 'RampUp' : 4, | |
|
65 | 'RampDown' : 5, | |
|
66 | 'Noise' : 6, | |
|
67 | 'Custom' : 30, | |
|
68 | 'Play' :31, } | |
|
69 | ||
|
70 | closed=False | |
|
71 | opened=True | |
|
72 | ||
|
73 | ||
|
74 |
class |
|
|
75 | class limitRange(): | |
|
76 | def __init__(self,Min,Max,name="Unknow",unit=""): | |
|
77 | self.Min = Min | |
|
78 | self.Max = Max | |
|
79 | self.name = name | |
|
80 | self.unit = unit | |
|
81 | ||
|
82 | def conform(self,value): | |
|
83 | if value<self.Min: | |
|
84 | raise UserWarning("Parameter "+self.name+" out of bound\nValue="+str(value)+"\nForce to "+str(self.Min)) | |
|
85 | return self.Min | |
|
86 | if value>self.Max: | |
|
87 | raise UserWarning("Parameter "+self.name+" out of bound\nValue="+str(value)+"\nForce to "+str(self.Max)) | |
|
88 | return self.Max | |
|
89 | return value | |
|
90 | ||
|
91 |
def |
|
|
92 |
|
|
|
93 | ||
|
94 | errors = {0: RuntimeError("No card opened"), | |
|
95 | 1: UserWarning("Parameter out of bound"), | |
|
96 | } | |
|
97 | def __init__(self,libdwf,hdwf): | |
|
98 | self.limits=[] | |
|
99 | self.ACQ_IN_RANGES=[0.0] | |
|
100 | if hdwf.value == nodev.value: | |
|
101 | return | |
|
102 | self.__hdwf=hdwf | |
|
103 | self.__libdwf=libdwf | |
|
104 | Mind=c_double() | |
|
105 | Maxd=c_double() | |
|
106 | Mini=c_int() | |
|
107 | Maxi=c_int() | |
|
108 | StepsCount=c_int() | |
|
109 | Steps=(c_double*32)() | |
|
110 | self.__libdwf.FDwfAnalogInBufferSizeInfo(self.__hdwf, byref(Mini), byref(Maxi)) | |
|
111 | self.ACQ_BUF=self.limitRange(Mini.value,Maxi.value,"ACQ Buffer Size","Sps") | |
|
112 | self.limits.append(self.ACQ_BUF) | |
|
113 | self.__libdwf.FDwfAnalogInFrequencyInfo(self.__hdwf, byref(Mind), byref(Maxd)) | |
|
114 | self.ACQ_FREQ=self.limitRange(Mind.value,Maxd.value,"ACQ Frequency","Hz") | |
|
115 | self.limits.append(self.ACQ_FREQ) | |
|
116 | self.__libdwf.FDwfAnalogInChannelRangeSteps(self.__hdwf, byref(Steps), byref(StepsCount)) | |
|
117 | self.ACQ_IN_RANGES=Steps[0:StepsCount.value] | |
|
118 | self.__libdwf.FDwfAnalogOutNodeAmplitudeInfo(self.__hdwf,c_int(0), AnalogOutNodeCarrier, | |
|
119 | byref(Mind), byref(Maxd)) | |
|
120 | self.GEN_AMPL=self.limitRange(Mind.value,Maxd.value,"GEN Amplitude","V") | |
|
121 | self.limits.append(self.GEN_AMPL) | |
|
122 | self.__libdwf.FDwfAnalogOutNodeFrequencyInfo(self.__hdwf,c_int(0), AnalogOutNodeCarrier, | |
|
123 | byref(Mind), byref(Maxd)) | |
|
124 | self.GEN_FREQ=self.limitRange(Mind.value,Maxd.value,"GEN Frequency","Hz") | |
|
125 | self.limits.append(self.GEN_FREQ) | |
|
126 | self.__libdwf.FDwfAnalogOutNodeOffsetInfo(self.__hdwf,c_int(0), AnalogOutNodeCarrier, | |
|
127 | byref(Mind), byref(Maxd)) | |
|
128 | self.GEN_OFFSET=self.limitRange(Mind.value,Maxd.value,"GEN Offset","V") | |
|
129 | self.limits.append(self.GEN_OFFSET) | |
|
130 | self.__libdwf.FDwfAnalogOutNodeDataInfo(self.__hdwf,c_int(0), AnalogOutNodeCarrier, | |
|
131 | byref(Mini), byref(Maxi)) | |
|
132 | self.GEN_BUFF=self.limitRange(Mini.value,Maxi.value,"GEN Buffer size","Sps") | |
|
133 | self.limits.append(self.GEN_BUFF) | |
|
134 | ||
|
135 | ||
|
136 | def __conformParam(self,minVal,maxVal,val): | |
|
137 | if val<minVal: | |
|
138 | raise self.errors.get(1) | |
|
139 | print("Force to "+str(minVal)) | |
|
140 | return minVal | |
|
141 | if val>maxVal: | |
|
142 | raise self.errors.get(1) | |
|
143 | print("Force to "+str(maxVal)) | |
|
144 | return maxVal | |
|
145 | return val | |
|
146 | ||
|
147 | def acqFreq(self, value): | |
|
148 | return self.ACQ_FREQ.conform(value) | |
|
149 | ||
|
150 | def acqBufSize(self, value): | |
|
151 | return self.ACQ_BUF.conform(value) | |
|
152 | ||
|
153 | def genFreq(self, value): | |
|
154 | return self.GEN_FREQ.conform(value) | |
|
155 | ||
|
156 | def genAmplitude(self, value): | |
|
157 | return self.GEN_AMPL.conform(value) | |
|
158 | ||
|
159 | def genOffset(self, value): | |
|
160 | return self.GEN_OFFSET.conform(value) | |
|
161 | ||
|
162 | def genBuffSize(self, value): | |
|
163 | return self.GEN_BUFF.conform(value) | |
|
164 | ||
|
165 |
def |
|
|
166 | for i in self.limits: | |
|
167 | i.printme() | |
|
168 | print("ACQ Input ranes: "+str(self.ACQ_IN_RANGES)) | |
|
169 | ||
|
170 | ||
|
171 | class discovery(): | |
|
172 | ||
|
173 | errors = {0: RuntimeError("No card opened"), | |
|
174 | 1: UserWarning("Parameter out of bound"), | |
|
175 | } | |
|
176 | def __init__(self,card=-1): | |
|
177 | if sys.platform.startswith("win"): | |
|
178 | self.__libdwf = cdll.dwf | |
|
179 |
|
|
|
180 |
self.__libdwf = cdll. |
|
|
181 | else: | |
|
182 |
self.__libdwf = cdll.LoadLibrary("libdwf. |
|
|
183 | self.__opened = True | |
|
184 | self.__hdwf = c_int() | |
|
185 | self.__libdwf.FDwfDeviceOpen(c_int(card), byref(self.__hdwf)) | |
|
186 |
|
|
|
187 | szerr = create_string_buffer(512) | |
|
188 | self.__libdwf.FDwfGetLastErrorMsg(szerr) | |
|
189 | print(szerr.value) | |
|
190 | print("failed to open device") | |
|
191 | self.__opened=False | |
|
192 | self.__limits=discoveryLimits(self.__libdwf,self.__hdwf) | |
|
193 | self.__limits.printme() | |
|
194 | ||
|
195 | @property | |
|
196 | def opened(self): | |
|
197 | return self.__opened | |
|
198 | ||
|
199 | ||
|
200 | ############################################################# | |
|
201 | # Power Supply | |
|
202 | ############################################################# | |
|
203 | def setPower(self,fiveVolt=1,minusFiveVolt=1,master=True): | |
|
204 | if not self.__opened: | |
|
205 | raise self.errors.get(0) | |
|
206 | # enable positive supply | |
|
207 | self.__libdwf.FDwfAnalogIOChannelNodeSet(self.__hdwf, 0, 0, c_double(fiveVolt)) | |
|
208 |
# enable |
|
|
209 |
self.__libdwf.FDwfAnalogIOChannelNodeSet(self.__hdwf, |
|
|
210 | # master enable | |
|
211 |
|
|
|
212 | ||
|
213 | def getPower(self): | |
|
214 | if not self.__opened: | |
|
215 | raise self.errors.get(0) | |
|
216 | supplyVoltage = c_double() | |
|
217 | supplyCurrent = c_double() | |
|
218 |
|
|
|
219 | self.__libdwf.FDwfAnalogIOStatus(self.__hdwf) | |
|
220 | self.__libdwf.FDwfAnalogIOChannelNodeStatus(self.__hdwf, c_int(3), c_int(0), byref(supplyVoltage)) | |
|
221 |
self.__libdwf.FDwfAnalogIO |
|
|
222 |
self.__libdwf.FDwfAnalogIO |
|
|
223 | return [IsEnabled.value,supplyVoltage.value,supplyCurrent.value] | |
|
224 | ||
|
225 | ############################################################# | |
|
226 | # AnalogIn | |
|
227 | ############################################################# | |
|
228 | def analogInRead(self,ch1=True,ch2=True,frequency=100000000,samplesCount=100,ch1range=5.0,ch2range=5.0,trigger=trigsrcNone): | |
|
229 | if not self.__opened: | |
|
230 | raise self.errors.get(0) | |
|
231 | cnt=self.__limits.acqBufSize(samplesCount) | |
|
232 | self.__libdwf.FDwfAnalogInFrequencySet(self.__hdwf, c_double(self.__limits.acqFreq(frequency))) | |
|
233 | f=c_double() | |
|
234 |
self.__libdwf.FDwfAnalogInFrequency |
|
|
235 | frequency=f.value | |
|
236 |
self.__libdwf.FDwfAnalogIn |
|
|
237 | self.__libdwf.FDwfAnalogInChannelEnableSet(self.__hdwf, c_int(0), c_bool(ch1)) | |
|
238 |
self.__libdwf.FDwfAnalogIn |
|
|
239 |
self.__libdwf.FDwfAnalogInChannelEnableSet(self.__hdwf, c_int( |
|
|
240 |
self.__libdwf.FDwfAnalogInChannelRangeSet(self.__hdwf, c_int( |
|
|
241 | self.setAnaloInTrigger(trigger) | |
|
242 |
self.__libdwf.FDwfAnalogInC |
|
|
243 | status = c_byte() | |
|
244 | while True: | |
|
245 | self.__libdwf.FDwfAnalogInStatus(self.__hdwf, c_int(1), byref(status)) | |
|
246 | if status.value == DwfStateDone.value : | |
|
247 | break | |
|
248 | time.sleep(0.1) | |
|
249 | if ch1: | |
|
250 | ch1data = (c_double*cnt)() | |
|
251 | self.__libdwf.FDwfAnalogInStatusData(self.__hdwf, 0, ch1data, cnt) | |
|
252 | if ch2: | |
|
253 | ch2data = (c_double*cnt)() | |
|
254 | self.__libdwf.FDwfAnalogInStatusData(self.__hdwf, 1, ch2data, cnt) | |
|
255 | return [np.array([ch1data,ch2data]),frequency] | |
|
256 | else: | |
|
257 | return [np.array([ch1data]),frequency] | |
|
258 |
|
|
|
259 | ch2data = (c_double*cnt)() | |
|
260 | self.__libdwf.FDwfAnalogInStatusData(self.__hdwf, 1, ch2data, cnt) | |
|
261 | return [np.array([ch2data]),frequency] | |
|
262 | ||
|
263 | ||
|
264 | def setAnaloInTrigger(self,trigger=trigAuto,autoTimeout=0.0): | |
|
265 | if not self.__opened: | |
|
266 | raise self.errors.get(0) | |
|
267 | if trigger == trigAuto: | |
|
268 | self.__libdwf.FDwfAnalogInTriggerSourceSet(self.__hdwf,trigsrcDetectorAnalogIn) | |
|
269 | self.__libdwf.FDwfAnalogInTriggerAutoTimeoutSet(self.__hdwf,c_double(autoTimeout)) | |
|
270 | return | |
|
271 | if trigger == trigNormal: | |
|
272 | self.__libdwf.FDwfAnalogInTriggerSourceSet(self.__hdwf,trigsrcDetectorAnalogIn) | |
|
273 | self.__libdwf.FDwfAnalogInTriggerAutoTimeoutSet(self.__hdwf,c_double(0.0)) | |
|
274 | return | |
|
275 |
self.__libdwf.FDwfAnalogInTrigger |
|
|
276 | ||
|
277 | ############################################################# | |
|
278 | # AnalogOut | |
|
279 | ############################################################# | |
|
280 | def analogOutGen(self,frequency=1000, shape='Sine', channel=0, amplitude=1.0, offset=0.0): | |
|
281 | self.__libdwf.FDwfAnalogOutConfigure(self.__hdwf, c_int(channel), c_bool(False)) | |
|
282 | self.__libdwf.FDwfAnalogOutNodeEnableSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_bool(True)) | |
|
283 |
self.__libdwf.FDwfAnalogOut |
|
|
284 |
self.__libdwf.FDwfAnalogOutNode |
|
|
285 |
self.__libdwf.FDwfAnalogOutNode |
|
|
286 |
self.__libdwf.FDwfAnalogOutNode |
|
|
287 |
self.__libdwf.FDwfAnalogOut |
|
|
288 | ||
|
289 | def analogOutGenArbit(self,samplesBuffer ,repeatingFrequency=100, channel=0, amplitude=1.0, offset=0.0): | |
|
290 | self.__libdwf.FDwfAnalogOutConfigure(self.__hdwf, c_int(channel), c_bool(False)) | |
|
291 | cnt=self.__limits.genBuffSize(len(samplesBuffer)) | |
|
292 | buf=(c_double*cnt)() | |
|
293 | buf[:]=samplesBuffer[0:cnt] | |
|
294 | repeatingFrequency = self.__limits.genFreq(repeatingFrequency*cnt)/cnt | |
|
295 | self.__libdwf.FDwfAnalogOutNodeEnableSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_bool(True)) | |
|
296 | self.__libdwf.FDwfAnalogOutNodeFunctionSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_int(shapes.get("Custom"))) | |
|
297 |
self.__libdwf.FDwfAnalogOutNode |
|
|
298 |
self.__libdwf.FDwfAnalogOutNode |
|
|
299 |
self.__libdwf.FDwfAnalogOutNode |
|
|
300 |
self.__libdwf.FDwfAnalogOutNode |
|
|
301 |
self.__libdwf.FDwfAnalogOut |
|
|
302 | ||
|
303 | ||
|
304 | def __del__(self): | |
|
305 | if self.__opened: | |
|
306 | self.__libdwf.FDwfDeviceClose(self.__hdwf) | |
|
307 | ||
|
308 | ||
|
309 | ||
|
310 | ||
|
311 | if __name__ == '__main__': | |
|
312 | print("open first dev") | |
|
313 | test = discovery() | |
|
314 | test.setPower() | |
|
315 | for i in range(2): | |
|
316 | time.sleep(0.2) | |
|
317 | print(test.getPower()) | |
|
318 | test.analogOutGen() | |
|
319 | res=test.analogInRead(frequency=1000000,samplesCount=1000) | |
|
320 | print(res) | |
|
321 | plt.plot(range(len(res[0][0])),res[0][0]) | |
|
322 | plt.plot(range(len(res[0][0])),res[0][1]) | |
|
323 | plt.show() | |
|
324 | test.temp() | |
|
325 | # del test | |
|
326 | quit() | |
|
327 | ||
|
328 | ||
|
329 | ||
|
330 | ||
|
331 | ||
|
1 | #!/usr/bin/env python | |
|
2 | #-*- coding: utf-8 -*- | |
|
3 | """Simple python library to drive the analog discovery module from www.digilentinc.com | |
|
4 | """ | |
|
5 | ||
|
6 | from ctypes import * | |
|
7 | import time | |
|
8 | import sys | |
|
9 | import os | |
|
10 | import matplotlib.pyplot as plt | |
|
11 | import numpy as np | |
|
12 | ||
|
13 | __author__ = "Alexis Jeandet" | |
|
14 | __copyright__ = "Copyright 2015, Laboratory of Plasma Physics" | |
|
15 | __credits__ = [] | |
|
16 | __license__ = "GPLv2" | |
|
17 | __version__ = "1.0.0" | |
|
18 | __maintainer__ = "Alexis Jeandet" | |
|
19 | __email__ = "alexis.jeandet@member.fsf.org" | |
|
20 | __status__ = "Production" | |
|
21 | ||
|
22 | ||
|
23 | nodev = c_int(0) | |
|
24 | DwfStateDone = c_int(2) | |
|
25 | ||
|
26 | DECIAnalogInChannelCount = c_int(1) | |
|
27 | DECIAnalogOutChannelCount = c_int(2) | |
|
28 | DECIAnalogIOChannelCount = c_int(3) | |
|
29 | DECIDigitalInChannelCount = c_int(4) | |
|
30 | DECIDigitalOutChannelCount = c_int(5) | |
|
31 | DECIDigitalIOChannelCount = c_int(6) | |
|
32 | DECIAnalogInBufferSize = c_int(7) | |
|
33 | DECIAnalogOutBufferSize = c_int(8) | |
|
34 | DECIDigitalInBufferSize = c_int(9) | |
|
35 | DECIDigitalOutBufferSize = c_int(10) | |
|
36 | ||
|
37 | trigsrcNone = c_byte(0) | |
|
38 | trigsrcPC = c_byte(1) | |
|
39 | trigsrcDetectorAnalogIn = c_byte(2) | |
|
40 | trigsrcDetectorDigitalIn = c_byte(3) | |
|
41 | trigsrcAnalogIn = c_byte(4) | |
|
42 | trigsrcDigitalIn = c_byte(5) | |
|
43 | trigsrcDigitalOut = c_byte(6) | |
|
44 | trigsrcAnalogOut1 = c_byte(7) | |
|
45 | trigsrcAnalogOut2 = c_byte(8) | |
|
46 | trigsrcAnalogOut3 = c_byte(9) | |
|
47 | trigsrcAnalogOut4 = c_byte(10) | |
|
48 | trigsrcExternal1 = c_byte(11) | |
|
49 | trigsrcExternal2 = c_byte(12) | |
|
50 | trigsrcExternal3 = c_byte(13) | |
|
51 | trigsrcExternal4 = c_byte(14) | |
|
52 | trigAuto = c_byte(254) | |
|
53 | trigNormal = c_byte(255) | |
|
54 | ||
|
55 | AnalogOutNodeCarrier = c_int(0) | |
|
56 | AnalogOutNodeFM = c_int(1) | |
|
57 | AnalogOutNodeAM = c_int(2) | |
|
58 | ||
|
59 | ||
|
60 | shapes = {'DC' : 0, | |
|
61 | 'Sine' : 1, | |
|
62 | 'Square' : 2, | |
|
63 | 'Triangle' : 3, | |
|
64 | 'RampUp' : 4, | |
|
65 | 'RampDown' : 5, | |
|
66 | 'Noise' : 6, | |
|
67 | 'Custom' : 30, | |
|
68 | 'Play' :31, } | |
|
69 | ||
|
70 | closed=False | |
|
71 | opened=True | |
|
72 | ||
|
73 | ||
|
74 | class DiscoveryLimits(): | |
|
75 | class limitRange(): | |
|
76 | def __init__(self,Min,Max,name="Unknow",unit=""): | |
|
77 | self.Min = Min | |
|
78 | self.Max = Max | |
|
79 | self.name = name | |
|
80 | self.unit = unit | |
|
81 | ||
|
82 | def conform(self,value): | |
|
83 | if value<self.Min: | |
|
84 | raise UserWarning("Parameter "+self.name+" out of bound\nValue="+str(value)+"\nForce to "+str(self.Min)) | |
|
85 | return self.Min | |
|
86 | if value>self.Max: | |
|
87 | raise UserWarning("Parameter "+self.name+" out of bound\nValue="+str(value)+"\nForce to "+str(self.Max)) | |
|
88 | return self.Max | |
|
89 | return value | |
|
90 | ||
|
91 | def __str__(self): | |
|
92 | return self.name + ":\n Min="+str(self.Min)+" "+self.unit+",Max="+str(self.Max)+" "+self.unit | |
|
93 | ||
|
94 | errors = {0: RuntimeError("No card opened"), | |
|
95 | 1: UserWarning("Parameter out of bound"), | |
|
96 | } | |
|
97 | def __init__(self,libdwf,hdwf): | |
|
98 | self.limits=[] | |
|
99 | self.ACQ_IN_RANGES=[0.0] | |
|
100 | if hdwf.value == nodev.value: | |
|
101 | return | |
|
102 | self.__hdwf=hdwf | |
|
103 | self.__libdwf=libdwf | |
|
104 | Mind=c_double() | |
|
105 | Maxd=c_double() | |
|
106 | Mini=c_int() | |
|
107 | Maxi=c_int() | |
|
108 | StepsCount=c_int() | |
|
109 | Steps=(c_double*32)() | |
|
110 | self.__libdwf.FDwfAnalogInBufferSizeInfo(self.__hdwf, byref(Mini), byref(Maxi)) | |
|
111 | self.ACQ_BUF=self.limitRange(Mini.value,Maxi.value,"ACQ Buffer Size","Sps") | |
|
112 | self.limits.append(self.ACQ_BUF) | |
|
113 | self.__libdwf.FDwfAnalogInFrequencyInfo(self.__hdwf, byref(Mind), byref(Maxd)) | |
|
114 | self.ACQ_FREQ=self.limitRange(Mind.value,Maxd.value,"ACQ Frequency","Hz") | |
|
115 | self.limits.append(self.ACQ_FREQ) | |
|
116 | self.__libdwf.FDwfAnalogInChannelRangeSteps(self.__hdwf, byref(Steps), byref(StepsCount)) | |
|
117 | self.ACQ_IN_RANGES=Steps[0:StepsCount.value] | |
|
118 | self.__libdwf.FDwfAnalogOutNodeAmplitudeInfo(self.__hdwf,c_int(0), AnalogOutNodeCarrier, | |
|
119 | byref(Mind), byref(Maxd)) | |
|
120 | self.GEN_AMPL=self.limitRange(Mind.value,Maxd.value,"GEN Amplitude","V") | |
|
121 | self.limits.append(self.GEN_AMPL) | |
|
122 | self.__libdwf.FDwfAnalogOutNodeFrequencyInfo(self.__hdwf,c_int(0), AnalogOutNodeCarrier, | |
|
123 | byref(Mind), byref(Maxd)) | |
|
124 | self.GEN_FREQ=self.limitRange(Mind.value,Maxd.value,"GEN Frequency","Hz") | |
|
125 | self.limits.append(self.GEN_FREQ) | |
|
126 | self.__libdwf.FDwfAnalogOutNodeOffsetInfo(self.__hdwf,c_int(0), AnalogOutNodeCarrier, | |
|
127 | byref(Mind), byref(Maxd)) | |
|
128 | self.GEN_OFFSET=self.limitRange(Mind.value,Maxd.value,"GEN Offset","V") | |
|
129 | self.limits.append(self.GEN_OFFSET) | |
|
130 | self.__libdwf.FDwfAnalogOutNodeDataInfo(self.__hdwf,c_int(0), AnalogOutNodeCarrier, | |
|
131 | byref(Mini), byref(Maxi)) | |
|
132 | self.GEN_BUFF=self.limitRange(Mini.value,Maxi.value,"GEN Buffer size","Sps") | |
|
133 | self.limits.append(self.GEN_BUFF) | |
|
134 | ||
|
135 | ||
|
136 | def __conformParam(self,minVal,maxVal,val): | |
|
137 | if val<minVal: | |
|
138 | raise self.errors.get(1) | |
|
139 | print("Force to "+str(minVal)) | |
|
140 | return minVal | |
|
141 | if val>maxVal: | |
|
142 | raise self.errors.get(1) | |
|
143 | print("Force to "+str(maxVal)) | |
|
144 | return maxVal | |
|
145 | return val | |
|
146 | ||
|
147 | def acqFreq(self, value): | |
|
148 | return self.ACQ_FREQ.conform(value) | |
|
149 | ||
|
150 | def acqBufSize(self, value): | |
|
151 | return self.ACQ_BUF.conform(value) | |
|
152 | ||
|
153 | def genFreq(self, value): | |
|
154 | return self.GEN_FREQ.conform(value) | |
|
155 | ||
|
156 | def genAmplitude(self, value): | |
|
157 | return self.GEN_AMPL.conform(value) | |
|
158 | ||
|
159 | def genOffset(self, value): | |
|
160 | return self.GEN_OFFSET.conform(value) | |
|
161 | ||
|
162 | def genBuffSize(self, value): | |
|
163 | return self.GEN_BUFF.conform(value) | |
|
164 | ||
|
165 | def __str__(self): | |
|
166 | res=str() | |
|
167 | for i in self.limits: | |
|
168 | res+=i.__str__()+"\n" | |
|
169 | res+="ACQ Input ranes: "+str(self.ACQ_IN_RANGES) | |
|
170 | return res | |
|
171 | ||
|
172 | ||
|
173 | class Discovery(): | |
|
174 | ||
|
175 | errors = {0: RuntimeError("No card opened"), | |
|
176 | 1: UserWarning("Parameter out of bound"), | |
|
177 | } | |
|
178 | def __init__(self,card=-1): | |
|
179 | if sys.platform.startswith("win"): | |
|
180 | self.__libdwf = cdll.dwf | |
|
181 | elif sys.platform.startswith("darwin"): | |
|
182 | self.__libdwf = cdll.LoadLibrary("libdwf.dylib") | |
|
183 | else: | |
|
184 | self.__libdwf = cdll.LoadLibrary("libdwf.so") | |
|
185 | self.__opened = True | |
|
186 | self.__hdwf = c_int() | |
|
187 | self.__libdwf.FDwfDeviceOpen(c_int(card), byref(self.__hdwf)) | |
|
188 | if self.__hdwf.value == nodev.value: | |
|
189 | szerr = create_string_buffer(512) | |
|
190 | self.__libdwf.FDwfGetLastErrorMsg(szerr) | |
|
191 | print(szerr.value) | |
|
192 | print("failed to open device") | |
|
193 | self.__opened=False | |
|
194 | self.__limits=DiscoveryLimits(self.__libdwf,self.__hdwf) | |
|
195 | print(self.__limits) | |
|
196 | ||
|
197 | @property | |
|
198 | def opened(self): | |
|
199 | return self.__opened | |
|
200 | ||
|
201 | ||
|
202 | ############################################################# | |
|
203 | # Power Supply | |
|
204 | ############################################################# | |
|
205 | def set_power(self,fiveVolt=1,minusFiveVolt=1,master=True): | |
|
206 | if not self.__opened: | |
|
207 | raise self.errors.get(0) | |
|
208 | # enable positive supply | |
|
209 | self.__libdwf.FDwfAnalogIOChannelNodeSet(self.__hdwf, 0, 0, c_double(fiveVolt)) | |
|
210 | # enable negative supply | |
|
211 | self.__libdwf.FDwfAnalogIOChannelNodeSet(self.__hdwf, 1, 0, c_double(minusFiveVolt)) | |
|
212 | # master enable | |
|
213 | return self.__libdwf.FDwfAnalogIOEnableSet(self.__hdwf, master) | |
|
214 | ||
|
215 | def get_power(self): | |
|
216 | if not self.__opened: | |
|
217 | raise self.errors.get(0) | |
|
218 | supplyVoltage = c_double() | |
|
219 | supplyCurrent = c_double() | |
|
220 | IsEnabled = c_bool() | |
|
221 | self.__libdwf.FDwfAnalogIOStatus(self.__hdwf) | |
|
222 | self.__libdwf.FDwfAnalogIOChannelNodeStatus(self.__hdwf, c_int(3), c_int(0), byref(supplyVoltage)) | |
|
223 | self.__libdwf.FDwfAnalogIOChannelNodeStatus(self.__hdwf, c_int(3), c_int(1), byref(supplyCurrent)) | |
|
224 | self.__libdwf.FDwfAnalogIOEnableStatus(self.__hdwf, byref(IsEnabled)) | |
|
225 | return [IsEnabled.value,supplyVoltage.value,supplyCurrent.value] | |
|
226 | ||
|
227 | ############################################################# | |
|
228 | # AnalogIn | |
|
229 | ############################################################# | |
|
230 | def analog_in_read(self,ch1=True,ch2=True,frequency=100000000,samplesCount=100,ch1range=5.0,ch2range=5.0,trigger=trigsrcNone): | |
|
231 | if not self.__opened: | |
|
232 | raise self.errors.get(0) | |
|
233 | cnt=self.__limits.acqBufSize(samplesCount) | |
|
234 | self.__libdwf.FDwfAnalogInFrequencySet(self.__hdwf, c_double(self.__limits.acqFreq(frequency))) | |
|
235 | f=c_double() | |
|
236 | self.__libdwf.FDwfAnalogInFrequencyGet(self.__hdwf, byref(f)) | |
|
237 | frequency=f.value | |
|
238 | self.__libdwf.FDwfAnalogInBufferSizeSet(self.__hdwf, c_int(cnt)) | |
|
239 | self.__libdwf.FDwfAnalogInChannelEnableSet(self.__hdwf, c_int(0), c_bool(ch1)) | |
|
240 | self.__libdwf.FDwfAnalogInChannelRangeSet(self.__hdwf, c_int(0), c_double(ch1range)) | |
|
241 | self.__libdwf.FDwfAnalogInChannelEnableSet(self.__hdwf, c_int(1), c_bool(ch2)) | |
|
242 | self.__libdwf.FDwfAnalogInChannelRangeSet(self.__hdwf, c_int(1), c_double(ch2range)) | |
|
243 | self.set_analog_in_trigger(trigger) | |
|
244 | self.__libdwf.FDwfAnalogInConfigure(self.__hdwf, c_bool(False), c_bool(True)) | |
|
245 | status = c_byte() | |
|
246 | while True: | |
|
247 | self.__libdwf.FDwfAnalogInStatus(self.__hdwf, c_int(1), byref(status)) | |
|
248 | if status.value == DwfStateDone.value : | |
|
249 | break | |
|
250 | time.sleep(0.1) | |
|
251 | if ch1: | |
|
252 | ch1data = (c_double*cnt)() | |
|
253 | self.__libdwf.FDwfAnalogInStatusData(self.__hdwf, 0, ch1data, cnt) | |
|
254 | if ch2: | |
|
255 | ch2data = (c_double*cnt)() | |
|
256 | self.__libdwf.FDwfAnalogInStatusData(self.__hdwf, 1, ch2data, cnt) | |
|
257 | return [np.array([ch1data,ch2data]),frequency] | |
|
258 | else: | |
|
259 | return [np.array([ch1data]),frequency] | |
|
260 | if ch2: | |
|
261 | ch2data = (c_double*cnt)() | |
|
262 | self.__libdwf.FDwfAnalogInStatusData(self.__hdwf, 1, ch2data, cnt) | |
|
263 | return [np.array([ch2data]),frequency] | |
|
264 | ||
|
265 | ||
|
266 | def set_analog_in_trigger(self,trigger=trigAuto,autoTimeout=0.0): | |
|
267 | if not self.__opened: | |
|
268 | raise self.errors.get(0) | |
|
269 | if trigger == trigAuto: | |
|
270 | self.__libdwf.FDwfAnalogInTriggerSourceSet(self.__hdwf,trigsrcDetectorAnalogIn) | |
|
271 | self.__libdwf.FDwfAnalogInTriggerAutoTimeoutSet(self.__hdwf,c_double(autoTimeout)) | |
|
272 | return | |
|
273 | if trigger == trigNormal: | |
|
274 | self.__libdwf.FDwfAnalogInTriggerSourceSet(self.__hdwf,trigsrcDetectorAnalogIn) | |
|
275 | self.__libdwf.FDwfAnalogInTriggerAutoTimeoutSet(self.__hdwf,c_double(0.0)) | |
|
276 | return | |
|
277 | self.__libdwf.FDwfAnalogInTriggerSourceSet(self.__hdwf,trigger) | |
|
278 | ||
|
279 | ############################################################# | |
|
280 | # AnalogOut | |
|
281 | ############################################################# | |
|
282 | def analog_out_gen(self,frequency=1000, shape='Sine', channel=0, amplitude=1.0, offset=0.0): | |
|
283 | self.__libdwf.FDwfAnalogOutConfigure(self.__hdwf, c_int(channel), c_bool(False)) | |
|
284 | self.__libdwf.FDwfAnalogOutNodeEnableSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_bool(True)) | |
|
285 | self.__libdwf.FDwfAnalogOutNodeFunctionSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_int(shapes.get(shape))) | |
|
286 | self.__libdwf.FDwfAnalogOutNodeFrequencySet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_double(self.__limits.genFreq(frequency))) | |
|
287 | self.__libdwf.FDwfAnalogOutNodeAmplitudeSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_double(self.__limits.genAmplitude(amplitude))) | |
|
288 | self.__libdwf.FDwfAnalogOutNodeOffsetSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_double(self.__limits.genOffset(offset))) | |
|
289 | self.__libdwf.FDwfAnalogOutConfigure(self.__hdwf, c_int(channel), c_bool(True)) | |
|
290 | ||
|
291 | def analog_out_gen_arbit(self,samplesBuffer ,repeatingFrequency=100, channel=0, amplitude=1.0, offset=0.0): | |
|
292 | self.__libdwf.FDwfAnalogOutConfigure(self.__hdwf, c_int(channel), c_bool(False)) | |
|
293 | cnt=self.__limits.genBuffSize(len(samplesBuffer)) | |
|
294 | buf=(c_double*cnt)() | |
|
295 | buf[:]=samplesBuffer[0:cnt] | |
|
296 | repeatingFrequency = self.__limits.genFreq(repeatingFrequency*cnt)/cnt | |
|
297 | self.__libdwf.FDwfAnalogOutNodeEnableSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_bool(True)) | |
|
298 | self.__libdwf.FDwfAnalogOutNodeFunctionSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_int(shapes.get("Custom"))) | |
|
299 | self.__libdwf.FDwfAnalogOutNodeFrequencySet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_double(repeatingFrequency)) | |
|
300 | self.__libdwf.FDwfAnalogOutNodeAmplitudeSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_double(self.__limits.genAmplitude(amplitude))) | |
|
301 | self.__libdwf.FDwfAnalogOutNodeOffsetSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_double(self.__limits.genOffset(offset))) | |
|
302 | self.__libdwf.FDwfAnalogOutNodeDataSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, buf, c_int(cnt)) | |
|
303 | self.__libdwf.FDwfAnalogOutConfigure(self.__hdwf, c_int(channel), c_bool(True)) | |
|
304 | ||
|
305 | ||
|
306 | def __del__(self): | |
|
307 | if self.__opened: | |
|
308 | self.__libdwf.FDwfDeviceClose(self.__hdwf) | |
|
309 | ||
|
310 | ||
|
311 | ||
|
312 | ||
|
313 | if __name__ == '__main__': | |
|
314 | print("open first dev") | |
|
315 | test = Discovery() | |
|
316 | test.set_power() | |
|
317 | for i in range(2): | |
|
318 | time.sleep(0.2) | |
|
319 | print(test.get_power()) | |
|
320 | test.analog_out_gen() | |
|
321 | res=test.analog_in_read(frequency=1000000,samplesCount=1000) | |
|
322 | print(res) | |
|
323 | plt.plot(range(len(res[0][0])),res[0][0]) | |
|
324 | plt.plot(range(len(res[0][0])),res[0][1]) | |
|
325 | plt.show() | |
|
326 | test.temp() | |
|
327 | # del test | |
|
328 | quit() | |
|
329 | ||
|
330 | ||
|
331 | ||
|
332 | ||
|
333 |
@@ -1,128 +1,126 | |||
|
1 | #-*- coding: utf-8 -*- | |
|
2 | #Copyright 2015 Alexis Jeandet | |
|
3 | #This file is part of lppinstru. | |
|
4 | ||
|
5 | #lppinstru is free software: you can redistribute it and/or modify | |
|
6 | #it under the terms of the GNU General Public License as published by | |
|
7 | #the Free Software Foundation, either version 3 of the License, or | |
|
8 | #(at your option) any later version. | |
|
9 | ||
|
10 | #pydiscovery is distributed in the hope that it will be useful, | |
|
11 | #but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|
12 | #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|
13 | #GNU General Public License for more details. | |
|
14 | ||
|
15 | #You should have received a copy of the GNU General Public License | |
|
16 | #along with pydiscovery. If not, see <http://www.gnu.org/licenses/>. | |
|
17 | import time | |
|
18 | import sys | |
|
19 | import os | |
|
20 | import matplotlib.pyplot as plt | |
|
21 | import numpy as np | |
|
22 | import serial | |
|
23 | ||
|
24 | class gpd_xxx(): | |
|
25 | conf = {"GPD-3303S" : (2,0.0,30.0), | |
|
26 | "GPD-3303" : (), | |
|
27 | } | |
|
28 | trackingMode = {"Independent" : '0', | |
|
29 | "Series" : '1', | |
|
30 | "Parallel" : '2', | |
|
31 | } | |
|
32 | trackingModeStat = {"01": "Independent", | |
|
33 | "11" : "Series", | |
|
34 | "10" : "Parallel", | |
|
35 | } | |
|
36 | def __init__(self,port): | |
|
37 | self.i=0 | |
|
38 | self.__port=serial.Serial(port,timeout=0.5) | |
|
39 | ||
|
40 | def idn(self): | |
|
41 |
self.__port. |
|
|
42 | self.__port.write(b"*IDN?\n") | |
|
43 | return self.__port.readall() | |
|
44 | ||
|
45 | def setVoltage(self,index,tension): | |
|
46 | if index < 2: | |
|
47 | self.__port.write("VSET"+str(index+1)+":"+str(tension)+"\n") | |
|
48 | else: | |
|
49 | raise UserWarning("Parameter out of bound") | |
|
50 | ||
|
51 | def voltageSet(self,index): | |
|
52 | if index < 2: | |
|
53 |
self.__port. |
|
|
54 | self.__port.write("VSET"+str(index+1)+"?\n") | |
|
55 | V=self.__port.readall() | |
|
56 | return float(V.split("V")[0]) | |
|
57 | else: | |
|
58 | raise UserWarning("Parameter out of bound") | |
|
59 |
|
|
|
60 | def voltage(self,index): | |
|
61 | if index < 2: | |
|
62 |
self.__port. |
|
|
63 | self.__port.write("VOUT"+str(index+1)+"?\n") | |
|
64 | V=self.__port.readall() | |
|
65 | return float(V.split("V")[0]) | |
|
66 | else: | |
|
67 | raise UserWarning("Parameter out of bound") | |
|
68 | ||
|
69 | def setCurrentLimit(self,index,limit): | |
|
70 | if index < 2: | |
|
71 | self.__port.write("ISET"+str(index+1)+":"+str(limit)+"\n") | |
|
72 | else: | |
|
73 | raise UserWarning("Parameter out of bound") | |
|
74 | ||
|
75 | def currentLimit(self,index): | |
|
76 | if index < 2: | |
|
77 |
self.__port. |
|
|
78 | self.__port.write("ISET"+str(index+1)+"?\n") | |
|
79 | I = self.__port.readall() | |
|
80 | return float(I.split("A")[0]) | |
|
81 | else: | |
|
82 | raise UserWarning("Parameter out of bound") | |
|
83 |
|
|
|
84 | def current(self,index): | |
|
85 | if index < 2: | |
|
86 |
self.__port. |
|
|
87 | self.__port.write("IOUT"+str(index+1)+"?\n") | |
|
88 | I = self.__port.readall() | |
|
89 | return float(I.split("A")[0]) | |
|
90 | else: | |
|
91 | raise UserWarning("Parameter out of bound") | |
|
92 |
|
|
|
93 | def turnOn(self,on=True): | |
|
94 |
|
|
|
95 |
self.__port.write("OUT |
|
|
96 | else: | |
|
97 | self.__port.write("OUT0\n") | |
|
98 | ||
|
99 | def setTracking(self,mode="Independent"): | |
|
100 | self.__port.write("TRACK"+self.trackingMode.get(mode)+"\n") | |
|
101 |
|
|
|
102 | def setBeep(self,on=True): | |
|
103 |
|
|
|
104 |
self.__port.write("BEEP |
|
|
105 | else: | |
|
106 | self.__port.write("BEEP0\n") | |
|
107 | ||
|
108 | def tracking(self): | |
|
109 |
self.__port. |
|
|
110 | self.__port.setTimeout(0.1) | |
|
111 | STAT = self.__port.readall() | |
|
112 | BITS = STAT.split(" ") | |
|
113 | return self.trackingModeStat.get(''.join(BITS[2:4])) | |
|
114 | ||
|
115 | def save(self,mem=1): | |
|
116 | if mem>=1 and mem<=4: | |
|
117 | self.__port.write("SAV"+str(mem)+"\n") | |
|
118 | else: | |
|
119 | raise UserWarning("Parameter mem of bound 1-4") | |
|
120 | ||
|
121 | def recal(self,mem=1): | |
|
122 | if mem>=1 and mem<=4: | |
|
123 | self.__port.write("RCL"+str(mem)+"\n") | |
|
124 | else: | |
|
125 | raise UserWarning("Parameter mem of bound 1-4") | |
|
126 | ||
|
127 | if __name__ == '__main__': | |
|
128 | print("") | |
|
1 | #!/usr/bin/env python | |
|
2 | #-*- coding: utf-8 -*- | |
|
3 | """Simple python library to communicate with GW Instek GPD-Series power supplies. | |
|
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 | ||
|
22 | class gpd_xxx(): | |
|
23 | conf = {"GPD-3303S" : (2,0.0,30.0), | |
|
24 | "GPD-3303" : (), | |
|
25 | } | |
|
26 | trackingMode = {"Independent" : '0', | |
|
27 | "Series" : '1', | |
|
28 | "Parallel" : '2', | |
|
29 | } | |
|
30 | trackingModeStat = {"01": "Independent", | |
|
31 | "11" : "Series", | |
|
32 | "10" : "Parallel", | |
|
33 | } | |
|
34 | def __init__(self,port): | |
|
35 | self.i=0 | |
|
36 | self.__port=serial.Serial(port,timeout=0.5) | |
|
37 | ||
|
38 | def idn(self): | |
|
39 | self.__port.setTimeout(0.1) | |
|
40 | self.__port.write(b"*IDN?\n") | |
|
41 | return self.__port.readall() | |
|
42 | ||
|
43 | def setVoltage(self,index,tension): | |
|
44 | if index < 2: | |
|
45 | self.__port.write("VSET"+str(index+1)+":"+str(tension)+"\n") | |
|
46 | else: | |
|
47 | raise UserWarning("Parameter out of bound") | |
|
48 | ||
|
49 | def voltageSet(self,index): | |
|
50 | if index < 2: | |
|
51 | self.__port.setTimeout(0.1) | |
|
52 | self.__port.write("VSET"+str(index+1)+"?\n") | |
|
53 | V=self.__port.readall() | |
|
54 | return float(V.split("V")[0]) | |
|
55 | else: | |
|
56 | raise UserWarning("Parameter out of bound") | |
|
57 | ||
|
58 | def voltage(self,index): | |
|
59 | if index < 2: | |
|
60 | self.__port.setTimeout(0.1) | |
|
61 | self.__port.write("VOUT"+str(index+1)+"?\n") | |
|
62 | V=self.__port.readall() | |
|
63 | return float(V.split("V")[0]) | |
|
64 | else: | |
|
65 | raise UserWarning("Parameter out of bound") | |
|
66 | ||
|
67 | def setCurrentLimit(self,index,limit): | |
|
68 | if index < 2: | |
|
69 | self.__port.write("ISET"+str(index+1)+":"+str(limit)+"\n") | |
|
70 | else: | |
|
71 | raise UserWarning("Parameter out of bound") | |
|
72 | ||
|
73 | def currentLimit(self,index): | |
|
74 | if index < 2: | |
|
75 | self.__port.setTimeout(0.1) | |
|
76 | self.__port.write("ISET"+str(index+1)+"?\n") | |
|
77 | I = self.__port.readall() | |
|
78 | return float(I.split("A")[0]) | |
|
79 | else: | |
|
80 | raise UserWarning("Parameter out of bound") | |
|
81 | ||
|
82 | def current(self,index): | |
|
83 | if index < 2: | |
|
84 | self.__port.setTimeout(0.1) | |
|
85 | self.__port.write("IOUT"+str(index+1)+"?\n") | |
|
86 | I = self.__port.readall() | |
|
87 | return float(I.split("A")[0]) | |
|
88 | else: | |
|
89 | raise UserWarning("Parameter out of bound") | |
|
90 | ||
|
91 | def turnOn(self,on=True): | |
|
92 | if on: | |
|
93 | self.__port.write("OUT1\n") | |
|
94 | else: | |
|
95 | self.__port.write("OUT0\n") | |
|
96 | ||
|
97 | def setTracking(self,mode="Independent"): | |
|
98 | self.__port.write("TRACK"+self.trackingMode.get(mode)+"\n") | |
|
99 | ||
|
100 | def setBeep(self,on=True): | |
|
101 | if on: | |
|
102 | self.__port.write("BEEP1\n") | |
|
103 | else: | |
|
104 | self.__port.write("BEEP0\n") | |
|
105 | ||
|
106 | def tracking(self): | |
|
107 | self.__port.write("STATUS?\n") | |
|
108 | self.__port.setTimeout(0.1) | |
|
109 | STAT = self.__port.readall() | |
|
110 | BITS = STAT.split(" ") | |
|
111 | return self.trackingModeStat.get(''.join(BITS[2:4])) | |
|
112 | ||
|
113 | def save(self,mem=1): | |
|
114 | if mem>=1 and mem<=4: | |
|
115 | self.__port.write("SAV"+str(mem)+"\n") | |
|
116 | else: | |
|
117 | raise UserWarning("Parameter mem of bound 1-4") | |
|
118 | ||
|
119 | def recal(self,mem=1): | |
|
120 | if mem>=1 and mem<=4: | |
|
121 | self.__port.write("RCL"+str(mem)+"\n") | |
|
122 | else: | |
|
123 | raise UserWarning("Parameter mem of bound 1-4") | |
|
124 | ||
|
125 | if __name__ == '__main__': | |
|
126 | print("") |
@@ -1,71 +1,71 | |||
|
1 | #!/usr/bin/env python | |
|
2 | #-*- coding: utf-8 -*- | |
|
3 | """Simple python library to compute transfert functions | |
|
4 | """ | |
|
5 | import time | |
|
6 | import sys | |
|
7 | import os | |
|
8 | import matplotlib.pyplot as plt | |
|
9 | import numpy as np | |
|
10 | from scipy import fftpack | |
|
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__ = " |
|
|
20 | ||
|
21 | ||
|
22 | ||
|
23 | def __parseFFT(FFTi,FFTo,signalFreq,samplingFreq): | |
|
24 | index=signalFreq*len(FFTi)/samplingFreq | |
|
25 | powI=np.abs(FFTi[index-4:index+4]) | |
|
26 | i=np.argmax(powI)+index-4 | |
|
27 | mod=np.abs(FFTo[i])/np.abs(FFTi[i]) | |
|
28 | arg=np.angle(FFTo[i])-np.angle(FFTi[i]) | |
|
29 | if arg<-np.pi: | |
|
30 | arg = (np.pi*2)+arg | |
|
31 | if arg>np.pi: | |
|
32 | arg = (-np.pi*2)+arg | |
|
33 | return [signalFreq,mod,arg] | |
|
34 | ||
|
35 | def __step(device,freq,offset=0.0,maxAmp=5.0,lastAmp=1.0): | |
|
36 |
device.analog |
|
|
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 |
|
|
45 | FFTi=fftpack.fft(res[0][0]) | |
|
46 | FFTo=fftpack.fft(res[0][1]) | |
|
47 | return __parseFFT(FFTi,FFTo,freq,res[1]) | |
|
48 | ||
|
49 | ||
|
50 | def computeTF(device,startFreq=1.0,stopFreq=100.0,offset=0.0,maxAmp=5.0,nstep=100): | |
|
51 | freq=np.zeros(nstep) | |
|
52 | f=[] | |
|
53 | mod=[] | |
|
54 | arg=[] | |
|
55 | for i in range(int(nstep)) : | |
|
56 | freq[i]=startFreq*np.power(10,((np.log10(stopFreq/startFreq))*i/(nstep-1))) | |
|
57 | lastAmp=0.1 | |
|
58 | for i in range(len(freq)): | |
|
59 | step=__step(device,freq[i],offset=offset,maxAmp=maxAmp,lastAmp=lastAmp) | |
|
60 | f.append(step[0]) | |
|
61 | mod.append(step[1]) | |
|
62 | arg.append(step[2]) | |
|
63 | return [f,mod,arg] | |
|
64 | ||
|
65 | ||
|
66 | if __name__ == '__main__': | |
|
67 | print("") | |
|
68 | ||
|
69 | ||
|
70 | ||
|
71 | ||
|
1 | #!/usr/bin/env python | |
|
2 | #-*- coding: utf-8 -*- | |
|
3 | """Simple python library to compute transfert functions | |
|
4 | """ | |
|
5 | import time | |
|
6 | import sys | |
|
7 | import os | |
|
8 | import matplotlib.pyplot as plt | |
|
9 | import numpy as np | |
|
10 | from scipy import fftpack | |
|
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 | ||
|
22 | ||
|
23 | def __parseFFT(FFTi,FFTo,signalFreq,samplingFreq): | |
|
24 | index=signalFreq*len(FFTi)/samplingFreq | |
|
25 | powI=np.abs(FFTi[index-4:index+4]) | |
|
26 | i=np.argmax(powI)+index-4 | |
|
27 | mod=np.abs(FFTo[i])/np.abs(FFTi[i]) | |
|
28 | arg=np.angle(FFTo[i])-np.angle(FFTi[i]) | |
|
29 | if arg<-np.pi: | |
|
30 | arg = (np.pi*2)+arg | |
|
31 | if arg>np.pi: | |
|
32 | arg = (-np.pi*2)+arg | |
|
33 | return [signalFreq,mod,arg] | |
|
34 | ||
|
35 | def __step(device,freq,offset=0.0,maxAmp=5.0,lastAmp=1.0): | |
|
36 | 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]) | |
|
47 | return __parseFFT(FFTi,FFTo,freq,res[1]) | |
|
48 | ||
|
49 | ||
|
50 | def computeTF(device,startFreq=1.0,stopFreq=100.0,offset=0.0,maxAmp=5.0,nstep=100): | |
|
51 | freq=np.zeros(nstep) | |
|
52 | f=[] | |
|
53 | mod=[] | |
|
54 | arg=[] | |
|
55 | for i in range(int(nstep)) : | |
|
56 | freq[i]=startFreq*np.power(10,((np.log10(stopFreq/startFreq))*i/(nstep-1))) | |
|
57 | lastAmp=0.1 | |
|
58 | for i in range(len(freq)): | |
|
59 | step=__step(device,freq[i],offset=offset,maxAmp=maxAmp,lastAmp=lastAmp) | |
|
60 | f.append(step[0]) | |
|
61 | mod.append(step[1]) | |
|
62 | arg.append(step[2]) | |
|
63 | return [f,mod,arg] | |
|
64 | ||
|
65 | ||
|
66 | if __name__ == '__main__': | |
|
67 | print("") | |
|
68 | ||
|
69 | ||
|
70 | ||
|
71 |
General Comments 0
You need to be logged in to leave comments.
Login now