node-red-contrib-periph-motor 1.3.3 → 1.4.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.
@@ -0,0 +1,136 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { SPIConnection } = require('periph/src/connection/spi');
5
+ const { UARTConnection } = require('periph/src/connection/uart');
6
+ const { TMC5160Full } = require('periph/src/chips/motor/tmc5160');
7
+
8
+ function gpio(spec, input) {
9
+ // "<chip>:<line>", e.g. "0:17" -- gpiod chip number and line offset
10
+ const [chip, line] = String(spec).split(':').map((v) => parseInt(v, 10));
11
+ const { Default } = require('opengpio');
12
+ if (input) {
13
+ const { PollingInputPin } = require('periph/src/connection/input_pin');
14
+ const pin = Default.input({ chip, line });
15
+ return new PollingInputPin(() => (pin.value ? 1 : 0), 1000);
16
+ }
17
+ const { GpioOutputPin } = require('periph/src/connection/output_pin');
18
+ return new GpioOutputPin(Default.output({ chip, line }));
19
+ }
20
+
21
+ function TMC5160Node(config) {
22
+ RED.nodes.createNode(this, config);
23
+ const node = this;
24
+ node.ready = false;
25
+ node.speed = parseFloat(config.speed) || 200;
26
+
27
+ (async () => {
28
+ try {
29
+ const uart = config.bus === 'uart';
30
+ let connection;
31
+ if (uart) {
32
+ connection = new UARTConnection(config.port, {
33
+ baudRate: parseInt(config.baudrate, 10) || 115200, timeoutMs: 100,
34
+ });
35
+ await connection.open();
36
+ } else {
37
+ connection = new SPIConnection(parseInt(config.spiBus, 10) || 0, parseInt(config.spiDevice, 10) || 0, {
38
+ mode: 3, maxSpeedHz: 1000000,
39
+ });
40
+ }
41
+ node.connection = connection;
42
+ node.driver = new TMC5160Full(connection, {
43
+ busType: uart ? 'uart' : 'spi',
44
+ rSenseOhm: parseFloat(config.rsense) || 0.075,
45
+ fclkHz: parseInt(config.fclk, 10) || 12000000,
46
+ enPin: config.enGpio ? gpio(config.enGpio, false) : null,
47
+ address: parseInt(config.address, 10) || 0,
48
+ echo: config.echo !== false,
49
+ diag0Pin: !uart && config.diag0Gpio ? gpio(config.diag0Gpio, true) : null,
50
+ diag1Pin: !uart && config.diag1Gpio ? gpio(config.diag1Gpio, true) : null,
51
+ });
52
+ await node.driver.init();
53
+ const current = parseFloat(config.current);
54
+ if (current > 0) await node.driver.setCurrent(current);
55
+ await node.driver.setSpeed(node.speed);
56
+ const accel = parseFloat(config.acceleration);
57
+ if (accel > 0) await node.driver.setAcceleration(accel);
58
+ node.ready = true;
59
+ } catch (e) {
60
+ node.error('TMC5160 init failed: ' + e.message);
61
+ }
62
+ })();
63
+
64
+ async function snapshot() {
65
+ const status = await node.driver.readStatus();
66
+ return {
67
+ position: await node.driver.position(),
68
+ velocity: await node.driver.velocity(),
69
+ position_reached: await node.driver.isPositionReached(),
70
+ standstill: status.stst,
71
+ fault: await node.driver.readFault(),
72
+ cs_actual: status.cs_actual,
73
+ sg_result: status.sg_result,
74
+ };
75
+ }
76
+
77
+ node.on('input', async function(msg, send, done) {
78
+ if (!node.driver || !node.ready) { done(); return; }
79
+ try {
80
+ const p = msg.payload;
81
+ let handled = false;
82
+ if (p && typeof p === 'object') {
83
+ if (typeof p.current === 'number') { await node.driver.setCurrent(p.current); handled = true; }
84
+ if (typeof p.acceleration === 'number') { await node.driver.setAcceleration(p.acceleration); handled = true; }
85
+ if (typeof p.stall_threshold === 'number') { await node.driver.setStallThreshold(p.stall_threshold); handled = true; }
86
+ if (p.zero === true) { await node.driver.setPosition(0); handled = true; }
87
+ if (typeof p.enable === 'boolean') {
88
+ if (p.enable) await node.driver.enable(); else await node.driver.disable();
89
+ handled = true;
90
+ }
91
+ if (typeof p.speed === 'number' && typeof p.move !== 'number') {
92
+ if (p.speed === 0) await node.driver.stop(); else await node.driver.run(p.speed);
93
+ handled = true;
94
+ }
95
+ if (p.stop === true) { await node.driver.stop(); handled = true; }
96
+ if (typeof p.move_to === 'number') { await node.driver.moveTo(p.move_to); handled = true; }
97
+ if (typeof p.move === 'number') {
98
+ await node.driver.move(p.move, typeof p.speed === 'number' ? p.speed : node.speed);
99
+ handled = true;
100
+ }
101
+ }
102
+ if (handled) {
103
+ msg.payload = await snapshot();
104
+ } else {
105
+ const snap = await snapshot();
106
+ const status = await node.driver.readStatus();
107
+ msg.payload = Object.assign(snap, {
108
+ tstep: await node.driver.readTstep(),
109
+ stallguard: status.stallguard,
110
+ otpw: status.otpw,
111
+ ot: status.ot,
112
+ });
113
+ }
114
+ send(msg);
115
+ done();
116
+ } catch (e) {
117
+ done(e);
118
+ }
119
+ });
120
+
121
+ node.on('close', async function(removed, done) {
122
+ try {
123
+ if (node.driver && node.ready) {
124
+ await node.driver.stop();
125
+ await node.driver.disable();
126
+ }
127
+ if (node.connection) await node.connection.close();
128
+ } catch (e) {
129
+ // bus may already be gone; nothing more to do on close
130
+ } finally {
131
+ done();
132
+ }
133
+ });
134
+ }
135
+ RED.nodes.registerType('periph-tmc5160', TMC5160Node);
136
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-motor",
3
- "version": "1.3.3",
3
+ "version": "1.4.0",
4
4
  "description": "Node-RED nodes for motor driver chips",
5
5
  "keywords": [
6
6
  "node-red",
@@ -13,7 +13,11 @@
13
13
  "node-red": {
14
14
  "nodes": {
15
15
  "drv2605": "nodes/drv2605/drv2605.js",
16
- "drv8830": "nodes/drv8830/drv8830.js"
16
+ "drv8830": "nodes/drv8830/drv8830.js",
17
+ "tmc2208": "nodes/tmc2208/tmc2208.js",
18
+ "tmc2209": "nodes/tmc2209/tmc2209.js",
19
+ "tmc2130": "nodes/tmc2130/tmc2130.js",
20
+ "tmc5160": "nodes/tmc5160/tmc5160.js"
17
21
  }
18
22
  },
19
23
  "dependencies": {