periph 1.0.1 → 1.2.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 +54 -12
- package/examples/accelerometer/adxl345/complete.js +52 -0
- package/examples/accelerometer/adxl345/demo.js +34 -0
- package/examples/accelerometer/adxl345/minimal.js +18 -0
- package/examples/accelerometer/adxl362/complete.js +57 -0
- package/examples/accelerometer/adxl362/demo.js +51 -0
- package/examples/accelerometer/adxl362/minimal.js +21 -0
- package/examples/adc_dac/ad7705/complete.js +32 -0
- package/examples/adc_dac/ad7705/demo.js +42 -0
- package/examples/adc_dac/ad7705/minimal.js +16 -0
- package/examples/adc_dac/ad7706/complete.js +35 -0
- package/examples/adc_dac/ad7706/demo.js +44 -0
- package/examples/adc_dac/ad7706/minimal.js +16 -0
- package/examples/adc_dac/hx710a/complete.js +45 -0
- package/examples/adc_dac/hx710a/demo.js +52 -0
- package/examples/adc_dac/hx710a/minimal.js +16 -0
- package/examples/adc_dac/hx710b/complete.js +45 -0
- package/examples/adc_dac/hx710b/demo.js +64 -0
- package/examples/adc_dac/hx710b/minimal.js +16 -0
- package/examples/adc_dac/hx711/complete.js +42 -0
- package/examples/adc_dac/hx711/demo.js +35 -0
- package/examples/adc_dac/hx711/minimal.js +16 -0
- package/examples/adc_dac/mcp4725/complete.js +19 -17
- package/examples/adc_dac/mcp4725/demo.js +5 -5
- package/examples/adc_dac/mcp4725/minimal.js +8 -6
- package/examples/adc_dac/mcp4728/complete.js +43 -0
- package/examples/adc_dac/mcp4728/demo.js +41 -0
- package/examples/adc_dac/mcp4728/minimal.js +13 -0
- package/examples/adc_dac/pcf8591/complete.js +41 -0
- package/examples/adc_dac/pcf8591/demo.js +39 -0
- package/examples/adc_dac/pcf8591/minimal.js +19 -0
- package/examples/comms/mcp2515/complete.js +75 -0
- package/examples/comms/mcp2515/demo.js +66 -0
- package/examples/comms/mcp2515/minimal.js +27 -0
- package/examples/comms/rda5807m/complete.js +55 -0
- package/examples/comms/rda5807m/demo.js +68 -0
- package/examples/comms/rda5807m/minimal.js +15 -0
- package/examples/comms/rfm9x/complete.js +50 -0
- package/examples/comms/rfm9x/demo.js +55 -0
- package/examples/comms/rfm9x/minimal.js +20 -0
- package/examples/display/pcf8576/complete.js +35 -0
- package/examples/display/pcf8576/demo.js +48 -0
- package/examples/display/pcf8576/minimal.js +20 -0
- package/examples/environmental/aht21/complete.js +32 -0
- package/examples/environmental/aht21/demo.js +46 -0
- package/examples/environmental/aht21/minimal.js +12 -0
- package/examples/environmental/bme280/complete.js +37 -0
- package/examples/environmental/bme280/demo.js +67 -0
- package/examples/environmental/bme280/minimal.js +20 -0
- package/examples/environmental/bme680/complete.js +40 -0
- package/examples/environmental/bme680/demo.js +54 -0
- package/examples/environmental/bme680/minimal.js +21 -0
- package/examples/gas/ens160/complete.js +54 -0
- package/examples/gas/ens160/demo.js +54 -0
- package/examples/gas/ens160/minimal.js +29 -0
- package/examples/gnss/neo6/complete.js +49 -0
- package/examples/gnss/neo6/demo.js +54 -0
- package/examples/gnss/neo6/minimal.js +30 -0
- package/examples/gyroscope/l3g4200d/complete.js +56 -0
- package/examples/gyroscope/l3g4200d/demo.js +49 -0
- package/examples/gyroscope/l3g4200d/minimal.js +19 -0
- package/examples/gyroscope/l3gd20h/complete.js +42 -0
- package/examples/gyroscope/l3gd20h/demo.js +29 -0
- package/examples/gyroscope/l3gd20h/minimal.js +13 -0
- package/examples/humidity/dht11/complete.js +22 -0
- package/examples/humidity/dht11/demo.js +38 -0
- package/examples/humidity/dht11/minimal.js +18 -0
- package/examples/imu/mpu6050/complete.js +58 -0
- package/examples/imu/mpu6050/demo.js +37 -0
- package/examples/imu/mpu6050/minimal.js +14 -0
- package/examples/imu/mpu9250/complete.js +65 -0
- package/examples/imu/mpu9250/demo.js +53 -0
- package/examples/imu/mpu9250/minimal.js +21 -0
- package/examples/imu/mpu9255/complete.js +73 -0
- package/examples/imu/mpu9255/demo.js +73 -0
- package/examples/imu/mpu9255/minimal.js +21 -0
- package/examples/io_expander/mcp23017/complete.js +70 -77
- package/examples/io_expander/mcp23017/demo.js +37 -33
- package/examples/io_expander/mcp23017/minimal.js +15 -11
- package/examples/io_expander/pcf8574/complete.js +60 -65
- package/examples/io_expander/pcf8574/demo.js +30 -25
- package/examples/io_expander/pcf8574/minimal.js +15 -11
- package/examples/io_expander/pcf8575/complete.js +24 -20
- package/examples/io_expander/pcf8575/demo.js +27 -23
- package/examples/io_expander/pcf8575/minimal.js +16 -12
- package/examples/io_expander/tpic6b595/complete.js +57 -0
- package/examples/io_expander/tpic6b595/demo.js +75 -0
- package/examples/io_expander/tpic6b595/minimal.js +26 -0
- package/examples/led/apa102/complete.js +72 -0
- package/examples/led/apa102/demo.js +92 -0
- package/examples/led/apa102/minimal.js +21 -0
- package/examples/led/sk6812rgbw/complete.js +42 -40
- package/examples/led/sk6812rgbw/demo.js +16 -16
- package/examples/led/sk6812rgbw/minimal.js +15 -17
- package/examples/led/ws2812b/complete.js +40 -38
- package/examples/led/ws2812b/demo.js +16 -16
- package/examples/led/ws2812b/minimal.js +13 -14
- package/examples/led/ws2814/complete.js +71 -0
- package/examples/led/ws2814/demo.js +82 -0
- package/examples/led/ws2814/minimal.js +23 -0
- package/examples/light/apds-9930/complete.js +46 -0
- package/examples/light/apds-9930/demo.js +31 -0
- package/examples/light/apds-9930/minimal.js +20 -0
- package/examples/light/apds9960/complete.js +64 -0
- package/examples/light/apds9960/demo.js +34 -0
- package/examples/light/apds9960/minimal.js +12 -0
- package/examples/magnetometer/as5600/complete.js +31 -29
- package/examples/magnetometer/as5600/demo.js +35 -33
- package/examples/magnetometer/as5600/minimal.js +7 -7
- package/examples/magnetometer/hmc5883l/complete.js +47 -0
- package/examples/magnetometer/hmc5883l/demo.js +50 -0
- package/examples/magnetometer/hmc5883l/minimal.js +15 -0
- package/examples/memory/24aa02uid/complete.js +42 -0
- package/examples/memory/24aa02uid/demo.js +42 -0
- package/examples/memory/24aa02uid/minimal.js +12 -0
- package/examples/other/mpr121/complete.js +50 -0
- package/examples/other/mpr121/demo.js +30 -0
- package/examples/other/mpr121/minimal.js +17 -0
- package/examples/power/ade7953/complete.js +44 -0
- package/examples/power/ade7953/demo.js +28 -0
- package/examples/power/ade7953/minimal.js +15 -0
- package/examples/power/ina219/complete.js +20 -18
- package/examples/power/ina219/demo.js +15 -13
- package/examples/power/ina219/minimal.js +8 -8
- package/examples/power/ina226/complete.js +24 -22
- package/examples/power/ina226/demo.js +22 -20
- package/examples/power/ina226/minimal.js +5 -5
- package/examples/power/ina3221/complete.js +51 -49
- package/examples/power/ina3221/demo.js +13 -13
- package/examples/power/ina3221/minimal.js +8 -8
- package/examples/pressure/bmp085/complete.js +29 -0
- package/examples/pressure/bmp085/demo.js +34 -0
- package/examples/pressure/bmp085/minimal.js +19 -0
- package/examples/pressure/bmp180/complete.js +20 -17
- package/examples/pressure/bmp180/demo.js +24 -22
- package/examples/pressure/bmp180/minimal.js +13 -11
- package/examples/pressure/bmp280/complete.js +29 -22
- package/examples/pressure/bmp280/demo.js +42 -23
- package/examples/pressure/bmp280/minimal.js +13 -11
- package/examples/pressure/bmp384/complete.js +29 -0
- package/examples/pressure/bmp384/demo.js +40 -0
- package/examples/pressure/bmp384/minimal.js +18 -0
- package/examples/pressure/bmp581/complete.js +34 -0
- package/examples/pressure/bmp581/demo.js +56 -0
- package/examples/pressure/bmp581/minimal.js +19 -0
- package/examples/pressure/lps22df/complete.js +50 -0
- package/examples/pressure/lps22df/demo.js +41 -0
- package/examples/pressure/lps22df/minimal.js +19 -0
- package/examples/pressure/lps28dfw/complete.js +37 -0
- package/examples/pressure/lps28dfw/demo.js +33 -0
- package/examples/pressure/lps28dfw/minimal.js +19 -0
- package/examples/pressure/lps33hw/complete.js +43 -0
- package/examples/pressure/lps33hw/demo.js +43 -0
- package/examples/pressure/lps33hw/minimal.js +19 -0
- package/examples/rfid/mfrc522/complete.js +44 -0
- package/examples/rfid/mfrc522/demo.js +70 -0
- package/examples/rfid/mfrc522/minimal.js +17 -0
- package/package.json +5 -1
- package/src/chips/accelerometer/adxl345.js +482 -0
- package/src/chips/accelerometer/adxl362.js +594 -0
- package/src/chips/adc_dac/ad7705.js +345 -0
- package/src/chips/adc_dac/ad7706.js +331 -0
- package/src/chips/adc_dac/hx710a.js +184 -0
- package/src/chips/adc_dac/hx710b.js +186 -0
- package/src/chips/adc_dac/hx711.js +165 -0
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +297 -0
- package/src/chips/adc_dac/pcf8591.js +196 -0
- package/src/chips/comms/mcp2515.js +659 -0
- package/src/chips/comms/rda5807m.js +393 -0
- package/src/chips/comms/rfm9x.js +590 -0
- package/src/chips/display/pcf8576.js +232 -0
- package/src/chips/environmental/aht21.js +185 -0
- package/src/chips/environmental/bme280.js +385 -0
- package/src/chips/environmental/bme680.js +512 -0
- package/src/chips/gas/ens160.js +298 -0
- package/src/chips/gnss/neo6.js +438 -0
- package/src/chips/gyroscope/l3g4200d.js +366 -0
- package/src/chips/gyroscope/l3gd20h.js +314 -0
- package/src/chips/humidity/dht11.js +135 -0
- package/src/chips/imu/mpu6050.js +289 -0
- package/src/chips/imu/mpu9250.js +394 -0
- package/src/chips/imu/mpu9255.js +447 -0
- package/src/chips/io_expander/mcp23017.js +240 -157
- package/src/chips/io_expander/pcf8574.js +149 -124
- package/src/chips/io_expander/pcf8575.js +164 -135
- package/src/chips/io_expander/tpic6b595.js +242 -0
- package/src/chips/led/_NeoPixelRGBBase.js +204 -0
- package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
- package/src/chips/led/apa102.js +236 -0
- package/src/chips/led/sk6812rgbw.js +15 -149
- package/src/chips/led/ws2812b.js +14 -139
- package/src/chips/led/ws2814.js +49 -0
- package/src/chips/light/apds-9930.js +342 -0
- package/src/chips/light/apds9960.js +499 -0
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +182 -0
- package/src/chips/other/mpr121.js +375 -0
- package/src/chips/power/ade7953.js +427 -0
- package/src/chips/power/ina219.js +45 -40
- package/src/chips/power/ina226.js +50 -45
- package/src/chips/power/ina3221.js +95 -76
- package/src/chips/pressure/bmp085.js +251 -0
- package/src/chips/pressure/bmp180.js +54 -37
- package/src/chips/pressure/bmp280.js +188 -206
- package/src/chips/pressure/bmp384.js +410 -0
- package/src/chips/pressure/bmp581.js +489 -0
- package/src/chips/pressure/lps22df.js +341 -0
- package/src/chips/pressure/lps28dfw.js +314 -0
- package/src/chips/pressure/lps33hw.js +255 -0
- package/src/chips/rfid/mfrc522.js +674 -0
- package/src/connection/connection.js +111 -0
- package/src/connection/dhtxx.js +138 -0
- package/src/connection/dhtxx_mock.js +39 -0
- package/src/connection/hx711.js +132 -0
- package/src/connection/hx711_mock.js +52 -0
- package/src/{transport → connection}/i2c.js +13 -9
- package/src/connection/i2c_mock.js +91 -0
- package/src/connection/input_pin.js +156 -0
- package/src/connection/neo6_mock.js +45 -0
- package/src/{transport → connection}/neopixel.js +23 -4
- package/src/connection/output_pin.js +41 -0
- package/src/connection/sipo.js +159 -0
- package/src/{transport → connection}/smbus.js +14 -10
- package/src/{transport → connection}/spi.js +12 -8
- package/src/connection/spi_mock.js +151 -0
- package/src/{transport → connection}/uart.js +33 -26
- package/examples/display/.gitkeep +0 -0
- package/examples/environmental/.gitkeep +0 -0
- package/examples/gnss/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/imu/.gitkeep +0 -0
- package/examples/light/.gitkeep +0 -0
- package/examples/pressure/.gitkeep +0 -0
- package/src/chips/display/.gitkeep +0 -0
- package/src/chips/environmental/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/imu/.gitkeep +0 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/pressure/.gitkeep +0 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
3
|
+
const { L3G4200DFull } = require('../../../src/chips/gyroscope/l3g4200d');
|
|
4
|
+
|
|
5
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x68', 16);
|
|
7
|
+
|
|
8
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
9
|
+
|
|
10
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
11
|
+
|
|
12
|
+
(async () => {
|
|
13
|
+
// --- Rotation detector: 200 Hz, ±500 dps, FIFO stream with watermark 10 ---
|
|
14
|
+
// 200 Hz ODR gives 5 ms per sample — fast enough to catch hand motion but
|
|
15
|
+
// not so noisy that the FIFO drains before the watermark is reached.
|
|
16
|
+
const gyro = new L3G4200DFull(connection); // Create L3G4200D driver, (connection, busType='i2c')
|
|
17
|
+
await gyro.configure(1, 0, 500); // Configure chip, (odr=200Hz, bandwidth=0, full_scale=500) → undefined
|
|
18
|
+
await gyro.enableHighpass(0, 4); // Enable high-pass, (mode=0, cutoff=4) → undefined
|
|
19
|
+
// cutoff index 4 at 200 Hz ODR ≈ 1 Hz; strips DC drift
|
|
20
|
+
await gyro.enableFifo(2, 10); // Enable FIFO, (mode=2=stream, watermark=10) → undefined
|
|
21
|
+
|
|
22
|
+
const thresholdRadS = 90.0 * (Math.PI / 180.0);
|
|
23
|
+
let alerts = 0;
|
|
24
|
+
|
|
25
|
+
// --- Loop: wait for FIFO watermark, drain, compute mean, alert on threshold ---
|
|
26
|
+
// Stream mode keeps the oldest samples; the FIFO never blocks but the host
|
|
27
|
+
// only acts once per watermark crossing to amortise I²C overhead.
|
|
28
|
+
for (let n = 0; n < 50; n++) {
|
|
29
|
+
while ((await gyro.fifoSamples()) < 10) { // Read FIFO count, () → number
|
|
30
|
+
await sleep(5);
|
|
31
|
+
}
|
|
32
|
+
const burst = await gyro.readFifo(); // Drain FIFO, () → [[x, y, z] rad/s, ...]
|
|
33
|
+
if (!burst.length) continue;
|
|
34
|
+
let mx = 0, my = 0, mz = 0;
|
|
35
|
+
for (const s of burst) { mx += s[0]; my += s[1]; mz += s[2]; }
|
|
36
|
+
mx /= burst.length; my /= burst.length; mz /= burst.length;
|
|
37
|
+
if (Math.abs(mx) > thresholdRadS || Math.abs(my) > thresholdRadS || Math.abs(mz) > thresholdRadS) {
|
|
38
|
+
alerts++;
|
|
39
|
+
console.log(`ALERT X=${mx.toFixed(2)} Y=${my.toFixed(2)} Z=${mz.toFixed(2)} rad/s`);
|
|
40
|
+
} else {
|
|
41
|
+
console.log(` X=${mx.toFixed(2)} Y=${my.toFixed(2)} Z=${mz.toFixed(2)} rad/s`);
|
|
42
|
+
}
|
|
43
|
+
await sleep(20);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
console.log(`Total alerts: ${alerts} / 50`);
|
|
47
|
+
await connection.close();
|
|
48
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
49
|
+
})();
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
3
|
+
const { L3G4200DMinimal } = require('../../../src/chips/gyroscope/l3g4200d');
|
|
4
|
+
|
|
5
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
6
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x68', 16);
|
|
7
|
+
|
|
8
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
9
|
+
const gyro = new L3G4200DMinimal(connection); // Create L3G4200D driver, (connection, busType='i2c')
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
for (let i = 0; i < 10; i++) {
|
|
13
|
+
const [x, y, z] = await gyro.angularRate(); // Read X/Y/Z angular rate, () → [number, number, number] rad/s
|
|
14
|
+
console.log(`X=${x.toFixed(3)} Y=${y.toFixed(3)} Z=${z.toFixed(3)} rad/s`);
|
|
15
|
+
await new Promise(r => setTimeout(r, 100));
|
|
16
|
+
}
|
|
17
|
+
await connection.close();
|
|
18
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
19
|
+
})();
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const { I2CConnection } = require('../../src/connection/i2c_linux');
|
|
2
|
+
const { L3GD20HFull } = require('../../src/chips/gyroscope/l3gd20h');
|
|
3
|
+
|
|
4
|
+
const conn = new I2CConnection(1, 0x6A);
|
|
5
|
+
const gyro = new L3GD20HFull(conn);
|
|
6
|
+
|
|
7
|
+
async function main() {
|
|
8
|
+
await gyro.configure(1, 0, 1); // Configure, (odr 0-3, bw 0-3, fullScale 0-2)
|
|
9
|
+
// sets ODR=190 Hz, bandwidth=default, full-scale=±500 dps
|
|
10
|
+
|
|
11
|
+
await gyro.configureHpFilter(0, 0); // Configure HPF, (mode 0-3, cutoff 0-15)
|
|
12
|
+
await gyro.enableHpFilter(true); // Enable HPF, (enable=true)
|
|
13
|
+
|
|
14
|
+
await gyro.configureFifo(1, 10); // Configure FIFO, (mode 0/1/2/3/7, watermark 0-31)
|
|
15
|
+
await gyro.enableFifo(true); // Enable FIFO, (enable=true)
|
|
16
|
+
|
|
17
|
+
await gyro.setPowerMode(L3GD20HFull.POWER_NORMAL); // Set power mode
|
|
18
|
+
|
|
19
|
+
const who = await gyro._readReg(0x0F, 1);
|
|
20
|
+
console.log(`WHO_AM_I: 0x${who[0].toString(16).toUpperCase()}`);
|
|
21
|
+
|
|
22
|
+
const temp = await gyro.temperature(); // Read temperature, () -> int
|
|
23
|
+
console.log(`Temperature: ${temp}`);
|
|
24
|
+
|
|
25
|
+
while (true) {
|
|
26
|
+
if (await gyro.dataReady()) { // Check data ready, () -> bool
|
|
27
|
+
const [x, y, z] = await gyro.gyro(); // Read angular rate, () -> [float, float, float] rad/s
|
|
28
|
+
console.log(`x=${x.toFixed(3)} y=${y.toFixed(3)} z=${z.toFixed(3)} rad/s`);
|
|
29
|
+
|
|
30
|
+
const [rx, ry, rz] = await gyro.gyroRaw(); // Read raw, () -> [int, int, int]
|
|
31
|
+
|
|
32
|
+
const level = await gyro.fifoLevel(); // FIFO level, () -> int
|
|
33
|
+
if (level > 0) {
|
|
34
|
+
const samples = await gyro.readFifo(); // Read FIFO, () -> [[float,float,float],...]
|
|
35
|
+
console.log(`FIFO: ${samples.length} samples`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
await new Promise(r => setTimeout(r, 10));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const { I2CConnection } = require('../../src/connection/i2c_linux');
|
|
2
|
+
const { L3GD20HFull } = require('../../src/chips/gyroscope/l3gd20h');
|
|
3
|
+
|
|
4
|
+
const conn = new I2CConnection(1, 0x6A);
|
|
5
|
+
const gyro = new L3GD20HFull(conn);
|
|
6
|
+
|
|
7
|
+
async function main() {
|
|
8
|
+
// --- Configure for shake detection at 190 Hz, ±500 dps ---
|
|
9
|
+
// 190 Hz ODR provides good temporal resolution for shake detection;
|
|
10
|
+
// ±500 dps full scale gives 17.5 mdps/digit sensitivity, suitable for
|
|
11
|
+
// detecting moderate to strong motion without clipping.
|
|
12
|
+
await gyro.configure(1, 0, 1); // Configure, (odr 0-3, bw 0-3, fullScale 0-2)
|
|
13
|
+
|
|
14
|
+
console.log('L3GD20H shake detector running. Shake the device...');
|
|
15
|
+
|
|
16
|
+
while (true) {
|
|
17
|
+
if (await gyro.dataReady()) { // Check data ready, () -> bool
|
|
18
|
+
const [x, y, z] = await gyro.gyro(); // Read angular rate, () -> [float, float, float] rad/s
|
|
19
|
+
const magnitude = Math.sqrt(x*x + y*y + z*z);
|
|
20
|
+
if (magnitude > 1.0) {
|
|
21
|
+
console.log(`SHAKE DETECTED: mag=${magnitude.toFixed(3)} (x=${x.toFixed(3)} y=${y.toFixed(3)} z=${z.toFixed(3)})`);
|
|
22
|
+
} else {
|
|
23
|
+
console.log(`x=${x.toFixed(3)} y=${y.toFixed(3)} z=${z.toFixed(3)} mag=${magnitude.toFixed(3)}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
main().catch(console.error);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const { I2CConnection } = require('../../src/connection/i2c_linux');
|
|
2
|
+
const { L3GD20HMinimal } = require('../../src/chips/gyroscope/l3gd20h');
|
|
3
|
+
|
|
4
|
+
const conn = new I2CConnection(1, 0x6A);
|
|
5
|
+
const gyro = new L3GD20HMinimal(conn);
|
|
6
|
+
|
|
7
|
+
async function loop() {
|
|
8
|
+
const [x, y, z] = await gyro.gyro(); // Read angular rate, () -> [float, float, float] rad/s
|
|
9
|
+
console.log(`x=${x.toFixed(3)} y=${y.toFixed(3)} z=${z.toFixed(3)} rad/s`);
|
|
10
|
+
setTimeout(loop, 100);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
loop().catch(console.error);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { DHTxxConnection } = require('periph/src/connection/dhtxx');
|
|
4
|
+
const { DHT11Full } = require('periph/src/chips/humidity/dht11');
|
|
5
|
+
|
|
6
|
+
const DATA_PIN = parseInt(process.env.DHT11_PIN || '4', 10);
|
|
7
|
+
const connection = new DHTxxConnection(DATA_PIN);
|
|
8
|
+
const dht = new DHT11Full(connection, 3); // Create DHT11 driver, (connection, max_retries=3)
|
|
9
|
+
|
|
10
|
+
(async function() {
|
|
11
|
+
const t = dht.readTemperature(); // Read temperature, () → float °C
|
|
12
|
+
// returns a fresh conversion each call
|
|
13
|
+
const h = dht.readHumidity(); // Read humidity, () → float %RH
|
|
14
|
+
// returns a fresh conversion each call
|
|
15
|
+
const r = dht.readRetry(5); // Read with retries, (max_retries=5) → {temperature, humidity}
|
|
16
|
+
// retries up to 5 times on checksum error
|
|
17
|
+
const raw = dht.readRaw(); // Read raw frame, () → Buffer
|
|
18
|
+
// returns the validated 5-byte frame
|
|
19
|
+
console.log(`t=${t} h=${h} retry_t=${r.temperature} raw[0]=0x${raw[0].toString(16).padStart(2,'0').toUpperCase()}`);
|
|
20
|
+
connection.close();
|
|
21
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
22
|
+
})();
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { DHTxxConnection } = require('periph/src/connection/dhtxx');
|
|
4
|
+
const { DHT11Full } = require('periph/src/chips/humidity/dht11');
|
|
5
|
+
|
|
6
|
+
const DATA_PIN = parseInt(process.env.DHT11_PIN || '4', 10);
|
|
7
|
+
const connection = new DHTxxConnection(DATA_PIN);
|
|
8
|
+
const dht = new DHT11Full(connection, 3); // Create DHT11 driver, (connection, max_retries=3)
|
|
9
|
+
|
|
10
|
+
function comfort(h) {
|
|
11
|
+
if (h < 30.0) return 'dry';
|
|
12
|
+
if (h > 60.0) return 'humid';
|
|
13
|
+
return 'comfortable';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// --- Indoor comfort monitor ---
|
|
17
|
+
// Reads temperature and humidity every 5 seconds and prints a one-line
|
|
18
|
+
// status with a comfort assessment. Demonstrates reliable real-world polling
|
|
19
|
+
// with retry-based error recovery.
|
|
20
|
+
(async function() {
|
|
21
|
+
for (let n = 0; n < 60; n++) {
|
|
22
|
+
let r;
|
|
23
|
+
try {
|
|
24
|
+
r = dht.readRetry(3); // Read with retries, (max_retries=3) → {temperature, humidity}
|
|
25
|
+
} catch (e) {
|
|
26
|
+
// --- Handle read failure ---
|
|
27
|
+
// After all retries are exhausted, log a warning and continue.
|
|
28
|
+
// The next loop iteration will try again with a fresh sample.
|
|
29
|
+
console.log('WARN: DHT11 read failed after retries');
|
|
30
|
+
await new Promise(r => setTimeout(r, 5000));
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
console.log(`${r.temperature} C, ${r.humidity} %RH, ${comfort(r.humidity)}`);
|
|
34
|
+
await new Promise(r => setTimeout(r, 5000));
|
|
35
|
+
}
|
|
36
|
+
connection.close();
|
|
37
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
38
|
+
})();
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { DHTxxConnection } = require('periph/src/connection/dhtxx');
|
|
4
|
+
const { DHT11Minimal } = require('periph/src/chips/humidity/dht11');
|
|
5
|
+
|
|
6
|
+
const DATA_PIN = parseInt(process.env.DHT11_PIN || '4', 10);
|
|
7
|
+
const connection = new DHTxxConnection(DATA_PIN);
|
|
8
|
+
const dht = new DHT11Minimal(connection); // Create DHT11 driver, (connection)
|
|
9
|
+
|
|
10
|
+
(async function() {
|
|
11
|
+
for (let i = 0; i < 5; i++) {
|
|
12
|
+
const r = dht.read(); // Read temperature & humidity, () → {temperature: float °C, humidity: float %RH}
|
|
13
|
+
console.log(`${r.temperature} C, ${r.humidity} %RH`);
|
|
14
|
+
await new Promise(r => setTimeout(r, 2000));
|
|
15
|
+
}
|
|
16
|
+
connection.close();
|
|
17
|
+
console.log('===DONE: 0 passed, 0 failed===');
|
|
18
|
+
})();
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { MPU6050Full } = require('../../../src/chips/imu/mpu6050');
|
|
5
|
+
|
|
6
|
+
const connection = new I2CConnection(1, 0x68);
|
|
7
|
+
const imu = new MPU6050Full(connection); // Create MPU6050 driver, (connection, addr=0x68) → None
|
|
8
|
+
|
|
9
|
+
(async () => {
|
|
10
|
+
const [ax, ay, az] = await imu.accel(); // Read 3-axis acceleration, () → [float, float, float] m/s²
|
|
11
|
+
// converts raw accel register to m/s² (16384 LSB/g at ±2g)
|
|
12
|
+
const [gx, gy, gz] = await imu.gyro(); // Read 3-axis angular rate, () → [float, float, float] rad/s
|
|
13
|
+
// converts raw gyro register to rad/s (131.0 LSB/(°/s) at ±250dps)
|
|
14
|
+
|
|
15
|
+
await imu.configureGyro(1); // Configure gyro range, (fullScale=0) → None
|
|
16
|
+
// sets FS_SEL: 0=±250, 1=±500, 2=±1000, 3=±2000 dps
|
|
17
|
+
await imu.configureAccel(1); // Configure accel range, (fullScale=0) → None
|
|
18
|
+
// sets AFS_SEL: 0=±2g, 1=±4g, 2=±8g, 3=±16g
|
|
19
|
+
await imu.configureDlpf(3); // Configure DLPF bandwidth, (dlpf=3) → None
|
|
20
|
+
// sets DLPF_CFG: 0=260Hz … 6=5Hz (gyro/accel BW)
|
|
21
|
+
await imu.configureSampleRate(4); // Configure sample rate, (divider=4) → None
|
|
22
|
+
// sets SMPLRT_DIV: output rate = 1kHz / (1 + divider)
|
|
23
|
+
|
|
24
|
+
console.log('temp:', await imu.temperature()); // Read die temperature, () → float °C
|
|
25
|
+
// converts raw temp register: raw/340 + 36.53
|
|
26
|
+
|
|
27
|
+
const [rax, ray, raz] = await imu.accelRaw(); // Read raw accel values, () → [int, int, int]
|
|
28
|
+
// returns raw 16-bit signed accelerometer register values
|
|
29
|
+
const [rgx, rgy, rgz] = await imu.gyroRaw(); // Read raw gyro values, () → [int, int, int]
|
|
30
|
+
// returns raw 16-bit signed gyroscope register values
|
|
31
|
+
|
|
32
|
+
console.log('data_ready:', await imu.dataReady()); // Check data ready flag, () → bool
|
|
33
|
+
// reads DATA_RDY_INT bit from INT_STATUS register
|
|
34
|
+
|
|
35
|
+
await imu.setSleep(true); // Enter sleep mode, (sleep=true) → None
|
|
36
|
+
// sets SLEEP bit in PWR_MGMT_1
|
|
37
|
+
const end1 = Date.now() + 10;
|
|
38
|
+
while (Date.now() < end1) {}
|
|
39
|
+
await imu.setSleep(false); // Wake from sleep, (sleep=true) → None
|
|
40
|
+
// clears SLEEP bit in PWR_MGMT_1
|
|
41
|
+
|
|
42
|
+
await imu.setStandby(true, false, false, false, false, false); // Set axes standby, (xa, ya, za, xg, yg, zg) → None
|
|
43
|
+
// puts individual axes into low-power standby mode
|
|
44
|
+
await imu.setStandby(); // Clear all standby, (xa=false, ...) → None
|
|
45
|
+
// restores all axes from standby
|
|
46
|
+
|
|
47
|
+
await imu.enableFifo(true, true, false); // Enable FIFO sources, (gyro=true, accel=true, temp=false) → None
|
|
48
|
+
// configures FIFO_EN and sets FIFO_EN bit in USER_CTRL
|
|
49
|
+
await imu.resetFifo(); // Reset FIFO buffer, () → None
|
|
50
|
+
// sets FIFO_RST bit in USER_CTRL to clear the buffer
|
|
51
|
+
console.log('fifo_count:', await imu.fifoCount()); // Read FIFO byte count, () → int
|
|
52
|
+
// reads FIFO_COUNTH/L: number of bytes available
|
|
53
|
+
const data = await imu.readFifo(); // Read FIFO data, () → Buffer
|
|
54
|
+
// reads all available bytes from FIFO_R_W register
|
|
55
|
+
console.log('fifo read:', data.length, 'bytes');
|
|
56
|
+
|
|
57
|
+
await connection.close();
|
|
58
|
+
})();
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { MPU6050Full } = require('../../../src/chips/imu/mpu6050');
|
|
5
|
+
|
|
6
|
+
const connection = new I2CConnection(1, 0x68);
|
|
7
|
+
|
|
8
|
+
// --- Configure for motion logging with moderate dynamic range ---
|
|
9
|
+
// ±4g captures typical tilting and handling forces without clipping;
|
|
10
|
+
// ±500 dps covers fast rotations while retaining sub-degree resolution.
|
|
11
|
+
const imu = new MPU6050Full(connection); // Create MPU6050 driver, (connection, addr=0x68) → None
|
|
12
|
+
|
|
13
|
+
(async () => {
|
|
14
|
+
await imu.configureAccel(1); // Configure accel range, (fullScale=0) → None
|
|
15
|
+
await imu.configureGyro(1); // Configure gyro range, (fullScale=0) → None
|
|
16
|
+
|
|
17
|
+
console.log('roll pitch |accel| |gyro|');
|
|
18
|
+
|
|
19
|
+
setInterval(async () => {
|
|
20
|
+
// gate reads on dataReady so each sample reflects a fresh conversion
|
|
21
|
+
while (!(await imu.dataReady())) {} // Check data ready flag, () → bool
|
|
22
|
+
|
|
23
|
+
const [ax, ay, az] = await imu.accel(); // Read 3-axis acceleration, () → [float, float, float] m/s²
|
|
24
|
+
const [gx, gy, gz] = await imu.gyro(); // Read 3-axis angular rate, () → [float, float, float] rad/s
|
|
25
|
+
|
|
26
|
+
// --- Compute tilt angles from the accelerometer gravity vector ---
|
|
27
|
+
// roll and pitch are reliable when the device is quasi-static;
|
|
28
|
+
// gyro magnitude indicates how fast the board is being rotated.
|
|
29
|
+
const roll = Math.atan2(ay, az) * 180.0 / Math.PI;
|
|
30
|
+
const pitch = Math.atan2(-ax, Math.sqrt(ay * ay + az * az)) * 180.0 / Math.PI;
|
|
31
|
+
const accelMag = Math.sqrt(ax * ax + ay * ay + az * az);
|
|
32
|
+
const gyroMag = Math.sqrt(gx * gx + gy * gy + gz * gz);
|
|
33
|
+
|
|
34
|
+
console.log(roll.toFixed(1).padEnd(9) + pitch.toFixed(1).padEnd(9) +
|
|
35
|
+
accelMag.toFixed(3).padEnd(11) + gyroMag.toFixed(3));
|
|
36
|
+
}, 100);
|
|
37
|
+
})();
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../../src/connection/i2c');
|
|
4
|
+
const { MPU6050Minimal } = require('../../../src/chips/imu/mpu6050');
|
|
5
|
+
|
|
6
|
+
const connection = new I2CConnection(1, 0x68);
|
|
7
|
+
const imu = new MPU6050Minimal(connection); // Create MPU6050 driver, (connection, addr=0x68) → None
|
|
8
|
+
|
|
9
|
+
setInterval(async () => {
|
|
10
|
+
const [ax, ay, az] = await imu.accel(); // Read 3-axis acceleration, () → [float, float, float] m/s²
|
|
11
|
+
const [gx, gy, gz] = await imu.gyro(); // Read 3-axis angular rate, () → [float, float, float] rad/s
|
|
12
|
+
console.log('accel:', ax.toFixed(2), ay.toFixed(2), az.toFixed(2),
|
|
13
|
+
' gyro:', gx.toFixed(2), gy.toFixed(2), gz.toFixed(2));
|
|
14
|
+
}, 100);
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../packages/periph/src/connection/i2c');
|
|
4
|
+
const { MPU9250Full } = require('../../packages/periph/src/chips/imu/mpu9250');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x68', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const magConnection = new I2CConnection(I2C_BUS, 0x0C); // AK8963, same bus, reached via I²C bypass
|
|
11
|
+
const imu = new MPU9250Full(connection, magConnection); // Create MPU9250 driver, (connection, magConnection) → void
|
|
12
|
+
|
|
13
|
+
async function main() {
|
|
14
|
+
const [ax, ay, az] = await imu.accel(); // Read 3-axis acceleration, () → [number, number, number] m/s²
|
|
15
|
+
// converts raw accel register to m/s² (16384 LSB/g at ±2g)
|
|
16
|
+
const [gx, gy, gz] = await imu.gyro(); // Read 3-axis angular rate, () → [number, number, number] rad/s
|
|
17
|
+
// converts raw gyro register to rad/s (131.0 LSB/(°/s) at ±250dps)
|
|
18
|
+
|
|
19
|
+
await imu.configureGyro(1); // Configure gyro range, (fullScale=0) → void
|
|
20
|
+
// sets GYRO_FS_SEL: 0=±250, 1=±500, 2=±1000, 3=±2000 dps
|
|
21
|
+
await imu.configureAccel(1); // Configure accel range, (fullScale=0) → void
|
|
22
|
+
// sets ACCEL_FS_SEL: 0=±2g, 1=±4g, 2=±8g, 3=±16g
|
|
23
|
+
await imu.configureDlpf(3, 3); // Configure DLPF bandwidth, (gyroDlpf=3, accelDlpf=3) → void
|
|
24
|
+
// sets DLPF_CFG/A_DLPFCFG: 0=256/460Hz … 6=5/10Hz (gyro/accel BW)
|
|
25
|
+
await imu.configureSampleRate(4); // Configure sample rate, (divider=4) → void
|
|
26
|
+
// sets SMPLRT_DIV: output rate = 1kHz / (1 + divider)
|
|
27
|
+
|
|
28
|
+
const t = await imu.temperature(); // Read die temperature, () → number °C
|
|
29
|
+
// converts raw temp register: raw/333.87 + 21.0
|
|
30
|
+
|
|
31
|
+
await imu.enableMag(16, 6); // Initialize magnetometer, (bits=16, mode=6) → void
|
|
32
|
+
// reads ASA calibration, sets 16-bit 100 Hz continuous mode
|
|
33
|
+
const [mx, my, mz] = await imu.mag(); // Read 3-axis magnetic field, () → [number, number, number] µT
|
|
34
|
+
// applies factory ASA calibration and sensitivity scaling
|
|
35
|
+
|
|
36
|
+
const [rax, ray, raz] = await imu.accelRaw(); // Read raw accel values, () → [number, number, number]
|
|
37
|
+
// returns raw 16-bit signed accelerometer register values
|
|
38
|
+
const [rgx, rgy, rgz] = await imu.gyroRaw(); // Read raw gyro values, () → [number, number, number]
|
|
39
|
+
// returns raw 16-bit signed gyroscope register values
|
|
40
|
+
const [rmx, rmy, rmz] = await imu.magRaw(); // Read raw mag values, () → [number, number, number]
|
|
41
|
+
// returns raw 16-bit signed magnetometer register values
|
|
42
|
+
|
|
43
|
+
const ready = await imu.dataReady(); // Check data ready flag, () → boolean
|
|
44
|
+
// reads RAW_DATA_RDY_INT bit from INT_STATUS register
|
|
45
|
+
|
|
46
|
+
await imu.setSleep(true); // Enter sleep mode, (sleep=true) → void
|
|
47
|
+
// sets SLEEP bit in PWR_MGMT_1
|
|
48
|
+
await new Promise(r => setTimeout(r, 10));
|
|
49
|
+
await imu.setSleep(false); // Wake from sleep, (sleep=true) → void
|
|
50
|
+
// clears SLEEP bit in PWR_MGMT_1
|
|
51
|
+
|
|
52
|
+
await imu.resetFifo(); // Reset FIFO buffer, () → void
|
|
53
|
+
// sets FIFO_RST bit in USER_CTRL to clear the buffer
|
|
54
|
+
await imu.enableFifo(true, true); // Enable FIFO sources, (gyro=true, accel=true, temp=false) → void
|
|
55
|
+
// configures FIFO_EN and sets FIFO_EN bit in USER_CTRL
|
|
56
|
+
await new Promise(r => setTimeout(r, 50));
|
|
57
|
+
const count = await imu.fifoCount(); // Read FIFO byte count, () → number
|
|
58
|
+
// reads FIFO_COUNTH/L: number of bytes available
|
|
59
|
+
const data = await imu.readFifo(); // Read FIFO data, () → Buffer
|
|
60
|
+
// reads all available bytes from FIFO_R_W register
|
|
61
|
+
await imu.resetFifo(); // Reset FIFO buffer, () → void
|
|
62
|
+
// sets FIFO_RST bit in USER_CTRL to clear the buffer
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../packages/periph/src/connection/i2c');
|
|
4
|
+
const { MPU9250Full } = require('../../packages/periph/src/chips/imu/mpu9250');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x68', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const magConnection = new I2CConnection(I2C_BUS, 0x0C); // AK8963, same bus, reached via I²C bypass
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
// --- Configure for tilt and heading estimation ---
|
|
14
|
+
// ±4g / ±500dps trade sensitivity for headroom against sharper motion than
|
|
15
|
+
// the ±2g / ±250dps defaults tolerate; 16-bit continuous magnetometer mode
|
|
16
|
+
// keeps a fresh heading available on every poll.
|
|
17
|
+
const imu = new MPU9250Full(connection, magConnection); // Create MPU9250 driver, (connection, magConnection) → void
|
|
18
|
+
await imu.configureAccel(1); // Configure accel range, (fullScale=0) → void
|
|
19
|
+
await imu.configureGyro(1); // Configure gyro range, (fullScale=0) → void
|
|
20
|
+
await imu.enableMag(16, 6); // Initialize magnetometer, (bits=16, mode=6) → void
|
|
21
|
+
|
|
22
|
+
console.log('roll pitch heading |accel| |gyro|');
|
|
23
|
+
|
|
24
|
+
while (true) {
|
|
25
|
+
// gate reads on dataReady so each sample reflects a fresh conversion
|
|
26
|
+
while (!await imu.dataReady()) { // Check data ready flag, () → boolean
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const [ax, ay, az] = await imu.accel(); // Read 3-axis acceleration, () → [number, number, number] m/s²
|
|
30
|
+
const [gx, gy, gz] = await imu.gyro(); // Read 3-axis angular rate, () → [number, number, number] rad/s
|
|
31
|
+
const [mx, my, mz] = await imu.mag(); // Read 3-axis magnetic field, () → [number, number, number] µT
|
|
32
|
+
|
|
33
|
+
// --- Compute tilt angles from the accelerometer gravity vector ---
|
|
34
|
+
// roll and pitch are reliable when the device is quasi-static;
|
|
35
|
+
// gyro magnitude indicates how fast the board is being rotated.
|
|
36
|
+
const roll = Math.atan2(ay, az) * 180.0 / Math.PI;
|
|
37
|
+
const pitch = Math.atan2(-ax, Math.sqrt(ay * ay + az * az)) * 180.0 / Math.PI;
|
|
38
|
+
|
|
39
|
+
// --- Compute magnetic heading (simplified, no tilt compensation) ---
|
|
40
|
+
// Magnetometer axes differ from accel/gyro axes; user must account for this in fusion.
|
|
41
|
+
const heading = Math.atan2(my, mx) * 180.0 / Math.PI;
|
|
42
|
+
|
|
43
|
+
const accelMag = Math.sqrt(ax * ax + ay * ay + az * az);
|
|
44
|
+
const gyroMag = Math.sqrt(gx * gx + gy * gy + gz * gz);
|
|
45
|
+
|
|
46
|
+
console.log('%s %s %s %s %s',
|
|
47
|
+
roll.toFixed(1), pitch.toFixed(1), heading.toFixed(1),
|
|
48
|
+
accelMag.toFixed(3), gyroMag.toFixed(3));
|
|
49
|
+
await new Promise(r => setTimeout(r, 100));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../packages/periph/src/connection/i2c');
|
|
4
|
+
const { MPU9250Minimal } = require('../../packages/periph/src/chips/imu/mpu9250');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x68', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const imu = new MPU9250Minimal(connection); // Create MPU9250 driver, (connection) → void
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
while (true) {
|
|
14
|
+
const [ax, ay, az] = await imu.accel(); // Read 3-axis acceleration, () → [number, number, number] m/s²
|
|
15
|
+
const [gx, gy, gz] = await imu.gyro(); // Read 3-axis angular rate, () → [number, number, number] rad/s
|
|
16
|
+
console.log('accel: %.2f %.2f %.2f gyro: %.2f %.2f %.2f', ax, ay, az, gx, gy, gz);
|
|
17
|
+
await new Promise(r => setTimeout(r, 100));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
main().catch(err => { console.error(err); process.exit(1); });
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { I2CConnection } = require('../../packages/periph/src/connection/i2c');
|
|
4
|
+
const { MPU9255Full } = require('../../packages/periph/src/chips/imu/mpu9255');
|
|
5
|
+
|
|
6
|
+
const I2C_BUS = parseInt(process.env.I2C_BUS || '1', 10);
|
|
7
|
+
const I2C_ADDR = parseInt(process.env.I2C_ADDR || '0x68', 16);
|
|
8
|
+
|
|
9
|
+
const connection = new I2CConnection(I2C_BUS, I2C_ADDR);
|
|
10
|
+
const magConnection = new I2CConnection(I2C_BUS, 0x0C); // AK8963, same bus, reached via I²C bypass
|
|
11
|
+
const imu = new MPU9255Full(connection, magConnection); // Create MPU9255 driver, (connection, magConnection) → void
|
|
12
|
+
|
|
13
|
+
async function main() {
|
|
14
|
+
const [ax, ay, az] = await imu.accel(); // Read 3-axis acceleration, () → [number, number, number] m/s²
|
|
15
|
+
// converts raw accel register to m/s² (16384 LSB/g at ±2g)
|
|
16
|
+
const [gx, gy, gz] = await imu.gyro(); // Read 3-axis angular rate, () → [number, number, number] rad/s
|
|
17
|
+
// converts raw gyro register to rad/s (131.0 LSB/(°/s) at ±250dps)
|
|
18
|
+
|
|
19
|
+
await imu.configureGyro(1); // Configure gyro range, (fullScale=0) → void
|
|
20
|
+
// sets GYRO_FS_SEL: 0=±250, 1=±500, 2=±1000, 3=±2000 dps
|
|
21
|
+
await imu.configureAccel(1); // Configure accel range, (fullScale=0) → void
|
|
22
|
+
// sets ACCEL_FS_SEL: 0=±2g, 1=±4g, 2=±8g, 3=±16g
|
|
23
|
+
await imu.configureDlpf(3, 3); // Configure DLPF bandwidth, (gyroDlpf=3, accelDlpf=3) → void
|
|
24
|
+
// sets DLPF_CFG/A_DLPFCFG: 0=256/460Hz … 6=5/10Hz (gyro/accel BW)
|
|
25
|
+
await imu.configureSampleRate(4); // Configure sample rate, (divider=4) → void
|
|
26
|
+
// sets SMPLRT_DIV: output rate = 1kHz / (1 + divider)
|
|
27
|
+
|
|
28
|
+
const t = await imu.temperature(); // Read die temperature, () → number °C
|
|
29
|
+
// converts raw temp register: raw/333.87 + 21.0
|
|
30
|
+
|
|
31
|
+
await imu.enableMag(16, 6); // Initialize magnetometer, (bits=16, mode=6) → void
|
|
32
|
+
// reads ASA calibration, sets 16-bit 100 Hz continuous mode
|
|
33
|
+
const [mx, my, mz] = await imu.mag(); // Read 3-axis magnetic field, () → [number, number, number] µT
|
|
34
|
+
// applies factory ASA calibration and sensitivity scaling
|
|
35
|
+
|
|
36
|
+
const [rax, ray, raz] = await imu.accelRaw(); // Read raw accel values, () → [number, number, number]
|
|
37
|
+
// returns raw 16-bit signed accelerometer register values
|
|
38
|
+
const [rgx, rgy, rgz] = await imu.gyroRaw(); // Read raw gyro values, () → [number, number, number]
|
|
39
|
+
// returns raw 16-bit signed gyroscope register values
|
|
40
|
+
const [rmx, rmy, rmz] = await imu.magRaw(); // Read raw mag values, () → [number, number, number]
|
|
41
|
+
// returns raw 16-bit signed magnetometer register values
|
|
42
|
+
|
|
43
|
+
const ready = await imu.dataReady(); // Check data ready flag, () → boolean
|
|
44
|
+
// reads RAW_DATA_RDY_INT bit from INT_STATUS register
|
|
45
|
+
|
|
46
|
+
await imu.setSleep(true); // Enter sleep mode, (sleep=true) → void
|
|
47
|
+
// sets SLEEP bit in PWR_MGMT_1
|
|
48
|
+
await new Promise(r => setTimeout(r, 10));
|
|
49
|
+
await imu.setSleep(false); // Wake from sleep, (sleep=true) → void
|
|
50
|
+
// clears SLEEP bit in PWR_MGMT_1
|
|
51
|
+
|
|
52
|
+
await imu.resetFifo(); // Reset FIFO buffer, () → void
|
|
53
|
+
// sets FIFO_RST bit in USER_CTRL to clear the buffer
|
|
54
|
+
await imu.enableFifo(true, true); // Enable FIFO sources, (gyro=true, accel=true, temp=false) → void
|
|
55
|
+
// configures FIFO_EN and sets FIFO_EN bit in USER_CTRL
|
|
56
|
+
await new Promise(r => setTimeout(r, 50));
|
|
57
|
+
const count = await imu.fifoCount(); // Read FIFO byte count, () → number
|
|
58
|
+
// reads FIFO_COUNTH/L: number of bytes available
|
|
59
|
+
const data = await imu.readFifo(); // Read FIFO data, () → Buffer
|
|
60
|
+
// reads all available bytes from FIFO_R_W register
|
|
61
|
+
await imu.resetFifo(); // Reset FIFO buffer, () → void
|
|
62
|
+
// sets FIFO_RST bit in USER_CTRL to clear the buffer
|
|
63
|
+
|
|
64
|
+
await imu.configureWakeOnMotion(64, 31.25); // Configure wake-on-motion, (thresholdMg=64, odrHz=31.25) → void
|
|
65
|
+
// disables gyro, arms hardware motion detection at 31.25 Hz
|
|
66
|
+
const motion = await imu.motionDetected(); // Check motion detected, () → boolean
|
|
67
|
+
// reads WOM_INT bit from INT_STATUS register (clears on read)
|
|
68
|
+
|
|
69
|
+
console.log('t=%.2f mag=%.1f,%.1f,%.1f ready=%s motion=%s',
|
|
70
|
+
t, mx, my, mz, ready, motion);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
main().catch(err => { console.error(err); process.exit(1); });
|