Skip to content

Commit

Permalink
Added Shart equations #69
Browse files Browse the repository at this point in the history
Allow incorrect feed values #71
  • Loading branch information
rstrouse committed Jun 15, 2024
1 parent 14b2b6a commit 736cd76
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 20 deletions.
5 changes: 5 additions & 0 deletions boards/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ export class Utils {
let tK = (beta * rtemp) / (beta + (rtemp * Math.log(resistance / ref)));
return this.convert.temperature.convertUnits(tK, 'k', units);
},
shart3: (resistance: number, sA: number, sB: number, sC: number, units:string): number => {
let rlog = Math.log(resistance);
let tK = 1 / (sA + sB * rlog + sC * rlog * rlog * rlog);
return this.convert.temperature.convertUnits(tK, 'k', units);
},
convertUnits: (val: number, from: string, to: string) => {
if (typeof val !== 'number') return null;
let fn = this.convert.temperature[from.toLowerCase()];
Expand Down
74 changes: 72 additions & 2 deletions devices/temperature.json
Original file line number Diff line number Diff line change
Expand Up @@ -842,8 +842,8 @@
"name": "10k Temperature Probe",
"input": "ohms",
"precision": 2,
"deviceClass": "GenericDeviceBase",
"module": "./genericDevices",
"deviceClass": "Thermistor10k",
"module": "./Temperature",
"convertValue": "device.values.inputUnits = device.options.inputType === 'raw' ? '' : device.options.inputType === 'volt' ? 'volts' : device.options.inputResistanceUnits === 1000 ? 'kOhms': 'ohms'; device.values.units = device.options.units; device.values.maxVal = (device.options.inputType === 'raw') ? (1 << device.options.inputBitness) : device.options.inputType === 'volt' ? device.options.vccRef : 10000; device.values.resistance = (device.options.inputType === 'ohms') ? device.values.adcValue * device.options.inputResistanceUnits : (10000 * device.values.adcValue) / (device.values.maxVal - device.values.adcValue); device.values.temperature = (Math.round(maps.thermistor10k.interpolate(device.values.resistance, device.values.units || 'F') * 100)/100) + (device.options.calibration || 0);",
"inputs": [
{
Expand Down Expand Up @@ -1331,6 +1331,76 @@
}
]
},
{
"field": {
"type": "fieldset",
"legend": "Calculation",
"cssClass": "pnl-calculation"
},
"options": [
{
"field": {
"type": "pickList",
"labelText": "",
"binding": "options.calcType",
"value": "interpolate",
"required": true,
"canEdit": false,
"inputAttrs": {
"style": {
"width": "12rem"
}
},
"labelAttrs": {
"style": {
"hidden": true,
"width": "0rem"
}
},
"style": {
"display": "block"
},
"columns": [
{
"binding": "val",
"hidden": true,
"text": "Val",
"style": {
"whiteSpace": "nowrap"
}
},
{
"binding": "name",
"text": "Type",
"hidden": true,
"style": {
"whiteSpace": "nowrap"
}
},
{
"binding": "desc",
"text": "Method",
"style": {
"whiteSpace": "nowrap"
}
}
],
"items": [
{
"val": "interpolate",
"name": "10k thermistor table",
"desc": "10k thermistor table"
},
{
"val": "shart",
"name": "Steinhart-Hart Equation",
"desc": "Steinhart-Hart Equation"
}
]
}
}
]
},
{
"field": {
"type": "fieldset",
Expand Down
70 changes: 59 additions & 11 deletions generic/Temperature.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,68 @@
import { logger } from "../logger/Logger";
import { DeviceBinding } from "../boards/Controller";
import { setTimeout, clearTimeout } from "timers";
import { GenericDeviceBase } from "./genericDevices";
import { AnalogDevices } from "../devices/AnalogDevices";
import { utils } from "../boards/Constants";

import * as fs from 'fs';
import { GenericDeviceBase } from "./genericDevices";
import { webApp } from "../web/Server";

export class Themistor10k extends GenericDeviceBase {
protected logError(err, msg?: string) { logger.error(`${this.device.name} ${typeof msg !== 'undefined' ? msg + ' ' : ''}${typeof err !== 'undefined' ? err.message : ''}`); }
public async setValues(vals): Promise<any> {
try {
return Promise.resolve(this.values);
export class Thermistor10k extends GenericDeviceBase {
public setValue(prop, value) {
let replaceSymbols = /(?:\]\.|\[|\.)/g
let _prop = prop.indexOf(',') > -1 ? prop.replace(replaceSymbols, ',').split(',') : prop;
// Execute a function, load a module, or ...
let dt = this.device.getDeviceType();
let val = value;
if (typeof dt.inputs !== 'undefined') {
let inp = dt.inputs.find(x => x.name === prop);
if (typeof inp !== 'undefined') {
switch (inp.dataType) {
case 'number':
if (typeof value.value !== 'undefined') val = value.value;
else if (typeof value.adcValue !== 'undefined') val = value.adcValue;

}
}
}

//let obj = this.device.values;
// for (let i = 0; i < _prop.length; i++) {
// obj = obj[_prop[i]];
// }
// obj = value;
this.device.values[_prop] = val;
this.convertValue(val);
webApp.emitToClients('genericDataValues', { id: this.device.id, typeId: this.device.typeId, values: this.values });
this.emitFeeds();
}
public convertValue(value: number) {
let device = this.device;
let maps = AnalogDevices.maps;
device.values.inputUnits = device.options.inputType === 'raw' ? '' : device.options.inputType === 'volt' ? 'volts' : device.options.inputResistanceUnits === 1000 ? 'kOhms' : 'ohms';
device.values.units = device.options.units;
device.values.maxVal = (device.options.inputType === 'raw') ? (1 << device.options.inputBitness) : device.options.inputType === 'volt' ? device.options.vccRef : 10000;
switch (device.options.inputType) {
case 'ohms':
device.values.resistance = device.values.adcValue * device.options.inputResistanceUnits;
break;
case 'kohms':
device.values.resistance = (10000 * device.values.adcValue) / (device.values.maxVal - device.values.adcValue);
break;
}
switch (device.options.calcType) {
case 'shart':
device.values.tempK = utils.convert.temperature.shart3(device.values.resistance, 0.001125308852122, 0.000234711863267, 0.000000085663516, 'K');
device.values.tempC = utils.convert.temperature.convertUnits(device.values.tempK, 'K', 'C');
device.values.tempF = utils.convert.temperature.convertUnits(device.values.tempK, 'K', 'F');
device.values.temperature = utils.convert.temperature.convertUnits(device.values.tempK, 'K', device.values.units || 'F') + (device.options.calibration || 0);
break;
default:
device.values.tempK = (Math.round(maps['thermistor10k'].interpolate(device.values.resistance, 'K') * 100) / 100);
device.values.tempC = utils.convert.temperature.convertUnits(device.values.tempK, 'K', 'C');
device.values.tempF = utils.convert.temperature.convertUnits(device.values.tempK, 'K', 'F');
device.values.temperature = utils.convert.temperature.convertUnits(device.values.tempK, 'K', device.values.units || 'F') + (device.options.calibration || 0);
break;
}
catch (err) { this.logError(err); Promise.reject(err); }
finally { }
return value;
}
}

Expand Down
19 changes: 12 additions & 7 deletions pages/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,14 +601,19 @@ var dataBinder = {
if (typeof val === 'number') return val;
else if (typeof val === 'undefined' || val === null) return;
else if (typeof val.getTime === 'function') return val.getTime();
var tval = val.replace(/[^0-9\.\-]+/g, '');
var v;
if (tval.indexOf('.') !== -1) {
v = parseFloat(tval);
v = v.round(tval.length - tval.indexOf('.'));
try {
var tval = val.replace(/[^0-9\.\-]+/g, '');
var v;
if (tval.indexOf('.') !== -1) {
v = parseFloat(tval);
v = v.round(tval.length - tval.indexOf('.'));
}
else v = parseInt(tval, 10);
return v;
} catch (err) {
console.log({ msg: 'Error parsing number', val: val, err: err });
return NaN;
}
else v = parseInt(tval, 10);
return v;
},
formatDuration: function (dur) {
var fmt = '';
Expand Down

0 comments on commit 736cd76

Please sign in to comment.