| 1 |
# -*- coding: utf-8 -*- |
| 2 |
''' |
| 3 |
EYES for Young Engineers and Scientists -Junior (EYES Junior 1.0) |
| 4 |
Python library to communicate to the PIC24FV32KA302 uC running 'eyesj.c' |
| 5 |
Author : Ajith Kumar B.P, [email protected], [email protected] |
| 6 |
License : GNU GPL version 3 |
| 7 |
Started on 25-Mar-2012 |
| 8 |
Last edit : 25-Oct-2012, added storing calibration to EEPROM |
| 9 |
* |
| 10 |
The micro-controller pins used are mapped into 13 I/O channels (numbered 0 to 12) |
| 11 |
and act like a kind of logical channels. The Python function calls refer to them |
| 12 |
using the corresponding number, ie 0 => A0. |
| 13 |
|
| 14 |
* 0 : A0, Analog Comaparator(A5) output. |
| 15 |
* 1 : A1, -5V to +5V range Analog Input |
| 16 |
* 2 : A2, -5V to +5V range Analog Input |
| 17 |
* 3 : IN1 , Can function as Digital or 0 to 5V Analog Input |
| 18 |
* 4 : IN2, Can function as Digital or 0 to 5V Analog Input |
| 19 |
* 5 : SEN, Simial to A3 & A4, but has a 5K external pullup resistor (Comp input) |
| 20 |
* 6 : SQR1-read, Input wired to SQR1 output |
| 21 |
* 7 : SQR2-read, Input wired to SQR2 output |
| 22 |
* 8 : SQR1 control, 0 to 5V programmable Squarewave. Setting Freq = 0 means 5V, Freq = -1 means 0V |
| 23 |
* 9 : SQR2 control, 0 to 5V programmable Squarewave |
| 24 |
* 10: Digital output OD1, |
| 25 |
* 11: CCS, Controls the 1mA constant current source. |
| 26 |
* A12: Analog Input AN0 / RA0 (dummy entry for RA0), special case |
| 27 |
''' |
| 28 |
|
| 29 |
import serial, struct, numpy, time, subprocess, sys, os, os.path |
| 30 |
|
| 31 |
fileOpen=open |
| 32 |
|
| 33 |
# allows to pack numeric values into byte strings |
| 34 |
Byte = struct.Struct("B") # size 1 |
| 35 |
ShortInt = struct.Struct("H") # size 2 |
| 36 |
Integer= struct.Struct("I") # size 4 |
| 37 |
|
| 38 |
#Commands with One byte argument (41 to 80) |
| 39 |
GETVERSION = Byte.pack(1) |
| 40 |
READCMP = Byte.pack(2) # Status of comparator output |
| 41 |
READTEMP = Byte.pack(3) # IC Temperature |
| 42 |
GETPORTB = Byte.pack(4) |
| 43 |
|
| 44 |
#Commands with One byte argument (41 to 80) |
| 45 |
READADC = Byte.pack(41) # Read the ADC channel |
| 46 |
GETSTATE = Byte.pack(42) # Digital Input Status |
| 47 |
NANODELAY = Byte.pack(43) # from IN2 to SEN, using CTMU, send current range |
| 48 |
SETADCREF = Byte.pack(44) # non-zero value selects external +Vref option |
| 49 |
READADCSM = Byte.pack(45) # Read the ADC channel, in Sleep Mode |
| 50 |
IRSEND1 = Byte.pack(46) # Sends one byte over IR on SQR1 |
| 51 |
RDEEPROM = Byte.pack(47) # Read nwords starting from addr |
| 52 |
|
| 53 |
# Commands with Two bytes argument (81 to 120) |
| 54 |
R2RTIME = Byte.pack(81) # Time from rising edge to rising edge,arguments pin1 & pin2 |
| 55 |
R2FTIME = Byte.pack(82) |
| 56 |
F2RTIME = Byte.pack(83) |
| 57 |
F2FTIME = Byte.pack(84) |
| 58 |
MULTIR2R = Byte.pack(85) # Time between rising edges, arguments pin & skipcycles |
| 59 |
SET2RTIME = Byte.pack(86) # From a Dout transition to the Din transition |
| 60 |
SET2FTIME = Byte.pack(87) # |
| 61 |
CLR2RTIME = Byte.pack(88) # |
| 62 |
CLR2FTIME = Byte.pack(89) # |
| 63 |
HTPUL2RTIME = Byte.pack(90) # High True Pulse to HIGH |
| 64 |
HTPUL2FTIME = Byte.pack(91) # High True Pulse to LOW |
| 65 |
LTPUL2RTIME = Byte.pack(92) # |
| 66 |
LTPUL2FTIME = Byte.pack(93) # |
| 67 |
SETPULWIDTH = Byte.pack(94) # Width setting for PULSE2* functions |
| 68 |
SETSTATE = Byte.pack(95) # SQR1, SQR2, OD & CCS only |
| 69 |
SETDAC = Byte.pack(96) # 12 bit DAC setting |
| 70 |
SETCURRENT = Byte.pack(97) # ADC channel, CTMU Irange |
| 71 |
SETACTION = Byte.pack(98) # capture modifiers, action, target pin |
| 72 |
SETTRIGVAL = Byte.pack(99) # Analog trigger level, 2 bytes |
| 73 |
SRFECHOTIME = Byte.pack(100) # Trigger to Echo time for SRF0x modules |
| 74 |
|
| 75 |
# Commands with Three bytes argument (121 to 160) |
| 76 |
SETSQR1 = Byte.pack(121) # Square wave on OSC2 |
| 77 |
SETSQR2 = Byte.pack(122) # Square wave on OSC3 |
| 78 |
WREEPROM = Byte.pack(123) # write 1 word to the address |
| 79 |
|
| 80 |
#Commands with Four bytes argument (161 to 200) |
| 81 |
MEASURECV = Byte.pack(163) # ch, irange, duration |
| 82 |
SETPWM1 = Byte.pack(164) # PWM on SQR1 output. Send ocxrx and ocx |
| 83 |
SETPWM2 = Byte.pack(165) # PWM on SQR1 output. |
| 84 |
IRSEND4 = Byte.pack(166) # 4 byte IR |
| 85 |
|
| 86 |
#Commands with Five bytes argument (201 to 240) |
| 87 |
CAPTURE = Byte.pack(201) # Ch, 2 byte NS, 2 byte TG |
| 88 |
CAPTURE_HR = Byte.pack(202) # Ch, 2 byte NS, 2 byte TG |
| 89 |
SETSQRS = Byte.pack(203) # Set both square waves, with specified phase difference. scale, ocr, diff |
| 90 |
|
| 91 |
#Commands with Six bytes argument (241 to 255) |
| 92 |
CAPTURE2 = Byte.pack(241) # ch1, ch2, NS, TG (1, 1, 2, 2)bytes |
| 93 |
CAPTURE2_HR = Byte.pack(242) # ch1, ch2, NS, TG (1, 1, 2, 2)bytes |
| 94 |
CAPTURE3 = Byte.pack(243) # ch1&ch2, ch3, ns , tg |
| 95 |
CAPTURE4 = Byte.pack(244) # ch1&ch2, ch3&ch4, ns , tg |
| 96 |
|
| 97 |
# Actions before capturing waveforms |
| 98 |
AANATRIG = Byte.pack(0) # Trigger on analog input level, set by SETRIGVAL |
| 99 |
AWAITHI = Byte.pack(1) |
| 100 |
AWAITLO = Byte.pack(2) |
| 101 |
AWAITRISE = Byte.pack(3) |
| 102 |
AWAITFALL = Byte.pack(4) |
| 103 |
ASET = Byte.pack(5) |
| 104 |
ACLR = Byte.pack(6) |
| 105 |
APULSEHT = Byte.pack(7) |
| 106 |
APULSELT = Byte.pack(8) |
| 107 |
|
| 108 |
BUFSIZE = 1800 # status + adcinfo + 1800 data |
| 109 |
|
| 110 |
#Serial devices to search for EYES hardware. |
| 111 |
linux_list = ['/dev/ttyACM0','/dev/ttyACM1','/dev/ttyACM2', '/dev/ttyACM3', '/dev/ttyAMA0'] |
| 112 |
|
| 113 |
def warningWithResult(warn, res): |
| 114 |
return warn + str(res, encoding="utf-8") |
| 115 |
|
| 116 |
def open(dev = None): |
| 117 |
''' |
| 118 |
If EYES hardware in found, returns an instance of 'Eyes', else returns None. |
| 119 |
''' |
| 120 |
obj = Eyesjun() |
| 121 |
if obj.fd != None: |
| 122 |
obj.disable_actions() # Disable capture modifiers |
| 123 |
obj.load_calibration() |
| 124 |
return obj |
| 125 |
print (('Could not find EYES Junior hardware')) |
| 126 |
print (('Check the connections.')) |
| 127 |
|
| 128 |
BAUDRATE = 115200 # Serial communication |
| 129 |
|
| 130 |
class Eyesjun: |
| 131 |
fd = None # init should fill this |
| 132 |
connected = False |
| 133 |
calibrated = False |
| 134 |
refval = 5.000 # regulator output, nominal value |
| 135 |
DACMAX = refval # DAC upper limit |
| 136 |
DACM = 4095.0/refval |
| 137 |
tgap = 0.004 # 0.004 ms shift between two channels of capture2 |
| 138 |
m12 = [refval/4095] + [2*refval/4095]*2 + [refval/4095]*10 |
| 139 |
m8 = [refval/255] + [2*refval/255] *2 + [refval/255] *10 |
| 140 |
c = [0.0] + [-refval]*2 + [0.0]*10 |
| 141 |
sen_pullup = 5100.0 |
| 142 |
cap_calib = 1.0 # Default values, to be loaded from file. |
| 143 |
socket_cap = 30.0 # Set by calibrate.py |
| 144 |
msg = '' |
| 145 |
|
| 146 |
def __init__(self, dev = None): |
| 147 |
""" |
| 148 |
Searches for EYES hardware on USB-to-Serial adapters. Presence of the |
| 149 |
device is done by reading the version string. Timeout set to 4 sec |
| 150 |
TODO : Supporting more than one EYES on a PC to be done. The question is how to find out |
| 151 |
whether a port is already open or not, without doing any transactions to it. |
| 152 |
""" |
| 153 |
if os.name == 'nt': # for Windows machines, search COM1 to COM255 |
| 154 |
device_list = [] |
| 155 |
for k in range(1,255): |
| 156 |
s = 'COM%d'%k |
| 157 |
device_list.append(s) |
| 158 |
for k in range(1,11): |
| 159 |
device_list.append(k) |
| 160 |
else: |
| 161 |
device_list = [] # Gather unused devices from linux_list |
| 162 |
for dev in linux_list: |
| 163 |
cmd='lsof -t {0} 2>&1; true'.format(dev) |
| 164 |
res = subprocess.check_output(cmd, shell=True) |
| 165 |
if len(res)==0: |
| 166 |
device_list.append(dev) |
| 167 |
|
| 168 |
for dev in device_list: |
| 169 |
try: |
| 170 |
handle = serial.Serial(dev, BAUDRATE, stopbits=1, timeout = 0.3) #8,1,no parity |
| 171 |
except: |
| 172 |
continue |
| 173 |
|
| 174 |
print('Port %s is existing '%dev) |
| 175 |
if handle.isOpen() != True: |
| 176 |
print ('but could not open') |
| 177 |
continue |
| 178 |
print('and opened. ') |
| 179 |
handle.flush() |
| 180 |
time.sleep(.5) |
| 181 |
while handle.inWaiting() > 0 : |
| 182 |
handle.flushInput() |
| 183 |
handle.write(GETVERSION) |
| 184 |
res = handle.read(1) |
| 185 |
ver = handle.read(5) # 5 character version number |
| 186 |
if ver[:2] == b'ej': |
| 187 |
self.device = dev |
| 188 |
self.fd = handle |
| 189 |
self.version = ver |
| 190 |
handle.timeout = 4.0 # r2rtime on .7 Hz require this |
| 191 |
print('Connected to ',ver.decode()) |
| 192 |
self.connected = True |
| 193 |
return # Successful return |
| 194 |
else: # If it is not our device close the file |
| 195 |
handle.close() |
| 196 |
print ('No EYES Junior hardware detected') |
| 197 |
self.fd = None |
| 198 |
return |
| 199 |
#--------------------------------------------------------------------------- |
| 200 |
def sendByte(self, bval): |
| 201 |
""" |
| 202 |
sends one byte to the interface |
| 203 |
@parameter bval : a bytes of lenght 1 |
| 204 |
""" |
| 205 |
self.fd.write(bval) |
| 206 |
time.sleep(0.005) # This delay is for MCP2200 + uC |
| 207 |
return |
| 208 |
|
| 209 |
def sendInt(self,ival): |
| 210 |
""" |
| 211 |
send on integer to the inteface |
| 212 |
@parameter ival an integer lesser than 65536 |
| 213 |
""" |
| 214 |
delay=0.005 # This delay is for MCP2200 + uC |
| 215 |
self.fd.write(Byte.pack(ival & 255)) |
| 216 |
time.sleep(delay) |
| 217 |
self.fd.write(Byte.pack(ival >> 8)) |
| 218 |
time.sleep(delay) |
| 219 |
return |
| 220 |
|
| 221 |
def get_version(self): |
| 222 |
""" |
| 223 |
reads the version number from the device |
| 224 |
@return a str |
| 225 |
""" |
| 226 |
self.sendByte(GETVERSION) |
| 227 |
res = self.fd.read(1) |
| 228 |
if res != b'D': |
| 229 |
self.msg = warningWithResult('GETVERSION ERROR', ver) |
| 230 |
return |
| 231 |
ver = self.fd.read(5) |
| 232 |
if sys.version_info.major == 3: |
| 233 |
ver=ver.decode("utf-8") |
| 234 |
return ver |
| 235 |
|
| 236 |
"""------------------------EEPROM---------------------""" |
| 237 |
def eeprom_write(self, addr, data): |
| 238 |
""" |
| 239 |
writes a few bytes to the EEPROM |
| 240 |
@param addr a small integer |
| 241 |
@param byte an integer (16 bits) |
| 242 |
@return 1 (number of written words) |
| 243 |
""" |
| 244 |
self.sendByte(WREEPROM) |
| 245 |
self.sendByte(Byte.pack(addr)) |
| 246 |
self.sendInt(data) |
| 247 |
res = self.fd.read(1) |
| 248 |
if res != b'D': |
| 249 |
self.msg = warningWithResult('WREEPROM ERROR ', res) |
| 250 |
print (warningWithResult('WREEPROM ERROR', res)) |
| 251 |
return None |
| 252 |
return 1 |
| 253 |
|
| 254 |
def eeprom_read(self, addr): |
| 255 |
""" |
| 256 |
read data from the EEPROM |
| 257 |
@param addr a small integer (8bit) |
| 258 |
@return an integer value (16 bits) |
| 259 |
""" |
| 260 |
self.sendByte(RDEEPROM) |
| 261 |
self.sendByte(Byte.pack(addr)) |
| 262 |
res = self.fd.read(1) |
| 263 |
if res != b'D': |
| 264 |
self.msg = warningWithResult('RDEEPROM ERROR ', res) |
| 265 |
return None |
| 266 |
res = self.fd.read(2) |
| 267 |
if sys.version_info.major == 3: |
| 268 |
return res[0] | (res[1] << 8) |
| 269 |
else: |
| 270 |
return ord(res[0]) | (ord(res[1]) << 8) |
| 271 |
|
| 272 |
def store_float(self, addr, data): # store a floating point number to EEPROM |
| 273 |
ss = struct.pack('f', data) |
| 274 |
if sys.version_info.major == 3: |
| 275 |
lo = ss[0] | (ss[1] << 8) |
| 276 |
hi = ss[2] | (ss[3] << 8) |
| 277 |
else: |
| 278 |
lo = ord(ss[0]) | (ord(ss[1]) << 8) |
| 279 |
hi = ord(ss[2]) | (ord(ss[3]) << 8) |
| 280 |
if self.eeprom_write(addr, lo) == None: |
| 281 |
return None |
| 282 |
if self.eeprom_write(addr+1, hi) == None: |
| 283 |
return None |
| 284 |
return 1 |
| 285 |
|
| 286 |
def restore_float(self, addr): # restore a floating point number from EEPROM |
| 287 |
lo = self.eeprom_read(addr) |
| 288 |
hi = self.eeprom_read(addr+1) |
| 289 |
data = (hi << 16) | lo |
| 290 |
ss = struct.pack('I', data) |
| 291 |
res = struct.unpack('f', ss) |
| 292 |
return res[0] # return the float |
| 293 |
|
| 294 |
AM1 = 0 # EEPROM location of the parameters, y = mx + c, for A1 and A2 |
| 295 |
AC1 = 2 |
| 296 |
AM2 = 4 |
| 297 |
AC2 = 6 |
| 298 |
ASOC = 8 # Socket cap IN1 |
| 299 |
ACCF = 10 # Capacitance error factor |
| 300 |
ARP = 12 # Pullup Resistance |
| 301 |
|
| 302 |
def storeCF_a1a2(self, m1,c1,m2,c2): # slope & intercept for A1 and A2 |
| 303 |
if self.store_float(self.AM1, m1) == None: |
| 304 |
return None |
| 305 |
self.store_float(self.AC1, c1) |
| 306 |
self.store_float(self.AM2, m2) |
| 307 |
self.store_float(self.AC2, c2) |
| 308 |
return 4 # Number of items written |
| 309 |
|
| 310 |
def storeCF_cap(self, soc, ccf): #Socket capacitance and error factor |
| 311 |
if self.store_float(self.ASOC, soc) == None: |
| 312 |
return None |
| 313 |
self.store_float(self.ACCF, ccf) |
| 314 |
return 2 |
| 315 |
|
| 316 |
def storeCF_sen(self, r): # pullup resistor value |
| 317 |
if self.store_float(self.ARP, r) == None: |
| 318 |
return None |
| 319 |
return 1 |
| 320 |
|
| 321 |
def load_calibration(self): |
| 322 |
ca = cb = cc = False |
| 323 |
try: |
| 324 |
m1 = self.restore_float(self.AM1) |
| 325 |
c1 = self.restore_float(self.AC1) |
| 326 |
m2 = self.restore_float(self.AM2) |
| 327 |
c2 = self.restore_float(self.AC2) |
| 328 |
m = 10.0/4095 |
| 329 |
c = -5.0 |
| 330 |
dm = m * 0.02 # maximum 2% deviation |
| 331 |
dc = 5 * 0.02 |
| 332 |
# print (m1,c1,m2,c2, dm, dc) |
| 333 |
if abs(m1-m) < dm and abs(m2-m) < dm and abs(c1-c) < dc and abs(c2-c) < dc: |
| 334 |
self.m12[1] = m1 |
| 335 |
self.c[1] = c1 |
| 336 |
self.m12[2] = m2 |
| 337 |
self.c[2] = c2 |
| 338 |
self.m8[1] = m1 * 4095./255 # Scale factors for 8 bit read |
| 339 |
self.m8[2] = m2 * 4095./255 |
| 340 |
ca = True |
| 341 |
# print ('Calibration Factors :', m1,c1,m2,c2) |
| 342 |
else: |
| 343 |
print ('Invalid Calibration factors for A1,A2', m1,c1,m2,c2) |
| 344 |
except: |
| 345 |
print ('Could not load A1 & A2 Calibration') |
| 346 |
|
| 347 |
try: |
| 348 |
soc = self.restore_float(self.ASOC) |
| 349 |
ccf = self.restore_float(self.ACCF) |
| 350 |
if (.8 < ccf < 1.2) and (20 < soc < 50): |
| 351 |
self.cap_calib = ccf |
| 352 |
self.socket_cap = soc |
| 353 |
cb = True |
| 354 |
#print ('IN1 Calibration :', ccf, soc) |
| 355 |
else: |
| 356 |
print (('Invalid Calibration factors for IN1'), soc, ccf) |
| 357 |
except: |
| 358 |
print (('Could not load IN1 Capacitor Calibration')) |
| 359 |
|
| 360 |
try: |
| 361 |
r = self.restore_float(self.ARP) |
| 362 |
if 4950 < r < 5250: |
| 363 |
self.sen_pullup = r |
| 364 |
cc = True |
| 365 |
#print (('SEN Pullup :'), r) |
| 366 |
else: |
| 367 |
print (('Invalid Pullup resistor value'), r) |
| 368 |
except: |
| 369 |
print (('Could not load SEN Pullup calibration')) |
| 370 |
|
| 371 |
if ca and cb and cc == True: |
| 372 |
self.calibrated = True |
| 373 |
|
| 374 |
#------------------------- Infrared comm. ---------------- |
| 375 |
def irsend1(self, d1): |
| 376 |
""" |
| 377 |
send one byte by IR |
| 378 |
@param d1 a small integer (8bit) |
| 379 |
""" |
| 380 |
self.sendByte(IRSEND1) |
| 381 |
self.sendByte(Byte.pack(d1)) |
| 382 |
res = self.fd.read(1) |
| 383 |
if res != b'D': |
| 384 |
self.msg = warningWithResult(('IRSEND1 ERROR '), res) |
| 385 |
print (warningWithResult(('IRSEND1 ERROR '), res)) |
| 386 |
return |
| 387 |
return 1 |
| 388 |
|
| 389 |
def irsend4(self, d1,d2,d3,d4): |
| 390 |
""" |
| 391 |
send four bytes by IR |
| 392 |
@param d1 a small integer (8bit) |
| 393 |
@param d2 a small integer (8bit) |
| 394 |
@param d3 a small integer (8bit) |
| 395 |
@param d4 a small integer (8bit) |
| 396 |
""" |
| 397 |
self.sendByte(IRSEND4) |
| 398 |
self.sendByte(Byte.pack(d1)) |
| 399 |
self.sendByte(Byte.pack(d2)) |
| 400 |
self.sendByte(Byte.pack(d3)) |
| 401 |
self.sendByte(Byte.pack(d4)) |
| 402 |
res = self.fd.read(1) |
| 403 |
if res != b'D': |
| 404 |
self.msg = warningWithResult(('IRSEND4 ERROR '), res) |
| 405 |
print (warningWithResult(('IRSEND4 ERROR '), res)) |
| 406 |
return |
| 407 |
return 1 |
| 408 |
|
| 409 |
#--------------------------------------CTMU ------------- |
| 410 |
ctmui = [550, 0.55, 5.5, 55.0] |
| 411 |
def nano_delay(self, i): |
| 412 |
''' |
| 413 |
Using the CTMU of PIC, measure r2r from IN2 or SEN. uses cap of IN1. Incomplete |
| 414 |
ch = 3 |
| 415 |
self.sendByte(NANODELAY) |
| 416 |
self.sendByte(self.rval[i]) |
| 417 |
res = self.fd.read(1) |
| 418 |
if res != b'D': |
| 419 |
print (('MEASUREDELAY ERROR'), res) |
| 420 |
return |
| 421 |
res = self.fd.read(2) |
| 422 |
iv = ord(res[0]) | (ord(res[1]) << 8) |
| 423 |
print (iv) |
| 424 |
v = self.m12[ch] * iv + self.c[ch] |
| 425 |
return v |
| 426 |
''' |
| 427 |
return |
| 428 |
|
| 429 |
def measure_cv(self, ch, ctime, i = 5.5): |
| 430 |
''' |
| 431 |
Using the CTMU of PIC, charges a capacitor connected to IN1, IN2 or SEN, |
| 432 |
for 'ctime' microseconds and then mesures the voltage across it. |
| 433 |
The value of current can be set to .55uA, 5.5 uA, 55uA or 550 uA |
| 434 |
@param ch channel number |
| 435 |
@param ctime duration in microseconds |
| 436 |
@param i value of the current (defaults to 5.5 uA) |
| 437 |
''' |
| 438 |
if i > 500: # 550 uA |
| 439 |
irange = 0 |
| 440 |
elif i > 50: #55 uA |
| 441 |
irange = 3 |
| 442 |
elif i > 5: #5.5 uA, default value |
| 443 |
irange = 2 |
| 444 |
else: # 0.55 uA |
| 445 |
irange = 1 |
| 446 |
|
| 447 |
if ch not in [3,4]: |
| 448 |
self.msg = ('Current to be set only on IN1(3) or IN2(4)') |
| 449 |
print (('Current to be set only on IN1 or IN2')) |
| 450 |
return |
| 451 |
self.sendByte(MEASURECV) |
| 452 |
self.sendByte(Byte.pack(ch)) |
| 453 |
self.sendByte(Byte.pack(irange)) |
| 454 |
self.sendInt(ctime) |
| 455 |
res = self.fd.read(1) |
| 456 |
if res != b'D': |
| 457 |
self.msg = warningWithResult(('MEASURECV ERROR '), res) |
| 458 |
print (warningWithResult(('MEASURECV ERROR '), res)) |
| 459 |
return |
| 460 |
res = self.fd.read(2) |
| 461 |
if sys.version_info.major == 3: |
| 462 |
iv = res[0] | (res[1] << 8) |
| 463 |
else: |
| 464 |
iv = ord(res[0]) | (ord(res[1]) << 8) |
| 465 |
v = self.m12[ch] * iv + self.c[ch] |
| 466 |
return v |
| 467 |
|
| 468 |
def measure_cap_raw(self, ctmin = 10): |
| 469 |
''' |
| 470 |
Measures the capacitance connected between IN1 and GND. Stray |
| 471 |
capacitance should be subtracted from the measured |
| 472 |
value. Measurement is done by charging the capacitor with 5.5 uA |
| 473 |
for a given time interval. Any error in the value of current |
| 474 |
is corrected by calibrating. |
| 475 |
''' |
| 476 |
for ctime in range(ctmin, 1000, 10): |
| 477 |
v = self.measure_cv(3, ctime, 5.5) # 5.5 uA range is chosen |
| 478 |
if v > 2.0: break |
| 479 |
if (v > 4) or (v == 0): |
| 480 |
self.msg = ('Error measuring capacitance %5.3f') %v |
| 481 |
print (('Error measuring capacitance'), v) |
| 482 |
return None |
| 483 |
return 5.5 * ctime / v # returns value in pF |
| 484 |
|
| 485 |
def measure_cap(self, ctmin = 10): |
| 486 |
''' |
| 487 |
Measures the capacitance connected between IN1 and GND. |
| 488 |
Returns the value after applying corrections. |
| 489 |
''' |
| 490 |
cap = self.measure_cap_raw() |
| 491 |
if cap != None: |
| 492 |
return (cap - self.socket_cap) * self.cap_calib |
| 493 |
else: |
| 494 |
return None |
| 495 |
|
| 496 |
def measure_res(self): |
| 497 |
''' |
| 498 |
Measures the resistance connected between SEN and GND. |
| 499 |
''' |
| 500 |
v = self.get_voltage(5) |
| 501 |
if .1 < v < 4.9: |
| 502 |
return 1.0 * self.sen_pullup * v /(5-v) |
| 503 |
else: |
| 504 |
self.msg = ('Resistance NOT in 100 Ohm to 100 kOhm range') |
| 505 |
return |
| 506 |
|
| 507 |
def set_current(self, ch, i): # channel 3 or 4, 0 means stop CTMU |
| 508 |
''' |
| 509 |
Sets CTMU current 'i' on a channel 'ch' and returns the voltage measured |
| 510 |
across the load. Allowed values of current are .55, 5.5, 55 and |
| 511 |
550 uAmps. |
| 512 |
@param ch channel number |
| 513 |
@param i value of the current |
| 514 |
''' |
| 515 |
if i > 500: # 550 uA |
| 516 |
irange = 0 |
| 517 |
elif i > 50: #55 uA |
| 518 |
irange = 3 |
| 519 |
elif i > 5: #5.5 uA, default value |
| 520 |
irange = 2 |
| 521 |
else: # 0.55 uA |
| 522 |
irange = 1 |
| 523 |
if i == 0 : # indication to stop CTMU |
| 524 |
ch = 0 |
| 525 |
if ch not in [0,3,4]: # 0 means stopping CTMU |
| 526 |
self.msg = ('Current to be set only on IN1 or IN2') |
| 527 |
print (('Current to be set only on IN1 or IN2')) |
| 528 |
return |
| 529 |
self.sendByte(SETCURRENT) |
| 530 |
self.sendByte(Byte.pack(ch)) |
| 531 |
self.sendByte(irange) |
| 532 |
res = self.fd.read(1) |
| 533 |
if res != b'D': |
| 534 |
self.msg = warningWithResult(('SETCURRENT ERROR'), res) |
| 535 |
print (warningWithResult(('SETCURRENT ERROR'), res)) |
| 536 |
return |
| 537 |
res = self.fd.read(2) |
| 538 |
if sys.version_info.major == 3: |
| 539 |
iv = res[0] | (res[1] << 8) |
| 540 |
else: |
| 541 |
iv = ord(res[0]) | (ord(res[1]) << 8) |
| 542 |
v = self.m12[ch] * iv + self.c[ch] |
| 543 |
return v |
| 544 |
|
| 545 |
def read_temp(self): |
| 546 |
''' |
| 547 |
Reads the temperature of uC, currently of no use. Have to see |
| 548 |
whether this can be used for correcting the drift of the 5V |
| 549 |
regulator with temperature. |
| 550 |
''' |
| 551 |
self.sendByte(READTEMP) |
| 552 |
res = self.fd.read(1) |
| 553 |
if res != b'D': |
| 554 |
print (('READTEMP error '), res) |
| 555 |
self.msg = warningWithResult(('READTEMP error'),res) |
| 556 |
return |
| 557 |
res = self.fd.read(2) |
| 558 |
if sys.version_info.major == 3: |
| 559 |
iv = res[0] | (res[1] << 8) |
| 560 |
else: |
| 561 |
iv = ord(res[0]) | (ord(res[1]) << 8) |
| 562 |
return iv |
| 563 |
|
| 564 |
#---------- Time Interval Measurements ---------------------- |
| 565 |
|
| 566 |
def tim_helper(self, cmd, src, dst): |
| 567 |
''' |
| 568 |
Helper function for all Time measurement calls. Command, |
| 569 |
Source and destination pins are imputs. Returns time in |
| 570 |
microseconds, -1 on error. |
| 571 |
@param cmd a bytes of length 1 |
| 572 |
@param src a channel number for the source |
| 573 |
@param dst a channel number for the destination |
| 574 |
''' |
| 575 |
if cmd == MULTIR2R: |
| 576 |
if src not in [0,3,4,5,6,7]: |
| 577 |
print (('Pin should be digital input capable: 0,3,4,5,6 or 7')) |
| 578 |
self.msg = ('Pin should be digital input capable: 0,3,4,5,6 or 7') |
| 579 |
return -1 |
| 580 |
if dst > 249: |
| 581 |
self.msg = ('skip exceeded 249 edges') |
| 582 |
print (('skip exceeded 249 edges')) |
| 583 |
return -1 |
| 584 |
if cmd in [R2RTIME, R2FTIME, F2RTIME, F2FTIME]: |
| 585 |
if src not in [0,3,4,5,6,7] or dst not in [0,3,4,5,6,7]: |
| 586 |
self.msg = ('Both pins should be digital input capable: 0,3,4,5,6 or 7') |
| 587 |
print (('Both pins should be digital input capable: 0,3,4,5,6 or 7')) |
| 588 |
return -1 |
| 589 |
if cmd in [SET2RTIME, SET2FTIME, CLR2RTIME, CLR2FTIME, HTPUL2RTIME, HTPUL2FTIME, LTPUL2RTIME, LTPUL2FTIME]: |
| 590 |
if src not in [8,9,10,11]: |
| 591 |
self.msg = ('Starting pin should be digital output capable: 8,9,10 or 11') |
| 592 |
print (('Starting pin should be digital output capable: 8,9,10 or 11')) |
| 593 |
return -1 |
| 594 |
if dst not in [0,3,4,5,6,7]: |
| 595 |
self.msg = ('Destination pin should be digital input capable: 0,3,4,5,6 or 7') |
| 596 |
print (('Destination pin should be digital input capable: 0,3,4,5,6 or 7')) |
| 597 |
return -1 |
| 598 |
self.sendByte(cmd) |
| 599 |
self.sendByte(Byte.pack(src)) |
| 600 |
self.sendByte(Byte.pack(dst)) |
| 601 |
res = self.fd.read(1) |
| 602 |
if res != b'D': |
| 603 |
self.msg = ('Time measurement command error') |
| 604 |
print (('Time measurement command %d error ') %ord(cmd), res) |
| 605 |
return -1.0 |
| 606 |
res = self.fd.read(1) |
| 607 |
data = self.fd.read(4) |
| 608 |
raw = struct.unpack('I'* 1, data) # 32 bit data from T4/T5 counter, 0.125us cycles |
| 609 |
ncycle = raw[0] + 0 # .25 usec correction |
| 610 |
return round(float(ncycle)*0.125) # returns in microseconds |
| 611 |
|
| 612 |
"""-------------- Passive Time Interval Measurements --------------------""" |
| 613 |
def r2rtime(self, pin1, pin2): |
| 614 |
''' |
| 615 |
Time between two rising edges. The pins must be distinct. For same pin, use multi_r2rtime |
| 616 |
''' |
| 617 |
return self.tim_helper(R2RTIME, pin1, pin2) |
| 618 |
|
| 619 |
def f2ftime(self, pin1, pin2): |
| 620 |
''' |
| 621 |
Time between two falling edges. The pins must be distinct. For same pin, use multi_r2rtime |
| 622 |
''' |
| 623 |
return self.tim_helper(F2FTIME, pin1, pin2) |
| 624 |
|
| 625 |
def r2ftime(self, pin1, pin2): |
| 626 |
''' |
| 627 |
Time between a rising edge to a falling edge. The pins could be same or distinct. |
| 628 |
''' |
| 629 |
print('r2r:', pin1, pin2) |
| 630 |
return self.tim_helper(R2FTIME, pin1, pin2) |
| 631 |
|
| 632 |
def f2rtime(self, pin1, pin2): |
| 633 |
''' |
| 634 |
Time between a falling edge to a rising edge. The pins could be same or distinct. |
| 635 |
''' |
| 636 |
return self.tim_helper(F2RTIME, pin1, pin2) |
| 637 |
|
| 638 |
def multi_r2rtime(self, pin, skip=0): |
| 639 |
''' |
| 640 |
Time between rising edges, could skip desired number of edges in between. (pin, 9) will give time required for |
| 641 |
10 cycles of a squarewave, increases resolution. |
| 642 |
''' |
| 643 |
return self.tim_helper(MULTIR2R, pin, skip) |
| 644 |
|
| 645 |
def get_frequency(self, pin): |
| 646 |
''' |
| 647 |
This function measures the frequency of an external 0 to 5V PULSE on digital inputs, by calling multi_r2rtime(). |
| 648 |
''' |
| 649 |
t = self.multi_r2rtime(pin) |
| 650 |
if t < 0: |
| 651 |
return t |
| 652 |
if 0 < t < 10000: |
| 653 |
t = self.multi_r2rtime(pin,9) |
| 654 |
return 1.0e7/t |
| 655 |
return 1.0e6 / t |
| 656 |
|
| 657 |
"""---------- Active time interval measurements ------------""" |
| 658 |
def set2rtime(self, pin1, pin2): |
| 659 |
''' |
| 660 |
Time from setting pin1 to a rising edge on pin2. |
| 661 |
''' |
| 662 |
return self.tim_helper(SET2RTIME, pin1, pin2) |
| 663 |
|
| 664 |
def set2ftime(self, pin1, pin2): |
| 665 |
''' |
| 666 |
Time from setting pin1 to a falling edge on pin2. |
| 667 |
''' |
| 668 |
return self.tim_helper(SET2FTIME, pin1, pin2) |
| 669 |
|
| 670 |
def clr2rtime(self, pin1, pin2): |
| 671 |
''' |
| 672 |
Time from clearin pin1 to a rising edge on pin2. |
| 673 |
''' |
| 674 |
return self.tim_helper(CLR2RTIME, pin1, pin2) |
| 675 |
|
| 676 |
def clr2ftime(self, pin1, pin2): |
| 677 |
''' |
| 678 |
Time from clearing pin1 to a falling edge on pin2. |
| 679 |
''' |
| 680 |
return self.tim_helper(CLR2FTIME, pin1, pin2) |
| 681 |
|
| 682 |
def htpulse2rtime(self, pin1, pin2): |
| 683 |
''' |
| 684 |
Time from a HIGH True pulse on pin1 to a rising edge on pin2. |
| 685 |
''' |
| 686 |
return self.tim_helper(HTPUL2RTIME, pin1, pin2) |
| 687 |
|
| 688 |
def htpulse2ftime(self, pin1, pin2): |
| 689 |
''' |
| 690 |
Time from HIGH True pulse on pin1 to a falling edge on pin2. |
| 691 |
''' |
| 692 |
return self.tim_helper(HTPUL2FTIME, pin1, pin2) |
| 693 |
|
| 694 |
def ltpulse2rtime(self, pin1, pin2): |
| 695 |
''' |
| 696 |
Time from a LOW True pulse on pin1 to a rising edge on pin2. |
| 697 |
''' |
| 698 |
return self.tim_helper(LTPUL2RTIME, pin1, pin2) |
| 699 |
|
| 700 |
def ltpulse2ftime(self, pin1, pin2): |
| 701 |
''' |
| 702 |
Time from LOW True pulse on pin1 to a falling edge on pin2. |
| 703 |
''' |
| 704 |
return self.tim_helper(LTPUL2FTIME, pin1, pin2) |
| 705 |
|
| 706 |
def srfechotime(self, pin1, pin2): |
| 707 |
''' |
| 708 |
Time from Trigger on Echo for SRF0x module. Trig on pin1 and Echo on pin2. |
| 709 |
''' |
| 710 |
return self.tim_helper(SRFECHOTIME, pin1, pin2) |
| 711 |
|
| 712 |
"""------------------------- Digital I/O-----------------------------""" |
| 713 |
def set_state(self, pin, state): |
| 714 |
''' |
| 715 |
Sets the status of Digital outputs SQR1, SQR2, OD1 or CCS. It |
| 716 |
will work on SQR1 & SQR2 only if the frequency is set to zero. |
| 717 |
@param pin pin number |
| 718 |
@param state a value 0 or 1 |
| 719 |
''' |
| 720 |
self.sendByte(SETSTATE) |
| 721 |
self.sendByte(Byte.pack(pin)) |
| 722 |
self.sendByte(Byte.pack(state)) |
| 723 |
res = self.fd.read(1) |
| 724 |
if res != b'D': |
| 725 |
self.msg = ('SETSTATE error ') |
| 726 |
print (('SETSTATE error '), res) |
| 727 |
return |
| 728 |
return state |
| 729 |
|
| 730 |
def get_state(self, pin): |
| 731 |
''' |
| 732 |
gets the status of the digital input pin. IN1, IN2 & SEN are |
| 733 |
set to digital mode before sensing input level. |
| 734 |
@param pin a pin number |
| 735 |
@return the state of the pin as a small integer |
| 736 |
''' |
| 737 |
self.sendByte(GETSTATE) |
| 738 |
self.sendByte(Byte.pack(pin)) |
| 739 |
res = self.fd.read(1) |
| 740 |
if res != b'D': |
| 741 |
self.msg = ('GETSTATE error ') |
| 742 |
print (('GETSTATE error '), res) |
| 743 |
return |
| 744 |
res = self.fd.read(1) |
| 745 |
return ord(res) |
| 746 |
|
| 747 |
def get_portb(self): |
| 748 |
''' |
| 749 |
Reads portB, returns 16 bits of data. |
| 750 |
''' |
| 751 |
self.sendByte(GETPORTB) |
| 752 |
res = self.fd.read(1) |
| 753 |
if res != b'D': |
| 754 |
self.msg = ('GETPORTB error ') |
| 755 |
print (('GETPORTB error '), res) |
| 756 |
return |
| 757 |
res = self.fd.read(2) |
| 758 |
raw = struct.unpack('H', res) # 16 bit data in byte array |
| 759 |
print('%x'%raw) |
| 760 |
return raw[0] |
| 761 |
|
| 762 |
"""------- Square Wave Generation & Measuring the Frequency ----------""" |
| 763 |
def set_pwm(self, osc, ds, resol=14): # osc and duty cycle, resolution 14 bits byn default |
| 764 |
''' |
| 765 |
Sets PWM on SQR1 / SQR2. The frequency is decided by the resolution in bits. |
| 766 |
''' |
| 767 |
if resol < 4 or resol > 16 or ds < 0 or ds > 100: |
| 768 |
return |
| 769 |
ocxrs = 2**resol |
| 770 |
ocx = int(0.01 * ds * ocxrs + 0.5) |
| 771 |
#print(ocxrs, ocx) |
| 772 |
|
| 773 |
if osc == 0: |
| 774 |
self.sendByte(SETPWM1) |
| 775 |
else: |
| 776 |
self.sendByte(SETPWM2) |
| 777 |
self.sendInt(ocxrs-1) # ocxrs |
| 778 |
self.sendInt(ocx) # ocx |
| 779 |
res = self.fd.read(1) |
| 780 |
if res != b'D': |
| 781 |
self.msg = ('SETPWM error ') |
| 782 |
print(('SETPWM error '), res) |
| 783 |
return |
| 784 |
return ds |
| 785 |
|
| 786 |
def set_sqr1_pwm(self, dc, resol=14): # Duty cycle, resolution 14 bits (f = 488Hz) by default |
| 787 |
''' |
| 788 |
Sets 488 Hz PWM on SQR1. Duty cycle is specified in percentage. The third argument, PWM resolution, is |
| 789 |
14 bits by default. Decreasing this by one doubles the frequency. |
| 790 |
''' |
| 791 |
return self.set_pwm(0,dc,resol) |
| 792 |
|
| 793 |
def set_sqr2_pwm(self, dc, resol = 14): |
| 794 |
''' |
| 795 |
Sets 488 Hz PWM on SQR2. Duty cycle is specified in percentage. The third argument, PWM resolution, is |
| 796 |
14 bits by default. Decreasing this by one doubles the frequency. |
| 797 |
''' |
| 798 |
return self.set_pwm(1,dc,resol) |
| 799 |
|
| 800 |
def set_sqr1_dc(self, volt): |
| 801 |
''' |
| 802 |
PWM DAC on SQR1. Resolution is 10 bits (f = 7.8 kHz) by default. External Filter is required to get the DC |
| 803 |
The voltage can be set from 0 to 5 volts. |
| 804 |
''' |
| 805 |
return 1.0*self.set_pwm(0, volt * 20.0, 10)/20 # 100% means 5 volts., 10 bit resolution, 8kHz |
| 806 |
|
| 807 |
def set_sqr2_dc(self, volt): |
| 808 |
''' |
| 809 |
PWM DAC on SQR2. Resolution is 10 bits (f = 7.8 kHz) by default. External Filter is required to get the DC |
| 810 |
The voltage can be set from 0 to 5 volts. |
| 811 |
''' |
| 812 |
return 1.0*self.set_pwm(1, volt * 20.0, 10)/20 #5V correspods to 100% |
| 813 |
|
| 814 |
def set_osc(self, chan, freq): # Freq in Hertz, osc 1 or 2 |
| 815 |
''' |
| 816 |
Sets the output frequency of the SQR1 (chan=8) or SQR2 (chan = 9). The function returns actual freqency set. |
| 817 |
''' |
| 818 |
if chan != 8 and chan != 9: |
| 819 |
self.msg = ('Invalid channel number') |
| 820 |
print(('Invalid Channel')) |
| 821 |
return |
| 822 |
OCRS = 0 |
| 823 |
TCKPS = 0 |
| 824 |
if freq < 0: # Disable Timer and Set Output LOW |
| 825 |
TCKPS = 254 |
| 826 |
elif freq == 0: |
| 827 |
TCKPS = 255 |
| 828 |
else: |
| 829 |
T = 0.125e-6 # Fosc = 16MHz |
| 830 |
mtvals = [T, T*8, T*64, T*256] # Possible Timer period values |
| 831 |
per = 1.0/freq # T requested |
| 832 |
for k in range(4): # Find the optimum scaling, OCR value |
| 833 |
if per < mtvals[k]*50000: |
| 834 |
TCKPS = k |
| 835 |
OCRS = 1.0*per/mtvals[k] |
| 836 |
OCRS = int(OCRS+0.5) |
| 837 |
freq = 1./(mtvals[k]*OCRS) |
| 838 |
#print(freq,'--', k, OCRS, 1./(mtvals[k]*OCRS), TCKPS) |
| 839 |
break |
| 840 |
if TCKPS < 4 and OCRS == 0: |
| 841 |
print(('Invalid Freqency')) |
| 842 |
return |
| 843 |
if chan == 8: |
| 844 |
self.sendByte(SETSQR1) |
| 845 |
elif chan == 9: |
| 846 |
self.sendByte(SETSQR2) |
| 847 |
self.sendByte(Byte.pack(TCKPS)) # prescaling for timer |
| 848 |
self.sendInt(OCRS) # OCRS value |
| 849 |
res = self.fd.read(1) |
| 850 |
if res != b'D': |
| 851 |
print(('SETSQR error '), res) |
| 852 |
return 'Error: '+res |
| 853 |
return freq |
| 854 |
|
| 855 |
def set_sqr1(self, freq): |
| 856 |
''' |
| 857 |
Sets the frequency of SQR1 (between .7Hz and 200kHz). All intermediate values are not possible. |
| 858 |
Returns the actual value set. |
| 859 |
''' |
| 860 |
res = self.set_osc(8, freq) |
| 861 |
if freq == 0: |
| 862 |
return 0 |
| 863 |
else: |
| 864 |
return res |
| 865 |
|
| 866 |
def set_sqr2(self, freq): |
| 867 |
''' |
| 868 |
Sets the frequency of SQR2 (between .7Hz and 200kHz). All intermediate values are not possible. |
| 869 |
Returns the actual value set. |
| 870 |
''' |
| 871 |
res = self.set_osc(9, freq) |
| 872 |
if freq == 0: |
| 873 |
return 0 |
| 874 |
else: |
| 875 |
return res |
| 876 |
|
| 877 |
def set_sqrs(self, freq, diff=0): # Freq in Hertz, phase difference in % of T |
| 878 |
''' |
| 879 |
Sets the output frequency of both SQR1 & SQR2. The function returns actual value set. The second argument is the |
| 880 |
phase difference between them in percentage. |
| 881 |
''' |
| 882 |
if freq == 0: # Disable both Square waves |
| 883 |
self.set_sqr1(0) |
| 884 |
self.set_sqr2(0) |
| 885 |
return 0 |
| 886 |
elif freq < 0: # Disable both Square waves |
| 887 |
self.set_sqr1(-1) |
| 888 |
self.set_sqr2(-1) |
| 889 |
return 0 |
| 890 |
if diff < 0 or diff >= 100.0: |
| 891 |
self.msg = ('Invalid phase difference') |
| 892 |
print(('Invalid phase difference')) |
| 893 |
return |
| 894 |
OCRS = 0 |
| 895 |
TCKPS = 0 |
| 896 |
T = 0.125e-6 # Fosc = 16MHz |
| 897 |
mtvals = [T, T*8, T*64, T*256] # Possible Timer period values |
| 898 |
per = 1.0/freq # T requested |
| 899 |
for k in range(4): # Find the optimum scaling, OCR value |
| 900 |
if per < mtvals[k]*50000: |
| 901 |
TCKPS = k |
| 902 |
OCRS = 1.0*per/mtvals[k] |
| 903 |
OCRS = int(OCRS+0.5) |
| 904 |
freq = 1./(mtvals[k]*OCRS) |
| 905 |
#print(freq,'--', k, OCRS, 1./(mtvals[k]*OCRS)) |
| 906 |
break |
| 907 |
if TCKPS < 4 and OCRS == 0: |
| 908 |
self.msg = ('Invalid Freqency') |
| 909 |
print(('Invalid Freqency')) |
| 910 |
return |
| 911 |
TG = int(1.0*diff*OCRS/100 +0.5) |
| 912 |
if TG == 0: TG = 1 # Need to examine this |
| 913 |
#print('TCKPS ', TCKPS, 'ocrs ', OCRS, TG) |
| 914 |
|
| 915 |
self.sendByte(SETSQRS) |
| 916 |
self.sendByte(Byte.pack(TCKPS)) # prescaling for timer |
| 917 |
self.sendInt(OCRS) # OCRS value |
| 918 |
self.sendInt(TG) # time difference |
| 919 |
res = self.fd.read(1) |
| 920 |
if res != b'D': |
| 921 |
self.msg = ('SETSQRS error ') |
| 922 |
print(('SETSQRS error '), res) |
| 923 |
return |
| 924 |
return freq |
| 925 |
|
| 926 |
"""---------------------- ADC & DAC -------------------""" |
| 927 |
|
| 928 |
def write_dac(self, iv): |
| 929 |
''' |
| 930 |
Writes the 12 bit I2C DAC to the desired value. |
| 931 |
@param iv a value (integer between 0 and 4095) |
| 932 |
''' |
| 933 |
if iv < 0: iv = 0 # Force within limits |
| 934 |
if iv > 4095: iv = 4095 |
| 935 |
|
| 936 |
self.sendByte(SETDAC) |
| 937 |
self.sendInt(iv) |
| 938 |
res = self.fd.read(1) |
| 939 |
if res != b'D': |
| 940 |
self.msg = ('SETDAC error ') |
| 941 |
print(('SETDAC error '), res) |
| 942 |
return |
| 943 |
|
| 944 |
def read_adc(self, ch): # Sleep mode conversion |
| 945 |
''' |
| 946 |
Reads the specified ADC channel, Low level routine. |
| 947 |
@param ch channel number |
| 948 |
@return a value between 0 ans 4095 |
| 949 |
''' |
| 950 |
if ch < 0 or ch > 31: |
| 951 |
print(('Argument error')) |
| 952 |
return |
| 953 |
self.sendByte(READADCSM) |
| 954 |
self.sendByte(Byte.pack(ch)) |
| 955 |
res = self.fd.read(1) |
| 956 |
if res != b'D': |
| 957 |
self.msg = ('READADC error ') |
| 958 |
print(('READADC error '), res) |
| 959 |
return |
| 960 |
res = self.fd.read(2) |
| 961 |
if sys.version_info.major == 3: |
| 962 |
iv = res[0] | (res[1] << 8) |
| 963 |
else: |
| 964 |
iv = ord(res[0]) | (ord(res[1]) << 8) |
| 965 |
return iv |
| 966 |
|
| 967 |
def set_voltage(self, v): |
| 968 |
''' |
| 969 |
Sets the PVS output. range is from -5 to + 5 volts. Reads the actual value to apply correction. |
| 970 |
Returns the voltage readback of the voltage at PVS. |
| 971 |
''' |
| 972 |
if v < 0 or v > 5.0: |
| 973 |
self.msg = ('invalid voltage') |
| 974 |
print(('invalid voltage')) |
| 975 |
return |
| 976 |
goal = int(v * self.DACM + 0.5) |
| 977 |
iv = goal |
| 978 |
for k in range(10): |
| 979 |
self.write_dac(iv) |
| 980 |
isv = self.read_adc(12) # actual value |
| 981 |
err = goal - isv |
| 982 |
#print('iv & isv err', iv, isv, err , k) |
| 983 |
if abs(err) <= 1: break |
| 984 |
iv = iv + int(err/2) # Even if it exceeds 4095, write_dac() will fix it |
| 985 |
sv = self.get_voltage(12) # The voltage actually set |
| 986 |
return sv |
| 987 |
|
| 988 |
def set_adcref(self, option): |
| 989 |
''' |
| 990 |
Sets the ADC reference option. Vdd ot external +Vref |
| 991 |
@param option 0 means Vdd, else means external +Vref |
| 992 |
@return option's value |
| 993 |
''' |
| 994 |
self.sendByte(SETADCREF) |
| 995 |
self.sendByte(Byte.pack(option)) |
| 996 |
res = self.fd.read(1) |
| 997 |
if res != b'D': |
| 998 |
self.msg = ('SETADCREF error ') |
| 999 |
print(('SETADCREF error '), res) |
| 1000 |
return |
| 1001 |
return option |
| 1002 |
|
| 1003 |
def read_adcNS(self, ch): # No Sleep mode conversion |
| 1004 |
''' |
| 1005 |
Reads the specified ADC channel, Low level routine. |
| 1006 |
@param ch channel number |
| 1007 |
@return a value from 0 to 4095. |
| 1008 |
''' |
| 1009 |
if ch < 0 or ch > 31: |
| 1010 |
self.msg = ('READADC: Argument error') |
| 1011 |
print(('Argument error')) |
| 1012 |
return |
| 1013 |
self.sendByte(READADC) |
| 1014 |
self.sendByte(Byte.pack(ch)) |
| 1015 |
res = self.fd.read(1) |
| 1016 |
if res != b'D': |
| 1017 |
self.msg = ('READADC error') |
| 1018 |
print(('READADC error'), res) |
| 1019 |
return |
| 1020 |
res = self.fd.read(2) |
| 1021 |
if sys.version_info.major == 3: |
| 1022 |
iv = res[0] | (res[1] << 8) |
| 1023 |
else: |
| 1024 |
iv = ord(res[0]) | (ord(res[1]) << 8) |
| 1025 |
return iv |
| 1026 |
|
| 1027 |
def get_voltage(self, ch): # Sleep mode |
| 1028 |
''' |
| 1029 |
Reads the specified channel of the ADC. Returns -5V to 5V for channels 0 and 1 |
| 1030 |
0V to 5V for other channels. |
| 1031 |
''' |
| 1032 |
if (ch > 31): |
| 1033 |
self.msg = ('get_voltage: Argument error') |
| 1034 |
print(('Argument error')) |
| 1035 |
return |
| 1036 |
iv = self.read_adc(ch) |
| 1037 |
#print('get_v: iv = ', iv) |
| 1038 |
v = self.m12[ch] * iv + self.c[ch] |
| 1039 |
return v |
| 1040 |
|
| 1041 |
def get_voltageNS(self, ch): # No Sleep Mode |
| 1042 |
''' |
| 1043 |
Reads the specified channel of the ADC. Returns -5V to 5V for channels 0 and 1 |
| 1044 |
0V to 5V for other channels. |
| 1045 |
''' |
| 1046 |
if (ch > 31): |
| 1047 |
self.msg = ('get_voltageNS: Argument error') |
| 1048 |
print(('Argument error')) |
| 1049 |
return |
| 1050 |
iv = self.read_adcNS(ch) |
| 1051 |
#print('get_v: iv = ', iv) |
| 1052 |
v = self.m12[ch] * iv + self.c[ch] |
| 1053 |
return v |
| 1054 |
|
| 1055 |
def get_voltage_within(self, ch, vmax): # Channel and the expected maximum value, < 5V |
| 1056 |
''' |
| 1057 |
Sets the DAC to vmax and uses it as external +Vref, to increase resolution |
| 1058 |
''' |
| 1059 |
if ch > 31 or vmax > 5.0: |
| 1060 |
self.msg = ('Argument error') |
| 1061 |
print(('Argument error')) |
| 1062 |
return |
| 1063 |
VM = self.set_voltage(vmax) |
| 1064 |
self.set_adcref(1) # External +Vref, from DAC |
| 1065 |
res = self.get_voltage(ch) |
| 1066 |
self.set_adcref(0) # Back to Vref+ = Vdd |
| 1067 |
return res * VM/5.0 |
| 1068 |
|
| 1069 |
def get_voltage_time(self, ch): |
| 1070 |
''' |
| 1071 |
Reads the specified channel of the ADC. Returns -5V to 5V for channels 0 and 1 |
| 1072 |
0V to 5V for other channels. Adds the PC time info |
| 1073 |
''' |
| 1074 |
if (ch > 31): |
| 1075 |
self.msg = ('get_voltage_time: Argument error') |
| 1076 |
print(('Argument error')) |
| 1077 |
return |
| 1078 |
return (time.time(), self.get_voltage(ch)) |
| 1079 |
|
| 1080 |
def get_voltageNS_time(self, ch): # No Sleep mode conversion |
| 1081 |
''' |
| 1082 |
Reads the specified channel of the ADC. Returns -5V to 5V for channels 0 and 1 |
| 1083 |
0V to 5V for other channels. Adds the PC time info |
| 1084 |
''' |
| 1085 |
if (ch > 31): |
| 1086 |
self.msg = ('Argument error') |
| 1087 |
print(('Argument error')) |
| 1088 |
return ('Error ') |
| 1089 |
return (time.time(), self.get_voltageNS(ch)) |
| 1090 |
|
| 1091 |
|
| 1092 |
def capture(self, ch, ns, tg): |
| 1093 |
''' |
| 1094 |
makes a capture of data from the ADC with 8 bits precision. |
| 1095 |
@param ch channel number, |
| 1096 |
@param ns number of samples |
| 1097 |
@param tg time gap between samples. |
| 1098 |
@return a vector of ns timesstamps and another of ns values from the ADC |
| 1099 |
''' |
| 1100 |
if tg < 4: # Minimum time required |
| 1101 |
self.msg = ('Minimum Timegap is 4 us') |
| 1102 |
tg = 4 |
| 1103 |
ns=int(ns) |
| 1104 |
self.sendByte(CAPTURE) |
| 1105 |
self.sendByte(Byte.pack(ch)) |
| 1106 |
self.sendInt(ns) |
| 1107 |
self.sendInt(tg) |
| 1108 |
res = self.fd.read(1) |
| 1109 |
if res != b'D': |
| 1110 |
self.msg = ('CAPTURE error') |
| 1111 |
print(('CAPTURE error '), res) |
| 1112 |
return |
| 1113 |
res = self.fd.read(1) # adc_size info from other end, ignored |
| 1114 |
data = self.fd.read(ns) |
| 1115 |
dl = len(data) |
| 1116 |
if dl != ns: |
| 1117 |
self.msg = ('CAPTURE: size mismatch %d %d') %(ns,dl) |
| 1118 |
print(('CAPTURE: size mismatch '), ns, dl) |
| 1119 |
return |
| 1120 |
|
| 1121 |
#ta = [] |
| 1122 |
#va = [] |
| 1123 |
raw = struct.unpack('B'* ns, data) # 1 byte words in the structure |
| 1124 |
|
| 1125 |
ta = numpy.zeros(ns) |
| 1126 |
va = numpy.zeros(ns) |
| 1127 |
for i in range(ns): |
| 1128 |
ta[i] = 0.001 * i * tg # microseconds to milliseconds |
| 1129 |
va[i] = raw[i] * self.m8[ch] + self.c[ch] |
| 1130 |
return ta,va |
| 1131 |
|
| 1132 |
def capture_hr(self, ch, ns, tg): |
| 1133 |
''' |
| 1134 |
Captures data in high resolution (2 bytes, with 12 significant bits) |
| 1135 |
@param ch channel number |
| 1136 |
@param ns number of samples |
| 1137 |
@param tg time gap between two samples |
| 1138 |
@return a vector of ns timesstamps and another of ns values from the ADC |
| 1139 |
''' |
| 1140 |
if tg < 4: |
| 1141 |
self.msg = ('Minimum Timegap is 4 us') |
| 1142 |
tg = 4 |
| 1143 |
ns=int(ns) |
| 1144 |
self.sendByte(CAPTURE_HR) |
| 1145 |
self.sendByte(Byte.pack(ch)) |
| 1146 |
self.sendInt(ns) |
| 1147 |
self.sendInt(tg) |
| 1148 |
res = self.fd.read(1) |
| 1149 |
if res != b'D': |
| 1150 |
self.msg = ('CAPTURE error ') |
| 1151 |
print(('CAPTURE error '), res) |
| 1152 |
return |
| 1153 |
res = self.fd.read(1) # adc_size info from other end, ignored |
| 1154 |
data = self.fd.read(ns*2) |
| 1155 |
dl = len(data) |
| 1156 |
if dl != ns*2: |
| 1157 |
self.msg = ('CAPTURE: size mismatch %d %d') %(ns, dl) |
| 1158 |
print(('CAPTURE: size mismatch '), ns, dl) |
| 1159 |
return |
| 1160 |
|
| 1161 |
raw = struct.unpack('H'* ns, data) # 1 byte words in the structure |
| 1162 |
ta = numpy.zeros(ns) |
| 1163 |
va = numpy.zeros(ns) |
| 1164 |
for i in range(ns): |
| 1165 |
ta[i] = 0.001 * i * tg # microseconds to milliseconds |
| 1166 |
va[i] = raw[i] * self.m12[ch] + self.c[ch] |
| 1167 |
return ta,va |
| 1168 |
|
| 1169 |
|
| 1170 |
def capture2(self, cha, chb, ns, tg): |
| 1171 |
''' |
| 1172 |
Captures from 2 channels, data precision is 8 bits |
| 1173 |
@param cha first channel |
| 1174 |
@param chb second channel |
| 1175 |
@param ns number of samples |
| 1176 |
@param tg time gap between samples |
| 1177 |
@return 4 vectors of data: time, voltage, time, voltage |
| 1178 |
''' |
| 1179 |
if tg < 8: |
| 1180 |
self.msg = ('Minimum Timegap is (4*number of channels)usec') |
| 1181 |
tg = 8 |
| 1182 |
ns=int(ns) |
| 1183 |
self.sendByte(CAPTURE2) |
| 1184 |
self.sendByte(Byte.pack(cha)) |
| 1185 |
self.sendByte(Byte.pack(chb)) |
| 1186 |
self.sendInt(ns) |
| 1187 |
self.sendInt(tg) |
| 1188 |
res = self.fd.read(1) |
| 1189 |
if res != b'D': |
| 1190 |
self.msg =('CAPTURE2 error ') |
| 1191 |
print(('CAPTURE2 error '), res) |
| 1192 |
return |
| 1193 |
res = self.fd.read(1) # adc_size info from other end, ignored |
| 1194 |
data = self.fd.read(ns*2) |
| 1195 |
dl = len(data) |
| 1196 |
if dl != ns*2: |
| 1197 |
self.msg = ('CAPTURE2: size mismatch') |
| 1198 |
print(('CAPTURE2: size mismatch'), ns*2, dl) |
| 1199 |
return |
| 1200 |
|
| 1201 |
raw = struct.unpack('B'* 2*ns, data) # 8 bit data in byte array |
| 1202 |
taa = numpy.zeros(ns) |
| 1203 |
vaa = numpy.zeros(ns) |
| 1204 |
tba = numpy.zeros(ns) |
| 1205 |
vba = numpy.zeros(ns) |
| 1206 |
for i in range(ns): |
| 1207 |
taa[i] = (0.001 * i * tg) |
| 1208 |
vaa[i] = (raw[2*i] * self.m8[cha] + self.c[cha]) |
| 1209 |
tba[i] = (0.001 * i * tg + self.tgap) |
| 1210 |
vba[i] = (raw[2*i +1] * self.m8[chb] + self.c[chb]) |
| 1211 |
return taa,vaa,tba,vba |
| 1212 |
|
| 1213 |
def capture2_hr(self, cha, chb, ns, tg): |
| 1214 |
''' |
| 1215 |
Captures from 2 channels, data precision is 12 bits |
| 1216 |
@param cha first channel |
| 1217 |
@param chb second channel |
| 1218 |
@param ns number of samples |
| 1219 |
@param tg time gap between samples |
| 1220 |
@return 4 vectors of data: time, voltage, time, voltage |
| 1221 |
''' |
| 1222 |
if tg < 8: |
| 1223 |
self.msg = ('Minimum Timegap is (4*number of channels)usec') |
| 1224 |
tg = 8 |
| 1225 |
ns=int(ns) |
| 1226 |
self.sendByte(CAPTURE2_HR) |
| 1227 |
self.sendByte(Byte.pack(cha)) |
| 1228 |
self.sendByte(Byte.pack(chb)) |
| 1229 |
self.sendInt(ns) |
| 1230 |
self.sendInt(tg) |
| 1231 |
res = self.fd.read(1) |
| 1232 |
if res != b'D': |
| 1233 |
self.msg = ('CAPTURE2_HR error ') |
| 1234 |
print(('CAPTURE2_HR error '), res) |
| 1235 |
return |
| 1236 |
res = self.fd.read(1) # adc_size info from other end, ignored |
| 1237 |
data = self.fd.read(ns*2*2) |
| 1238 |
dl = len(data) |
| 1239 |
if dl != ns*2*2: |
| 1240 |
self.msg = ('CAPTURE2_HR: size mismatch') |
| 1241 |
print(('CAPTURE2_HR: size mismatch'), ns*2*2, dl) |
| 1242 |
return |
| 1243 |
|
| 1244 |
taa = numpy.zeros(ns) |
| 1245 |
vaa = numpy.zeros(ns) |
| 1246 |
tba = numpy.zeros(ns) |
| 1247 |
vba = numpy.zeros(ns) |
| 1248 |
raw = struct.unpack('H'* 2*ns, data) # 16 bit data in byte array |
| 1249 |
for i in range(ns): |
| 1250 |
taa[i] = (0.001 * i * tg) |
| 1251 |
vaa[i] = (raw[2*i] * self.m12[cha] + self.c[cha]) |
| 1252 |
tba[i] = (0.001 * i * tg + self.tgap) |
| 1253 |
vba[i] = (raw[2*i +1] * self.m12[chb] + self.c[chb]) |
| 1254 |
return taa,vaa,tba,vba |
| 1255 |
|
| 1256 |
def capture3(self, ch1, ch2, ch3, ns, tg): |
| 1257 |
''' |
| 1258 |
Captures from 2 channels, data precision is 12 bits |
| 1259 |
@param ch1 first channel |
| 1260 |
@param ch2 second channel |
| 1261 |
@param ch3 third channel |
| 1262 |
@param ns number of samples |
| 1263 |
@param tg time gap between samples |
| 1264 |
@return 6 vectors of data: 3 x (time, voltage) |
| 1265 |
''' |
| 1266 |
if tg < 12: |
| 1267 |
self.msg = ('Minimum Timegap is (4*number of channels)usec') |
| 1268 |
tg = 12 |
| 1269 |
ch12 = (ch2 << 4) | ch1 # first two channels packed in 1 byte |
| 1270 |
ns=int(ns) |
| 1271 |
self.sendByte(CAPTURE3) |
| 1272 |
self.sendByte(Byte.pack(ch12)) |
| 1273 |
self.sendByte(Byte.pack(ch3)) |
| 1274 |
self.sendInt(ns) |
| 1275 |
self.sendInt(tg) |
| 1276 |
res = self.fd.read(1) |
| 1277 |
if res != b'D': |
| 1278 |
self.msg = ('CAPTURE3 error ') |
| 1279 |
print(('CAPTURE3 error '), res) |
| 1280 |
return |
| 1281 |
res = self.fd.read(1) # adc_size info from other end, ignored |
| 1282 |
data = self.fd.read(ns*3) |
| 1283 |
dl = len(data) |
| 1284 |
if dl != ns*3: |
| 1285 |
self.msg = ('CAPTURE3: size mismatch ') |
| 1286 |
print(('CAPTURE3: size mismatch '), ns*3, dl) |
| 1287 |
return |
| 1288 |
taa = numpy.zeros(ns) |
| 1289 |
vaa = numpy.zeros(ns) |
| 1290 |
tba = numpy.zeros(ns) |
| 1291 |
vba = numpy.zeros(ns) |
| 1292 |
tca = numpy.zeros(ns) |
| 1293 |
vca = numpy.zeros(ns) |
| 1294 |
raw = struct.unpack('B'* 3*ns, data) # 8 bit data in byte array |
| 1295 |
for i in range(ns): |
| 1296 |
taa[i] = (0.001 * i * tg) |
| 1297 |
vaa[i] = (raw[3*i] * self.m8[ch1] + self.c[ch1]) |
| 1298 |
tba[i] = (0.001 * i * tg + self.tgap) |
| 1299 |
vba[i] = (raw[3*i +1] * self.m8[ch2] + self.c[ch2]) |
| 1300 |
tca[i] = (0.001 * i * tg + 2*self.tgap) |
| 1301 |
vca[i] = (raw[3*i +2] * self.m8[ch3] + self.c[ch3]) |
| 1302 |
return taa,vaa, tba,vba, tca,vca |
| 1303 |
|
| 1304 |
|
| 1305 |
def capture4(self, ch1, ch2, ch3, ch4, ns, tg): |
| 1306 |
''' |
| 1307 |
Captures from 2 channels, data precision is 12 bits |
| 1308 |
@param ch1 first channel |
| 1309 |
@param ch2 second channel |
| 1310 |
@param ch3 third channel |
| 1311 |
@param ch4 fourth channel |
| 1312 |
@param ns number of samples |
| 1313 |
@param tg time gap between samples |
| 1314 |
@return 8 vectors of data: 4 x (time, voltage) |
| 1315 |
''' |
| 1316 |
if tg < 16: |
| 1317 |
self.msg = ('Minimum Timegap is (4*number of channels)usec') |
| 1318 |
tg = 16 |
| 1319 |
ch12 = (ch2 << 4) | ch1 # first two channels packed in 1 byte |
| 1320 |
ch34 = (ch4 << 4) | ch3 # other two channels packed in 1 byte |
| 1321 |
ns=int(ns) |
| 1322 |
self.sendByte(CAPTURE4) |
| 1323 |
self.sendByte(Byte.pack(ch12)) |
| 1324 |
self.sendByte(Byte.pack(ch34)) |
| 1325 |
self.sendInt(ns) |
| 1326 |
self.sendInt(tg) |
| 1327 |
res = self.fd.read(1) |
| 1328 |
if res != b'D': |
| 1329 |
self.msg = ('CAPTURE4 error =') |
| 1330 |
print(('CAPTURE4 error ='), ord(res)) |
| 1331 |
return |
| 1332 |
res = self.fd.read(1) # adc_size info from other end, ignored |
| 1333 |
data = self.fd.read(ns*4) |
| 1334 |
dl = len(data) |
| 1335 |
if dl != ns*4: |
| 1336 |
self.msg = ('CAPTURE4: size mismatch ') |
| 1337 |
print(('CAPTURE4: size mismatch '), ns*4, dl) |
| 1338 |
return |
| 1339 |
taa = numpy.zeros(ns) |
| 1340 |
vaa = numpy.zeros(ns) |
| 1341 |
tba = numpy.zeros(ns) |
| 1342 |
vba = numpy.zeros(ns) |
| 1343 |
tca = numpy.zeros(ns) |
| 1344 |
vca = numpy.zeros(ns) |
| 1345 |
tda = numpy.zeros(ns) |
| 1346 |
vda = numpy.zeros(ns) |
| 1347 |
raw = struct.unpack('B'* 4*ns, data) # 8 bit data in byte array |
| 1348 |
#print(raw) |
| 1349 |
for i in range(ns): |
| 1350 |
taa[i] = (0.001 * i * tg) |
| 1351 |
vaa[i] = (raw[4*i] * self.m8[ch1] + self.c[ch1]) |
| 1352 |
tba[i] = (0.001 * i * tg + self.tgap) |
| 1353 |
vba[i] = (raw[4*i +1] * self.m8[ch2] + self.c[ch2]) |
| 1354 |
tca[i] = (0.001 * i * tg + 2*self.tgap) |
| 1355 |
vca[i] = (raw[4*i +2] * self.m8[ch3] + self.c[ch3]) |
| 1356 |
tda[i] = (0.001 * i * tg + 3*self.tgap) |
| 1357 |
vda[i] = (raw[4*i +3] * self.m8[ch4] + self.c[ch4]) |
| 1358 |
return taa,vaa, tba,vba, tca,vca, tda, vda |
| 1359 |
|
| 1360 |
def capture01(self, np, tg): |
| 1361 |
''' |
| 1362 |
captures channels A0 and A1 simultaneously, with 8 bit resolution |
| 1363 |
''' |
| 1364 |
return self.capture2(1,2,np,tg) |
| 1365 |
|
| 1366 |
def capture01_hr(self, np, tg): |
| 1367 |
''' |
| 1368 |
captures channels A0 and A1 simultaneously, with 12 bit resolution |
| 1369 |
''' |
| 1370 |
return self.capture2_hr(1,2,np,tg) |
| 1371 |
|
| 1372 |
def set_trigger(self, tval): |
| 1373 |
self.sendByte(SETTRIGVAL) |
| 1374 |
self.sendInt(tval) |
| 1375 |
res = self.fd.read(1) |
| 1376 |
if res != b'D': |
| 1377 |
self.msg = ('SETTRIGVAL error ') |
| 1378 |
print(('SETTRIGVAL error '), res) |
| 1379 |
return |
| 1380 |
return tval |
| 1381 |
|
| 1382 |
"""----------------- Modifiers for Capture ----------------------------""" |
| 1383 |
def disable_actions(self): |
| 1384 |
''' |
| 1385 |
Disable all modifiers to the capture call. The capture calls will be set to |
| 1386 |
do analog triggering on the first channel captured. |
| 1387 |
''' |
| 1388 |
self.sendByte(SETACTION) |
| 1389 |
self.sendByte(AANATRIG) |
| 1390 |
self.sendByte(Byte.pack(0)) # Self trigger on channel zero means the first channel captured |
| 1391 |
res = self.fd.read(1) |
| 1392 |
if res != b'D': |
| 1393 |
self.msg = ('ERROR: SETACTION') |
| 1394 |
print(('ERROR: SETACTION'), res) |
| 1395 |
return |
| 1396 |
return 0 |
| 1397 |
|
| 1398 |
def enable_action(self, action, ch): |
| 1399 |
""" |
| 1400 |
Enables some action |
| 1401 |
@param action a bytes of length 1 |
| 1402 |
@param ch a channel number |
| 1403 |
@return action's value |
| 1404 |
""" |
| 1405 |
actions=(AANATRIG, AWAITHI, AWAITLO, AWAITRISE, AWAITFALL, ASET, |
| 1406 |
ACLR, APULSEHT, APULSELT) |
| 1407 |
if action not in actions or ch < 1 or ch > 11: |
| 1408 |
self.msg = 'Invalid actions or source specified' |
| 1409 |
return |
| 1410 |
self.sendByte(SETACTION) |
| 1411 |
self.sendByte(action) |
| 1412 |
self.sendByte(Byte.pack(ch)) |
| 1413 |
res = self.fd.read(1) |
| 1414 |
if res != b'D': |
| 1415 |
self.msg = ('SETACTION ERR') |
| 1416 |
return |
| 1417 |
return action |
| 1418 |
|
| 1419 |
def set_trig_source(self, ch): |
| 1420 |
''' |
| 1421 |
Analog Trigger of the desired channel |
| 1422 |
''' |
| 1423 |
return self.enable_action(AANATRIG, ch) |
| 1424 |
|
| 1425 |
def enable_wait_high(self, ch): |
| 1426 |
''' |
| 1427 |
Wait for a HIGH on the specified 'pin' just before every Capture. |
| 1428 |
''' |
| 1429 |
return self.enable_action(AWAITHI, ch) |
| 1430 |
|
| 1431 |
def enable_wait_low(self, ch): |
| 1432 |
''' |
| 1433 |
Wait for a LOW on the specified 'pin' just before every Capture. |
| 1434 |
''' |
| 1435 |
return self.enable_action(AWAITLO, ch) |
| 1436 |
|
| 1437 |
def enable_wait_rising(self, ch): |
| 1438 |
''' |
| 1439 |
Wait for a rising EDGE on the specified 'pin' just before every Capture. |
| 1440 |
''' |
| 1441 |
return self.enable_action(AWAITRISE, ch) |
| 1442 |
|
| 1443 |
def enable_wait_falling(self, ch): |
| 1444 |
''' |
| 1445 |
Wait for a falling EDGE on the specified 'pin' just before every Capture. |
| 1446 |
''' |
| 1447 |
return self.enable_action(AWAITFALL, ch) |
| 1448 |
|
| 1449 |
def enable_set_high(self, ch): |
| 1450 |
''' |
| 1451 |
Sets the specified 'pin' HIGH, just before every Capture. |
| 1452 |
''' |
| 1453 |
return self.enable_action(ASET, ch) |
| 1454 |
|
| 1455 |
def enable_set_low(self, ch): |
| 1456 |
''' |
| 1457 |
Sets the specified 'pin' LOW, just before every Capture. |
| 1458 |
''' |
| 1459 |
return self.enable_action(ACLR, ch) |
| 1460 |
|
| 1461 |
def enable_pulse_high(self, ch): |
| 1462 |
''' |
| 1463 |
Generate a HIGH TRUE Pulse on the specified 'pin', just before every Capture. |
| 1464 |
width is specified by the set_pulsewidth() function. |
| 1465 |
''' |
| 1466 |
return self.enable_action(APULSEHT, ch) |
| 1467 |
|
| 1468 |
def enable_pulse_low(self, ch): |
| 1469 |
''' |
| 1470 |
Generate a LOW TRUE Pulse on the specified 'pin', just before every Capture. |
| 1471 |
''' |
| 1472 |
return self.enable_action(APULSELT, ch) |
| 1473 |
|
| 1474 |
def set_pulsewidth(self, width): |
| 1475 |
''' |
| 1476 |
Sets the 'pulse_width' parameter for pulse2rtime() command. |
| 1477 |
Also used by usound_time() and the elable_pulse_high/low() functions |
| 1478 |
@param width an integer value (microseconds |
| 1479 |
@return the value of width |
| 1480 |
''' |
| 1481 |
if width < 1 or width > 500: |
| 1482 |
self.msg = ('Invalid pulse width') |
| 1483 |
return |
| 1484 |
self.sendByte(SETPULWIDTH) |
| 1485 |
self.sendInt(width) |
| 1486 |
res = self.fd.read(1) |
| 1487 |
if res != b'D': |
| 1488 |
self.msg = ('ERROR: SETPULWIDTH') |
| 1489 |
print(('ERROR: SETPULWIDTH'), res) |
| 1490 |
return |
| 1491 |
return width |
| 1492 |
|
| 1493 |
"""-----------DIRECT PORT ACCESS FUNCTIONS-----------""" |
| 1494 |
"""-----------(Use only if you know what you are doing)---------""" |
| 1495 |
def set_ddr(self, port, direc): |
| 1496 |
self.dwrite(SETDDR) |
| 1497 |
self.dwrite(Byte.pack(port)) # 0 to 3 for A,B,C and D |
| 1498 |
self.dwrite(Byte.pack(direc)) |
| 1499 |
self.fd.read(1) |
| 1500 |
return |
| 1501 |
|
| 1502 |
def set_port(self, port, val): |
| 1503 |
self.dwrite(SETPORT) |
| 1504 |
self.dwrite(Byte.pack(port)) # 0 to 3 for A,B,C and D |
| 1505 |
self.dwrite(Byte.pack(val)) |
| 1506 |
self.fd.read(1) |
| 1507 |
return |
| 1508 |
|
| 1509 |
def get_port(self, port): |
| 1510 |
self.dwrite(SETPORT) |
| 1511 |
self.dwrite(Byte.pack(port)) # 0 to 3 for A,B,C and D |
| 1512 |
self.fd.read(1) |
| 1513 |
data = self.fd.read(1) # get the status byte only |
| 1514 |
return ord(data) |
| 1515 |
|
| 1516 |
"""---------------------- may go to eyeutils.py --------------------""" |
| 1517 |
def minimum(self,va): |
| 1518 |
vmin = 1.0e10 # need to change |
| 1519 |
for v in va: |
| 1520 |
if v < vmin: |
| 1521 |
vmin = v |
| 1522 |
return vmin |
| 1523 |
|
| 1524 |
def maximum(self,va): |
| 1525 |
vmax = 1.0e-10 # need to change |
| 1526 |
for v in va: |
| 1527 |
if v > vmax: |
| 1528 |
vmax = v |
| 1529 |
return vmax |
| 1530 |
|
| 1531 |
def rms(self,va): |
| 1532 |
vsum = 0.0 |
| 1533 |
for v in va: |
| 1534 |
vsum += v**2 |
| 1535 |
v = 1.0*vsum / len(va) |
| 1536 |
return math.sqrt(v) |
| 1537 |
|
| 1538 |
def mean(self,va): |
| 1539 |
vsum = 0.0 |
| 1540 |
for v in va: |
| 1541 |
vsum += v |
| 1542 |
v = 1.0*vsum / len(va) |
| 1543 |
return v |
| 1544 |
|
| 1545 |
def save(self, data, filename = 'plot.dat'): |
| 1546 |
''' |
| 1547 |
Input data is of the form, [ [x1,y1], [x2,y2],....] where x and y are vectors |
| 1548 |
''' |
| 1549 |
if data == None: return |
| 1550 |
if type(filename) == tuple: |
| 1551 |
filename = filename[0] |
| 1552 |
|
| 1553 |
if filename: |
| 1554 |
f = fileOpen(filename,'w') |
| 1555 |
for xy in data: |
| 1556 |
for k in range(len(xy[0])): |
| 1557 |
f.write('%5.3f %5.3f\n'%(xy[0][k], xy[1][k])) |
| 1558 |
f.write('\n') |
| 1559 |
f.close() |
| 1560 |
|
| 1561 |
|
| 1562 |
def grace(self, data, xlab = '', ylab = '', title = ''): |
| 1563 |
''' |
| 1564 |
Input data is of the form, [ [x1,y1], [x2,y2],....] where x and y are vectors |
| 1565 |
''' |
| 1566 |
from expeyes.eyeplot import ExtPlotter |
| 1567 |
ep=ExtPlotter() |
| 1568 |
status=ep.plot(data, xLabel=xlab, yLabel=ylab, title=title) |
| 1569 |
return status |
| 1570 |
|
| 1571 |
|
| 1572 |
# Local Variables: |
| 1573 |
# python-indent: 4 |
| 1574 |
# End: |
| 1575 |
|