REAL MAGNETOMETER DATA
The AK8963 runs in continuous 16-bit mode (0.15 µT/LSB); every sample is validity-gated by the ST1/ST2 status registers. Actual microteslas, not noise.
// ATTITUDE & HEADING SYSTEM
A Python driver for the InvenSense MPU-9250 — accelerometer, gyroscope and AK8963 magnetometer over I2C on the Raspberry Pi. Tested without hardware. Documented down to the silicon.
DRAG THE CUBE — BODY-FRAME AXES, AS THE DRIVER REPORTS THEM
The AK8963 runs in continuous 16-bit mode (0.15 µT/LSB); every sample is validity-gated by the ST1/ST2 status registers. Actual microteslas, not noise.
WHO_AM_I and the magnetometer id are verified before configuring anything — relabeled MPU-6500 boards get diagnosed, not silently tolerated.
Magnetometer readings are remapped into the accel/gyro body frame, so heading and fusion math get consistent axes out of the box.
A drift-free ticker samples at your rate; get_avg() synchronously returns the average of everything since the previous call.
The I2C bus is injectable — the whole test suite runs against an in-memory fake, on any machine, in under a second.
Two dies, two byte orders, two access modes — thehardware doc explains the chip the datasheets assume you already understand.
# On the Raspberry Pi (I2C enabled)
git clone https://github.com/olagopirez/sextante.git
cd sextante
pip install -e .import time
from mpu9250 import MPU9250
mpu = MPU9250(rate=50)
mpu.initialize() # self-checks the chip, calibrates, starts sampling
while True:
time.sleep(1)
print(mpu.get_avg().get_json()) # °/s, g, µT, °CAt rest you should see A3 ≈ 1.0 (gravity), gyros ≈ 0 and a realistic temperature — a five-second wiring sanity check.
The MPU-9250 is discontinued and heavily counterfeited. self_check()reads WHO_AM_I and tells you what you actually own:
WHO_AM_I | CHIP | VERDICT |
|---|---|---|
0x71 | MPU-9250 | GENUINE ✓ |
0x73 | MPU-9255 | WORKS FINE ✓ |
0x70 | MPU-6500 | RELABEL — NO MAGNETOMETER |
0x68 | MPU-6050 | DIFFERENT PART |