The MPU-9250 is an impressive IMU (Inertial Measurement Unit) sensor: For 5 bucks you get "9 axes": A three axes accelerometer, a three axes gyro and a three axis magnetometer (compass).
But dealing with the sensor is anything but easy! It just gives you raw values, you need to do a lot to get a stable angle calculation.
And the only Node-RED node existing is not working anymore on newer node.js versions!
Thus, I decided to start my last winter project for 2020/21: We will go step by step into the integration of the MPU-9250 in Node-RED:
we will use a special note to be able to use Python in Node-RED
we will read out registers, create objects in Python and hand them back to Node-RED
we will combine high and low bytes to words
we will calibrate the accelerometer sensor, using the 15 bit offset registers
we will perform some trigonometry to calculate the rotation angle
we will create a dashboard in Node-RED to get our readings and results
This is part 4:
We will read-out the gyro, magnetometer and temperature sensor today and add it to the dashboard, next to our accelerometer reading (and pitch and roll graphs).
The magnetometer is a bit tricky. The datasheet of the MPU-9250 is already separated: The registers for the magnetometer are separated. Why? Because it is a separate sensor and manufacturer. It is the AK8964 of AshahiKASEI.
It must be dealt differently.
On this sensor, the order of high and low byte is the opposite compared to the accelerometer and gyro.
The sensitivity is specified in uT/LSB (e.g. the accelerometer stated LSB/g). So: Upside down. Needs to be considered as well.
But the weird part is the 14/16 bit mismatch. The AK8963 has two modes: 16 bit with 0.15uT/LSB and 14 bit with 0.6uT/LSB.
The MPU datasheets offers a 16 bit register, mentioned a 16bit ADC but states 0.6uT/LSB. This can't work out. This would exceed the measurement range of +-4800uT by far.
Most likely the 16bit register carries just a 14bit value. Thus, the word needs to be shifted by 2 bit to the right.
The other possibility is that it is the 16bit mode. In this case the sensitivity can't be 0.6uT/LSB but must be 0.15uT/LSB.
If somebody did some testing or did realize it already, we are all happy about some comments here below.
Last remark: With this part we do have all the things together. But the more comprehensive stuff is to use them now for some mathematics.
E.g. using the magnetometer needs calibration and tilt compensation. We did already to some tilt calculation with pitch and roll.
Optimizing the accelerometer readings can be done using the gyro. It can help to compensate movements which would influence the readings of the earth's gravity.
All this is interesting and it makes the sensor so comprehensive, powerful but complex. As my time is limited, even though I did the tilt compensation for a magnetometer (in this case a Philips (now NXP) MR, not Hall sensor) "many" years ago, I will stop the MPU tutorial here.
With this tutorial, you learned how to use Python in node red and some Python basics. You learned about registers (hardware related programming) as well as using objects and the Node-RED dashboard. We did some trigonometry for angle calculation. This sensor and tutorial touches many different aspects of programming and electronics. If you need to dig deeper into the MPU-9250, you should refer to the links below. Especially Matlab has some great tutorials. E.g. for drones the MPU-9250 is awesome. But: There are libraries and ready to use algorithms, if you don't want to start your mathematics career today but rather focusing on the application itself. Your choice.
Thank you for your company up to here. I got my first Raspberry PICO and will to a short introduction on this channel soon. Stay safe and all the best!
Recommended links:
• Understanding Sensor Fusion and Tracking, ... ||| great explanation from Mathworks about IMU. Best I know!
https://www.nxp.com/docs/en/applicati... ||| Application Node from NXP about Tilt Sensing
• 9-Axis IMU LESSON 6: Determine Tilt From 3... ||| Tutorials about tilt calculation of IMUs