| 1 |
############# MATHEMATICAL AND ANALYTICS ############### |
| 2 |
import functools,sys |
| 3 |
from functools import partial |
| 4 |
import numpy as np |
| 5 |
import time,struct |
| 6 |
from collections import OrderedDict |
| 7 |
|
| 8 |
|
| 9 |
try: |
| 10 |
from scipy import optimize |
| 11 |
except: |
| 12 |
print('scipy not available') |
| 13 |
|
| 14 |
def find_peak(va): |
| 15 |
vmax = 0.0 |
| 16 |
size = len(va) |
| 17 |
index = 0 |
| 18 |
for i in range(1,size): # skip first 2 channels, DC |
| 19 |
if va[i] > vmax: |
| 20 |
vmax = va[i] |
| 21 |
index = i |
| 22 |
return index |
| 23 |
|
| 24 |
def bswap(val): |
| 25 |
return struct.unpack('<H', struct.pack('>H', val))[0] |
| 26 |
def makeuint16(lsb, msb): |
| 27 |
return ((msb & 0xFF) << 8) | (lsb & 0xFF) |
| 28 |
|
| 29 |
#-------------------------- Fourier Transform ------------------------------------ |
| 30 |
def fft(ya, si): |
| 31 |
''' |
| 32 |
Returns positive half of the Fourier transform of the signal ya. |
| 33 |
Sampling interval 'si', in milliseconds |
| 34 |
''' |
| 35 |
NP = len(ya) |
| 36 |
if NP%2: #odd number |
| 37 |
ya = ya[:-1] |
| 38 |
NP-=1 |
| 39 |
v = np.array(ya) |
| 40 |
tr = abs(np.fft.fft(v))/NP |
| 41 |
frq = np.fft.fftfreq(NP, si) |
| 42 |
x = frq.reshape(2,int(NP/2)) |
| 43 |
y = tr.reshape(2,int(NP/2)) |
| 44 |
return x[0], y[0] |
| 45 |
|
| 46 |
def find_frequency(x,y): # Returns the fundamental frequency using FFT |
| 47 |
tx,ty = fft(y, x[1]-x[0]) |
| 48 |
index = find_peak(ty) |
| 49 |
if index == 0: |
| 50 |
return None |
| 51 |
else: |
| 52 |
return tx[index] |
| 53 |
|
| 54 |
#-------------------------- Sine Fit ------------------------------------------------ |
| 55 |
def sine_eval(x,p): # y = a * sin(2*pi*f*x + phi)+ offset |
| 56 |
return p[0] * np.sin(2*np.pi*p[1]*x+p[2])+p[3] |
| 57 |
|
| 58 |
def sine_erf(p,x,y): |
| 59 |
return y - sine_eval(x,p) |
| 60 |
|
| 61 |
|
| 62 |
def fit_sine(xa,ya, freq = 0): # Time in mS, V in volts, freq in Hz, accepts numpy arrays |
| 63 |
size = len(ya) |
| 64 |
mx = max(ya) |
| 65 |
mn = min(ya) |
| 66 |
amp = (mx-mn)/2 |
| 67 |
off = np.average(ya) |
| 68 |
if freq == 0: # Guess frequency not given |
| 69 |
freq = find_frequency(xa,ya) |
| 70 |
if freq == None: |
| 71 |
return None |
| 72 |
#print 'guess a & freq = ', amp, freq |
| 73 |
par = [amp, freq, 0.0, off] # Amp, freq, phase , offset |
| 74 |
par, pcov = optimize.leastsq(sine_erf, par, args=(xa, ya)) |
| 75 |
return par |
| 76 |
|
| 77 |
|
| 78 |
#--------------------------Damped Sine Fit ------------------------------------------------ |
| 79 |
def dsine_eval(x,p): |
| 80 |
return p[0] * np.sin(2*np.pi*p[1]*x+p[2]) * np.exp(-p[4]*x) + p[3] |
| 81 |
def dsine_erf(p,x,y): |
| 82 |
return y - dsine_eval(x,p) |
| 83 |
|
| 84 |
|
| 85 |
def fit_dsine(xlist, ylist, freq = 0): |
| 86 |
size = len(xlist) |
| 87 |
xa = np.array(xlist, dtype=np.float) |
| 88 |
ya = np.array(ylist, dtype=np.float) |
| 89 |
amp = (max(ya)-min(ya))/2 |
| 90 |
off = np.average(ya) |
| 91 |
if freq == 0: |
| 92 |
freq = find_frequency(xa,ya) |
| 93 |
if freq==None: return None |
| 94 |
par = [amp, freq, 0.0, off, 0.] # Amp, freq, phase , offset, decay constant |
| 95 |
par, pcov = optimize.leastsq(dsine_erf, par,args=(xa,ya)) |
| 96 |
|
| 97 |
return par |
| 98 |
|
| 99 |
|
| 100 |
############# MATHEMATICAL AND ANALYTICS ############### |
| 101 |
|
| 102 |
class LOGGER: |
| 103 |
def __init__(self,I2C): |
| 104 |
self.sensors={ |
| 105 |
0x39:{ |
| 106 |
'name':'TSL2561 Luminosity Sensor', |
| 107 |
'init':self.TSL2561_init, |
| 108 |
'read':self.TSL2561_all, |
| 109 |
'fields':['total','IR'], |
| 110 |
'min':[0,0], |
| 111 |
'max':[2**15,2**15], |
| 112 |
'config':[{ |
| 113 |
'name':'gain', |
| 114 |
'options':['1x','16x'], |
| 115 |
'function':self.TSL2561_gain |
| 116 |
}, |
| 117 |
{ |
| 118 |
'name':'Integration Time', |
| 119 |
'options':['3 mS','101 mS','402 mS'], |
| 120 |
'function':self.TSL2561_timing |
| 121 |
} |
| 122 |
] }, |
| 123 |
0x1E:{ |
| 124 |
'name':'HMC5883L 3 Axis Magnetometer ', |
| 125 |
'init':self.HMC5883L_init, |
| 126 |
'read':self.HMC5883L_all, |
| 127 |
'fields':['Mx','My','Mz'], |
| 128 |
'min':[-5000,-5000,-5000], |
| 129 |
'max':[5000,5000,5000], |
| 130 |
'config':[{ |
| 131 |
'name':'gain', |
| 132 |
'options':['1x','16x'], |
| 133 |
'function':self.TSL2561_gain |
| 134 |
}, |
| 135 |
{ |
| 136 |
'name':'Integration Time', |
| 137 |
'options':['3 mS','101 mS','402 mS'], |
| 138 |
'function':self.TSL2561_timing |
| 139 |
} |
| 140 |
] }, |
| 141 |
0x48:{ |
| 142 |
'name':'ADS1115', |
| 143 |
'init':self.ADS1115_init, |
| 144 |
'read':self.ADS1115_read, |
| 145 |
'fields':['Voltage'], |
| 146 |
'min':[-20], |
| 147 |
'max':[20], |
| 148 |
'config':[{ |
| 149 |
'name':'channel', |
| 150 |
'options':['UNI_0','UNI_1','UNI_2','UNI_3','DIFF_01','DIFF_23'], |
| 151 |
'function':self.ADS1115_channel |
| 152 |
}, |
| 153 |
{ |
| 154 |
'name':'Data Rate', |
| 155 |
'options':['8','16','32','64','128','250','475','860'], |
| 156 |
'function':self.ADS1115_rate |
| 157 |
}, |
| 158 |
{ |
| 159 |
'name':'Gain', |
| 160 |
'options':['GAIN_TWOTHIRDS','GAIN_ONE','GAIN_TWO','GAIN_FOUR','GAIN_EIGHT','GAIN_SIXTEEN'], |
| 161 |
'function':self.ADS1115_gain |
| 162 |
} |
| 163 |
] }, |
| 164 |
0x68:{ |
| 165 |
'name':'MPU6050 3 Axis Accelerometer and Gyro (Ax, Ay, Az, Temp, Gx, Gy, Gz) ', |
| 166 |
'init':self.MPU6050_init, |
| 167 |
'read':self.MPU6050_all, |
| 168 |
'fields':['Ax','Ay','Az','Temp','Gx','Gy','Gz'], |
| 169 |
'min':[-1*2**15,-1*2**15,-1*2**15,0,-1*2**15,-1*2**15,-1*2**15], |
| 170 |
'max':[2**15,2**15,2**15,2**16,2**15,2**15,2**15], |
| 171 |
'config':[{ |
| 172 |
'name':'Gyroscope Range', |
| 173 |
'options':['250','500','1000','2000'], |
| 174 |
'function':self.MPU6050_gyro_range |
| 175 |
}, |
| 176 |
{ |
| 177 |
'name':'Accelerometer Range', |
| 178 |
'options':['2x','4x','8x','16x'], |
| 179 |
'function':self.MPU6050_accel_range |
| 180 |
}, |
| 181 |
{ |
| 182 |
'name':'Kalman', |
| 183 |
'options':['OFF','0.001','0.01','0.1','1','10'], |
| 184 |
'function':self.MPU6050_kalman_set |
| 185 |
} |
| 186 |
]}, |
| 187 |
118:{ |
| 188 |
'name':'BMP280 Pressure and Temperature sensor', |
| 189 |
'init':self.BMP280_init, |
| 190 |
'read':self.BMP280_all, |
| 191 |
'fields':['Pressure','Temp','rH %%'], |
| 192 |
'min':[0,0,0], |
| 193 |
'max':[1600,100,100], |
| 194 |
}, |
| 195 |
12:{ #0xc |
| 196 |
'name':'AK8963 Mag', |
| 197 |
'init':self.AK8963_init, |
| 198 |
'read':self.AK8963_all, |
| 199 |
'fields':['X','Y','Z'], |
| 200 |
'min':[-32767,-32767,-32767], |
| 201 |
'max':[32767,32767,32767], |
| 202 |
}, |
| 203 |
119:{ |
| 204 |
'name':'MS5611 Pressure and Temperature Sensor', |
| 205 |
'init':self.MS5611_init, |
| 206 |
'read':self.MS5611_all, |
| 207 |
'fields':['Pressure','Temp','Alt'], |
| 208 |
'min':[0,0,0], |
| 209 |
'max':[1600,100,10], |
| 210 |
}, |
| 211 |
0x41:{ #A0 pin connected to Vs . Otherwise address 0x40 conflicts with PCA board. |
| 212 |
'name':'INA3221 Current Sensor', |
| 213 |
'init':self.INA3221_init, |
| 214 |
'read':self.INA3221_all, |
| 215 |
'fields':['CH1','CH2','CH3'], |
| 216 |
'min':[0,0,0], |
| 217 |
'max':[1000,1000,1000], |
| 218 |
}, |
| 219 |
0x29:{ #VL53L0X. |
| 220 |
'name':'VL53L0X time of flight sensor', |
| 221 |
'init':self.VL53L0X_init, |
| 222 |
'read':self.VL53L0X_all, |
| 223 |
'fields':['mm'], |
| 224 |
'min':[0], |
| 225 |
'max':[1000], |
| 226 |
}, |
| 227 |
0x5A:{ |
| 228 |
'name':'MLX90614 Passive IR thermometer', |
| 229 |
'init':self.MLX90614_init, |
| 230 |
'read':self.MLX90614_all, |
| 231 |
'fields':['TEMP'], |
| 232 |
'min':[0], |
| 233 |
'max':[350]} |
| 234 |
} |
| 235 |
self.controllers={ |
| 236 |
0x62:{ |
| 237 |
'name':'MCP4725 DAC', |
| 238 |
'init':self.MCP4725_init, |
| 239 |
'write':[['CH0',0,4095,0,self.MCP4725_set]], |
| 240 |
}, |
| 241 |
} |
| 242 |
|
| 243 |
self.special={ |
| 244 |
0x40:{ |
| 245 |
'name':'PCA9685 PWM', |
| 246 |
'init':self.PCA9685_init, |
| 247 |
'write':[['Channel 1',0,180,90,functools.partial(self.PCA9685_set,1)], #name, start, stop, default, function |
| 248 |
['Channel 2',0,180,90,functools.partial(self.PCA9685_set,2)], |
| 249 |
['Channel 3',0,180,90,functools.partial(self.PCA9685_set,3)], |
| 250 |
['Channel 4',0,180,90,functools.partial(self.PCA9685_set,4)], |
| 251 |
], |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
for a in self.sensors: |
| 256 |
self.sensors[a]['type'] = 'input' |
| 257 |
|
| 258 |
self.I2C = I2C |
| 259 |
self.I2CWriteBulk = I2C.writeBulk |
| 260 |
self.I2CReadBulk = I2C.readBulk |
| 261 |
self.I2CScan = I2C.scan |
| 262 |
|
| 263 |
''' |
| 264 |
def I2CScan(self): |
| 265 |
def I2CWriteBulk(self,address,bytestream): |
| 266 |
def I2CReadBulk(self,address,register,total): |
| 267 |
''' |
| 268 |
class KalmanFilter(object): |
| 269 |
''' |
| 270 |
Credits:http://scottlobdell.me/2014/08/kalman-filtering-python-reading-sensor-input/ |
| 271 |
''' |
| 272 |
def __init__(self, var, est,initial_values): #var = process variance. est = estimated measurement var |
| 273 |
self.var = np.array(var) |
| 274 |
self.est = np.array(est) |
| 275 |
self.posteri_estimate = np.array(initial_values) |
| 276 |
self.posteri_error_estimate = np.ones(len(var),dtype=np.float16) |
| 277 |
|
| 278 |
def input(self, vals): |
| 279 |
vals = np.array(vals) |
| 280 |
priori_estimate = self.posteri_estimate |
| 281 |
priori_error_estimate = self.posteri_error_estimate + self.var |
| 282 |
|
| 283 |
blending_factor = priori_error_estimate / (priori_error_estimate + self.est) |
| 284 |
self.posteri_estimate = priori_estimate + blending_factor * (vals - priori_estimate) |
| 285 |
self.posteri_error_estimate = (1 - blending_factor) * priori_error_estimate |
| 286 |
|
| 287 |
def output(self): |
| 288 |
return self.posteri_estimate |
| 289 |
|
| 290 |
|
| 291 |
MPU6050_kalman = 0 |
| 292 |
def MPU6050_init(self): |
| 293 |
self.I2CWriteBulk(0x68,[0x1B,0<<3]) #Gyro Range . 250 |
| 294 |
self.I2CWriteBulk(0x68,[0x1C,0<<3]) #Accelerometer Range. 2 |
| 295 |
self.I2CWriteBulk(0x68,[0x6B, 0x00]) #poweron |
| 296 |
|
| 297 |
def MPU6050_gyro_range(self,val): |
| 298 |
self.I2CWriteBulk(0x68,[0x1B,val<<3]) #Gyro Range . 250,500,1000,2000 -> 0,1,2,3 -> shift left by 3 positions |
| 299 |
|
| 300 |
def MPU6050_accel_range(self,val): |
| 301 |
print(val) |
| 302 |
self.I2CWriteBulk(0x68,[0x1C,val<<3]) #Accelerometer Range. 2,4,8,16 -> 0,1,2,3 -> shift left by 3 positions |
| 303 |
|
| 304 |
def MPU6050_kalman_set(self,val): |
| 305 |
if not val: |
| 306 |
self.MPU6050_kalman = 0 |
| 307 |
return |
| 308 |
noise=[] |
| 309 |
for a in range(50): |
| 310 |
noise.append(np.array(self.MPU6050_all(disableKalman=True))) |
| 311 |
noise = np.array(noise) |
| 312 |
self.MPU6050_kalman = self.KalmanFilter(1e6*np.ones(noise.shape[1])/(10**val), np.std(noise,0)**2, noise[-1]) |
| 313 |
|
| 314 |
|
| 315 |
def MPU6050_accel(self): |
| 316 |
b = self.I2CReadBulk(0x68, 0x3B ,6) |
| 317 |
if b is None:return None |
| 318 |
if None not in b: |
| 319 |
return [(b[x*2+1]<<8)|b[x*2] for x in range(3)] #X,Y,Z |
| 320 |
|
| 321 |
def MPU6050_gyro(self): |
| 322 |
b = self.I2CReadBulk(0x68, 0x3B+6 ,6) |
| 323 |
if b is None:return None |
| 324 |
if None not in b: |
| 325 |
return [(b[x*2+1]<<8)|b[x*2] for x in range(3)] #X,Y,Z |
| 326 |
|
| 327 |
def MPU6050_all(self,disableKalman=False): |
| 328 |
''' |
| 329 |
returns a 7 element list. Ax,Ay,Az,T,Gx,Gy,Gz |
| 330 |
returns None if communication timed out with I2C sensor |
| 331 |
disableKalman can be set to True if Kalman was previously enabled. |
| 332 |
''' |
| 333 |
b = self.I2CReadBulk(0x68, 0x3B ,14) |
| 334 |
if not b:return None |
| 335 |
if None not in b: |
| 336 |
if len(b)!=14:return None |
| 337 |
if (not self.MPU6050_kalman) or disableKalman: |
| 338 |
return [ np.int16((b[x*2]<<8)|b[x*2+1]) for x in range(7) ] #Ax,Ay,Az, Temp, Gx, Gy,Gz |
| 339 |
else: |
| 340 |
self.MPU6050_kalman.input([ np.int16((b[x*2]<<8)|b[x*2+1]) for x in range(7) ]) |
| 341 |
return self.MPU6050_kalman.output() |
| 342 |
|
| 343 |
|
| 344 |
######## AK8963 magnetometer attacched to MPU925x ####### |
| 345 |
AK8963_ADDRESS =0x0C |
| 346 |
_AK8963_CNTL = 0x0A |
| 347 |
def AK8963_init(self): |
| 348 |
self.I2CWriteBulk(self.AK8963_ADDRESS,[self._AK8963_CNTL,0]) #power down mag |
| 349 |
self.I2CWriteBulk(self.AK8963_ADDRESS,[self._AK8963_CNTL,(1<<4)|6]) #mode (0=14bits,1=16bits) <<4 | (2=8Hz , 6=100Hz) |
| 350 |
def AK8963_all(self,disableKalman=False): |
| 351 |
vals,tmt=self.I2CReadBulk(self.AK8963_ADDRESS,0x03,7) #6+1 . 1(ST2) should not have bit 4 (0x8) true. It's ideally 16 . overflow bit |
| 352 |
if tmt:return None |
| 353 |
ax,ay,az = struct.unpack('hhh',bytes(vals[:6])) |
| 354 |
if not vals[6]&0x08:return [ax,ay,az] |
| 355 |
else: return None |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
####### BMP280 ################### |
| 360 |
# https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf |
| 361 |
## Partly from https://github.com/farmerkeith/BMP280-library/blob/master/farmerkeith_BMP280.cpp |
| 362 |
BMP280_ADDRESS = 118 |
| 363 |
BMP280_REG_CONTROL = 0xF4 |
| 364 |
BMP280_REG_RESULT = 0xF6 |
| 365 |
BMP280_HUMIDITY_ENABLED = False |
| 366 |
_BMP280_humidity_calib = [0]*6 |
| 367 |
BMP280_oversampling = 0 |
| 368 |
_BMP280_PRESSURE_MIN_HPA = 0 |
| 369 |
_BMP280_PRESSURE_MAX_HPA = 1600 |
| 370 |
_BMP280_sea_level_pressure = 1013.25 #for calibration.. from circuitpython library |
| 371 |
def BMP280_init(self): |
| 372 |
b = self.I2CWriteBulk(self.BMP280_ADDRESS,[0xE0,0xB6]) #reset |
| 373 |
time.sleep(0.1) |
| 374 |
self.BMP280_HUMIDITY_ENABLED = False |
| 375 |
b = self.I2CReadBulk(self.BMP280_ADDRESS, 0xD0 ,1) |
| 376 |
print(b) |
| 377 |
if b is None:return None |
| 378 |
b = b[0] |
| 379 |
if b in [0x58,0x56,0x57]: |
| 380 |
print('BMP280. ID:',b) |
| 381 |
elif b==0x60: |
| 382 |
self.BMP280_HUMIDITY_ENABLED = True |
| 383 |
print('BME280 . includes humidity') |
| 384 |
else: |
| 385 |
print('ID unknown',b) |
| 386 |
# get calibration data |
| 387 |
b = self.I2CReadBulk(self.BMP280_ADDRESS, 0x88 ,24) #24 bytes containing calibration data |
| 388 |
coeff = list(struct.unpack('<HhhHhhhhhhhh', bytes(b))) |
| 389 |
coeff = [float(i) for i in coeff] |
| 390 |
self._BMP280_temp_calib = coeff[:3] |
| 391 |
self._BMP280_pressure_calib = coeff[3:] |
| 392 |
self._BMP280_t_fine = 0. |
| 393 |
|
| 394 |
|
| 395 |
if self.BMP280_HUMIDITY_ENABLED: |
| 396 |
self.I2CWriteBulk(self.BMP280_ADDRESS, [0xF2,0b101]) #ctrl_hum. oversampling x 16 |
| 397 |
#humidity calibration read |
| 398 |
self._BMP280_humidity_calib = [0]*6 |
| 399 |
self._BMP280_humidity_calib[0] = self.I2CReadBulk(self.BMP280_ADDRESS,0xA1,1)[0]#H1 |
| 400 |
coeff = self.I2CReadBulk(self.BMP280_ADDRESS,0xE1, 7) |
| 401 |
coeff = list(struct.unpack('<hBbBbb', bytes(coeff))) |
| 402 |
self._BMP280_humidity_calib[1] = float(coeff[0]) |
| 403 |
self._BMP280_humidity_calib[2] = float(coeff[1]) |
| 404 |
self._BMP280_humidity_calib[3] = float((coeff[2] << 4) | (coeff[3] & 0xF)) |
| 405 |
self._BMP280_humidity_calib[4] = float((coeff[4] << 4) | (coeff[3] >> 4)) |
| 406 |
self._BMP280_humidity_calib[5] = float(coeff[5]) |
| 407 |
|
| 408 |
self.I2CWriteBulk(self.BMP280_ADDRESS, [0xF4,0xFF]) # ctrl_meas (oversampling of pressure, temperature) |
| 409 |
|
| 410 |
|
| 411 |
def _BMP280_calcTemperature(self,adc_t): |
| 412 |
v1 = (adc_t / 16384.0 - self._BMP280_temp_calib[0] / 1024.0) * self._BMP280_temp_calib[1] |
| 413 |
v2 = ((adc_t / 131072.0 - self._BMP280_temp_calib[0] / 8192.0) * ( adc_t / 131072.0 - self._BMP280_temp_calib[0] / 8192.0)) * self._BMP280_temp_calib[2] |
| 414 |
self._BMP280_t_fine = int(v1+v2) |
| 415 |
return (v1+v2) / 5120.0 #actual temperature. |
| 416 |
|
| 417 |
def _BMP280_calcPressure(self,adc_p,adc_t): |
| 418 |
self._BMP280_calcTemperature(adc_t) #t_fine has been set now. |
| 419 |
# Algorithm from the BMP280 driver. adapted from adafruit adaptation of |
| 420 |
# https://github.com/BoschSensortec/BMP280_driver/blob/master/bmp280.c |
| 421 |
var1 = self._BMP280_t_fine / 2.0 - 64000.0 |
| 422 |
var2 = var1 * var1 * self._BMP280_pressure_calib[5] / 32768.0 |
| 423 |
var2 = var2 + var1 * self._BMP280_pressure_calib[4] * 2.0 |
| 424 |
var2 = var2 / 4.0 + self._BMP280_pressure_calib[3] * 65536.0 |
| 425 |
var3 = self._BMP280_pressure_calib[2] * var1 * var1 / 524288.0 |
| 426 |
var1 = (var3 + self._BMP280_pressure_calib[1] * var1) / 524288.0 |
| 427 |
var1 = (1.0 + var1 / 32768.0) * self._BMP280_pressure_calib[0] |
| 428 |
if not var1: |
| 429 |
return _BMP280_PRESSURE_MIN_HPA |
| 430 |
pressure = 1048576.0 - adc_p |
| 431 |
pressure = ((pressure - var2 / 4096.0) * 6250.0) / var1 |
| 432 |
var1 = self._BMP280_pressure_calib[8] * pressure * pressure / 2147483648.0 |
| 433 |
var2 = pressure * self._BMP280_pressure_calib[7] / 32768.0 |
| 434 |
pressure = pressure + (var1 + var2 + self._BMP280_pressure_calib[6]) / 16.0 |
| 435 |
pressure /= 100 |
| 436 |
if pressure < self._BMP280_PRESSURE_MIN_HPA: |
| 437 |
return self._BMP280_PRESSURE_MIN_HPA |
| 438 |
if pressure > self._BMP280_PRESSURE_MAX_HPA: |
| 439 |
return self._BMP280_PRESSURE_MAX_HPA |
| 440 |
return pressure |
| 441 |
|
| 442 |
def _BMP280_calcHumidity(self,adc_h,adc_t): |
| 443 |
self._BMP280_calcTemperature(adc_t) #t fine set. |
| 444 |
var1 = float(self._BMP280_t_fine) - 76800.0 |
| 445 |
var2 = (self._BMP280_humidity_calib[3] * 64.0 + (self._BMP280_humidity_calib[4] / 16384.0) * var1) |
| 446 |
var3 = adc_h - var2 |
| 447 |
var4 = self._BMP280_humidity_calib[1] / 65536.0 |
| 448 |
var5 = (1.0 + (self._BMP280_humidity_calib[2] / 67108864.0) * var1) |
| 449 |
var6 = 1.0 + (self._BMP280_humidity_calib[5] / 67108864.0) * var1 * var5 |
| 450 |
var6 = var3 * var4 * (var5 * var6) |
| 451 |
humidity = var6 * (1.0 - self._BMP280_humidity_calib[0] * var6 / 524288.0) |
| 452 |
if humidity > 100: |
| 453 |
return 100 |
| 454 |
if humidity < 0: |
| 455 |
return 0 |
| 456 |
|
| 457 |
return humidity |
| 458 |
|
| 459 |
def BMP280_all(self): |
| 460 |
if self.BMP280_HUMIDITY_ENABLED: |
| 461 |
data = self.I2CReadBulk(self.BMP280_ADDRESS, 0xF7,8) |
| 462 |
else: |
| 463 |
data = self.I2CReadBulk(self.BMP280_ADDRESS, 0xF7,6) |
| 464 |
if data is None:return None |
| 465 |
if None not in data: |
| 466 |
# Convert pressure and temperature data to 19-bits |
| 467 |
adc_p = (((data[0] & 0xFF) * 65536.) + ((data[1] & 0xFF) * 256.) + (data[2] & 0xF0)) / 16. |
| 468 |
adc_t = (((data[3] & 0xFF) * 65536.) + ((data[4] & 0xFF) * 256.) + (data[5] & 0xF0)) / 16. |
| 469 |
if self.BMP280_HUMIDITY_ENABLED: |
| 470 |
adc_h = (data[6] * 256.) + data[7] |
| 471 |
return [self._BMP280_calcPressure(adc_p,adc_t), self._BMP280_calcTemperature(adc_t), self._BMP280_calcHumidity(adc_h,adc_t)] |
| 472 |
else: |
| 473 |
return [self._BMP280_calcPressure(adc_p,adc_t), self._BMP280_calcTemperature(adc_t), 0] |
| 474 |
|
| 475 |
return None |
| 476 |
|
| 477 |
####### MS5611 Altimeter ################### |
| 478 |
MS5611_ADDRESS = 119 |
| 479 |
|
| 480 |
def MS5611_init(self): |
| 481 |
self.I2CWriteBulk(self.MS5611_ADDRESS, [0x1E]) # reset |
| 482 |
time.sleep(0.5) |
| 483 |
self._MS5611_calib = np.zeros(6) |
| 484 |
|
| 485 |
#calibration data. |
| 486 |
#pressure gain, offset . T coeff of P gain, offset. Ref temp. T coeff of T. all unsigned shorts. |
| 487 |
b = self.I2CReadBulk(self.MS5611_ADDRESS, 0xA2 ,2) |
| 488 |
if b is None:return None |
| 489 |
self._MS5611_calib[0] = struct.unpack('!H', bytes(b))[0] |
| 490 |
b = self.I2CReadBulk(self.MS5611_ADDRESS, 0xA4 ,2) |
| 491 |
self._MS5611_calib[1] = struct.unpack('!H', bytes(b))[0] |
| 492 |
b = self.I2CReadBulk(self.MS5611_ADDRESS, 0xA6 ,2) |
| 493 |
self._MS5611_calib[2] = struct.unpack('!H', bytes(b))[0] |
| 494 |
b = self.I2CReadBulk(self.MS5611_ADDRESS, 0xA8 ,2) |
| 495 |
self._MS5611_calib[3] = struct.unpack('!H', bytes(b))[0] |
| 496 |
b = self.I2CReadBulk(self.MS5611_ADDRESS, 0xAA ,2) |
| 497 |
self._MS5611_calib[4] = struct.unpack('!H', bytes(b))[0] |
| 498 |
b = self.I2CReadBulk(self.MS5611_ADDRESS, 0xAC ,2) |
| 499 |
self._MS5611_calib[5] = struct.unpack('!H', bytes(b))[0] |
| 500 |
print('Calibration for MS5611:',self._MS5611_calib) |
| 501 |
|
| 502 |
|
| 503 |
def MS5611_all(self): |
| 504 |
self.I2CWriteBulk(self.MS5611_ADDRESS, [0x48]) # 0x48 Pressure conversion(OSR = 4096) command |
| 505 |
time.sleep(0.01) #10mS |
| 506 |
b = self.I2CReadBulk(self.MS5611_ADDRESS, 0x00 ,3) #data. |
| 507 |
D1 = b[0]*65536 + b[1]*256 + b[2] #msb2, msb1, lsb |
| 508 |
|
| 509 |
self.I2CWriteBulk(self.MS5611_ADDRESS, [0x58]) # 0x58 Temperature conversion(OSR = 4096) command |
| 510 |
time.sleep(0.01) |
| 511 |
b = self.I2CReadBulk(self.MS5611_ADDRESS, 0x00 ,3) #data. |
| 512 |
D2 = b[0]*65536 + b[1]*256 + b[2] #msb2, msb1, lsb |
| 513 |
|
| 514 |
|
| 515 |
dT = D2 - self._MS5611_calib[4] * 256 |
| 516 |
TEMP = 2000 + dT * self._MS5611_calib[5] / 8388608 |
| 517 |
OFF = self._MS5611_calib[1] * 65536 + (self._MS5611_calib[3] * dT) / 128 |
| 518 |
SENS = self._MS5611_calib[0] * 32768 + (self._MS5611_calib[2] * dT ) / 256 |
| 519 |
T2 = 0; OFF2 = 0; SENS2 = 0 |
| 520 |
if TEMP >= 2000 : |
| 521 |
T2 = 0 |
| 522 |
OFF2 = 0 |
| 523 |
SENS2 = 0 |
| 524 |
elif TEMP < 2000 : |
| 525 |
T2 = (dT * dT) / 2147483648 |
| 526 |
OFF2 = 5 * ((TEMP - 2000) * (TEMP - 2000)) / 2 |
| 527 |
SENS2 = 5 * ((TEMP - 2000) * (TEMP - 2000)) / 4 |
| 528 |
if TEMP < -1500 : |
| 529 |
OFF2 = OFF2 + 7 * ((TEMP + 1500) * (TEMP + 1500)) |
| 530 |
SENS2 = SENS2 + 11 * ((TEMP + 1500) * (TEMP + 1500)) / 2 |
| 531 |
|
| 532 |
TEMP = TEMP - T2 |
| 533 |
OFF = OFF - OFF2 |
| 534 |
SENS = SENS - SENS2 |
| 535 |
pressure = ((((D1 * SENS) / 2097152) - OFF) / 32768.0) / 100.0 |
| 536 |
cTemp = TEMP / 100.0 |
| 537 |
return [pressure,cTemp,0] |
| 538 |
|
| 539 |
|
| 540 |
### INA3221 3 channel , high side current sensor ############# |
| 541 |
INA3221_ADDRESS = 0x41 |
| 542 |
_INA3221_REG_CONFIG = 0x0 |
| 543 |
_INA3221_SHUNT_RESISTOR_VALUE = 0.1 |
| 544 |
_INA3221_REG_SHUNTVOLTAGE = 0x01 |
| 545 |
_INA3221_REG_BUSVOLTAGE = 0x02 |
| 546 |
|
| 547 |
def INA3221_init(self): |
| 548 |
self.I2CWriteBulk(self.INA3221_ADDRESS,[self._INA3221_REG_CONFIG, 0b01110101, 0b00100111 ]) #cont shunt. |
| 549 |
|
| 550 |
def INA3221_all(self): |
| 551 |
I = [0.,0.,0.] |
| 552 |
b = self.I2CReadBulk(self.INA3221_ADDRESS,self._INA3221_REG_SHUNTVOLTAGE , 2) |
| 553 |
if b is None:return None |
| 554 |
b[1]&=0xF8; I[0] = struct.unpack('!h',bytes(b))[0] |
| 555 |
b = self.I2CReadBulk(self.INA3221_ADDRESS,self._INA3221_REG_SHUNTVOLTAGE +2 , 2) |
| 556 |
if b is None:return None |
| 557 |
b[1]&=0xF8; I[1] = struct.unpack('!h',bytes(b))[0] |
| 558 |
b = self.I2CReadBulk(self.INA3221_ADDRESS,self._INA3221_REG_SHUNTVOLTAGE +4 , 2) |
| 559 |
if b is None:return None |
| 560 |
b[1]&=0xF8; I[2] = struct.unpack('!h',bytes(b))[0] |
| 561 |
return [0.005*I[0]/self._INA3221_SHUNT_RESISTOR_VALUE,0.005*I[1]/self._INA3221_SHUNT_RESISTOR_VALUE,0.005*I[2]/self._INA3221_SHUNT_RESISTOR_VALUE] |
| 562 |
|
| 563 |
|
| 564 |
TSL_GAIN = 0x00 # 0x00=1x , 0x10 = 16x |
| 565 |
TSL_TIMING = 0x00 # 0x00=3 mS , 0x01 = 101 mS, 0x02 = 402mS |
| 566 |
def TSL2561_init(self): |
| 567 |
self.I2CWriteBulk(0x39,[0x80 , 0x03 ]) #poweron |
| 568 |
self.I2CWriteBulk(0x39,[0x80 | 0x01, self.TSL_GAIN|self.TSL_TIMING ]) |
| 569 |
return self.TSL2561_all() |
| 570 |
|
| 571 |
def TSL2561_gain(self,gain): |
| 572 |
self.TSL_GAIN = gain<<4 |
| 573 |
self.TSL2561_config(self.TSL_GAIN,self.TSL_TIMING) |
| 574 |
|
| 575 |
def TSL2561_timing(self,timing): |
| 576 |
self.TSL_TIMING = timing |
| 577 |
self.TSL2561_config(self.TSL_GAIN,self.TSL_TIMING) |
| 578 |
|
| 579 |
def TSL2561_rate(self,timing): |
| 580 |
self.TSL_TIMING = timing |
| 581 |
self.TSL2561_config(self.TSL_GAIN,self.TSL_TIMING) |
| 582 |
|
| 583 |
def TSL2561_config(self,gain,timing): |
| 584 |
self.I2CWriteBulk(0x39,[0x80 | 0x01, gain|timing]) #Timing register 0x01. gain[1x,16x] | timing[13mS,100mS,400mS] |
| 585 |
|
| 586 |
def TSL2561_all(self): |
| 587 |
''' |
| 588 |
returns a 2 element list. total,IR |
| 589 |
returns None if communication timed out with I2C sensor |
| 590 |
''' |
| 591 |
b = self.I2CReadBulk(0x39,0x80 | 0x20 | 0x0C ,4) |
| 592 |
if b is None:return None |
| 593 |
if None not in b: |
| 594 |
return [ (b[x*2+1]<<8)|b[x*2] for x in range(2) ] #total, IR |
| 595 |
|
| 596 |
def MLX90614_init(self): |
| 597 |
pass |
| 598 |
|
| 599 |
def MLX90614_all(self): |
| 600 |
''' |
| 601 |
return a single element list. None if failed |
| 602 |
''' |
| 603 |
vals = self.I2CReadBulk(0x5A, 0x07 ,3) |
| 604 |
if vals is None:return None |
| 605 |
if vals: |
| 606 |
if len(vals)==3: |
| 607 |
return [((((vals[1]&0x007f)<<8)+vals[0])*0.02)-0.01 - 273.15] |
| 608 |
else: |
| 609 |
return None |
| 610 |
else: |
| 611 |
return None |
| 612 |
|
| 613 |
def MCP4725_init(self): |
| 614 |
pass |
| 615 |
|
| 616 |
def MCP4725_set(self,val): |
| 617 |
''' |
| 618 |
Set the DAC value. 0 - 4095 |
| 619 |
''' |
| 620 |
self.I2CWriteBulk(0x62, [0x40,(val>>4)&0xFF,(val&0xF)<<4]) |
| 621 |
|
| 622 |
####################### HMC5883L MAGNETOMETER ############### |
| 623 |
|
| 624 |
HMC5883L_ADDRESS = 0x1E |
| 625 |
HMC_CONFA=0x00 |
| 626 |
HMC_CONFB=0x01 |
| 627 |
HMC_MODE=0x02 |
| 628 |
HMC_STATUS=0x09 |
| 629 |
|
| 630 |
#--------CONFA register bits. 0x00----------- |
| 631 |
HMCSamplesToAverage=0 |
| 632 |
HMCSamplesToAverage_choices=[1,2,4,8] |
| 633 |
|
| 634 |
HMCDataOutputRate=6 |
| 635 |
HMCDataOutputRate_choices=[0.75,1.5,3,7.5,15,30,75] |
| 636 |
|
| 637 |
HMCMeasurementConf=0 |
| 638 |
|
| 639 |
#--------CONFB register bits. 0x01----------- |
| 640 |
HMCGainValue = 7 #least sensitive |
| 641 |
HMCGain_choices = [8,7,6,5,4,3,2,1] |
| 642 |
HMCGainScaling=[1370.,1090.,820.,660.,440.,390.,330.,230.] |
| 643 |
|
| 644 |
def HMC5883L_init(self): |
| 645 |
self.__writeHMCCONFA__() |
| 646 |
self.__writeHMCCONFB__() |
| 647 |
self.I2CWriteBulk(self.HMC5883L_ADDRESS,[self.HMC_MODE,0]) #enable continuous measurement mode |
| 648 |
|
| 649 |
def __writeHMCCONFB__(self): |
| 650 |
self.I2CWriteBulk(self.HMC5883L_ADDRESS,[self.HMC_CONFB,self.HMCGainValue<<5]) #set gain |
| 651 |
|
| 652 |
def __writeHMCCONFA__(self): |
| 653 |
self.I2CWriteBulk(self.HMC5883L_ADDRESS,[self.HMC_CONFA,(self.HMCDataOutputRate<<2)|(self.HMCSamplesToAverage<<5)|(self.HMCMeasurementConf)]) |
| 654 |
|
| 655 |
def HMC5883L_getVals(self,addr,bytes): |
| 656 |
vals = self.I2C.readBulk(self.HMC5883L_ADDRESS,addr,bytes) |
| 657 |
return vals |
| 658 |
|
| 659 |
def HMC5883L_all(self): |
| 660 |
vals=self.HMC5883L_getVals(0x03,6) |
| 661 |
if vals: |
| 662 |
if len(vals)==6: |
| 663 |
return [np.int16(vals[a*2]&0xff<<8|vals[a*2+1]&0xff)/self.HMCGainScaling[self.HMCGainValue] for a in range(3)] |
| 664 |
else: |
| 665 |
return False |
| 666 |
else: |
| 667 |
return False |
| 668 |
|
| 669 |
PCA9685_address = 64 |
| 670 |
def PCA9685_init(self): |
| 671 |
prescale_val = int((25000000.0 / 4096 / 60.) - 1) # default clock at 25MHz |
| 672 |
#self.I2CWriteBulk(self.PCA9685_address, [0x00,0x10]) #MODE 1 , Sleep |
| 673 |
print('clock set to,',prescale_val) |
| 674 |
self.I2CWriteBulk(self.PCA9685_address, [0xFE,prescale_val]) #PRESCALE , prescale value |
| 675 |
self.I2CWriteBulk(self.PCA9685_address, [0x00,0x80]) #MODE 1 , restart |
| 676 |
self.I2CWriteBulk(self.PCA9685_address, [0x01,0x04]) #MODE 2 , Totem Pole |
| 677 |
|
| 678 |
pass |
| 679 |
|
| 680 |
CH0 = 0x6 #LED0 start register |
| 681 |
CH0_ON_L = 0x6 #channel0 output and brightness control byte 0 |
| 682 |
CH0_ON_H = 0x7 #channel0 output and brightness control byte 1 |
| 683 |
CH0_OFF_L = 0x8 #channel0 output and brightness control byte 2 |
| 684 |
CH0_OFF_H = 0x9 #channel0 output and brightness control byte 3 |
| 685 |
CHAN_WIDTH = 4 |
| 686 |
def PCA9685_set(self,chan,angle): |
| 687 |
''' |
| 688 |
chan: 1-16 |
| 689 |
Set the servo angle for SG90: angle(0 - 180) |
| 690 |
''' |
| 691 |
Min = 180 |
| 692 |
Max = 650 |
| 693 |
val = int((( Max-Min ) * ( angle/180. ))+Min) |
| 694 |
print(chan,angle,val) |
| 695 |
self.I2CWriteBulk(self.PCA9685_address, [self.CH0_ON_L + self.CHAN_WIDTH * (chan - 1),0]) # |
| 696 |
self.I2CWriteBulk(self.PCA9685_address, [self.CH0_ON_H + self.CHAN_WIDTH * (chan - 1),0]) # Turn on immediately. At 0. |
| 697 |
self.I2CWriteBulk(self.PCA9685_address, [self.CH0_OFF_L + self.CHAN_WIDTH * (chan - 1),val&0xFF]) #Turn off after val width 0-4095 |
| 698 |
self.I2CWriteBulk(self.PCA9685_address, [self.CH0_OFF_H + self.CHAN_WIDTH * (chan - 1),(val>>8)&0xFF]) |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
def VL53L0X_decode_vcsel_period(self,vcsel_period_reg): |
| 703 |
vcsel_period_pclks = (vcsel_period_reg + 1) << 1; |
| 704 |
return vcsel_period_pclks |
| 705 |
|
| 706 |
VL53L0X_REG_IDENTIFICATION_MODEL_ID = 0x00c0 |
| 707 |
VL53L0X_REG_IDENTIFICATION_REVISION_ID = 0x00c2 |
| 708 |
VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD = 0x0050 |
| 709 |
VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD = 0x0070 |
| 710 |
VL53L0X_REG_SYSRANGE_START = 0x000 |
| 711 |
|
| 712 |
VL53L0X_REG_RESULT_INTERRUPT_STATUS = 0x0013 |
| 713 |
VL53L0X_REG_RESULT_RANGE_STATUS = 0x0014 |
| 714 |
|
| 715 |
|
| 716 |
VL53L0X_address = 0x29 #41 |
| 717 |
|
| 718 |
def VL53L0X_init(self): |
| 719 |
val1 = self.I2CReadBulk(self.VL53L0X_address, self.VL53L0X_REG_IDENTIFICATION_REVISION_ID,1)[0] |
| 720 |
#print ("Revision ID: " + hex(val1)) |
| 721 |
val1 = self.I2CReadBulk(self.VL53L0X_address, self.VL53L0X_REG_IDENTIFICATION_MODEL_ID,1)[0] |
| 722 |
#print ("Device ID: " + hex(val1)) |
| 723 |
val1 = self.I2CReadBulk(self.VL53L0X_address, self.VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD,1)[0] |
| 724 |
#print ("PRE_RANGE_CONFIG_VCSEL_PERIOD=" + hex(val1) + " decode: " + str(self.VL53L0X_decode_vcsel_period(val1))) |
| 725 |
val1 = self.I2CReadBulk(self.VL53L0X_address, self.VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD,1)[0] |
| 726 |
#print ("FINAL_RANGE_CONFIG_VCSEL_PERIOD=" + hex(val1) + " decode: " + str(self.VL53L0X_decode_vcsel_period(val1))) |
| 727 |
|
| 728 |
def VL53L0X_all(self): |
| 729 |
val1 = self.I2CWriteBulk(self.VL53L0X_address, [self.VL53L0X_REG_SYSRANGE_START, 0x01]) |
| 730 |
cnt = 0 |
| 731 |
while (cnt < 100): # 1 second waiting time max |
| 732 |
time.sleep(0.010) |
| 733 |
val = self.I2CReadBulk(self.VL53L0X_address, self.VL53L0X_REG_RESULT_RANGE_STATUS,1)[0] |
| 734 |
if (val & 0x01): |
| 735 |
break |
| 736 |
cnt += 1 |
| 737 |
if(cnt == 100): #timeout |
| 738 |
return None |
| 739 |
if not (val & 0x01): # Not ready. |
| 740 |
return None |
| 741 |
|
| 742 |
# Status = VL53L0X_ReadMulti(Dev, 0x14, localBuffer, 12); |
| 743 |
data = self.I2CReadBulk(self.VL53L0X_address, 0x14, 12) |
| 744 |
#print ("ambient count " + str(makeuint16(data[7], data[6]))) |
| 745 |
#print ("signal count " + str(makeuint16(data[9], data[8]))) |
| 746 |
d = makeuint16(data[11], data[10]) |
| 747 |
|
| 748 |
DeviceRangeStatusInternal = ((data[0] & 0x78) >> 3) |
| 749 |
#print (DeviceRangeStatusInternal) |
| 750 |
|
| 751 |
return [d] |
| 752 |
|
| 753 |
|
| 754 |
## ADS1115 |
| 755 |
REG_POINTER_MASK = 0x3 |
| 756 |
REG_POINTER_CONVERT = 0 |
| 757 |
REG_POINTER_CONFIG = 1 |
| 758 |
REG_POINTER_LOWTHRESH=2 |
| 759 |
REG_POINTER_HITHRESH =3 |
| 760 |
|
| 761 |
REG_CONFIG_OS_MASK =0x8000 |
| 762 |
REG_CONFIG_OS_SINGLE =0x8000 |
| 763 |
REG_CONFIG_OS_BUSY =0x0000 |
| 764 |
REG_CONFIG_OS_NOTBUSY =0x8000 |
| 765 |
|
| 766 |
REG_CONFIG_MUX_MASK =0x7000 |
| 767 |
REG_CONFIG_MUX_DIFF_0_1 =0x0000 # Differential P = AIN0, N = AIN1 =default) |
| 768 |
REG_CONFIG_MUX_DIFF_0_3 =0x1000 # Differential P = AIN0, N = AIN3 |
| 769 |
REG_CONFIG_MUX_DIFF_1_3 =0x2000 # Differential P = AIN1, N = AIN3 |
| 770 |
REG_CONFIG_MUX_DIFF_2_3 =0x3000 # Differential P = AIN2, N = AIN3 |
| 771 |
REG_CONFIG_MUX_SINGLE_0 =0x4000 # Single-ended AIN0 |
| 772 |
REG_CONFIG_MUX_SINGLE_1 =0x5000 # Single-ended AIN1 |
| 773 |
REG_CONFIG_MUX_SINGLE_2 =0x6000 # Single-ended AIN2 |
| 774 |
REG_CONFIG_MUX_SINGLE_3 =0x7000 # Single-ended AIN3 |
| 775 |
|
| 776 |
REG_CONFIG_PGA_MASK =0x0E00 #bits 11:9 |
| 777 |
REG_CONFIG_PGA_6_144V =(0<<9) # +/-6.144V range = Gain 2/3 |
| 778 |
REG_CONFIG_PGA_4_096V =(1<<9) # +/-4.096V range = Gain 1 |
| 779 |
REG_CONFIG_PGA_2_048V =(2<<9) # +/-2.048V range = Gain 2 =default) |
| 780 |
REG_CONFIG_PGA_1_024V =(3<<9) # +/-1.024V range = Gain 4 |
| 781 |
REG_CONFIG_PGA_0_512V =(4<<9) # +/-0.512V range = Gain 8 |
| 782 |
REG_CONFIG_PGA_0_256V =(5<<9) # +/-0.256V range = Gain 16 |
| 783 |
|
| 784 |
REG_CONFIG_MODE_MASK =0x0100 #bit 8 |
| 785 |
REG_CONFIG_MODE_CONTIN =(0<<8) # Continuous conversion mode |
| 786 |
REG_CONFIG_MODE_SINGLE =(1<<8) # Power-down single-shot mode =default) |
| 787 |
|
| 788 |
REG_CONFIG_DR_MASK =0x00E0 |
| 789 |
REG_CONFIG_DR_8SPS =(0<<5) #8 SPS |
| 790 |
REG_CONFIG_DR_16SPS =(1<<5) #16 SPS |
| 791 |
REG_CONFIG_DR_32SPS =(2<<5) #32 SPS |
| 792 |
REG_CONFIG_DR_64SPS =(3<<5) #64 SPS |
| 793 |
REG_CONFIG_DR_128SPS =(4<<5) #128 SPS |
| 794 |
REG_CONFIG_DR_250SPS =(5<<5) #260 SPS |
| 795 |
REG_CONFIG_DR_475SPS =(6<<5) #475 SPS |
| 796 |
REG_CONFIG_DR_860SPS =(7<<5) #860 SPS |
| 797 |
|
| 798 |
REG_CONFIG_CMODE_MASK =0x0010 |
| 799 |
REG_CONFIG_CMODE_TRAD =0x0000 |
| 800 |
REG_CONFIG_CMODE_WINDOW =0x0010 |
| 801 |
|
| 802 |
REG_CONFIG_CPOL_MASK =0x0008 |
| 803 |
REG_CONFIG_CPOL_ACTVLOW =0x0000 |
| 804 |
REG_CONFIG_CPOL_ACTVHI =0x0008 |
| 805 |
|
| 806 |
REG_CONFIG_CLAT_MASK =0x0004 |
| 807 |
REG_CONFIG_CLAT_NONLAT =0x0000 |
| 808 |
REG_CONFIG_CLAT_LATCH =0x0004 |
| 809 |
|
| 810 |
REG_CONFIG_CQUE_MASK =0x0003 |
| 811 |
REG_CONFIG_CQUE_1CONV =0x0000 |
| 812 |
REG_CONFIG_CQUE_2CONV =0x0001 |
| 813 |
REG_CONFIG_CQUE_4CONV =0x0002 |
| 814 |
REG_CONFIG_CQUE_NONE =0x0003 |
| 815 |
ADS1115_gains = OrderedDict([('GAIN_TWOTHIRDS',REG_CONFIG_PGA_6_144V),('GAIN_ONE',REG_CONFIG_PGA_4_096V),('GAIN_TWO',REG_CONFIG_PGA_2_048V),('GAIN_FOUR',REG_CONFIG_PGA_1_024V),('GAIN_EIGHT',REG_CONFIG_PGA_0_512V),('GAIN_SIXTEEN',REG_CONFIG_PGA_0_256V)]) |
| 816 |
ADS1115_gain_scaling = OrderedDict([('GAIN_TWOTHIRDS',0.1875),('GAIN_ONE',0.125),('GAIN_TWO',0.0625),('GAIN_FOUR',0.03125),('GAIN_EIGHT',0.015625),('GAIN_SIXTEEN',0.0078125)]) |
| 817 |
ADS1115_scaling = 0.125 |
| 818 |
ADS1115_channels = OrderedDict([('UNI_0',0),('UNI_1',1),('UNI_2',2),('UNI_3',3),('DIFF_01','01'),('DIFF_23','23')]) |
| 819 |
ADS1115_rates = OrderedDict([(8,REG_CONFIG_DR_8SPS),(16,REG_CONFIG_DR_16SPS),(32,REG_CONFIG_DR_32SPS),(64,REG_CONFIG_DR_64SPS),(128,REG_CONFIG_DR_128SPS),(250,REG_CONFIG_DR_250SPS),(475,REG_CONFIG_DR_475SPS),(860,REG_CONFIG_DR_860SPS)]) #sampling data rate |
| 820 |
ADS1115_DATARATE = 250 #250SPS [ 8, 16, 32, 64, 128, 250, 475, 860 ] |
| 821 |
ADS1115_GAIN = REG_CONFIG_PGA_4_096V # +/-4.096V range = Gain 1 . [+-6, +-4, +-2, +-1, +-0.5, +- 0.25] |
| 822 |
ADS1115_CHANNEL = 0 # ref: type_selection |
| 823 |
ADS1115_ADDRESS = 0x48 |
| 824 |
|
| 825 |
def ADS1115_init(self): |
| 826 |
self.I2CWriteBulk(self.ADS1115_ADDRESS,[0x80 , 0x03 ]) #poweron |
| 827 |
|
| 828 |
def ADS1115_gain(self,gain): |
| 829 |
''' |
| 830 |
options : 'GAIN_TWOTHIRDS','GAIN_ONE','GAIN_TWO','GAIN_FOUR','GAIN_EIGHT','GAIN_SIXTEEN' |
| 831 |
''' |
| 832 |
print('setting gain:',str(gain)) |
| 833 |
if(type(gain) == int): #From the UI selectors which return index |
| 834 |
self.ADS1115_GAIN = list(self.ADS1115_gains.items())[gain][1] |
| 835 |
print('set gain with index selection:',self.ADS1115_GAIN) |
| 836 |
self.ADS1115_scaling = list(self.ADS1115_gain_scaling.items())[gain][1] |
| 837 |
print('Scaling factor:',self.ADS1115_scaling) |
| 838 |
else: |
| 839 |
self.ADS1115_GAIN = self.ADS1115_gains.get(gain,self.REG_CONFIG_PGA_4_096V) |
| 840 |
self.ADS1115_scaling = self.ADS1115_gain_scaling.get(gain) |
| 841 |
print('set gain type B:',str(gain),self.ADS1115_GAIN, self.ADS1115_scaling) |
| 842 |
|
| 843 |
|
| 844 |
def ADS1115_channel(self,channel): |
| 845 |
''' |
| 846 |
options 'UNI_0','UNI_1','UNI_2','UNI_3','DIFF_01','DIFF_23' |
| 847 |
''' |
| 848 |
self.ADS1115_CHANNEL = self.ADS1115_channels.get(channel,0) |
| 849 |
|
| 850 |
def ADS1115_rate(self,rate): |
| 851 |
''' |
| 852 |
data rate options 8,16,32,64,128,250,475,860 SPS |
| 853 |
''' |
| 854 |
self.ADS1115_DATARATE = int(rate) # #default 250 sps |
| 855 |
|
| 856 |
def ADS1115_read(self): |
| 857 |
''' |
| 858 |
returns a voltage from ADS1115 channel selected using ADS1115_channel. default UNI_0 (Unipolar from channel 0) |
| 859 |
''' |
| 860 |
if self.ADS1115_CHANNEL in [0,1,2,3]: |
| 861 |
config = (self.REG_CONFIG_CQUE_NONE # Disable the comparator (default val) |
| 862 |
|self.REG_CONFIG_CLAT_NONLAT # Non-latching (default val) |
| 863 |
|self.REG_CONFIG_CPOL_ACTVLOW #Alert/Rdy active low (default val) |
| 864 |
|self.REG_CONFIG_CMODE_TRAD # Traditional comparator (default val) |
| 865 |
|(self.ADS1115_rates.get(self.ADS1115_DATARATE,self.REG_CONFIG_DR_250SPS)) # 250 samples per second (default) |
| 866 |
|(self.REG_CONFIG_MODE_SINGLE) # Single-shot mode (default) |
| 867 |
|self.ADS1115_GAIN) |
| 868 |
if self.ADS1115_CHANNEL == 0 : config |= self.REG_CONFIG_MUX_SINGLE_0 |
| 869 |
elif self.ADS1115_CHANNEL == 1 : config |= self.REG_CONFIG_MUX_SINGLE_1 |
| 870 |
elif self.ADS1115_CHANNEL == 2 : config |= self.REG_CONFIG_MUX_SINGLE_2 |
| 871 |
elif self.ADS1115_CHANNEL == 3 : config |= self.REG_CONFIG_MUX_SINGLE_3 |
| 872 |
#Set 'start single-conversion' bit |
| 873 |
config |= self.REG_CONFIG_OS_SINGLE |
| 874 |
self.I2CWriteBulk(self.ADS1115_ADDRESS,[self.REG_POINTER_CONFIG,(config>>8)&0xFF,config&0xFF]) |
| 875 |
time.sleep(1./self.ADS1115_DATARATE+.002) #convert to mS to S |
| 876 |
|
| 877 |
b = self.I2CReadBulk(self.ADS1115_ADDRESS, self.REG_POINTER_CONVERT ,2) |
| 878 |
if b is not None: |
| 879 |
x = ( (b[0]<<8)|b[1] )*self.ADS1115_scaling*1e-3 |
| 880 |
return [( (b[0]<<8)|b[1] )*self.ADS1115_scaling*1e-3] # scale and convert to volts |
| 881 |
|
| 882 |
elif self.ADS1115_CHANNEL in ['01','23']: |
| 883 |
return [0] |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
class inputs(): |
| 889 |
def __init__(self,p): |
| 890 |
self.p = p |
| 891 |
self.analogInputs = self.p.allAnalogChannels |
| 892 |
self.permanentInputs = [{ |
| 893 |
'name':'Voltmeter', |
| 894 |
'init':self.init, |
| 895 |
'read':self.readAllVoltages, |
| 896 |
'fields':self.analogInputs, |
| 897 |
'min':[min(self.p.analogInputSources[a].R[0],self.p.analogInputSources[a].R[1]) for a in self.analogInputs], |
| 898 |
'max':[max(self.p.analogInputSources[a].R[0],self.p.analogInputSources[a].R[1]) for a in self.analogInputs], |
| 899 |
},{ |
| 900 |
'name':'Time', |
| 901 |
'init':self.initTime, |
| 902 |
'read':self.getTime, |
| 903 |
'fields':['Seconds'], |
| 904 |
'min':[0], |
| 905 |
'max':[100], |
| 906 |
},{ |
| 907 |
'name':'Frequency', |
| 908 |
'init':self.init, |
| 909 |
'read':self.get_frequency, |
| 910 |
'fields':['IN2','SEN'], |
| 911 |
'min':[0,0], |
| 912 |
'max':[1e6,1e4], |
| 913 |
'autorefresh':False, |
| 914 |
},{ |
| 915 |
'name':'Capacitance', |
| 916 |
'init':self.init, |
| 917 |
'read':self.get_capacitance, |
| 918 |
'fields':['pF'], |
| 919 |
'min':[1.], |
| 920 |
'max':[1.e8], |
| 921 |
'autorefresh':False, |
| 922 |
},{ |
| 923 |
'name':'Resistance', |
| 924 |
'init':self.init, |
| 925 |
'read':self.get_resistance, |
| 926 |
'fields':['R'], |
| 927 |
'min':[1], |
| 928 |
'max':[1e6], |
| 929 |
},{ |
| 930 |
'name':'Ultrasound Echo', |
| 931 |
'init':self.init, |
| 932 |
'read':self.sr04_distance, |
| 933 |
'fields':['Distance(cm)'], |
| 934 |
'min':[1], |
| 935 |
'max':[80], |
| 936 |
'autorefresh':False, |
| 937 |
},{ |
| 938 |
'name':'Oscilloscope', |
| 939 |
'init':self.init, |
| 940 |
'read':None, |
| 941 |
'min':[0], |
| 942 |
'max':[10], |
| 943 |
},] |
| 944 |
|
| 945 |
if self.p.version_number>=5: #SEELAB 3.0 . Includes SPI |
| 946 |
self.permanentInputs.append({ |
| 947 |
'name':'MAX6675_temp', |
| 948 |
'init':self.init_MAX6675, |
| 949 |
'read':self.read_MAX6675, |
| 950 |
'fields':['TEMP'], |
| 951 |
'min':[0], |
| 952 |
'max':[400], |
| 953 |
'autorefresh':True, |
| 954 |
'refreshDelay':500, #mS. |
| 955 |
}) |
| 956 |
self.permanentInputs.append({ |
| 957 |
'name':'MAX31865_temp', |
| 958 |
'init':self.init_MAX31865, |
| 959 |
'read':self.read_MAX31865, |
| 960 |
'fields':['TEMP','Res'], |
| 961 |
'min':[-30,50], |
| 962 |
'max':[400,200], |
| 963 |
'autorefresh':True, |
| 964 |
'refreshDelay':60, #mS. |
| 965 |
'spinboxes':[{ |
| 966 |
'name':'Immerse in Ice Bath & Adjust till Temp = 0', |
| 967 |
'minimum':800, |
| 968 |
'maximum':1200, |
| 969 |
'value':1017, |
| 970 |
'function':self.MAX31865_zero |
| 971 |
} |
| 972 |
] |
| 973 |
|
| 974 |
|
| 975 |
}) |
| 976 |
|
| 977 |
|
| 978 |
for a in self.permanentInputs: |
| 979 |
a['type'] = 'input' |
| 980 |
|
| 981 |
def initTime(self): |
| 982 |
self.startTime = time.time() |
| 983 |
def getTime(self): |
| 984 |
return [time.time() - self.startTime] |
| 985 |
|
| 986 |
def get_frequency(self): |
| 987 |
return [self.p.get_freq('IN2'),self.p.get_freq('SEN')] |
| 988 |
def get_capacitance(self): |
| 989 |
return [self.p.get_capacitance()*1e12] |
| 990 |
def get_resistance(self): |
| 991 |
return [self.p.get_resistance()] |
| 992 |
def sr04_distance(self): |
| 993 |
return [self.p.sr04_distance()] |
| 994 |
def init(self): |
| 995 |
pass |
| 996 |
def readAllVoltages(self): |
| 997 |
return [self.p.get_average_voltage(a) for a in self.analogInputs] |
| 998 |
|
| 999 |
def init_MAX6675(self): |
| 1000 |
self.startTime = time.time() |
| 1001 |
self.p.SPI.set_parameters(1,2,0,0,1) |
| 1002 |
|
| 1003 |
def read_MAX6675(self): |
| 1004 |
try: |
| 1005 |
self.startTime = time.time() |
| 1006 |
self.p.SPI.start('CS1') |
| 1007 |
time.sleep(0.003) |
| 1008 |
self.p.SPI.stop('CS1') |
| 1009 |
time.sleep(0.001) |
| 1010 |
self.p.SPI.start('CS1') |
| 1011 |
#v1 = self.p.SPI.send8(0xFF) |
| 1012 |
#v2 = self.p.SPI.send8(0xFF) |
| 1013 |
#val = (v1<<8)|v2 |
| 1014 |
val = self.p.SPI.send16(0xFFFF) |
| 1015 |
self.p.SPI.stop('CS1') |
| 1016 |
|
| 1017 |
|
| 1018 |
if(val&0x4): |
| 1019 |
print('thermocouple not attached. :',val) |
| 1020 |
return [0] |
| 1021 |
print(val) |
| 1022 |
return [(val>>3)*0.25] |
| 1023 |
except Exception as e: |
| 1024 |
print (e) |
| 1025 |
return [0] |
| 1026 |
|
| 1027 |
MAX31865_Res0 = 101.7; # Resistance at 0 degC for 430ohm R_Ref |
| 1028 |
def init_MAX31865(self): |
| 1029 |
self.p.SPI.set_parameters(1,2,0,0,1) |
| 1030 |
self.p.SPI.start('CS1') |
| 1031 |
val = self.p.SPI.send16(0x8000|0x00C3) #address 0 , value B2 |
| 1032 |
self.p.SPI.stop('CS1') |
| 1033 |
|
| 1034 |
def read_MAX31865(self): |
| 1035 |
try: |
| 1036 |
self.p.SPI.start('CS1') |
| 1037 |
cnf = self.p.SPI.send16(0x0000) |
| 1038 |
RTD_ADC_Code = self.p.SPI.send16(0x0000)>>1 |
| 1039 |
self.p.SPI.stop('CS1') |
| 1040 |
|
| 1041 |
R_REF = 430.0 # Reference Resistor |
| 1042 |
a = .00390830; b = -.000000577500; c = -0.00000000000418301 |
| 1043 |
Res_RTD = (RTD_ADC_Code * R_REF) / 32768.0 # PT100 Resistance |
| 1044 |
temp_C = -(a*self.MAX31865_Res0) + np.sqrt(a*a*self.MAX31865_Res0*self.MAX31865_Res0 - 4*(b*self.MAX31865_Res0)*(self.MAX31865_Res0 - Res_RTD)) |
| 1045 |
temp_C = temp_C / (2*(b*self.MAX31865_Res0)) |
| 1046 |
temp_C_line = (RTD_ADC_Code/32.0) - 256.0 |
| 1047 |
if (temp_C < 0): |
| 1048 |
temp_C = (RTD_ADC_Code/32) - 256 |
| 1049 |
|
| 1050 |
return [temp_C,Res_RTD] |
| 1051 |
except Exception as e: |
| 1052 |
print (e) |
| 1053 |
return [0,0] |
| 1054 |
|
| 1055 |
def MAX31865_zero(self,val): |
| 1056 |
val = val/10. |
| 1057 |
print('zero:',val) |
| 1058 |
self.MAX31865_Res0 = val |
| 1059 |
|
| 1060 |
|
| 1061 |
class outputs(): |
| 1062 |
p=None |
| 1063 |
if sys.version_info.major==3: |
| 1064 |
DDS_MAX_FREQ = 0xFFFFFFF-1 #24 bit resolution |
| 1065 |
else: |
| 1066 |
DDS_MAX_FREQ = eval("0xFFFFFFFL-1") #24 bit resolution |
| 1067 |
#control bytes |
| 1068 |
DDS_B28 = 13 |
| 1069 |
DDS_HLB = 12 |
| 1070 |
DDS_FSELECT = 11 |
| 1071 |
DDS_PSELECT = 10 |
| 1072 |
DDS_RESET = 8 |
| 1073 |
DDS_SLEEP1 =7 |
| 1074 |
DDS_SLEEP12 =6 |
| 1075 |
DDS_OPBITEN =5 |
| 1076 |
DDS_DIV2 =3 |
| 1077 |
DDS_MODE =1 |
| 1078 |
|
| 1079 |
DDS_FSYNC =9 |
| 1080 |
|
| 1081 |
DDS_SINE =(0) |
| 1082 |
DDS_TRIANGLE =(1<<DDS_MODE) |
| 1083 |
DDS_SQUARE =(1<<DDS_OPBITEN) |
| 1084 |
DDS_RESERVED =(1<<DDS_OPBITEN)|(1<<DDS_MODE) |
| 1085 |
clockScaler = 4 # 8MHz |
| 1086 |
|
| 1087 |
def __init__(self,p): |
| 1088 |
self.permanentOutputs=[] |
| 1089 |
self.setDevice(p) |
| 1090 |
|
| 1091 |
def setDevice(self,dev): |
| 1092 |
self.p = dev |
| 1093 |
self.permanentOutputs = [{ |
| 1094 |
'name':'PV1', |
| 1095 |
'init':self.init, |
| 1096 |
'write':self.p.set_pv1, |
| 1097 |
'fields':['PV1'], |
| 1098 |
'min':[-5], |
| 1099 |
'max':[5], |
| 1100 |
},{ |
| 1101 |
'name':'PV2', |
| 1102 |
'init':self.init, |
| 1103 |
'write':self.p.set_pv2, |
| 1104 |
'fields':['PV2'], |
| 1105 |
'min':[-3.3], |
| 1106 |
'max':[3.3], |
| 1107 |
},{ |
| 1108 |
'name':'SQ1', |
| 1109 |
'init':self.init, |
| 1110 |
'write':self.p.set_sqr1, |
| 1111 |
'fields':['SQ1'], |
| 1112 |
'min':[1], |
| 1113 |
'max':[100000], |
| 1114 |
},{ |
| 1115 |
'name':'WG', |
| 1116 |
'init':self.init, |
| 1117 |
'write':self.p.set_sine, |
| 1118 |
'fields':['WG'], |
| 1119 |
'min':[1], |
| 1120 |
'max':[5000], |
| 1121 |
},] |
| 1122 |
if self.p.version_number>=5: #SEELAB 3.0 . Includes SPI |
| 1123 |
self.permanentOutputs.append({ |
| 1124 |
'name':'AD9833_DDS', |
| 1125 |
'init':self.init_AD9833, |
| 1126 |
'write':self.set_AD9833, |
| 1127 |
'fields':['FREQ'], |
| 1128 |
'min':[1], |
| 1129 |
'max':[2e6], |
| 1130 |
'outputconfig':[{ |
| 1131 |
'name':'wavetype', |
| 1132 |
'options':['Sine','Triangle'], |
| 1133 |
'function':self.set_waveform_mode |
| 1134 |
}, |
| 1135 |
], |
| 1136 |
}) |
| 1137 |
for a in self.permanentOutputs: |
| 1138 |
a['type'] = 'output' |
| 1139 |
def init(self): |
| 1140 |
pass |
| 1141 |
|
| 1142 |
def write(self,con): |
| 1143 |
self.p.SPI.start(self.CS) |
| 1144 |
self.p.SPI.send16(con) |
| 1145 |
self.p.SPI.stop(self.CS) |
| 1146 |
|
| 1147 |
def init_AD9833(self): |
| 1148 |
self.CS='CS1' |
| 1149 |
self.p.SPI.set_parameters(2,2,1,1,0) |
| 1150 |
self.p.SPI.map_reference_clock(self.clockScaler,'CS3') |
| 1151 |
print ('clock set to ',self.p.SPI.DDS_CLOCK ) |
| 1152 |
self.waveform_mode = self.DDS_SINE; |
| 1153 |
self.write(1<<self.DDS_RESET) |
| 1154 |
self.write((1<<self.DDS_B28) | self.waveform_mode ) #finished loading data |
| 1155 |
self.active_channel = 0 |
| 1156 |
self.frequency = 1000 |
| 1157 |
|
| 1158 |
def set_AD9833(self,freq,register=0,**args): |
| 1159 |
self.active_channel = register |
| 1160 |
self.frequency = freq |
| 1161 |
|
| 1162 |
freq_setting = int(round(freq * self.DDS_MAX_FREQ / self.p.SPI.DDS_CLOCK)) |
| 1163 |
modebits = (1<<self.DDS_B28)| self.waveform_mode |
| 1164 |
if register: |
| 1165 |
modebits|=(1<<self.DDS_FSELECT) |
| 1166 |
regsel = 0x8000 |
| 1167 |
else: |
| 1168 |
regsel=0x4000 |
| 1169 |
|
| 1170 |
self.write( (1<<self.DDS_RESET) | modebits ) #Ready to load DATA |
| 1171 |
self.write( (regsel | (freq_setting&0x3FFF))&0xFFFF ) #LSB |
| 1172 |
self.write( (regsel | ((freq_setting>>14)&0x3FFF))&0xFFFF ) #MSB |
| 1173 |
phase = args.get('phase',0) |
| 1174 |
self.write( 0xc000|phase) #Phase |
| 1175 |
self.write(modebits) #finished loading data |
| 1176 |
|
| 1177 |
def set_voltage_AD9833(self,v): |
| 1178 |
self.waveform_mode=self.DDS_TRIANGLE |
| 1179 |
self.set_AD9833(0,0,phase = v)#0xfff*v/.6) |
| 1180 |
|
| 1181 |
def select_frequency_register(self,register): |
| 1182 |
self.active_channel = register |
| 1183 |
modebits = self.waveform_mode |
| 1184 |
if register: modebits|=(1<<self.DDS_FSELECT) |
| 1185 |
self.write(modebits) |
| 1186 |
|
| 1187 |
|
| 1188 |
def set_waveform_mode(self,mode): |
| 1189 |
self.waveform_mode=mode+1 |
| 1190 |
modebits = mode+1 |
| 1191 |
if self.active_channel: modebits|=(1<<self.DDS_FSELECT) |
| 1192 |
self.write(modebits ) |
| 1193 |
|