periph 1.2.0 → 1.2.1
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 +7 -0
- package/examples/motor/drv8830/complete.js +50 -0
- package/examples/motor/drv8830/demo.js +57 -0
- package/examples/motor/drv8830/minimal.js +23 -0
- package/examples/rtc/ds3231/complete.js +77 -0
- package/examples/rtc/ds3231/demo.js +60 -0
- package/examples/rtc/ds3231/minimal.js +23 -0
- package/examples/rtc/pcf8523/complete.js +66 -0
- package/examples/rtc/pcf8523/demo.js +65 -0
- package/examples/rtc/pcf8523/minimal.js +20 -0
- package/examples/temperature/mcp9808/complete.js +89 -0
- package/examples/temperature/mcp9808/demo.js +62 -0
- package/examples/temperature/mcp9808/minimal.js +21 -0
- package/examples/temperature/tmp117/complete.js +90 -0
- package/examples/temperature/tmp117/demo.js +73 -0
- package/examples/temperature/tmp117/minimal.js +21 -0
- package/examples/tof/vl53l0x/complete.js +118 -0
- package/examples/tof/vl53l0x/demo.js +83 -0
- package/examples/tof/vl53l0x/minimal.js +25 -0
- package/examples/tof/vl53l1x/complete.js +151 -0
- package/examples/tof/vl53l1x/demo.js +100 -0
- package/examples/tof/vl53l1x/minimal.js +25 -0
- package/package.json +1 -1
- package/src/chips/imu/mpu9250.js +4 -4
- package/src/chips/imu/mpu9255.js +5 -5
- package/src/chips/motor/drv8830.js +239 -0
- package/src/chips/rtc/ds3231.js +504 -0
- package/src/chips/rtc/pcf8523.js +539 -0
- package/src/chips/temperature/mcp9808.js +488 -0
- package/src/chips/temperature/tmp117.js +471 -0
- package/src/chips/tof/_vl53_base.js +160 -0
- package/src/chips/tof/vl53l0x.js +816 -0
- package/src/chips/tof/vl53l1x.js +816 -0
- package/examples/motor/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Exercises every method in the VL53L1X Full API, and finally moves the
|
|
4
|
+
// sensor to another I²C address and back to 0x29.
|
|
5
|
+
|
|
6
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
7
|
+
const { VL53L1XFull } = require('periph/src/chips/tof/vl53l1x');
|
|
8
|
+
|
|
9
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
13
|
+
const connection = new I2CConnection(bus, VL53L1XFull.I2C_ADDRESS);
|
|
14
|
+
const sensor = new VL53L1XFull(connection); // Create VL53L1X Full driver, (connection)
|
|
15
|
+
await sensor.init(); // Wait for init sequence, () → None
|
|
16
|
+
// boot poll, ID check, ULD default config
|
|
17
|
+
|
|
18
|
+
console.log('model', (await sensor.modelId()).toString(16)); // Read model ID, () → number
|
|
19
|
+
// IDENTIFICATION__MODEL_ID, always 0xEA
|
|
20
|
+
console.log('module', (await sensor.moduleType()).toString(16)); // Read module type, () → number
|
|
21
|
+
// IDENTIFICATION__MODULE_TYPE, always 0xCC
|
|
22
|
+
console.log('revision', (await sensor.revisionId()).toString(16)); // Read revision ID, () → number
|
|
23
|
+
// mask revision, 0x10
|
|
24
|
+
|
|
25
|
+
const d = await sensor.distance(); // Measure distance, () → number mm
|
|
26
|
+
// single shot; blocks for about one timing budget
|
|
27
|
+
console.log('distance', d, 'mm, valid', await sensor.rangeValid()); // Check last measurement, () → boolean
|
|
28
|
+
// mapped range status === 0
|
|
29
|
+
console.log('range status', await sensor.rangeStatus()); // Read last range status, () → number
|
|
30
|
+
// 0 = valid, 2 = signal fail, 4 = out of bounds
|
|
31
|
+
const m = await sensor.readMeasurement(); // Read result block, () → {distanceMm, …}
|
|
32
|
+
// distance, status, signal/ambient MCPS, SPAD count
|
|
33
|
+
console.log(`signal ${m.signalRateMcps.toFixed(2)} MCPS, ambient ${m.ambientRateMcps.toFixed(2)} MCPS, ` +
|
|
34
|
+
`${m.effectiveSpadCount.toFixed(1)} SPADs`);
|
|
35
|
+
|
|
36
|
+
console.log('mode', await sensor.distanceMode()); // Read distance mode, () → string
|
|
37
|
+
// from PHASECAL_CONFIG__TIMEOUT_MACROP
|
|
38
|
+
await sensor.setDistanceMode('short'); // Set distance mode, (mode) → None
|
|
39
|
+
// ~1.3 m, robust in sunlight; keeps the budget
|
|
40
|
+
console.log('budget', await sensor.timingBudget(), 'us'); // Read timing budget, () → number µs
|
|
41
|
+
// decoded from the range timeout A register
|
|
42
|
+
await sensor.setTimingBudget(33000); // Set timing budget, (budgetUs µs) → None
|
|
43
|
+
// ULD table values 15000 (short only) … 500000
|
|
44
|
+
console.log('short mode', await sensor.distance(), 'mm'); // Measure distance, () → number mm
|
|
45
|
+
// 33 ms single shot
|
|
46
|
+
await sensor.setDistanceMode('long'); // Set distance mode, (mode) → None
|
|
47
|
+
// back to up to 4 m in the dark
|
|
48
|
+
await sensor.setTimingBudget(100000); // Set timing budget, (budgetUs µs) → None
|
|
49
|
+
// default 100 ms
|
|
50
|
+
|
|
51
|
+
await sensor.setInterMeasurement(200); // Set inter-measurement period, (periodMs ms) → None
|
|
52
|
+
// must be ≥ the timing budget
|
|
53
|
+
console.log('period', await sensor.interMeasurement(), 'ms'); // Read inter-measurement period, () → number ms
|
|
54
|
+
// oscillator ticks scaled by the PLL calibration
|
|
55
|
+
await sensor.startContinuous(200); // Start continuous ranging, (periodMs=0 ms) → None
|
|
56
|
+
// timed mode; 0 = as fast as the budget allows
|
|
57
|
+
for (let i = 0; i < 5; i++) {
|
|
58
|
+
console.log('continuous', await sensor.readContinuous(), 'mm'); // Read next continuous result, () → number mm
|
|
59
|
+
// waits for data ready, then clears it
|
|
60
|
+
}
|
|
61
|
+
while (!(await sensor.dataReady())) await sleep(10); // Check for a result, () → boolean
|
|
62
|
+
// GPIO1 line asserted
|
|
63
|
+
console.log('record', (await sensor.readMeasurement()).distanceMm, 'mm'); // Read result block, () → {distanceMm, …}
|
|
64
|
+
// non-blocking; clears the interrupt
|
|
65
|
+
await sensor.stopContinuous(); // Stop continuous ranging, () → None
|
|
66
|
+
// does not wait for a running measurement
|
|
67
|
+
await sleep(250);
|
|
68
|
+
|
|
69
|
+
console.log('signal limit', await sensor.signalRateLimit()); // Read signal-rate limit, () → number MCPS
|
|
70
|
+
// 9.7 fixed point, default 1.0
|
|
71
|
+
await sensor.setSignalRateLimit(0.5); // Set signal-rate limit, (limitMcps MCPS) → None
|
|
72
|
+
// lower = longer range, more noise
|
|
73
|
+
await sensor.setSignalRateLimit(1.0); // Set signal-rate limit, (limitMcps MCPS) → None
|
|
74
|
+
// restore the default
|
|
75
|
+
console.log('sigma', await sensor.sigmaThreshold(), 'mm'); // Read sigma threshold, () → number mm
|
|
76
|
+
// 14.2 fixed point, default 90
|
|
77
|
+
await sensor.setSigmaThreshold(60); // Set sigma threshold, (sigmaMm mm) → None
|
|
78
|
+
// stricter repeatability filter
|
|
79
|
+
await sensor.setSigmaThreshold(90); // Set sigma threshold, (sigmaMm mm) → None
|
|
80
|
+
// restore the default
|
|
81
|
+
|
|
82
|
+
const centre = await sensor.opticalCenter(); // Read optical-centre SPAD, () → number
|
|
83
|
+
// factory NVM value for this part's lens
|
|
84
|
+
console.log('optical centre', centre);
|
|
85
|
+
await sensor.setRoi(8, 8); // Set ROI size, (width SPADs, height SPADs) → None
|
|
86
|
+
// 4–16 each; narrows the field of view
|
|
87
|
+
await sensor.setRoiCenter(centre); // Set ROI centre, (spad) → None
|
|
88
|
+
// align the narrow ROI with the lens
|
|
89
|
+
console.log('roi', await sensor.roi(), 'centre', await sensor.roiCenter()); // Read ROI size, () → {width, height}; Read ROI centre, () → number
|
|
90
|
+
// in SPADs
|
|
91
|
+
await sensor.setRoi(16, 16); // Set ROI size, (width SPADs, height SPADs) → None
|
|
92
|
+
// full array; re-centres on SPAD 199
|
|
93
|
+
|
|
94
|
+
const original = await sensor.offset(); // Read range offset, () → number mm
|
|
95
|
+
// NVM factory value, 0.25 mm steps
|
|
96
|
+
await sensor.setOffset(original - 5.0); // Set range offset, (offsetMm mm) → None
|
|
97
|
+
// volatile override, −1024.0 to 1023.75 mm
|
|
98
|
+
console.log('offset', await sensor.offset(), 'mm'); // Read range offset, () → number mm
|
|
99
|
+
// 13-bit two's complement × 0.25
|
|
100
|
+
await sensor.setCrosstalkCompensation(0.01); // Set crosstalk compensation, (rateMcps MCPS) → None
|
|
101
|
+
// per-SPAD rate, 7.9 kcps register
|
|
102
|
+
console.log('crosstalk', await sensor.crosstalkCompensation(), 'MCPS'); // Read crosstalk compensation, () → number MCPS
|
|
103
|
+
// 0 = off
|
|
104
|
+
console.log('calibrated offset', await sensor.calibrateOffset(140), 'mm'); // Calibrate offset, (targetMm mm) → number mm
|
|
105
|
+
// 50 samples against a target at 140 mm; applies it
|
|
106
|
+
console.log('calibrated crosstalk', await sensor.calibrateCrosstalk(600)); // Calibrate crosstalk, (targetMm mm) → number MCPS
|
|
107
|
+
// 50 samples against a target at 600 mm; applies it
|
|
108
|
+
await sensor.setOffset(original); // Set range offset, (offsetMm mm) → None
|
|
109
|
+
// restore the factory value
|
|
110
|
+
await sensor.setCrosstalkCompensation(0); // Set crosstalk compensation, (rateMcps MCPS) → None
|
|
111
|
+
// compensation off
|
|
112
|
+
|
|
113
|
+
await sensor.recalibrate(); // Run temperature update, () → None
|
|
114
|
+
// full VHV; after a > 8 °C change, not while ranging
|
|
115
|
+
|
|
116
|
+
await sensor.setInterruptThresholds(100, 800); // Set distance thresholds, (lowMm mm, highMm mm) → None
|
|
117
|
+
// 1 mm resolution
|
|
118
|
+
console.log('thresholds', await sensor.interruptThresholds()); // Read distance thresholds, () → {lowMm, highMm}
|
|
119
|
+
// in mm
|
|
120
|
+
await sensor.enableInterrupt(VL53L1XFull.SOURCE_IN_WINDOW); // Select interrupt source, (source) → None
|
|
121
|
+
// fires while 100 mm ≤ range ≤ 800 mm
|
|
122
|
+
await sensor.disableInterrupt(VL53L1XFull.SOURCE_IN_WINDOW); // Disable interrupt source, (source) → None
|
|
123
|
+
// reverts to new-sample-ready (no disabled state)
|
|
124
|
+
await sensor.enableInterrupt(VL53L1XFull.SOURCE_NEW_SAMPLE_READY); // Select interrupt source, (source) → None
|
|
125
|
+
// the default data-ready source
|
|
126
|
+
|
|
127
|
+
await sensor.onInterrupt((status) => console.log('interrupt, source', status)); // Subscribe to GPIO1, (callback) → None
|
|
128
|
+
// interrupt is cleared before the callback
|
|
129
|
+
await sensor.startContinuous(200); // Start continuous ranging, (periodMs=0 ms) → None
|
|
130
|
+
// timed mode feeds the subscription
|
|
131
|
+
await sleep(1000);
|
|
132
|
+
await sensor.stopContinuous(); // Stop continuous ranging, () → None
|
|
133
|
+
// no more samples
|
|
134
|
+
await sensor.offInterrupt(); // Unsubscribe, () → None
|
|
135
|
+
// detaches the pin handler or stops the polling timer
|
|
136
|
+
console.log('pending', await sensor.pollInterrupt()); // Read and clear interrupt, () → number
|
|
137
|
+
// active SOURCE_* value, 0 = nothing pending
|
|
138
|
+
|
|
139
|
+
await sensor.setAddress(0x30); // Change I²C address, (address) → None
|
|
140
|
+
// volatile; this driver instance is now unusable
|
|
141
|
+
await connection.close();
|
|
142
|
+
const movedConnection = new I2CConnection(bus, 0x30);
|
|
143
|
+
const moved = new VL53L1XFull(movedConnection); // Create VL53L1X Full driver, (connection)
|
|
144
|
+
await moved.init(); // Wait for init sequence, () → None
|
|
145
|
+
// re-init at the new address is safe
|
|
146
|
+
console.log('at 0x30', await moved.distance(), 'mm'); // Measure distance, () → number mm
|
|
147
|
+
// same sensor, new address
|
|
148
|
+
await moved.setAddress(VL53L1XFull.I2C_ADDRESS); // Change I²C address, (address) → None
|
|
149
|
+
// back to the power-on 0x29
|
|
150
|
+
await movedConnection.close();
|
|
151
|
+
})();
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// Long-range doorway people counter with a split ROI. The sensor hangs
|
|
4
|
+
// overhead in a doorway (up to 2.5 m). Two narrow 8x16 ROIs (centre SPADs 167
|
|
5
|
+
// and 231, the left/right half-array centres used by ST's own people-counting
|
|
6
|
+
// code) form two virtual beams. Each zone learns its floor distance, then
|
|
7
|
+
// counts as occupied when something is more than 300 mm closer; the order in
|
|
8
|
+
// which the zones become occupied tells IN from OUT. Every 30 s a
|
|
9
|
+
// signal/ambient snapshot is printed and strong sunlight switches to short
|
|
10
|
+
// distance mode. After 60 s or 50 events the full ROI is restored.
|
|
11
|
+
|
|
12
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
13
|
+
const { VL53L1XFull } = require('periph/src/chips/tof/vl53l1x');
|
|
14
|
+
|
|
15
|
+
const ZONE_CENTRES = [167, 231]; // left, right
|
|
16
|
+
const OCCUPIED_MM = 300;
|
|
17
|
+
const MAX_EVENTS = 50;
|
|
18
|
+
const MAX_MS = 60000;
|
|
19
|
+
const SNAPSHOT_MS = 30000;
|
|
20
|
+
const BRIGHT_MCPS = 5.0;
|
|
21
|
+
|
|
22
|
+
(async () => {
|
|
23
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
24
|
+
const connection = new I2CConnection(bus, VL53L1XFull.I2C_ADDRESS);
|
|
25
|
+
const sensor = new VL53L1XFull(connection); // Create VL53L1X Full driver, (connection)
|
|
26
|
+
await sensor.init(); // Wait for init sequence, () → None
|
|
27
|
+
|
|
28
|
+
// --- Two virtual beams ---
|
|
29
|
+
// Long mode reaches the floor from a 2.5 m ceiling; a 33 ms budget keeps
|
|
30
|
+
// the two zones fast enough to catch a walking person. An 8x16 ROI covers
|
|
31
|
+
// one half of the SPAD array, so alternating the centre alternates beams.
|
|
32
|
+
await sensor.setDistanceMode('long'); // Set distance mode, (mode) → None
|
|
33
|
+
await sensor.setTimingBudget(33000); // Set timing budget, (budgetUs µs) → None
|
|
34
|
+
await sensor.setRoi(8, 16); // Set ROI size, (width SPADs, height SPADs) → None
|
|
35
|
+
console.log('optical centre SPAD', await sensor.opticalCenter(), '- zone centres', ZONE_CENTRES); // Read optical-centre SPAD, () → number
|
|
36
|
+
|
|
37
|
+
const measure = async (zone) => {
|
|
38
|
+
await sensor.setRoiCenter(ZONE_CENTRES[zone]); // Set ROI centre, (spad) → None
|
|
39
|
+
const d = await sensor.distance(); // Measure distance, () → number mm
|
|
40
|
+
return (await sensor.rangeValid()) ? d : null; // Check last measurement, () → boolean
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// --- Learn the empty doorway ---
|
|
44
|
+
// Twenty readings per zone give the floor distance each beam sees when
|
|
45
|
+
// nobody is there; invalid readings are ignored.
|
|
46
|
+
const baseline = [];
|
|
47
|
+
for (const zone of [0, 1]) {
|
|
48
|
+
const values = [];
|
|
49
|
+
for (let i = 0; i < 20; i++) {
|
|
50
|
+
const v = await measure(zone);
|
|
51
|
+
if (v !== null) values.push(v);
|
|
52
|
+
}
|
|
53
|
+
baseline.push(values.length ? values.reduce((a, b) => a + b, 0) / values.length : 4000);
|
|
54
|
+
}
|
|
55
|
+
console.log(`baseline left ${baseline[0].toFixed(0)} mm, right ${baseline[1].toFixed(0)} mm`);
|
|
56
|
+
|
|
57
|
+
// --- Count crossings ---
|
|
58
|
+
// A person entering blocks the left beam first, then the right one (and
|
|
59
|
+
// the reverse when leaving). Once both beams clear, the recorded order
|
|
60
|
+
// decides the direction.
|
|
61
|
+
let countIn = 0;
|
|
62
|
+
let countOut = 0;
|
|
63
|
+
let events = 0;
|
|
64
|
+
let sequence = [];
|
|
65
|
+
const start = Date.now();
|
|
66
|
+
let lastSnapshot = start;
|
|
67
|
+
while (events < MAX_EVENTS && Date.now() - start < MAX_MS) {
|
|
68
|
+
const readings = [await measure(0), await measure(1)];
|
|
69
|
+
const occupied = readings.map((r, z) => r !== null && r < baseline[z] - OCCUPIED_MM);
|
|
70
|
+
for (const zone of [0, 1]) {
|
|
71
|
+
if (occupied[zone] && !sequence.includes(zone)) sequence.push(zone);
|
|
72
|
+
}
|
|
73
|
+
if (!occupied[0] && !occupied[1] && sequence.length) {
|
|
74
|
+
if (sequence[0] === 0 && sequence[1] === 1) countIn++;
|
|
75
|
+
else if (sequence[0] === 1 && sequence[1] === 0) countOut++;
|
|
76
|
+
events++;
|
|
77
|
+
console.log(`IN ${countIn} OUT ${countOut} (left ${readings[0]}, right ${readings[1]})`);
|
|
78
|
+
sequence = [];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// --- Watch the light ---
|
|
82
|
+
// Sunlight through an open door raises the ambient rate and eats
|
|
83
|
+
// long-mode range; short mode keeps working up to ~1.3 m.
|
|
84
|
+
if (Date.now() - lastSnapshot >= SNAPSHOT_MS) {
|
|
85
|
+
lastSnapshot = Date.now();
|
|
86
|
+
await sensor.distance(); // Measure distance, () → number mm
|
|
87
|
+
const m = await sensor.readMeasurement(); // Read result block, () → {distanceMm, …}
|
|
88
|
+
console.log(`signal ${m.signalRateMcps.toFixed(2)} MCPS, ambient ${m.ambientRateMcps.toFixed(2)} MCPS`);
|
|
89
|
+
if (m.ambientRateMcps > BRIGHT_MCPS && (await sensor.distanceMode()) === 'long') { // Read distance mode, () → string
|
|
90
|
+
await sensor.setDistanceMode('short'); // Set distance mode, (mode) → None
|
|
91
|
+
console.log('bright ambient light: switched to short distance mode');
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// --- Restore the full field of view ---
|
|
97
|
+
await sensor.setRoi(16, 16); // Set ROI size, (width SPADs, height SPADs) → None
|
|
98
|
+
console.log(`done: IN ${countIn} OUT ${countOut}`);
|
|
99
|
+
await connection.close();
|
|
100
|
+
})();
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
4
|
+
const { VL53L1XMinimal } = require('periph/src/chips/tof/vl53l1x');
|
|
5
|
+
|
|
6
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
7
|
+
|
|
8
|
+
(async () => {
|
|
9
|
+
const bus = parseInt(process.env.I2C_BUS || '1', 10);
|
|
10
|
+
const connection = new I2CConnection(bus, VL53L1XMinimal.I2C_ADDRESS);
|
|
11
|
+
const sensor = new VL53L1XMinimal(connection); // Create VL53L1X driver, (connection)
|
|
12
|
+
await sensor.init(); // Wait for init sequence, () → None
|
|
13
|
+
|
|
14
|
+
for (let i = 0; i < 50; i++) {
|
|
15
|
+
const d = await sensor.distance(); // Measure distance, () → number mm
|
|
16
|
+
if (await sensor.rangeValid()) { // Check last measurement, () → boolean
|
|
17
|
+
console.log(`${d} mm`);
|
|
18
|
+
} else {
|
|
19
|
+
console.log('out of range');
|
|
20
|
+
}
|
|
21
|
+
await sleep(200);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
await connection.close();
|
|
25
|
+
})();
|
package/package.json
CHANGED
package/src/chips/imu/mpu9250.js
CHANGED
|
@@ -55,7 +55,7 @@ const _MAG_SENSITIVITY_16BIT = 0.15;
|
|
|
55
55
|
* - Gyroscope full-scale: ±250 dps (GYRO_FS_SEL=0)
|
|
56
56
|
* - Accelerometer full-scale: ±2 g (ACCEL_FS_SEL=0)
|
|
57
57
|
* - Gyroscope DLPF: 41 Hz bandwidth (CONFIG DLPF_CFG=3)
|
|
58
|
-
* - Accelerometer DLPF:
|
|
58
|
+
* - Accelerometer DLPF: 44.8 Hz bandwidth (ACCEL_CONFIG2 A_DLPFCFG=3)
|
|
59
59
|
* - Sample rate: 200 Hz (SMPLRT_DIV=4)
|
|
60
60
|
* - Clock: auto PLL (CLKSEL=1)
|
|
61
61
|
* - All six axes enabled
|
|
@@ -85,7 +85,7 @@ class MPU9250Minimal {
|
|
|
85
85
|
}
|
|
86
86
|
await this._writeReg(_REG_GYRO_CONFIG, 0x00);
|
|
87
87
|
await this._writeReg(_REG_ACCEL_CONFIG, 0x00);
|
|
88
|
-
await this._writeReg(_REG_ACCEL_CONFIG2,
|
|
88
|
+
await this._writeReg(_REG_ACCEL_CONFIG2, 0x03);
|
|
89
89
|
await this._writeReg(_REG_CONFIG, 0x03);
|
|
90
90
|
await this._writeReg(_REG_SMPLRT_DIV, 0x04);
|
|
91
91
|
const end2 = Date.now() + 35;
|
|
@@ -201,8 +201,8 @@ class MPU9250Full extends MPU9250Minimal {
|
|
|
201
201
|
|
|
202
202
|
/**
|
|
203
203
|
* Set digital low-pass filter bandwidth.
|
|
204
|
-
* @param {number} [gyroDlpf=3] - Gyro filter setting 0–
|
|
205
|
-
* @param {number} [accelDlpf=3] - Accel filter setting 0–
|
|
204
|
+
* @param {number} [gyroDlpf=3] - Gyro filter setting 0–7 (0=250 Hz, 1=184 Hz, 2=92 Hz, 3=41 Hz, 4=20 Hz, 5=10 Hz, 6=5 Hz, 7=3600 Hz).
|
|
205
|
+
* @param {number} [accelDlpf=3] - Accel filter setting 0–7 (0=218.1 Hz, 1=218.1 Hz, 2=99 Hz, 3=44.8 Hz, 4=21.2 Hz, 5=10.2 Hz, 6=5.05 Hz, 7=420 Hz).
|
|
206
206
|
* @returns {Promise<void>}
|
|
207
207
|
*/
|
|
208
208
|
async configureDlpf(gyroDlpf = 3, accelDlpf = 3) {
|
package/src/chips/imu/mpu9255.js
CHANGED
|
@@ -57,7 +57,7 @@ const _MAG_SENSITIVITY_16BIT = 0.15;
|
|
|
57
57
|
* - Gyroscope full-scale: ±250 dps (GYRO_FS_SEL=0)
|
|
58
58
|
* - Accelerometer full-scale: ±2 g (ACCEL_FS_SEL=0)
|
|
59
59
|
* - Gyroscope DLPF: 41 Hz bandwidth (CONFIG DLPF_CFG=3)
|
|
60
|
-
* - Accelerometer DLPF:
|
|
60
|
+
* - Accelerometer DLPF: 44.8 Hz bandwidth (ACCEL_CONFIG2 A_DLPFCFG=3)
|
|
61
61
|
* - Sample rate: 200 Hz (SMPLRT_DIV=4)
|
|
62
62
|
* - Clock: auto PLL (CLKSEL=1)
|
|
63
63
|
* - All six axes enabled
|
|
@@ -87,7 +87,7 @@ class MPU9255Minimal {
|
|
|
87
87
|
}
|
|
88
88
|
await this._writeReg(_REG_GYRO_CONFIG, 0x00);
|
|
89
89
|
await this._writeReg(_REG_ACCEL_CONFIG, 0x00);
|
|
90
|
-
await this._writeReg(_REG_ACCEL_CONFIG2,
|
|
90
|
+
await this._writeReg(_REG_ACCEL_CONFIG2, 0x03);
|
|
91
91
|
await this._writeReg(_REG_CONFIG, 0x03);
|
|
92
92
|
await this._writeReg(_REG_SMPLRT_DIV, 0x04);
|
|
93
93
|
const end2 = Date.now() + 35;
|
|
@@ -204,8 +204,8 @@ class MPU9255Full extends MPU9255Minimal {
|
|
|
204
204
|
|
|
205
205
|
/**
|
|
206
206
|
* Set digital low-pass filter bandwidth.
|
|
207
|
-
* @param {number} [gyroDlpf=3] - Gyro filter setting 0–
|
|
208
|
-
* @param {number} [accelDlpf=3] - Accel filter setting 0–
|
|
207
|
+
* @param {number} [gyroDlpf=3] - Gyro filter setting 0–7 (0=250 Hz, 1=184 Hz, 2=92 Hz, 3=41 Hz, 4=20 Hz, 5=10 Hz, 6=5 Hz, 7=3600 Hz).
|
|
208
|
+
* @param {number} [accelDlpf=3] - Accel filter setting 0–7 (0=218.1 Hz, 1=218.1 Hz, 2=99 Hz, 3=44.8 Hz, 4=21.2 Hz, 5=10.2 Hz, 6=5.05 Hz, 7=420 Hz).
|
|
209
209
|
* @returns {Promise<void>}
|
|
210
210
|
*/
|
|
211
211
|
async configureDlpf(gyroDlpf = 3, accelDlpf = 3) {
|
|
@@ -427,7 +427,7 @@ class MPU9255Full extends MPU9255Minimal {
|
|
|
427
427
|
|
|
428
428
|
await this._writeReg(_REG_PWR_MGMT_1, 0x01);
|
|
429
429
|
await this._writeReg(_REG_PWR_MGMT_2, 0x07);
|
|
430
|
-
await this._writeReg(_REG_ACCEL_CONFIG2,
|
|
430
|
+
await this._writeReg(_REG_ACCEL_CONFIG2, 0x01);
|
|
431
431
|
await this._writeReg(_REG_INT_ENABLE, 0x40);
|
|
432
432
|
await this._writeReg(_REG_MOT_DETECT_CTRL, 0xC0);
|
|
433
433
|
await this._writeReg(_REG_WOM_THR, thresholdLsb & 0xFF);
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_CONTROL = 0x00;
|
|
4
|
+
const _REG_FAULT = 0x01;
|
|
5
|
+
|
|
6
|
+
const _CTRL_IN1 = 0x01;
|
|
7
|
+
const _CTRL_IN2 = 0x02;
|
|
8
|
+
|
|
9
|
+
const _FAULT_FAULT = 0x01;
|
|
10
|
+
const _FAULT_OCP = 0x02;
|
|
11
|
+
const _FAULT_UVLO = 0x04;
|
|
12
|
+
const _FAULT_OTS = 0x08;
|
|
13
|
+
const _FAULT_ILIMIT = 0x10;
|
|
14
|
+
const _FAULT_CLEAR = 0x80;
|
|
15
|
+
|
|
16
|
+
const _VREF = 1.285;
|
|
17
|
+
const _VSET_MIN = 6;
|
|
18
|
+
const _VSET_MAX = 63;
|
|
19
|
+
|
|
20
|
+
const _DIRECTIONS = ['coast', 'forward', 'reverse', 'brake'];
|
|
21
|
+
|
|
22
|
+
/** Map |voltage| to a VSET code; 0 means coast (below the vset=6 floor). */
|
|
23
|
+
function _voltageToVset(voltage) {
|
|
24
|
+
const vset = Math.floor(Math.abs(voltage) * 16 / _VREF + 0.5);
|
|
25
|
+
if (vset < _VSET_MIN) return 0;
|
|
26
|
+
if (vset > _VSET_MAX) return _VSET_MAX;
|
|
27
|
+
return vset;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function _vsetToVoltage(vset) {
|
|
31
|
+
if (vset < _VSET_MIN) return 0;
|
|
32
|
+
return _VREF * vset / 16;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* DRV8830 low-voltage motor driver with I²C interface (Texas Instruments)
|
|
37
|
+
* — minimal interface.
|
|
38
|
+
*
|
|
39
|
+
* H-bridge driver for a single brushed DC motor, controlled entirely over
|
|
40
|
+
* I²C. The host commands a target output *voltage*; the chip PWM-regulates
|
|
41
|
+
* the bridge to hold that average voltage regardless of supply sag. Nine
|
|
42
|
+
* selectable addresses (0x60–0x68) via the tri-state A0/A1 strap pins.
|
|
43
|
+
*
|
|
44
|
+
* No register writes are needed at construction — the chip's POR default
|
|
45
|
+
* already leaves the motor in standby/coast.
|
|
46
|
+
*/
|
|
47
|
+
class DRV8830Minimal {
|
|
48
|
+
/**
|
|
49
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection (0x60–0x68).
|
|
50
|
+
*/
|
|
51
|
+
constructor(connection) {
|
|
52
|
+
this._conn = connection;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async _writeReg(reg, value) {
|
|
56
|
+
await this._conn.write(Buffer.from([reg & 0xFF, value & 0xFF]));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async _readReg(reg) {
|
|
60
|
+
const buf = await this._conn.writeRead(Buffer.from([reg & 0xFF]), 1);
|
|
61
|
+
return buf[0];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Confirm the device answers on the bus. The DRV8830 has no
|
|
66
|
+
* WHO_AM_I/identity register, so this is a plain CONTROL read; it makes
|
|
67
|
+
* no register writes.
|
|
68
|
+
* @returns {Promise<void>}
|
|
69
|
+
*/
|
|
70
|
+
async init() {
|
|
71
|
+
await this._readReg(_REG_CONTROL);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Drive the motor at a regulated output voltage. Writes VSET and
|
|
76
|
+
* IN1/IN2 together in one CONTROL write. A magnitude below the ~0.48 V
|
|
77
|
+
* floor is treated as 0 V (coast); above ~5.06 V it is clamped.
|
|
78
|
+
* @param {number} voltage - Signed target voltage in V: positive = forward, negative = reverse, 0 = coast.
|
|
79
|
+
* @returns {Promise<void>}
|
|
80
|
+
*/
|
|
81
|
+
async drive(voltage) {
|
|
82
|
+
const vset = _voltageToVset(voltage);
|
|
83
|
+
if (vset === 0) {
|
|
84
|
+
await this._writeReg(_REG_CONTROL, 0x00);
|
|
85
|
+
} else if (voltage > 0) {
|
|
86
|
+
await this._writeReg(_REG_CONTROL, (vset << 2) | _CTRL_IN1);
|
|
87
|
+
} else {
|
|
88
|
+
await this._writeReg(_REG_CONTROL, (vset << 2) | _CTRL_IN2);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Short-brake the motor (IN1 = IN2 = 1, both outputs high).
|
|
94
|
+
* @returns {Promise<void>}
|
|
95
|
+
*/
|
|
96
|
+
async brake() {
|
|
97
|
+
await this._writeReg(_REG_CONTROL, _CTRL_IN1 | _CTRL_IN2);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Put the bridge in standby/coast (IN1 = IN2 = 0) — same as `drive(0)`.
|
|
102
|
+
* @returns {Promise<void>}
|
|
103
|
+
*/
|
|
104
|
+
async stop() {
|
|
105
|
+
await this._writeReg(_REG_CONTROL, 0x00);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* DRV8830 full interface — extends Minimal with raw CONTROL access, output
|
|
111
|
+
* read-back, fault reporting/clearing, and the FAULTn interrupt API.
|
|
112
|
+
*
|
|
113
|
+
* Faults are never cleared implicitly: a latched OCP/ILIMIT fault also
|
|
114
|
+
* disables the H-bridge, so clearing is always an explicit `clearFault()`.
|
|
115
|
+
*/
|
|
116
|
+
class DRV8830Full extends DRV8830Minimal {
|
|
117
|
+
/**
|
|
118
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection (0x60–0x68).
|
|
119
|
+
*/
|
|
120
|
+
constructor(connection) {
|
|
121
|
+
super(connection);
|
|
122
|
+
this._callback = null;
|
|
123
|
+
this._edgeHandler = null;
|
|
124
|
+
this._pollTimer = null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Write the CONTROL register from raw fields.
|
|
129
|
+
* @param {number} vset - VSET DAC code, 6–63 (0–5 are reserved).
|
|
130
|
+
* @param {boolean} in1 - H-bridge input 1.
|
|
131
|
+
* @param {boolean} in2 - H-bridge input 2.
|
|
132
|
+
* @returns {Promise<void>}
|
|
133
|
+
* @throws {RangeError} If vset is outside 6–63.
|
|
134
|
+
*/
|
|
135
|
+
async setOutput(vset, in1, in2) {
|
|
136
|
+
if (!Number.isInteger(vset) || vset < _VSET_MIN || vset > _VSET_MAX) {
|
|
137
|
+
throw new RangeError('vset must be an integer 6-63');
|
|
138
|
+
}
|
|
139
|
+
await this._writeReg(_REG_CONTROL, (vset << 2) | (in1 ? _CTRL_IN1 : 0) | (in2 ? _CTRL_IN2 : 0));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Read back and decode the CONTROL register.
|
|
144
|
+
* @returns {Promise<{voltage: number, direction: ('forward'|'reverse'|'coast'|'brake')}>}
|
|
145
|
+
* voltage is the commanded magnitude in V (0 for a reserved VSET code).
|
|
146
|
+
*/
|
|
147
|
+
async readOutput() {
|
|
148
|
+
const ctrl = await this._readReg(_REG_CONTROL);
|
|
149
|
+
return { voltage: _vsetToVoltage(ctrl >> 2), direction: _DIRECTIONS[ctrl & 0x03] };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Read the FAULT register without clearing it.
|
|
154
|
+
* @returns {Promise<{fault: boolean, ocp: boolean, uvlo: boolean, ots: boolean, ilimit: boolean}>}
|
|
155
|
+
*/
|
|
156
|
+
async readFault() {
|
|
157
|
+
const f = await this._readReg(_REG_FAULT);
|
|
158
|
+
return {
|
|
159
|
+
fault: (f & _FAULT_FAULT) !== 0,
|
|
160
|
+
ocp: (f & _FAULT_OCP) !== 0,
|
|
161
|
+
uvlo: (f & _FAULT_UVLO) !== 0,
|
|
162
|
+
ots: (f & _FAULT_OTS) !== 0,
|
|
163
|
+
ilimit: (f & _FAULT_ILIMIT) !== 0,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Clear all fault status bits (CLEAR = 1); re-enables the H-bridge if an
|
|
169
|
+
* OCP/ILIMIT fault had latched it off.
|
|
170
|
+
* @returns {Promise<void>}
|
|
171
|
+
*/
|
|
172
|
+
async clearFault() {
|
|
173
|
+
await this._writeReg(_REG_FAULT, _FAULT_CLEAR);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// -- Interrupt API (Level 1: FAULTn) ------------------------------------
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Read the fault status — equivalent to `readFault()`, does not clear.
|
|
180
|
+
* @returns {Promise<{fault: boolean, ocp: boolean, uvlo: boolean, ots: boolean, ilimit: boolean}>}
|
|
181
|
+
*/
|
|
182
|
+
async pollInterrupt() {
|
|
183
|
+
return this.readFault();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Subscribe to fault interrupts. Uses `connection.intPin.onEdge()` (FAULTn
|
|
188
|
+
* falling edge) when an INT-line InputPin is wired; otherwise falls back
|
|
189
|
+
* to a 5 ms polling loop that fires on each new fault (FAULT bit rising).
|
|
190
|
+
* The fault is not cleared.
|
|
191
|
+
* @param {function({fault: boolean, ocp: boolean, uvlo: boolean, ots: boolean, ilimit: boolean}): void} callback
|
|
192
|
+
* @returns {Promise<void>}
|
|
193
|
+
*/
|
|
194
|
+
async onInterrupt(callback) {
|
|
195
|
+
this._callback = callback;
|
|
196
|
+
if (this._conn.intPin) {
|
|
197
|
+
this._edgeHandler = async () => {
|
|
198
|
+
const status = await this.pollInterrupt();
|
|
199
|
+
if (status.fault && this._callback) this._callback(status);
|
|
200
|
+
};
|
|
201
|
+
await this._conn.intPin.onEdge(this._edgeHandler, 'falling');
|
|
202
|
+
} else {
|
|
203
|
+
let wasFault = false;
|
|
204
|
+
let busy = false;
|
|
205
|
+
this._pollTimer = setInterval(async () => {
|
|
206
|
+
if (busy) return;
|
|
207
|
+
busy = true;
|
|
208
|
+
try {
|
|
209
|
+
const status = await this.pollInterrupt();
|
|
210
|
+
if (status.fault && !wasFault && this._callback) this._callback(status);
|
|
211
|
+
wasFault = status.fault;
|
|
212
|
+
} finally {
|
|
213
|
+
busy = false;
|
|
214
|
+
}
|
|
215
|
+
}, 5);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Unsubscribe from fault interrupts.
|
|
221
|
+
* @returns {Promise<void>}
|
|
222
|
+
*/
|
|
223
|
+
async offInterrupt() {
|
|
224
|
+
if (this._conn.intPin && this._edgeHandler) {
|
|
225
|
+
await this._conn.intPin.offEdge(this._edgeHandler);
|
|
226
|
+
this._edgeHandler = null;
|
|
227
|
+
}
|
|
228
|
+
if (this._pollTimer) {
|
|
229
|
+
clearInterval(this._pollTimer);
|
|
230
|
+
this._pollTimer = null;
|
|
231
|
+
}
|
|
232
|
+
this._callback = null;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Default 7-bit I²C address (A0 = A1 = GND). Valid range 0x60–0x68. */
|
|
237
|
+
DRV8830Minimal.I2C_ADDRESS = 0x60;
|
|
238
|
+
|
|
239
|
+
module.exports = { DRV8830Minimal, DRV8830Full };
|