Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/start_bootloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

u2f = USB2FIR()
u2f.start_bootloader()
u2f.close()
10 changes: 5 additions & 5 deletions examples/view_matplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
plt.ion()

ir = frame.reshape((24, 32))
graph = plt.imshow(ir,interpolation='none')
graph = plt.imshow(ir, interpolation='none')

plt.colorbar()
plt.clim(18,35)
plt.clim(18, 35)
plt.draw()

try:
while(1):

while 1:
u2f.updateFrame(frame)

ir = frame.reshape((24, 32))[:, ::-1]
Expand All @@ -30,7 +29,8 @@
plt.pause(0.0001)

except KeyboardInterrupt:
print("CTRL-C: Program Stopping via Keyboard Interrupt...")
u2f.close()
print("CTRL-C: Program Stopping via Keyboard Interrupt...")

finally:
print("Exiting Loop")
Expand Down
19 changes: 11 additions & 8 deletions examples/view_tk.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Tkinter as tk
import Queue
from pyusb2fir import USB2FIR
import threading
import queue
import threading
import tkinter as tk

from pyUSB2FIR.pyusb2fir.usb2fir import USB2FIR


def rgb(minimum, maximum, value):
minimum, maximum = float(minimum), float(maximum)
Expand Down Expand Up @@ -37,17 +39,15 @@ def __init__(self, *args, **kwargs):
self.label_temp = tk.Label(textvariable=self.tempstr)
self.label_temp.pack()


self.queue = Queue.Queue()

self.queue = queue.Queue()
self.updateMap()

def updateMap(self):

try:
tempvalues = self.queue.get_nowait()
self.setTempValues(tempvalues)
except Queue.Empty:
except queue.Empty:
pass

self.after(100, self.updateMap)
Expand Down Expand Up @@ -95,4 +95,7 @@ def setTempValues(self, tempvalues):
t.start()

app.mainloop()

t.do_run = False

u2f.close()
2 changes: 1 addition & 1 deletion pyusb2fir/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
# along with pyUSB2FIR. If not, see <http://www.gnu.org/licenses/>

from .usb2fir import USB2FIR
from .usb2fir import USB2FIR
61 changes: 34 additions & 27 deletions pyusb2fir/usb2fir.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
# along with pyUSB2FIR. If not, see <http://www.gnu.org/licenses/>

import libusb1
import usb1
import numpy as np
import usb1

USB2FIR_VID = 0x04D8
USB2FIR_PID = 0xEE7D
Expand Down Expand Up @@ -46,42 +46,42 @@ def uint4_to_int4(i):
else:
return i


def uint6_to_int6(i):
if i > 31:
return i - 64
else:
return i


def uint8_to_int8(i):
if i > 127:
return i - 256
else:
return i


def uint10_to_int10(i):
if i > 511:
return i - 1024
else:
return i


def uint16_to_int16(i):
if i > 32767:
return i - 65536
else:
return i

class MLXCommonParameters:


class MLXCommonParameters:
def __init__(self, eepromdata):


# extract VDD sensor parameters

self.kVdd = uint8_to_int8(eepromdata[0x33] >> 8) * 32
self.vdd25 = ((eepromdata[0x33] & 0xff) - 256) * 32 - 8192


# extract Ta sensor parameters

self.KvPTAT = eepromdata[0x32] >> 10
Expand All @@ -98,7 +98,6 @@ def __init__(self, eepromdata):

self.alphaPTAT = (eepromdata[0x10] >> 12) / 4.0 + 8.0


# extract offset

offsetAverage = uint16_to_int16(eepromdata[0x11])
Expand Down Expand Up @@ -126,18 +125,18 @@ def __init__(self, eepromdata):
# extract sensitivity

alphaRef = eepromdata[0x21]
alphaScale = (eepromdata[0x20] >> 12) + 30
accColumnScale = (eepromdata[0x20] & 0x00F0) >> 4;
accRowScale = (eepromdata[0x20] & 0x0F00) >> 8;
accRemScale = eepromdata[0x20] & 0x000F;
alphaScale = int((eepromdata[0x20] >> 12) + 30)
accColumnScale = (eepromdata[0x20] & 0x00F0) >> 4
accRowScale = (eepromdata[0x20] & 0x0F00) >> 8
accRemScale = eepromdata[0x20] & 0x000F

