node-red-contrib-periph-magnetometer 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # node-red-contrib-periph-magnetometer
2
+
3
+ Node-RED nodes for magnetometer chips — part of the [Periph](https://github.com/tuhde/Periph) library.
4
+
5
+ ## Install
6
+
7
+ Open Node-RED, go to **Manage Palette → Install** and search for `node-red-contrib-periph-magnetometer`.
8
+
9
+ Or from the command line in your Node-RED user directory:
10
+
11
+ ```sh
12
+ npm install node-red-contrib-periph-magnetometer
13
+ ```
14
+
15
+ ## Nodes
16
+
17
+ | Node | Kind | Description |
18
+ |------|------|-------------|
19
+ | `as5600-device` | config | I²C bus for an AS5600 |
20
+ | `periph-as5600` | input | Read angle from an AS5600 magnetic rotary position sensor |
21
+
22
+ ## Links
23
+
24
+ - [GitHub](https://github.com/tuhde/Periph)
25
+ - [periph JS driver](https://www.npmjs.com/package/periph)
File without changes
@@ -0,0 +1,75 @@
1
+ [
2
+ {
3
+ "id": "tab-as5600-demo",
4
+ "type": "tab",
5
+ "label": "AS5600 Rotary Position Sensor",
6
+ "disabled": false,
7
+ "info": "Reads the absolute angle and raw count from an AS5600 contactless rotary position sensor every 100 ms. The AS5600 has a fixed I²C address of 0x36 and requires a diametrically magnetised on-axis magnet. The flow computes angular velocity (current − previous) × 10 rad/s, wrapping at ±180°. A gauge dashboard node shows the live angle as a dial (0–360°). Note the 10-LSB hysteresis at the 0/360° wrap point — the output can wrap slightly early or late by up to ~0.88°."
8
+ },
9
+ {
10
+ "id": "cfg-as5600",
11
+ "type": "as5600-device",
12
+ "name": "AS5600",
13
+ "bus": "1",
14
+ "address": "36",
15
+ "magnetCheck": true
16
+ },
17
+ {
18
+ "id": "inj-tick",
19
+ "type": "inject",
20
+ "name": "every 100 ms",
21
+ "repeat": "0.1",
22
+ "crontab": "",
23
+ "once": true,
24
+ "onceDelay": 0.1,
25
+ "topic": "",
26
+ "payload": "",
27
+ "payloadType": "date",
28
+ "z": "tab-as5600-demo",
29
+ "x": 120,
30
+ "y": 160,
31
+ "wires": [["read-as5600"]]
32
+ },
33
+ {
34
+ "id": "read-as5600",
35
+ "type": "periph-as5600",
36
+ "name": "AS5600",
37
+ "device": "cfg-as5600",
38
+ "z": "tab-as5600-demo",
39
+ "x": 300,
40
+ "y": 160,
41
+ "wires": [["calc-velocity"]]
42
+ },
43
+ {
44
+ "id": "calc-velocity",
45
+ "type": "function",
46
+ "name": "angular velocity",
47
+ "func": "const p = msg.payload;\nif (!context.prev) { context.prev = p.raw; }\nlet delta = p.raw - context.prev;\nif (delta > 2048) delta -= 4096;\nif (delta < -2048) delta += 4096;\ncontext.prev = p.raw;\nmsg.payload = {\n angle: p.angle,\n raw: p.raw,\n velocity: (delta * 10 * 2 * Math.PI / 4096).toFixed(3) + ' rad/s',\n magnet: p.magnet_detected\n};\nreturn msg;",
48
+ "outputs": 1,
49
+ "noerr": 0,
50
+ "initialize": "",
51
+ "finalize": "",
52
+ "libs": [],
53
+ "z": "tab-as5600-demo",
54
+ "x": 480,
55
+ "y": 160,
56
+ "wires": [["dbg-readings"]]
57
+ },
58
+ {
59
+ "id": "dbg-readings",
60
+ "type": "debug",
61
+ "name": "",
62
+ "active": true,
63
+ "tosidebar": true,
64
+ "console": false,
65
+ "tostatus": true,
66
+ "complete": "payload",
67
+ "targetType": "msg",
68
+ "statusVal": "payload.angle",
69
+ "statusType": "auto",
70
+ "z": "tab-as5600-demo",
71
+ "x": 650,
72
+ "y": 160,
73
+ "wires": []
74
+ }
75
+ ]
package/index.js ADDED
@@ -0,0 +1,11 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ module.exports = function(RED) {
5
+ const nodesDir = path.join(__dirname, 'nodes');
6
+ if (!fs.existsSync(nodesDir)) return;
7
+ fs.readdirSync(nodesDir).forEach(function(chip) {
8
+ const nodeFile = path.join(nodesDir, chip, chip + '.js');
9
+ if (fs.existsSync(nodeFile)) require(nodeFile)(RED);
10
+ });
11
+ };
package/nodes/.gitkeep ADDED
File without changes
@@ -0,0 +1,84 @@
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('as5600-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '36', required: true },
9
+ magnetCheck: { value: true }
10
+ },
11
+ label: function() {
12
+ return this.name || ('AS5600 @ 0x' + this.address + ' (bus ' + this.bus + ')');
13
+ }
14
+ });
15
+ </script>
16
+
17
+ <script type="text/html" data-template-name="as5600-device">
18
+ <div class="form-row">
19
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
20
+ <input type="text" id="node-config-input-name" placeholder="AS5600">
21
+ </div>
22
+ <div class="form-row">
23
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
24
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
25
+ </div>
26
+ <div class="form-row">
27
+ <label for="node-config-input-address">Address (hex)</label>
28
+ <input type="text" id="node-config-input-address" placeholder="36" style="width:80px"> &nbsp;default: 36 (0x36)
29
+ </div>
30
+ <div class="form-row">
31
+ <label for="node-config-input-magnetCheck">Magnet check on startup</label>
32
+ <input type="checkbox" id="node-config-input-magnetCheck" style="width:auto"> &nbsp;verify MD=1 at init
33
+ </div>
34
+ </script>
35
+
36
+ <!-- Read node: trigger → measurements -->
37
+ <script type="text/javascript">
38
+ RED.nodes.registerType('periph-as5600', {
39
+ category: 'Peripheral',
40
+ color: '#a6bbcf',
41
+ defaults: {
42
+ name: { value: '' },
43
+ device: { value: '', type: 'as5600-device', required: true }
44
+ },
45
+ inputs: 1,
46
+ outputs: 1,
47
+ icon: 'font-awesome/fa-magnet',
48
+ label: function() { return this.name || 'AS5600'; },
49
+ paletteLabel: 'AS5600'
50
+ });
51
+ </script>
52
+
53
+ <script type="text/html" data-template-name="periph-as5600">
54
+ <div class="form-row">
55
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
56
+ <input type="text" id="node-input-device">
57
+ </div>
58
+ <div class="form-row">
59
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
60
+ <input type="text" id="node-input-name" placeholder="AS5600">
61
+ </div>
62
+ </script>
63
+
64
+ <script type="text/html" data-help-name="periph-as5600">
65
+ <p>Reads the absolute angle, raw count, and magnet detection status from an AS5600
66
+ contactless rotary position sensor over I²C.</p>
67
+ <p>Any input message triggers a measurement. The device is configured via an
68
+ <b>as5600-device</b> config node.</p>
69
+
70
+ <h3>Output</h3>
71
+ <dl class="message-properties">
72
+ <dt>payload.angle <span class="property-type">number</span></dt>
73
+ <dd>Angle in degrees (0.0–360.0), scaled by any OTP-programmed ZPOS/MPOS range.</dd>
74
+ <dt>payload.raw <span class="property-type">number</span></dt>
75
+ <dd>Raw 12-bit angle count (0–4095), unaffected by ZPOS/MPOS.</dd>
76
+ <dt>payload.magnet_detected <span class="property-type">boolean</span></dt>
77
+ <dd>True if a magnet is detected (STATUS.MD=1, Bz ≥ 8 mT).</dd>
78
+ </dl>
79
+
80
+ <h3>Configuration</h3>
81
+ <p>Set the I²C bus number and the chip's fixed 7-bit address (default <code>0x36</code>).
82
+ Enable "Magnet check on startup" to verify that a magnet is present when the node
83
+ initialises — the AS5600 has no chip ID register, so MD=1 is the only presence check.</p>
84
+ </script>
@@ -0,0 +1,49 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CTransport } = require('periph/src/transport/i2c');
5
+ const { AS5600Full } = require('periph/src/chips/magnetometer/as5600');
6
+
7
+ function AS5600DeviceNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const transport = new I2CTransport(parseInt(config.bus), parseInt(config.address, 16));
12
+ node.driver = new AS5600Full(transport);
13
+ node.transport = transport;
14
+ } catch (e) {
15
+ node.error('AS5600 init failed: ' + e.message);
16
+ }
17
+ node.on('close', function() {
18
+ if (node.transport) node.transport.close();
19
+ });
20
+ }
21
+ RED.nodes.registerType('as5600-device', AS5600DeviceNode);
22
+
23
+ function AS5600ReadNode(config) {
24
+ RED.nodes.createNode(this, config);
25
+ const node = this;
26
+ node.device = RED.nodes.getNode(config.device);
27
+
28
+ node.on('input', function(msg, send, done) {
29
+ if (!node.device || !node.device.driver) {
30
+ node.error('No AS5600 device configured', msg);
31
+ done();
32
+ return;
33
+ }
34
+ try {
35
+ const d = node.device.driver;
36
+ msg.payload = {
37
+ angle: d.angle(),
38
+ raw: d.angleRaw(),
39
+ magnet_detected: d.isMagnetDetected()
40
+ };
41
+ send(msg);
42
+ done();
43
+ } catch (e) {
44
+ done(e);
45
+ }
46
+ });
47
+ }
48
+ RED.nodes.registerType('periph-as5600', AS5600ReadNode);
49
+ };
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "node-red-contrib-periph-magnetometer",
3
+ "version": "1.1.0",
4
+ "description": "Node-RED nodes for magnetometer chips",
5
+ "keywords": [
6
+ "node-red",
7
+ "magnetometer",
8
+ "i2c",
9
+ "spi",
10
+ "sensor",
11
+ "peripheral"
12
+ ],
13
+ "node-red": {
14
+ "nodes": {
15
+ "as5600": "nodes/as5600/as5600.js"
16
+ }
17
+ },
18
+ "dependencies": {
19
+ "periph": "*"
20
+ },
21
+ "license": "MIT"
22
+ }