@@ -0,0 +1,87 | |||
|
1 | #!/usr/bin/env python | |
|
2 | #-*- coding: utf-8 -*- | |
|
3 | """Simple python library to drive the analog discovery module from www.digilentinc.com | |
|
4 | With Discoply addon connected https://hephaistos.lpp.polytechnique.fr/rhodecode/HG_REPOSITORIES/LPP/INSTRUMENTATION/PCB/DiscoPli | |
|
5 | """ | |
|
6 | ||
|
7 | from ctypes import * | |
|
8 | import time | |
|
9 | import sys | |
|
10 | import os | |
|
11 | import matplotlib.pyplot as plt | |
|
12 | import numpy as np | |
|
13 | from lppinstru import discovery | |
|
14 | ||
|
15 | __author__ = "Alexis Jeandet" | |
|
16 | __copyright__ = "Copyright 2015, Laboratory of Plasma Physics" | |
|
17 | __credits__ = [] | |
|
18 | __license__ = "GPLv2" | |
|
19 | __version__ = "1.0.0" | |
|
20 | __maintainer__ = "Alexis Jeandet" | |
|
21 | __email__ = "alexis.jeandet@member.fsf.org" | |
|
22 | __status__ = "Production" | |
|
23 | ||
|
24 | ||
|
25 | ||
|
26 | class discoply(discovery.Discovery): | |
|
27 | _gains={ | |
|
28 | "LTC-6910-1":[0, 1, 2, 5, 10, 20, 50, 100], | |
|
29 | "LTC-6910-2":[0, 1, 2, 4, 8, 16, 32, 64], | |
|
30 | "LTC-6910-3":[0, 1, 2, 3, 4, 5, 6, 7] | |
|
31 | } | |
|
32 | def __init__(self,card=-1,model="LTC-6910-1",gain_ch1=1,gain_ch2=1): | |
|
33 | super(discoply,self).__init__(card) | |
|
34 | self._model=model | |
|
35 | self.set_power() | |
|
36 | self.digital_io_output_enable(0x3F) | |
|
37 | self.gain_idx = [gain_ch1,gain_ch2] | |
|
38 | ||
|
39 | def auto_remove_offset(self,channel=0): | |
|
40 | out=0.0 | |
|
41 | for i in range(10): | |
|
42 | self.analog_out_gen(shape="DC",offset=out) | |
|
43 | time.sleep(0.2) | |
|
44 | mean=super(discoply,self).analog_in_read(frequency=1e5,samplesCount=8192)[0][channel].mean() | |
|
45 | out+=-mean*14./self.gain[channel] | |
|
46 | return mean | |
|
47 | ||
|
48 | def analog_in_read(self,ch1=True,ch2=True,frequency=100000000,samplesCount=100,ch1range=5.0,ch2range=5.0,trigger=discovery.trigsrcNone): | |
|
49 | data=super(discoply,self).analog_in_read(ch1,ch2,frequency,samplesCount,ch1range,ch2range,trigger) | |
|
50 | if self.gain[0] !=0: | |
|
51 | data[0][0]=data[0][0]/self.gain[0] | |
|
52 | if self.gain[1] !=0: | |
|
53 | data[0][1]=data[0][1]/self.gain[1] | |
|
54 | return data | |
|
55 | ||
|
56 | @property | |
|
57 | def offset(self): | |
|
58 | data=super(discoply,self).analog_in_read(frequency=1e4,samplesCount=8192)[0] | |
|
59 | return [data[0].mean(),data[1].mean()] | |
|
60 | ||
|
61 | @property | |
|
62 | def gain(self): | |
|
63 | return [ | |
|
64 | self._gains[self._model][self.gain_idx[0]], | |
|
65 | self._gains[self._model][self.gain_idx[1]]] | |
|
66 | ||
|
67 | @property | |
|
68 | def gain_idx(self): | |
|
69 | dio=self.digital_io | |
|
70 | return [dio&7,(dio>>3)&7] | |
|
71 | ||
|
72 | @gain_idx.setter | |
|
73 | def gain_idx(self,value): | |
|
74 | self.digital_io =(value[0]&7) + ((value[1]&7)<<3) | |
|
75 | ||
|
76 | def set_gain_idx(self,value,channel): | |
|
77 | gain = self.gain & ~(7 << [0,3][channel]) | |
|
78 | self.gain_idx = gain + (value << [0,3][channel]) | |
|
79 | ||
|
80 | ||
|
81 | if __name__ == '__main__': | |
|
82 | quit() | |
|
83 | ||
|
84 | ||
|
85 | ||
|
86 | ||
|
87 |
@@ -75,6 +75,10 AnalogOutNodeCarrier = c_int(0) | |||
|
75 | 75 | AnalogOutNodeFM = c_int(1) |
|
76 | 76 | AnalogOutNodeAM = c_int(2) |
|
77 | 77 | |
|
78 | filterDecimate = c_int(0) | |
|
79 | filterAverage = c_int(1) | |
|
80 | filterMinMax = c_int(2) | |
|
81 | ||
|
78 | 82 | |
|
79 | 83 | shapes = {'DC' : 0, |
|
80 | 84 | 'Sine' : 1, |
@@ -284,10 +288,12 class Discovery(object): | |||
|
284 | 288 | self.__libdwf.FDwfAnalogInFrequencyGet(self.__hdwf, byref(f)) |
|
285 | 289 | frequency=f.value |
|
286 | 290 | self.__libdwf.FDwfAnalogInBufferSizeSet(self.__hdwf, c_int(cnt)) |
|
287 | self.__libdwf.FDwfAnalogInChannelEnableSet(self.__hdwf, c_int(0), c_bool(ch1)) | |
|
288 | self.__libdwf.FDwfAnalogInChannelRangeSet(self.__hdwf, c_int(0), c_double(ch1range)) | |
|
289 | self.__libdwf.FDwfAnalogInChannelEnableSet(self.__hdwf, c_int(1), c_bool(ch2)) | |
|
290 |
self.__libdwf.FDwfAnalogInChannel |
|
|
291 | range,enabled = [ch1range,ch2range],[ch1,ch2] | |
|
292 | ||
|
293 | for ch in (0,1): | |
|
294 | self.__libdwf.FDwfAnalogInChannelEnableSet(self.__hdwf, c_int(ch), c_bool(enabled[ch])) | |
|
295 | self.__libdwf.FDwfAnalogInChannelRangeSet(self.__hdwf, c_int(ch), c_double(range[ch])) | |
|
296 | self.__libdwf.FDwfAnalogInChannelFilterSet(self.__hdwf,c_int(ch),filterAverage) | |
|
291 | 297 | self.set_analog_in_trigger(trigger) |
|
292 | 298 | self.__libdwf.FDwfAnalogInConfigure(self.__hdwf, c_bool(False), c_bool(True)) |
|
293 | 299 | status = c_byte() |
@@ -327,6 +333,12 class Discovery(object): | |||
|
327 | 333 | ############################################################# |
|
328 | 334 | # AnalogOut |
|
329 | 335 | ############################################################# |
|
336 | def analog_out_enable(self,channel=0): | |
|
337 | self.__libdwf.FDwfAnalogOutConfigure(self.__hdwf, c_int(channel), c_bool(True)) | |
|
338 | ||
|
339 | def analog_out_disable(self,channel=0): | |
|
340 | self.__libdwf.FDwfAnalogOutConfigure(self.__hdwf, c_int(channel), c_bool(False)) | |
|
341 | ||
|
330 | 342 | def analog_out_gen(self,frequency=1000, symmetry=50.0, shape='Sine', channel=0, amplitude=1.0, offset=0.0,phase=0.0, syncOnTrigger=False, triggerFrq=1.0, wait=0.0, runDuration=None): |
|
331 | 343 | self.__libdwf.FDwfAnalogOutConfigure(self.__hdwf, c_int(channel), c_bool(False)) |
|
332 | 344 | self.__libdwf.FDwfAnalogOutNodeEnableSet(self.__hdwf, c_int(channel), AnalogOutNodeCarrier, c_bool(True)) |
@@ -364,6 +376,9 class Discovery(object): | |||
|
364 | 376 | self.__libdwf.FDwfAnalogOutTriggerSourceSet(self.__hdwf, c_int(channel), trigSrc) |
|
365 | 377 | self.__libdwf.FDwfAnalogOutRepeatTriggerSet(self.__hdwf, c_int(channel), c_bool(trigRepeat)) |
|
366 | 378 | |
|
379 | def __del__(self): | |
|
380 | if self.__opened: | |
|
381 | self.__libdwf.FDwfDeviceClose(self.__hdwf) | |
|
367 | 382 | |
|
368 | 383 | def analog_out_status(self, channel=0): |
|
369 | 384 | status = c_byte(DwfStateDone.value) |
@@ -400,13 +415,25 class Discovery(object): | |||
|
400 | 415 | # self.__libdwf.FDwfAnalogOutWaitSet(self.__hdwf, c_int(channel), c_double(wait)) |
|
401 | 416 | # self.__libdwf.FDwfAnalogOutConfigure(self.__hdwf, c_int(channel), c_bool(True)) |
|
402 | 417 | |
|
418 | def digital_io_output_enable(self, value): | |
|
419 | self.__libdwf.FDwfDigitalIOOutputEnableSet(self.__hdwf, c_int(value)) | |
|
403 | 420 | |
|
404 | ||
|
421 | def digital_io_get(self): | |
|
422 | dwRead = c_uint32() | |
|
423 | self.__libdwf.FDwfDigitalIOStatus (self.__hdwf) | |
|
424 | self.__libdwf.FDwfDigitalIOInputStatus(self.__hdwf, byref(dwRead)) | |
|
425 | return dwRead.value | |
|
405 | 426 | |
|
406 |
def |
|
|
407 | if self.__opened: | |
|
408 | self.__libdwf.FDwfDeviceClose(self.__hdwf) | |
|
427 | def digital_io_set(self,value): | |
|
428 | self.__libdwf.FDwfDigitalIOOutputSet(self.__hdwf, c_int(value)) | |
|
409 | 429 | |
|
430 | @property | |
|
431 | def digital_io(self): | |
|
432 | return self.digital_io_get() | |
|
433 | ||
|
434 | @digital_io.setter | |
|
435 | def digital_io(self,value): | |
|
436 | self.digital_io_set(value) | |
|
410 | 437 | |
|
411 | 438 | if __name__ == '__main__': |
|
412 | 439 | print("open first dev") |
@@ -8,6 +8,7 import os | |||
|
8 | 8 | import matplotlib.pyplot as plt |
|
9 | 9 | import numpy as np |
|
10 | 10 | import serial |
|
11 | import struct | |
|
11 | 12 | |
|
12 | 13 | __author__ = "Alexis Jeandet" |
|
13 | 14 | __copyright__ = "Copyright 2015, Laboratory of Plasma Physics" |
@@ -51,8 +52,7 class dlp_temp(object): | |||
|
51 | 52 | if index < 3: |
|
52 | 53 | self.__port.write(self.sensors.get(index)) |
|
53 | 54 | dat=self.__port.read(9) |
|
54 | test=( int(ord(dat[0])) + (int(ord(dat[1]))*256) ) | |
|
55 | temp=float(test)*0.0625 | |
|
55 | temp=float(struct.unpack( "h", dat[:2])[0])*0.0625 | |
|
56 | 56 | return temp #(temp-32.0)/1.8 |
|
57 | 57 | raise UserWarning("Parameter out of bound") |
|
58 | 58 | |
@@ -60,8 +60,7 class dlp_temp(object): | |||
|
60 | 60 | if index < 3: |
|
61 | 61 | self.__port.write(self.aninputs.get(index)) |
|
62 | 62 | dat=self.__port.read(2) |
|
63 | test=( int(ord(dat[0])) + (int(ord(dat[1]))*256) ) | |
|
64 | val=float(test)/512.0 | |
|
63 | val=float(struct.unpack( "h", dat[:2])[0])/512.0 | |
|
65 | 64 | return val |
|
66 | 65 | raise UserWarning("Parameter out of bound") |
|
67 | 66 | |
@@ -107,21 +106,34 class dlp_temp(object): | |||
|
107 | 106 | return self.digit_in(0) |
|
108 | 107 | @GP2.setter |
|
109 | 108 | def GP2(self,value): |
|
110 | return self.digit_out(0,val) | |
|
109 | return self.digit_out(0,value) | |
|
111 | 110 | |
|
112 | 111 | @property |
|
113 | 112 | def GP0(self): |
|
114 | 113 | return self.digit_in(1) |
|
115 | 114 | @GP0.setter |
|
116 | 115 | def GP0(self,value): |
|
117 | return self.digit_out(1,val) | |
|
116 | return self.digit_out(1,value) | |
|
118 | 117 | |
|
119 | 118 | @property |
|
120 | 119 | def GP4(self): |
|
121 | 120 | return self.digit_in(2) |
|
122 | 121 | @GP4.setter |
|
123 | 122 | def GP4(self,value): |
|
124 | return self.digit_out(2,val) | |
|
123 | return self.digit_out(2,value) | |
|
124 | ||
|
125 | ||
|
126 | def main(argv): | |
|
127 | if len(argv)==4: | |
|
128 | print(argv[3]) | |
|
129 | if len(argv)>=3: | |
|
130 | dlp=dlp_temp(argv[0]) | |
|
131 | while(True): | |
|
132 | readout=[dlp.read_sensor(i) for i in range(int(argv[1]))] | |
|
133 | print("{date}\t{values}".format(date=time.strftime("%Y-%m-%dT%H:%M:%S"),values="\t".join([str(x) for x in readout] ))) | |
|
134 | time.sleep(float(argv[2])) | |
|
135 | ||
|
125 | 136 | |
|
126 | 137 | if __name__ == '__main__': |
|
127 |
print( |
|
|
138 | print(sys.argv) | |
|
139 | main(sys.argv[1:]) |
@@ -23,13 +23,15 class UsbGpib(object): | |||
|
23 | 23 | 1:"CONTROLLER" ,} |
|
24 | 24 | revmodedic = {"DEVICE":"0" , |
|
25 | 25 | "CONTROLLER":"1" ,} |
|
26 | def __init__(self,port,address=0): | |
|
27 | self._port=serial.Serial(port,timeout=0.1) | |
|
26 | def __init__(self,port,address=0,baudrate=9600): | |
|
27 | self._port=serial.Serial(port,timeout=0.1,baudrate=baudrate) | |
|
28 | 28 | self._address=address |
|
29 |
self. |
|
|
29 | self.write("++auto 1") | |
|
30 | self._auto=1 | |
|
31 | self.mode=1 | |
|
30 | 32 | self.write("++ver") |
|
31 | 33 | self.version=self.read() |
|
32 | self.write("++auto 1") | |
|
34 | ||
|
33 | 35 | |
|
34 | 36 | def set_as_device(self): |
|
35 | 37 | self.write("++mode 0") |
@@ -44,7 +46,10 class UsbGpib(object): | |||
|
44 | 46 | |
|
45 | 47 | @mode.setter |
|
46 | 48 | def mode(self,new_mode): |
|
49 | if type(new_mode) == type("str"): | |
|
47 | 50 | self._mode=self.revmodedic[new_mode] |
|
51 | elif type(new_mode) == type(1): | |
|
52 | self._mode=new_mode | |
|
48 | 53 | self.write("++mode %d" % self._mode) |
|
49 | 54 | |
|
50 | 55 | @property |
@@ -58,16 +63,26 class UsbGpib(object): | |||
|
58 | 63 | self.write("++addr %d" % self._address) |
|
59 | 64 | |
|
60 | 65 | def write(self,command): |
|
61 |
self._port.write( |
|
|
66 | self._port.write(("%s\n\r" % command).encode()) | |
|
62 | 67 | self._port.flush() |
|
63 | 68 | |
|
64 | 69 | |
|
65 |
def read(self,command="" |
|
|
70 | def read(self,command=""): | |
|
66 | 71 | if not command=="": |
|
67 | 72 | self.write(command) |
|
68 | if GPIB: | |
|
73 | if command[0:2]!="++" and self._auto==0: | |
|
69 | 74 | self.write("++read") |
|
70 | 75 | return self._port.readall() |
|
71 | 76 | |
|
72 | 77 | def idn(self): |
|
73 | 78 | return self.read("*IDN?") |
|
79 | ||
|
80 | @property | |
|
81 | def auto_read_after_write(self): | |
|
82 | self._auto=int(self.read("++auto")) | |
|
83 | return self._auto | |
|
84 | ||
|
85 | @auto_read_after_write.setter | |
|
86 | def auto_read_after_write(self,enabled): | |
|
87 | self._auto=enabled | |
|
88 | self.write("++auto %d" % self._auto) |
General Comments 0
You need to be logged in to leave comments.
Login now