144 lines
4.1 KiB
TypeScript
144 lines
4.1 KiB
TypeScript
|
/**
|
||
|
* @author Martin Karkowski
|
||
|
* @email m.karkowski@zema.de
|
||
|
* @create date 2021-05-13 07:49:23
|
||
|
* @modify date 2021-05-13 08:07:52
|
||
|
* @desc [description]
|
||
|
*/
|
||
|
|
||
|
import * as Tinkerforge from "tinkerforge";
|
||
|
import { NopeObservable } from "../../../lib/observables/nopeObservable";
|
||
|
import { INopeDispatcher } from "../../../lib/types/nope/nopeDispatcher.interface";
|
||
|
import { GenericBrick } from "../src/genericBrick.module";
|
||
|
import { BRICKS } from "../src/identifiers";
|
||
|
import { TinkerForgeConnector } from "../src/TinkerForgeConnector";
|
||
|
import { IDeviceConfiguration } from "../type/interfaces";
|
||
|
|
||
|
export default async function (
|
||
|
_dispatcher: INopeDispatcher,
|
||
|
UID: string,
|
||
|
ipcon: TinkerForgeConnector,
|
||
|
config: IDeviceConfiguration
|
||
|
) {
|
||
|
const sensor = new ThermoCoupleSensor(_dispatcher, UID, ipcon);
|
||
|
sensor.identifier = config.name;
|
||
|
await sensor.init(...config.params);
|
||
|
return sensor;
|
||
|
}
|
||
|
|
||
|
export class ThermoCoupleSensor extends GenericBrick {
|
||
|
public value = new NopeObservable<{
|
||
|
x: number;
|
||
|
y: number;
|
||
|
z: number;
|
||
|
}>();
|
||
|
|
||
|
constructor(
|
||
|
_dispatcher: INopeDispatcher,
|
||
|
UID: string,
|
||
|
ipcon: TinkerForgeConnector
|
||
|
) {
|
||
|
super(
|
||
|
_dispatcher,
|
||
|
UID,
|
||
|
ipcon,
|
||
|
BRICKS.ACCELEROMETER_BRICKLET,
|
||
|
"BrickletAccelerometer"
|
||
|
);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Konfiguriert die Datenrate, den Wertebereich und die Filterbandbreite.
|
||
|
*
|
||
|
* @param {number} dataRate The Following values are valid:
|
||
|
BrickletAccelerometer.DATA_RATE_OFF = 0
|
||
|
BrickletAccelerometer.DATA_RATE_3HZ = 1
|
||
|
BrickletAccelerometer.DATA_RATE_6HZ = 2
|
||
|
BrickletAccelerometer.DATA_RATE_12HZ = 3
|
||
|
BrickletAccelerometer.DATA_RATE_25HZ = 4
|
||
|
BrickletAccelerometer.DATA_RATE_50HZ = 5
|
||
|
BrickletAccelerometer.DATA_RATE_100HZ = 6
|
||
|
BrickletAccelerometer.DATA_RATE_400HZ = 7
|
||
|
BrickletAccelerometer.DATA_RATE_800HZ = 8
|
||
|
BrickletAccelerometer.DATA_RATE_1600HZ = 9
|
||
|
* @param {boolean} fullScale
|
||
|
BrickletAccelerometer.FULL_SCALE_2G = 0
|
||
|
BrickletAccelerometer.FULL_SCALE_4G = 1
|
||
|
BrickletAccelerometer.FULL_SCALE_6G = 2
|
||
|
BrickletAccelerometer.FULL_SCALE_8G = 3
|
||
|
BrickletAccelerometer.FULL_SCALE_16G = 4
|
||
|
* @param {number} filterBandwith
|
||
|
BrickletAccelerometer.FILTER_BANDWIDTH_800HZ = 0
|
||
|
BrickletAccelerometer.FILTER_BANDWIDTH_400HZ = 1
|
||
|
BrickletAccelerometer.FILTER_BANDWIDTH_200HZ = 2
|
||
|
BrickletAccelerometer.FILTER_BANDWIDTH_50HZ = 3
|
||
|
* @return {*} {Promise<void>}
|
||
|
* @memberof ThermoCoupleSensor
|
||
|
*/
|
||
|
setConfiguration(
|
||
|
dataRate: number,
|
||
|
fullScale: number,
|
||
|
filterBandwith: number
|
||
|
): Promise<void> {
|
||
|
// Now return a Promise, which will perform the Action:
|
||
|
const _this = this;
|
||
|
return new Promise((resolve, reject) => {
|
||
|
_this.instance.setConfiguration(
|
||
|
dataRate,
|
||
|
fullScale,
|
||
|
filterBandwith,
|
||
|
resolve,
|
||
|
reject
|
||
|
);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Helper Function, to assign the Frequency of the Sensor.
|
||
|
* As minimum Frequency we will use 10 Hz.
|
||
|
*
|
||
|
* @param {number} freq The Frequency of the Sensor, given in Hz
|
||
|
* @return {*} {Promise<void>}
|
||
|
* @memberof ThermoCoupleSensor
|
||
|
*/
|
||
|
setFrequency(freq: number): Promise<void> {
|
||
|
// Adapt the Max Frequency
|
||
|
const period = Math.round((1 / freq) * 1000 > 1 ? (1 / freq) * 1000 : 10);
|
||
|
|
||
|
// Now return a Promise, which will perform the Action:
|
||
|
const _this = this;
|
||
|
return new Promise((resolve, reject) => {
|
||
|
_this.instance.setAccelerationCallbackPeriod(period, resolve, reject);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
async init(
|
||
|
freq = 50,
|
||
|
dataRate = 6,
|
||
|
fullScale = 0,
|
||
|
filterBandwith = 0
|
||
|
): Promise<void> {
|
||
|
await super.init();
|
||
|
|
||
|
// Define the inital Parameters
|
||
|
await this.setFrequency(freq);
|
||
|
await this.setConfiguration(dataRate, fullScale, filterBandwith);
|
||
|
|
||
|
const _factor = 1000 * 9.80665;
|
||
|
const _this = this;
|
||
|
this.instance.on(
|
||
|
Tinkerforge.BrickletAccelerometer.CALLBACK_ACCELERATION,
|
||
|
// Callback function for temperature callback
|
||
|
(x, y, z) => {
|
||
|
// Temperature in [°C]
|
||
|
_this.value.setContent({
|
||
|
// Convert to mm/s²
|
||
|
x: x * _factor,
|
||
|
y: y * _factor,
|
||
|
z: z * _factor
|
||
|
});
|
||
|
}
|
||
|
);
|
||
|
}
|
||
|
}
|