Skip to content

Instantly share code, notes, and snippets.

@ffoxin
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ffoxin/2c47c4496ab85f574c1e to your computer and use it in GitHub Desktop.
Save ffoxin/2c47c4496ab85f574c1e to your computer and use it in GitHub Desktop.
wpc60
-55 0.99 475 490 505 3.02
-50 0.98 500 515 530 2.92
-40 0.96 552 567 582 2.74
-30 0.93 609 624 638 2.55
-20 0.91 669 684 698 2.35
-10 0.88 733 747 761 2.14
0 0.85 802 815 828 1.91
10 0.83 874 886 898 1.67
20 0.8 950 961 972 1.41
25 0.79 990 1000 1010 1.27
30 0.78 1029 1040 1051 1.39
40 0.75 1108 1122 1136 1.64
50 0.73 1192 1209 1225 1.91
60 0.71 1278 1299 1319 2.19
70 0.69 1369 1392 1416 2.49
80 0.67 1462 1490 1518 2.8
90 0.65 1559 1591 1623 3.12
100 0.63 1659 1696 1733 3.46
110 0.61 1762 1805 1847 3.83
120 0.58 1867 1915 1963 4.33
125 0.55 1919 1970 2020 4.66
130 0.52 1970 2023 2077 5.07
140 0.45 2065 2124 2184 6.28
150 0.35 2145 2211 2277 8.55
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
if __name__ == '__main__':
# load datasheet values from file
# format:
# (temp, temp_coef, res_min, res, res_max, temp_error)
data = []
with open('KTY81_110.txt') as ds:
lines = ds.read().splitlines()
data = [
(int(l[0]), float(l[1]), int(l[2]), int(l[3]), int(l[4]), float(l[5]))
for l in [ln.split() for ln in lines]
]
# retrieve resistance value
if len(sys.argv) > 1:
res = int(sys.argv[1])
print('Resistance: {}'.format(res))
else:
res = int(input('Resistance: '))
# check input
if res < data[0][2] or res > data[-1][4]:
print('Invalid resistance')
exit(0)
# find two corresponding records in table
for rec in data:
if res < rec[3]:
rec_next = rec
break
rec_prev = rec
# find mid value between two records
mid = (rec_next[3] - rec_prev[3]) * rec_prev[1] / (rec_prev[1] + rec_next[1]) + rec_prev[3]
if res < mid:
rec = rec_prev
else:
rec = rec_next
# calculate temp
temp = rec[0] + (res - rec[3]) * 100 / rec[3] / rec[1]
print('Temperature: {:.2f} °C'.format(temp))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment