node-red-contrib-periph-motor 1.3.3 → 1.5.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 +4 -0
- package/examples/tmc2130/demo.json +114 -0
- package/examples/tmc2208/demo.json +151 -0
- package/examples/tmc2209/demo.json +151 -0
- package/examples/tmc5160/demo.json +26 -0
- package/nodes/tmc2130/tmc2130.html +116 -0
- package/nodes/tmc2130/tmc2130.js +106 -0
- package/nodes/tmc2208/tmc2208.html +92 -0
- package/nodes/tmc2208/tmc2208.js +106 -0
- package/nodes/tmc2209/tmc2209.html +106 -0
- package/nodes/tmc2209/tmc2209.js +119 -0
- package/nodes/tmc5160/tmc5160.html +95 -0
- package/nodes/tmc5160/tmc5160.js +136 -0
- package/package.json +6 -2
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = function(RED) {
|
|
4
|
+
const { SPIConnection } = require('periph/src/connection/spi');
|
|
5
|
+
const { TMC2130Full } = require('periph/src/chips/motor/tmc2130');
|
|
6
|
+
|
|
7
|
+
function gpioOut(spec) {
|
|
8
|
+
// "<chip>:<line>", e.g. "0:17" -- gpiod chip number and line offset
|
|
9
|
+
const [chip, line] = String(spec).split(':').map((v) => parseInt(v, 10));
|
|
10
|
+
const { Default } = require('opengpio');
|
|
11
|
+
const { GpioOutputPin } = require('periph/src/connection/output_pin');
|
|
12
|
+
return new GpioOutputPin(Default.output({ chip, line }));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function TMC2130Node(config) {
|
|
16
|
+
RED.nodes.createNode(this, config);
|
|
17
|
+
const node = this;
|
|
18
|
+
node.ready = false;
|
|
19
|
+
node.speed = parseFloat(config.speed) || 200;
|
|
20
|
+
|
|
21
|
+
(async () => {
|
|
22
|
+
try {
|
|
23
|
+
const connection = new SPIConnection(parseInt(config.bus, 10) || 0, parseInt(config.device, 10) || 0, {
|
|
24
|
+
mode: 3,
|
|
25
|
+
maxSpeedHz: parseInt(config.speedHz, 10) || 1000000,
|
|
26
|
+
});
|
|
27
|
+
node.connection = connection;
|
|
28
|
+
node.driver = new TMC2130Full(connection, gpioOut(config.stepGpio), gpioOut(config.dirGpio), {
|
|
29
|
+
rSenseOhm: parseFloat(config.rsense) || 0.11,
|
|
30
|
+
enPin: config.enGpio ? gpioOut(config.enGpio) : null,
|
|
31
|
+
});
|
|
32
|
+
await node.driver.init();
|
|
33
|
+
const current = parseFloat(config.current);
|
|
34
|
+
if (current > 0) await node.driver.setCurrent(current);
|
|
35
|
+
const microsteps = parseInt(config.microsteps, 10);
|
|
36
|
+
if (microsteps > 0) await node.driver.setMicrosteps(microsteps);
|
|
37
|
+
node.ready = true;
|
|
38
|
+
} catch (e) {
|
|
39
|
+
node.error('TMC2130 init failed: ' + e.message);
|
|
40
|
+
}
|
|
41
|
+
})();
|
|
42
|
+
|
|
43
|
+
async function snapshot() {
|
|
44
|
+
const status = await node.driver.readStatus();
|
|
45
|
+
return {
|
|
46
|
+
standstill: status.stst,
|
|
47
|
+
fault: await node.driver.readFault(),
|
|
48
|
+
cs_actual: status.cs_actual,
|
|
49
|
+
sg_result: status.sg_result,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
node.on('input', async function(msg, send, done) {
|
|
54
|
+
if (!node.driver || !node.ready) { done(); return; }
|
|
55
|
+
try {
|
|
56
|
+
const p = msg.payload;
|
|
57
|
+
let handled = false;
|
|
58
|
+
if (p && typeof p === 'object') {
|
|
59
|
+
if (typeof p.microsteps === 'number') { await node.driver.setMicrosteps(p.microsteps); handled = true; }
|
|
60
|
+
if (typeof p.current === 'number') { await node.driver.setCurrent(p.current); handled = true; }
|
|
61
|
+
if (typeof p.stall_threshold === 'number') { await node.driver.setStallThreshold(p.stall_threshold); handled = true; }
|
|
62
|
+
if (typeof p.spreadcycle === 'boolean') { await node.driver.setChopperMode(p.spreadcycle); handled = true; }
|
|
63
|
+
if (typeof p.enable === 'boolean') {
|
|
64
|
+
if (p.enable) await node.driver.enable(); else await node.driver.disable();
|
|
65
|
+
handled = true;
|
|
66
|
+
}
|
|
67
|
+
if (typeof p.move === 'number') {
|
|
68
|
+
await node.driver.move(p.move, typeof p.speed === 'number' ? p.speed : node.speed);
|
|
69
|
+
handled = true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (handled) {
|
|
73
|
+
msg.payload = await snapshot();
|
|
74
|
+
} else {
|
|
75
|
+
const status = await node.driver.readStatus();
|
|
76
|
+
msg.payload = {
|
|
77
|
+
standstill: status.stst,
|
|
78
|
+
fault: await node.driver.readFault(),
|
|
79
|
+
cs_actual: status.cs_actual,
|
|
80
|
+
sg_result: status.sg_result,
|
|
81
|
+
tstep: await node.driver.readTstep(),
|
|
82
|
+
stallguard: status.stallguard,
|
|
83
|
+
otpw: status.otpw,
|
|
84
|
+
ot: status.ot,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
send(msg);
|
|
88
|
+
done();
|
|
89
|
+
} catch (e) {
|
|
90
|
+
done(e);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
node.on('close', async function(removed, done) {
|
|
95
|
+
try {
|
|
96
|
+
if (node.driver && node.ready) await node.driver.disable();
|
|
97
|
+
if (node.connection) await node.connection.close();
|
|
98
|
+
} catch (e) {
|
|
99
|
+
// bus may already be gone; nothing more to do on close
|
|
100
|
+
} finally {
|
|
101
|
+
done();
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
RED.nodes.registerType('periph-tmc2130', TMC2130Node);
|
|
106
|
+
};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType('periph-tmc2208', {
|
|
3
|
+
category: 'Peripheral',
|
|
4
|
+
color: '#C084FC',
|
|
5
|
+
defaults: {
|
|
6
|
+
name: { value: '' },
|
|
7
|
+
port: { value: '/dev/ttyS0', required: true },
|
|
8
|
+
baudrate: { value: '115200', required: true, validate: RED.validators.number() },
|
|
9
|
+
rsense: { value: '0.11', required: true, validate: RED.validators.number() },
|
|
10
|
+
current: { value: '0.5', validate: RED.validators.number() },
|
|
11
|
+
microsteps: { value: '16' },
|
|
12
|
+
enGpio: { value: '' }
|
|
13
|
+
},
|
|
14
|
+
inputs: 1,
|
|
15
|
+
outputs: 1,
|
|
16
|
+
icon: 'font-awesome/fa-cog',
|
|
17
|
+
label: function() { return this.name || 'TMC2208'; },
|
|
18
|
+
paletteLabel: 'TMC2208'
|
|
19
|
+
});
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<script type="text/html" data-template-name="periph-tmc2208">
|
|
23
|
+
<div class="form-row">
|
|
24
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
25
|
+
<input type="text" id="node-input-name" placeholder="TMC2208">
|
|
26
|
+
</div>
|
|
27
|
+
<div class="form-row">
|
|
28
|
+
<label for="node-input-port"><i class="fa fa-plug"></i> Serial port</label>
|
|
29
|
+
<input type="text" id="node-input-port" placeholder="/dev/ttyS0">
|
|
30
|
+
</div>
|
|
31
|
+
<div class="form-row">
|
|
32
|
+
<label for="node-input-baudrate"><i class="fa fa-tachometer"></i> Baud rate</label>
|
|
33
|
+
<input type="number" id="node-input-baudrate" min="9000" style="width:100px">
|
|
34
|
+
</div>
|
|
35
|
+
<div class="form-row">
|
|
36
|
+
<label for="node-input-rsense"><i class="fa fa-bolt"></i> Sense resistor (Ω)</label>
|
|
37
|
+
<input type="number" id="node-input-rsense" min="0.01" step="0.01" style="width:100px">
|
|
38
|
+
</div>
|
|
39
|
+
<div class="form-row">
|
|
40
|
+
<label for="node-input-current"><i class="fa fa-bolt"></i> Default current (A RMS)</label>
|
|
41
|
+
<input type="number" id="node-input-current" min="0" max="1.2" step="0.05" style="width:100px">
|
|
42
|
+
</div>
|
|
43
|
+
<div class="form-row">
|
|
44
|
+
<label for="node-input-microsteps"><i class="fa fa-th"></i> Default microsteps</label>
|
|
45
|
+
<select id="node-input-microsteps" style="width:100px">
|
|
46
|
+
<option value="1">1</option><option value="2">2</option><option value="4">4</option>
|
|
47
|
+
<option value="8">8</option><option value="16">16</option><option value="32">32</option>
|
|
48
|
+
<option value="64">64</option><option value="128">128</option><option value="256">256</option>
|
|
49
|
+
</select>
|
|
50
|
+
</div>
|
|
51
|
+
<div class="form-row">
|
|
52
|
+
<label for="node-input-enGpio"><i class="fa fa-microchip"></i> ENN GPIO (optional)</label>
|
|
53
|
+
<input type="text" id="node-input-enGpio" placeholder="not wired, e.g. 0:17" style="width:140px">
|
|
54
|
+
</div>
|
|
55
|
+
</script>
|
|
56
|
+
|
|
57
|
+
<script type="text/html" data-help-name="periph-tmc2208">
|
|
58
|
+
<p>Drives a two-phase bipolar stepper motor through a Trinamic TMC2202 / TMC2208 / TMC2224 over its single-wire UART. The chip's internal step generator moves the motor, so no STEP pulses are needed.</p>
|
|
59
|
+
|
|
60
|
+
<h3>Inputs</h3>
|
|
61
|
+
<dl class="message-properties">
|
|
62
|
+
<dt>payload.speed <span class="property-type">number</span></dt>
|
|
63
|
+
<dd>Full steps per second at the configured microstepping; negative = reverse, <code>0</code> stops.</dd>
|
|
64
|
+
<dt>payload.current <span class="property-type">number</span></dt>
|
|
65
|
+
<dd>Run current in A RMS (at most 1.2); hold current becomes one third of it.</dd>
|
|
66
|
+
<dt>payload.microsteps <span class="property-type">number</span></dt>
|
|
67
|
+
<dd>Microstep resolution: 1, 2, 4, 8, 16, 32, 64, 128 or 256.</dd>
|
|
68
|
+
<dt>payload.enable <span class="property-type">boolean</span></dt>
|
|
69
|
+
<dd><code>true</code> switches the power stage on, <code>false</code> off (the motor freewheels).</dd>
|
|
70
|
+
<dt>payload.stop <span class="property-type">boolean</span></dt>
|
|
71
|
+
<dd><code>true</code> stops the internal step generator.</dd>
|
|
72
|
+
<dt>payload <span class="property-type">any</span></dt>
|
|
73
|
+
<dd>Any other message polls the driver status.</dd>
|
|
74
|
+
</dl>
|
|
75
|
+
|
|
76
|
+
<h3>Outputs</h3>
|
|
77
|
+
<dl class="message-properties">
|
|
78
|
+
<dt>payload.speed <span class="property-type">number</span></dt>
|
|
79
|
+
<dd>Echo of the commanded speed (command messages only).</dd>
|
|
80
|
+
<dt>payload.standstill <span class="property-type">boolean</span></dt>
|
|
81
|
+
<dd>The motor is at standstill.</dd>
|
|
82
|
+
<dt>payload.fault <span class="property-type">number</span></dt>
|
|
83
|
+
<dd>Fault bitmask: 1 over-temperature pre-warning, 2 over-temperature, 4 short to GND, 8 short to supply, 16 open load; <code>0</code> = none.</dd>
|
|
84
|
+
<dt>payload.cs_actual <span class="property-type">number</span></dt>
|
|
85
|
+
<dd>Actual current scale 0–31.</dd>
|
|
86
|
+
<dt>payload.stealth, tstep, temperature_flags</dt>
|
|
87
|
+
<dd>Status polls only: StealthChop active, time between microsteps, temperature comparator bits (t120…t157 as bits 0–3).</dd>
|
|
88
|
+
</dl>
|
|
89
|
+
|
|
90
|
+
<h3>Configuration</h3>
|
|
91
|
+
<p>Wire MCU TX through a 1 kΩ resistor to PDN_UART and MCU RX directly to PDN_UART. Set the serial port and baud rate (9 kBd and up; the chip auto-detects it), the external sense resistor value (0.11 Ω on common boards), and the default run current and microsteps applied at start. The optional ENN GPIO field (<code>chip:line</code>, e.g. <code>0:17</code>, needs the <code>opengpio</code> package) drives the ENN pin; without it the node switches the power stage through the chopper (<code>TOFF</code>). The motor is stopped and the driver disabled when the node is closed.</p>
|
|
92
|
+
</script>
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = function(RED) {
|
|
4
|
+
const { UARTConnection } = require('periph/src/connection/uart');
|
|
5
|
+
const { TMC2208Full } = require('periph/src/chips/motor/tmc2208');
|
|
6
|
+
|
|
7
|
+
function TMC2208Node(config) {
|
|
8
|
+
RED.nodes.createNode(this, config);
|
|
9
|
+
const node = this;
|
|
10
|
+
node.ready = false;
|
|
11
|
+
|
|
12
|
+
(async () => {
|
|
13
|
+
try {
|
|
14
|
+
const connection = new UARTConnection(config.port, {
|
|
15
|
+
baudRate: parseInt(config.baudrate, 10) || 115200,
|
|
16
|
+
timeoutMs: 100,
|
|
17
|
+
});
|
|
18
|
+
await connection.open();
|
|
19
|
+
node.connection = connection;
|
|
20
|
+
let enPin = null;
|
|
21
|
+
if (config.enGpio) {
|
|
22
|
+
// "<chip>:<line>", e.g. "0:17" -- gpiod chip number and line offset
|
|
23
|
+
const [chip, line] = String(config.enGpio).split(':').map((v) => parseInt(v, 10));
|
|
24
|
+
const { Default } = require('opengpio');
|
|
25
|
+
const { GpioOutputPin } = require('periph/src/connection/output_pin');
|
|
26
|
+
enPin = new GpioOutputPin(Default.output({ chip, line }));
|
|
27
|
+
}
|
|
28
|
+
node.enPin = enPin;
|
|
29
|
+
node.driver = new TMC2208Full(connection, {
|
|
30
|
+
rSenseOhm: parseFloat(config.rsense) || 0.11,
|
|
31
|
+
enPin,
|
|
32
|
+
});
|
|
33
|
+
await node.driver.init();
|
|
34
|
+
const current = parseFloat(config.current);
|
|
35
|
+
if (current > 0) await node.driver.setCurrent(current);
|
|
36
|
+
const microsteps = parseInt(config.microsteps, 10);
|
|
37
|
+
if (microsteps > 0) await node.driver.setMicrosteps(microsteps);
|
|
38
|
+
node.ready = true;
|
|
39
|
+
} catch (e) {
|
|
40
|
+
node.error('TMC2208 init failed: ' + e.message);
|
|
41
|
+
}
|
|
42
|
+
})();
|
|
43
|
+
|
|
44
|
+
async function snapshot(extra) {
|
|
45
|
+
const status = await node.driver.readStatus();
|
|
46
|
+
const fault = await node.driver.readFault();
|
|
47
|
+
return Object.assign({
|
|
48
|
+
standstill: status.stst,
|
|
49
|
+
stealth: status.stealth,
|
|
50
|
+
fault,
|
|
51
|
+
cs_actual: status.cs_actual,
|
|
52
|
+
}, extra);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
node.on('input', async function(msg, send, done) {
|
|
56
|
+
if (!node.driver || !node.ready) { done(); return; }
|
|
57
|
+
try {
|
|
58
|
+
const p = msg.payload;
|
|
59
|
+
let handled = false;
|
|
60
|
+
let extra = {};
|
|
61
|
+
if (p && typeof p === 'object') {
|
|
62
|
+
if (typeof p.microsteps === 'number') { await node.driver.setMicrosteps(p.microsteps); handled = true; }
|
|
63
|
+
if (typeof p.current === 'number') { await node.driver.setCurrent(p.current); handled = true; }
|
|
64
|
+
if (typeof p.enable === 'boolean') {
|
|
65
|
+
if (p.enable) await node.driver.enable(); else await node.driver.disable();
|
|
66
|
+
handled = true;
|
|
67
|
+
}
|
|
68
|
+
if (p.stop === true) { await node.driver.stop(); extra.speed = 0; handled = true; }
|
|
69
|
+
if (typeof p.speed === 'number') { await node.driver.run(p.speed); extra.speed = p.speed; handled = true; }
|
|
70
|
+
}
|
|
71
|
+
if (handled) {
|
|
72
|
+
msg.payload = await snapshot(extra);
|
|
73
|
+
} else {
|
|
74
|
+
const status = await node.driver.readStatus();
|
|
75
|
+
msg.payload = {
|
|
76
|
+
standstill: status.stst,
|
|
77
|
+
stealth: status.stealth,
|
|
78
|
+
fault: await node.driver.readFault(),
|
|
79
|
+
cs_actual: status.cs_actual,
|
|
80
|
+
tstep: await node.driver.readTstep(),
|
|
81
|
+
temperature_flags: await node.driver.readTemperatureFlags(),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
send(msg);
|
|
85
|
+
done();
|
|
86
|
+
} catch (e) {
|
|
87
|
+
done(e);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
node.on('close', async function(removed, done) {
|
|
92
|
+
try {
|
|
93
|
+
if (node.driver && node.ready) {
|
|
94
|
+
await node.driver.stop();
|
|
95
|
+
await node.driver.disable();
|
|
96
|
+
}
|
|
97
|
+
if (node.connection) await node.connection.close();
|
|
98
|
+
} catch (e) {
|
|
99
|
+
// port may already be gone; nothing more to do on close
|
|
100
|
+
} finally {
|
|
101
|
+
done();
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
RED.nodes.registerType('periph-tmc2208', TMC2208Node);
|
|
106
|
+
};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType('periph-tmc2209', {
|
|
3
|
+
category: 'Peripheral',
|
|
4
|
+
color: '#C084FC',
|
|
5
|
+
defaults: {
|
|
6
|
+
name: { value: '' },
|
|
7
|
+
port: { value: '/dev/ttyS0', required: true },
|
|
8
|
+
baudrate: { value: '115200', required: true, validate: RED.validators.number() },
|
|
9
|
+
address: { value: '0', validate: RED.validators.number() },
|
|
10
|
+
rsense: { value: '0.11', required: true, validate: RED.validators.number() },
|
|
11
|
+
current: { value: '0.5', validate: RED.validators.number() },
|
|
12
|
+
microsteps: { value: '16' },
|
|
13
|
+
enGpio: { value: '' },
|
|
14
|
+
diagGpio: { value: '' }
|
|
15
|
+
},
|
|
16
|
+
inputs: 1,
|
|
17
|
+
outputs: 1,
|
|
18
|
+
icon: 'font-awesome/fa-cog',
|
|
19
|
+
label: function() { return this.name || 'TMC2209'; },
|
|
20
|
+
paletteLabel: 'TMC2209'
|
|
21
|
+
});
|
|
22
|
+
</script>
|
|
23
|
+
|
|
24
|
+
<script type="text/html" data-template-name="periph-tmc2209">
|
|
25
|
+
<div class="form-row">
|
|
26
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
27
|
+
<input type="text" id="node-input-name" placeholder="TMC2209">
|
|
28
|
+
</div>
|
|
29
|
+
<div class="form-row">
|
|
30
|
+
<label for="node-input-port"><i class="fa fa-plug"></i> Serial port</label>
|
|
31
|
+
<input type="text" id="node-input-port" placeholder="/dev/ttyS0">
|
|
32
|
+
</div>
|
|
33
|
+
<div class="form-row">
|
|
34
|
+
<label for="node-input-baudrate"><i class="fa fa-tachometer"></i> Baud rate</label>
|
|
35
|
+
<input type="number" id="node-input-baudrate" min="9000" style="width:100px">
|
|
36
|
+
</div>
|
|
37
|
+
<div class="form-row">
|
|
38
|
+
<label for="node-input-address"><i class="fa fa-hashtag"></i> Node address (0–3)</label>
|
|
39
|
+
<input type="number" id="node-input-address" min="0" max="3" style="width:100px">
|
|
40
|
+
</div>
|
|
41
|
+
<div class="form-row">
|
|
42
|
+
<label for="node-input-rsense"><i class="fa fa-bolt"></i> Sense resistor (Ω)</label>
|
|
43
|
+
<input type="number" id="node-input-rsense" min="0.01" step="0.01" style="width:100px">
|
|
44
|
+
</div>
|
|
45
|
+
<div class="form-row">
|
|
46
|
+
<label for="node-input-current"><i class="fa fa-bolt"></i> Default current (A RMS)</label>
|
|
47
|
+
<input type="number" id="node-input-current" min="0" max="1.4" step="0.05" style="width:100px">
|
|
48
|
+
</div>
|
|
49
|
+
<div class="form-row">
|
|
50
|
+
<label for="node-input-microsteps"><i class="fa fa-th"></i> Default microsteps</label>
|
|
51
|
+
<select id="node-input-microsteps" style="width:100px">
|
|
52
|
+
<option value="1">1</option><option value="2">2</option><option value="4">4</option>
|
|
53
|
+
<option value="8">8</option><option value="16">16</option><option value="32">32</option>
|
|
54
|
+
<option value="64">64</option><option value="128">128</option><option value="256">256</option>
|
|
55
|
+
</select>
|
|
56
|
+
</div>
|
|
57
|
+
<div class="form-row">
|
|
58
|
+
<label for="node-input-enGpio"><i class="fa fa-microchip"></i> ENN GPIO (optional)</label>
|
|
59
|
+
<input type="text" id="node-input-enGpio" placeholder="not wired, e.g. 0:17" style="width:140px">
|
|
60
|
+
</div>
|
|
61
|
+
<div class="form-row">
|
|
62
|
+
<label for="node-input-diagGpio"><i class="fa fa-microchip"></i> DIAG GPIO (optional)</label>
|
|
63
|
+
<input type="text" id="node-input-diagGpio" placeholder="not wired, e.g. 0:27" style="width:140px">
|
|
64
|
+
</div>
|
|
65
|
+
</script>
|
|
66
|
+
|
|
67
|
+
<script type="text/html" data-help-name="periph-tmc2209">
|
|
68
|
+
<p>Drives a two-phase bipolar stepper motor through a Trinamic TMC2209 over its single-wire UART. The chip's internal step generator moves the motor, so no STEP pulses are needed.</p>
|
|
69
|
+
|
|
70
|
+
<h3>Inputs</h3>
|
|
71
|
+
<dl class="message-properties">
|
|
72
|
+
<dt>payload.speed <span class="property-type">number</span></dt>
|
|
73
|
+
<dd>Full steps per second at the configured microstepping; negative = reverse, <code>0</code> stops.</dd>
|
|
74
|
+
<dt>payload.current <span class="property-type">number</span></dt>
|
|
75
|
+
<dd>Run current in A RMS (at most 1.4); hold current becomes one third of it.</dd>
|
|
76
|
+
<dt>payload.stall_threshold <span class="property-type">number</span></dt>
|
|
77
|
+
<dd>StallGuard4 threshold <code>SGTHRS</code> 0–255; a stall is signalled when <code>sg_result ≤ 2 · stall_threshold</code>.</dd>
|
|
78
|
+
<dt>payload.microsteps <span class="property-type">number</span></dt>
|
|
79
|
+
<dd>Microstep resolution: 1, 2, 4, 8, 16, 32, 64, 128 or 256.</dd>
|
|
80
|
+
<dt>payload.enable <span class="property-type">boolean</span></dt>
|
|
81
|
+
<dd><code>true</code> switches the power stage on, <code>false</code> off (the motor freewheels).</dd>
|
|
82
|
+
<dt>payload.stop <span class="property-type">boolean</span></dt>
|
|
83
|
+
<dd><code>true</code> stops the internal step generator.</dd>
|
|
84
|
+
<dt>payload <span class="property-type">any</span></dt>
|
|
85
|
+
<dd>Any other message polls the driver status.</dd>
|
|
86
|
+
</dl>
|
|
87
|
+
|
|
88
|
+
<h3>Outputs</h3>
|
|
89
|
+
<dl class="message-properties">
|
|
90
|
+
<dt>payload.speed <span class="property-type">number</span></dt>
|
|
91
|
+
<dd>Echo of the commanded speed (command messages only).</dd>
|
|
92
|
+
<dt>payload.standstill <span class="property-type">boolean</span></dt>
|
|
93
|
+
<dd>The motor is at standstill.</dd>
|
|
94
|
+
<dt>payload.fault <span class="property-type">number</span></dt>
|
|
95
|
+
<dd>Fault bitmask: 1 over-temperature pre-warning, 2 over-temperature, 4 short to GND, 8 short to supply, 16 open load; <code>0</code> = none.</dd>
|
|
96
|
+
<dt>payload.cs_actual <span class="property-type">number</span></dt>
|
|
97
|
+
<dd>Actual current scale 0–31.</dd>
|
|
98
|
+
<dt>payload.sg_result <span class="property-type">number</span></dt>
|
|
99
|
+
<dd>StallGuard4 result 0–510 (higher = less mechanical load).</dd>
|
|
100
|
+
<dt>payload.stealth, tstep, temperature_flags</dt>
|
|
101
|
+
<dd>Status polls only: StealthChop active, time between microsteps, temperature comparator bits (t120…t157 as bits 0–3).</dd>
|
|
102
|
+
</dl>
|
|
103
|
+
|
|
104
|
+
<h3>Configuration</h3>
|
|
105
|
+
<p>Wire MCU TX through a 1 kΩ resistor to PDN_UART and MCU RX directly to PDN_UART. Set the serial port, the node address (0–3, as set by the MS1_AD0/MS2_AD1 pins) and baud rate (9 kBd and up; the chip auto-detects it), the external sense resistor value (0.11 Ω on common boards), and the default run current and microsteps applied at start. The optional DIAG GPIO field (<code>chip:line</code>) reads the stall / error output. The optional ENN GPIO field (<code>chip:line</code>, e.g. <code>0:17</code>, needs the <code>opengpio</code> package) drives the ENN pin; without it the node switches the power stage through the chopper (<code>TOFF</code>). The motor is stopped and the driver disabled when the node is closed.</p>
|
|
106
|
+
</script>
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = function(RED) {
|
|
4
|
+
const { UARTConnection } = require('periph/src/connection/uart');
|
|
5
|
+
const { TMC2209Full } = require('periph/src/chips/motor/tmc2209');
|
|
6
|
+
|
|
7
|
+
function TMC2209Node(config) {
|
|
8
|
+
RED.nodes.createNode(this, config);
|
|
9
|
+
const node = this;
|
|
10
|
+
node.ready = false;
|
|
11
|
+
|
|
12
|
+
(async () => {
|
|
13
|
+
try {
|
|
14
|
+
const connection = new UARTConnection(config.port, {
|
|
15
|
+
baudRate: parseInt(config.baudrate, 10) || 115200,
|
|
16
|
+
timeoutMs: 100,
|
|
17
|
+
});
|
|
18
|
+
await connection.open();
|
|
19
|
+
node.connection = connection;
|
|
20
|
+
let enPin = null;
|
|
21
|
+
if (config.enGpio) {
|
|
22
|
+
// "<chip>:<line>", e.g. "0:17" -- gpiod chip number and line offset
|
|
23
|
+
const [chip, line] = String(config.enGpio).split(':').map((v) => parseInt(v, 10));
|
|
24
|
+
const { Default } = require('opengpio');
|
|
25
|
+
const { GpioOutputPin } = require('periph/src/connection/output_pin');
|
|
26
|
+
enPin = new GpioOutputPin(Default.output({ chip, line }));
|
|
27
|
+
}
|
|
28
|
+
node.enPin = enPin;
|
|
29
|
+
let diagPin = null;
|
|
30
|
+
if (config.diagGpio) {
|
|
31
|
+
const [chip, line] = String(config.diagGpio).split(':').map((v) => parseInt(v, 10));
|
|
32
|
+
const { Default } = require('opengpio');
|
|
33
|
+
const { PollingInputPin } = require('periph/src/connection/input_pin');
|
|
34
|
+
const diagIn = Default.input({ chip, line });
|
|
35
|
+
diagPin = new PollingInputPin(() => (diagIn.value ? 1 : 0), 1000);
|
|
36
|
+
}
|
|
37
|
+
node.driver = new TMC2209Full(connection, {
|
|
38
|
+
rSenseOhm: parseFloat(config.rsense) || 0.11,
|
|
39
|
+
address: parseInt(config.address, 10) || 0,
|
|
40
|
+
enPin,
|
|
41
|
+
diagPin,
|
|
42
|
+
});
|
|
43
|
+
await node.driver.init();
|
|
44
|
+
const current = parseFloat(config.current);
|
|
45
|
+
if (current > 0) await node.driver.setCurrent(current);
|
|
46
|
+
const microsteps = parseInt(config.microsteps, 10);
|
|
47
|
+
if (microsteps > 0) await node.driver.setMicrosteps(microsteps);
|
|
48
|
+
node.ready = true;
|
|
49
|
+
} catch (e) {
|
|
50
|
+
node.error('TMC2209 init failed: ' + e.message);
|
|
51
|
+
}
|
|
52
|
+
})();
|
|
53
|
+
|
|
54
|
+
async function snapshot(extra) {
|
|
55
|
+
const status = await node.driver.readStatus();
|
|
56
|
+
const fault = await node.driver.readFault();
|
|
57
|
+
return Object.assign({
|
|
58
|
+
standstill: status.stst,
|
|
59
|
+
stealth: status.stealth,
|
|
60
|
+
fault,
|
|
61
|
+
cs_actual: status.cs_actual,
|
|
62
|
+
sg_result: await node.driver.readStallguard(),
|
|
63
|
+
}, extra);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
node.on('input', async function(msg, send, done) {
|
|
67
|
+
if (!node.driver || !node.ready) { done(); return; }
|
|
68
|
+
try {
|
|
69
|
+
const p = msg.payload;
|
|
70
|
+
let handled = false;
|
|
71
|
+
let extra = {};
|
|
72
|
+
if (p && typeof p === 'object') {
|
|
73
|
+
if (typeof p.microsteps === 'number') { await node.driver.setMicrosteps(p.microsteps); handled = true; }
|
|
74
|
+
if (typeof p.current === 'number') { await node.driver.setCurrent(p.current); handled = true; }
|
|
75
|
+
if (typeof p.enable === 'boolean') {
|
|
76
|
+
if (p.enable) await node.driver.enable(); else await node.driver.disable();
|
|
77
|
+
handled = true;
|
|
78
|
+
}
|
|
79
|
+
if (typeof p.stall_threshold === 'number') { await node.driver.setStallThreshold(p.stall_threshold); handled = true; }
|
|
80
|
+
if (p.stop === true) { await node.driver.stop(); extra.speed = 0; handled = true; }
|
|
81
|
+
if (typeof p.speed === 'number') { await node.driver.run(p.speed); extra.speed = p.speed; handled = true; }
|
|
82
|
+
}
|
|
83
|
+
if (handled) {
|
|
84
|
+
msg.payload = await snapshot(extra);
|
|
85
|
+
} else {
|
|
86
|
+
const status = await node.driver.readStatus();
|
|
87
|
+
msg.payload = {
|
|
88
|
+
standstill: status.stst,
|
|
89
|
+
stealth: status.stealth,
|
|
90
|
+
fault: await node.driver.readFault(),
|
|
91
|
+
cs_actual: status.cs_actual,
|
|
92
|
+
sg_result: await node.driver.readStallguard(),
|
|
93
|
+
tstep: await node.driver.readTstep(),
|
|
94
|
+
temperature_flags: await node.driver.readTemperatureFlags(),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
send(msg);
|
|
98
|
+
done();
|
|
99
|
+
} catch (e) {
|
|
100
|
+
done(e);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
node.on('close', async function(removed, done) {
|
|
105
|
+
try {
|
|
106
|
+
if (node.driver && node.ready) {
|
|
107
|
+
await node.driver.stop();
|
|
108
|
+
await node.driver.disable();
|
|
109
|
+
}
|
|
110
|
+
if (node.connection) await node.connection.close();
|
|
111
|
+
} catch (e) {
|
|
112
|
+
// port may already be gone; nothing more to do on close
|
|
113
|
+
} finally {
|
|
114
|
+
done();
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
RED.nodes.registerType('periph-tmc2209', TMC2209Node);
|
|
119
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType('periph-tmc5160', {
|
|
3
|
+
category: 'Peripheral',
|
|
4
|
+
color: '#C084FC',
|
|
5
|
+
defaults: {
|
|
6
|
+
name: { value: '' },
|
|
7
|
+
bus: { value: 'spi', required: true },
|
|
8
|
+
spiBus: { value: '0', validate: RED.validators.number() },
|
|
9
|
+
spiDevice: { value: '0', validate: RED.validators.number() },
|
|
10
|
+
port: { value: '/dev/ttyS0' },
|
|
11
|
+
baudrate: { value: '115200', validate: RED.validators.number() },
|
|
12
|
+
address: { value: '0', validate: RED.validators.number() },
|
|
13
|
+
echo: { value: true },
|
|
14
|
+
enGpio: { value: '' },
|
|
15
|
+
diag0Gpio: { value: '' },
|
|
16
|
+
diag1Gpio: { value: '' },
|
|
17
|
+
rsense: { value: '0.075', required: true, validate: RED.validators.number() },
|
|
18
|
+
fclk: { value: '12000000', validate: RED.validators.number() },
|
|
19
|
+
current: { value: '1.0', validate: RED.validators.number() },
|
|
20
|
+
speed: { value: '200', validate: RED.validators.number() },
|
|
21
|
+
acceleration: { value: '400', validate: RED.validators.number() }
|
|
22
|
+
},
|
|
23
|
+
inputs: 1,
|
|
24
|
+
outputs: 1,
|
|
25
|
+
icon: 'font-awesome/fa-cog',
|
|
26
|
+
label: function() { return this.name || 'TMC5160'; },
|
|
27
|
+
paletteLabel: 'TMC5160'
|
|
28
|
+
});
|
|
29
|
+
</script>
|
|
30
|
+
|
|
31
|
+
<script type="text/html" data-template-name="periph-tmc5160">
|
|
32
|
+
<div class="form-row">
|
|
33
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
34
|
+
<input type="text" id="node-input-name" placeholder="TMC5160">
|
|
35
|
+
</div>
|
|
36
|
+
<div class="form-row">
|
|
37
|
+
<label for="node-input-bus"><i class="fa fa-plug"></i> Bus</label>
|
|
38
|
+
<select id="node-input-bus" style="width:100px"><option value="spi">SPI</option><option value="uart">UART</option></select>
|
|
39
|
+
</div>
|
|
40
|
+
<div class="form-row"><label for="node-input-spiBus">SPI bus</label><input type="number" id="node-input-spiBus" min="0" style="width:100px"></div>
|
|
41
|
+
<div class="form-row"><label for="node-input-spiDevice">SPI device (CS)</label><input type="number" id="node-input-spiDevice" min="0" style="width:100px"></div>
|
|
42
|
+
<div class="form-row"><label for="node-input-port">Serial port</label><input type="text" id="node-input-port" placeholder="/dev/ttyS0"></div>
|
|
43
|
+
<div class="form-row"><label for="node-input-baudrate">Baud rate</label><input type="number" id="node-input-baudrate" min="9000" style="width:100px"></div>
|
|
44
|
+
<div class="form-row"><label for="node-input-address">Node address</label><input type="number" id="node-input-address" min="0" max="253" style="width:100px"></div>
|
|
45
|
+
<div class="form-row"><label for="node-input-echo">UART echo</label><input type="checkbox" id="node-input-echo" style="width:auto"></div>
|
|
46
|
+
<div class="form-row"><label for="node-input-enGpio">DRV_ENN GPIO (optional)</label><input type="text" id="node-input-enGpio" placeholder="not wired, e.g. 0:17" style="width:140px"></div>
|
|
47
|
+
<div class="form-row"><label for="node-input-diag0Gpio">DIAG0 GPIO (SPI, optional)</label><input type="text" id="node-input-diag0Gpio" placeholder="e.g. 0:23" style="width:140px"></div>
|
|
48
|
+
<div class="form-row"><label for="node-input-diag1Gpio">DIAG1 GPIO (SPI, optional)</label><input type="text" id="node-input-diag1Gpio" placeholder="e.g. 0:24" style="width:140px"></div>
|
|
49
|
+
<div class="form-row"><label for="node-input-rsense">Sense resistor (Ω)</label><input type="number" id="node-input-rsense" min="0.01" step="0.005" style="width:100px"></div>
|
|
50
|
+
<div class="form-row"><label for="node-input-fclk">Chip clock (Hz)</label><input type="number" id="node-input-fclk" min="4000000" style="width:120px"></div>
|
|
51
|
+
<div class="form-row"><label for="node-input-current">Default current (A RMS)</label><input type="number" id="node-input-current" min="0" step="0.1" style="width:100px"></div>
|
|
52
|
+
<div class="form-row"><label for="node-input-speed">Default speed (full steps/s)</label><input type="number" id="node-input-speed" min="1" style="width:100px"></div>
|
|
53
|
+
<div class="form-row"><label for="node-input-acceleration">Default acceleration (full steps/s²)</label><input type="number" id="node-input-acceleration" min="1" style="width:100px"></div>
|
|
54
|
+
</script>
|
|
55
|
+
|
|
56
|
+
<script type="text/html" data-help-name="periph-tmc5160">
|
|
57
|
+
<p>Drives a two-phase bipolar stepper motor through a Trinamic TMC5160 over SPI (Mode 3) or single-wire UART. The chip's internal SixPoint ramp generator moves the motor; positions are in full steps, speeds in full steps/s and accelerations in full steps/s².</p>
|
|
58
|
+
|
|
59
|
+
<h3>Inputs</h3>
|
|
60
|
+
<dl class="message-properties">
|
|
61
|
+
<dt>payload.move_to <span class="property-type">number</span></dt>
|
|
62
|
+
<dd>Absolute target position in full steps (non-blocking).</dd>
|
|
63
|
+
<dt>payload.move <span class="property-type">number</span></dt>
|
|
64
|
+
<dd>Relative move in full steps; blocks until reached. <code>payload.speed</code> overrides the speed.</dd>
|
|
65
|
+
<dt>payload.speed <span class="property-type">number</span></dt>
|
|
66
|
+
<dd>Velocity mode in full steps/s (negative = reverse, <code>0</code> stops).</dd>
|
|
67
|
+
<dt>payload.stop <span class="property-type">boolean</span></dt>
|
|
68
|
+
<dd><code>true</code> decelerates to standstill.</dd>
|
|
69
|
+
<dt>payload.current / acceleration / stall_threshold <span class="property-type">number</span></dt>
|
|
70
|
+
<dd>Run current in A RMS; acceleration in full steps/s²; StallGuard2 threshold −64…63.</dd>
|
|
71
|
+
<dt>payload.zero <span class="property-type">boolean</span></dt>
|
|
72
|
+
<dd><code>true</code> defines the current position as 0.</dd>
|
|
73
|
+
<dt>payload.enable <span class="property-type">boolean</span></dt>
|
|
74
|
+
<dd><code>true</code> switches the power stage on, <code>false</code> off.</dd>
|
|
75
|
+
<dt>payload <span class="property-type">any</span></dt>
|
|
76
|
+
<dd>Any other message polls the driver status.</dd>
|
|
77
|
+
</dl>
|
|
78
|
+
|
|
79
|
+
<h3>Outputs</h3>
|
|
80
|
+
<dl class="message-properties">
|
|
81
|
+
<dt>payload.position, velocity <span class="property-type">number</span></dt>
|
|
82
|
+
<dd>Actual position (full steps) and velocity (full steps/s).</dd>
|
|
83
|
+
<dt>payload.position_reached, standstill <span class="property-type">boolean</span></dt>
|
|
84
|
+
<dd>Ramp and driver state.</dd>
|
|
85
|
+
<dt>payload.fault <span class="property-type">number</span></dt>
|
|
86
|
+
<dd>Bitmask: 1 over-temperature pre-warning, 2 over-temperature, 4 short to GND, 8 short to supply, 16 open load; <code>0</code> = none.</dd>
|
|
87
|
+
<dt>payload.cs_actual, sg_result <span class="property-type">number</span></dt>
|
|
88
|
+
<dd>Actual current scale 0–31 and StallGuard2 result 0–1023.</dd>
|
|
89
|
+
<dt>payload.tstep, stallguard, otpw, ot</dt>
|
|
90
|
+
<dd>Status polls only.</dd>
|
|
91
|
+
</dl>
|
|
92
|
+
|
|
93
|
+
<h3>Configuration</h3>
|
|
94
|
+
<p>Choose the bus. SPI: bus and device numbers (<code>/dev/spidevB.D</code>), optional DIAG0/DIAG1 GPIOs. UART: serial port, baud rate, node address and echo (TX through 1 kΩ to SWP, RX directly). GPIOs are given as <code>chip:line</code> and need the <code>opengpio</code> package. Set the sense resistor (0.075 Ω on common boards), the chip clock and the default current, speed and acceleration. The motor is stopped and the driver disabled when the node is closed.</p>
|
|
95
|
+
</script>
|