A driver library for the MPU6886 6-axis IMU (Inertial Measurement Unit) sensor for BlueScript. This package provides a high-level interface to easily initialize the sensor, configure ranges, and read accelerometer, gyroscope, and internal temperature data via the I2C bus.
Install this package in your BlueScript project:
bscript project install https://github.com/bluescript-lang/pkg-mpu6886.gitimport * as time from "time";
import { I2CMasterBus, I2CPort } from "i2c";
import { MPU6886, GyroRange, AccelRange } from "mpu6886";
// 1. Initialize I2C Bus (e.g., SDA: 21, SCL: 22)
const bus = new I2CMasterBus(I2CPort.I2C0, 21, 22);
// 2. Create MPU6886 instance and initialize
const imu = new MPU6886(bus);
if (!imu.init()) {
console.log("Failed to initialize MPU6886!");
bus.close();
// Stop execution or handle error appropriately
} else {
console.log("MPU6886 initialized successfully.");
}
// Optional: Change measurement ranges (Defaults are 8G and 2000 DPS)
imu.setAccelRange(AccelRange.G4);
imu.setGyroRange(GyroRange.Dps500);
// 3. Read data in a loop
while (true) {
const accel = imu.getAcceleration();
const gyro = imu.getGyroscope();
const temp = imu.getTemperature();
if (accel !== null && gyro !== null) {
console.log("Accel (G) X: " + accel.x + ", Y: " + accel.y + ", Z: " + accel.z);
console.log("Gyro (DPS) X: " + gyro.x + ", Y: " + gyro.y + ", Z: " + gyro.z);
console.log("Temp (C) " + temp);
} else {
console.log("Failed to read sensor data.");
}
time.delay(100);
}
// imu.deinit();
// bus.close();The primary class representing the MPU6886 sensor device.
Creates an instance of the MPU6886 sensor using an existing I2C bus.
- bus: An initialized
I2CMasterBusobject. - Note: Automatically targets I2C address
0x68at400kHz. Defaults toAccelRange.G8andGyroRange.Dps2000.
Wakes up and configures the sensor. Checks device ID (WHO_AM_I), resets power management, and applies default configurations.
- Returns:
trueif initialization is successful;falseif the device is not found or fails to respond.
Puts the sensor into sleep mode and closes the underlying I2C device handle.
Sets the full-scale measurement range of the gyroscope and updates the internal resolution multiplier.
- range: A
GyroRangeenum value. - Returns:
trueon success,falseon failure.
Sets the full-scale measurement range of the accelerometer and updates the internal resolution multiplier.
- range: An
AccelRangeenum value. - Returns:
trueon success,falseon failure.
Reads the current accelerometer data (X, Y, Z axes).
- Returns: A
Vector3Dobject representing acceleration in G, ornullif the I2C read fails.
Reads the current gyroscope data (X, Y, Z axes).
- Returns: A
Vector3Dobject representing angular velocity in DPS (degrees per second), ornullif the I2C read fails.
Reads the internal temperature sensor.
- Returns: Temperature in Celsius.
Manually applies raw hardware offsets to the gyroscope axes.
- Returns:
trueon success,falseon failure.
A simple data container used for returning 3-axis float readings.
x: floaty: floatz: float
Defines the full-scale range for the gyroscope.
| Name | Value | Measurement Range |
|---|---|---|
Dps250 |
0 | ±250 deg/s |
Dps500 |
1 | ±500 deg/s |
Dps1000 |
2 | ±1000 deg/s |
Dps2000 |
3 | ±2000 deg/s |
Defines the full-scale range for the accelerometer.
| Name | Value | Measurement Range |
|---|---|---|
G2 |
0 | ±2 G |
G4 |
1 | ±4 G |
G8 |
2 | ±8 G |
G16 |
3 | ±16 G |