accRow = []
for i in range(24):
accRow.append(uint4_to_int4((eepromdata[0x22 + i / 4] >> ((i % 4) * 4)) & 0xF))
accRow.append(uint4_to_int4((eepromdata[0x22 + i // 4] >> ((i % 4) * 4)) & 0xF))

accColumn = []
for i in range(32):
accColumn.append(uint4_to_int4((eepromdata[0x28 + i / 4] >> ((i % 4) * 4)) & 0xF))
accColumn.append(uint4_to_int4((eepromdata[0x28 + i // 4] >> ((i % 4) * 4)) & 0xF))

self.alpha = []
for i in range(24):
Expand Down Expand Up @@ -198,7 +197,7 @@ def __init__(self, eepromdata):

# extract corner temperatures

step = ((eepromdata[0x3F] & 0x3000) >> 12) * 10;
step = ((eepromdata[0x3F] & 0x3000) >> 12) * 10
self.ct = [-40, 0, 0, 0]
self.ct[2] = ((eepromdata[0x3F] & 0x00F0) >> 4) * step
self.ct[3] = ((eepromdata[0x3F] & 0x0F00) >> 8) * step + self.ct[2]
Expand All @@ -213,7 +212,7 @@ def __init__(self, eepromdata):

# extract the sensitivity alphaCP

alphaScale = ((eepromdata[0x20] & 0xF000) >> 12) + 27
alphaScale = int(((eepromdata[0x20] & 0xF000) >> 12) + 27)
self.cpAlpha = [0.0, 0.0]
self.cpAlpha[0] = (uint10_to_int10(eepromdata[0x39] & 0x03FF) + 0.0) / (1 << alphaScale)
self.cpAlpha[1] = uint6_to_int6((eepromdata[0x39] & 0xFC00) >> 10) + 0.0
Expand Down Expand Up @@ -241,14 +240,14 @@ def __init__(self, eepromdata):
self.tgc = uint8_to_int8(eepromdata[0x3C] & 0x0ff) / 32.0

# extract resolution setting
self.resolutionEE = (eepromdata[0x38] & 0x3000) >> 12;
self.resolutionEE = (eepromdata[0x38] & 0x3000) >> 12


self.alphaCorrR = [0] * 4
self.alphaCorrR[0] = 1 / (1 + self.ksTo[0] * 40)
self.alphaCorrR[1] = 1
self.alphaCorrR[2] = (1 + self.ksTo[2] * self.ct[2]);
self.alphaCorrR[3] = self.alphaCorrR[2] * (1 + self.ksTo[3] * (self.ct[3] - self.ct[2]));
self.alphaCorrR[2] = (1 + self.ksTo[2] * self.ct[2])
self.alphaCorrR[3] = self.alphaCorrR[2] * (1 + self.ksTo[3] * (self.ct[3] - self.ct[2]))



Expand All @@ -258,8 +257,10 @@ def __init__(self, i2caddress=0x33, refreshRate=3):
Initialize and open connection to USB2FIR.
"""
ctx = usb1.LibUSBContext()

self.usbdev = ctx.getByVendorIDAndProductID(USB2FIR_VID, USB2FIR_PID)
self.usbhandle = self.usbdev.open()

self.usbhandle.claimInterface(0)
self.i2caddress = i2caddress

Expand Down Expand Up @@ -365,7 +366,7 @@ def updateFrame(self, frame):
ta = (ptatArt / (1 + self.commonParameters.KvPTAT * (vdd - 3.3)) - self.commonParameters.vPTAT25)
ta = ta / self.commonParameters.KtPTAT + 25

tr = ta - 8;
tr = ta - 8

ta4 = np.power((ta + 273.15), 4)
tr4 = np.power((tr + 273.15), 4)
Expand All @@ -384,16 +385,19 @@ def updateFrame(self, frame):
irData = uint16_to_int16(irData) + 0.0
irData = irData * gain
irData = irData - self.commonParameters.offset[pixelidx] * (1 + self.commonParameters.kta[pixelidx] * (ta - 25)) * (1 + self.commonParameters.kv[pixelidx] * (vdd - 3.3))
irData = irData / emissivity;
irData = irData / emissivity

irData = irData - self.commonParameters.tgc * irDataCP

alphaCompensated = (self.commonParameters.alpha[pixelidx] - self.commonParameters.tgc * self.commonParameters.cpAlpha[subpage]) * (1 + self.commonParameters.KsTa * (ta - 25));
alphaCompensated = (self.commonParameters.alpha[pixelidx] - self.commonParameters.tgc *
self.commonParameters.cpAlpha[subpage]) * (
1 + self.commonParameters.KsTa * (ta - 25))

Sx = np.power(alphaCompensated, 3) * (irData + alphaCompensated * taTr);
Sx = np.sqrt(np.sqrt(Sx)) * self.commonParameters.ksTo[1];
Sx = np.power(alphaCompensated, 3) * (irData + alphaCompensated * taTr)
Sx = np.sqrt(np.sqrt(Sx)) * self.commonParameters.ksTo[1]

To = np.sqrt(np.sqrt(irData / (alphaCompensated * (1 - self.commonParameters.ksTo[1] * 273.15) + Sx) + taTr)) - 273.15;
To = np.sqrt(np.sqrt(
irData / (alphaCompensated * (1 - self.commonParameters.ksTo[1] * 273.15) + Sx) + taTr)) - 273.15

if To < self.commonParameters.ct[1]:
r = 0
Expand All @@ -403,10 +407,13 @@ def updateFrame(self, frame):
r = 2
else:
r = 3

To = np.sqrt(np.sqrt(irData / (alphaCompensated * self.commonParameters.alphaCorrR[r] * (1 + self.commonParameters.ksTo[r] * (To - self.commonParameters.ct[r]))) + taTr)) - 273.15;

To = np.sqrt(np.sqrt(irData / (alphaCompensated * self.commonParameters.alphaCorrR[r] * (
1 + self.commonParameters.ksTo[r] * (To - self.commonParameters.ct[r]))) + taTr)) - 273.15
frame[pixelidx] = To
pixelidx = pixelidx + 2

def close(self):
self.usbhandle.close()



4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

from setuptools import setup


def readme():
with open("README.rst") as f:
return f.read()


setup(name='pyusb2fir',
version='1.0',
description='USB2FIR - Interface for far infrared thermal sensor array MLX90640',
Expand All @@ -39,4 +41,4 @@ def readme():
'libusb1',
'numpy'
],
zip_safe=False)
zip_safe=False)