Using the MPU9250 to get Real-time Motion Data GPL3+

Содержание

Code and explanation for getting directional and motion data from the raw sensor outputs.

  • 136,702 views
  • 7 comments
  • 39 respects

Components and supplies

Apps and online services

About this project

Motion Processing is an important concept to know. If you want to interact with real time data you should be able to interact with motion parameters such as: linear acceleration, angular acceleration, and magnetic north.

The MPU9250 has an accelerometer, gyroscope, and a magnetometer. The information that we can get from a MPU9250’s are: yaw angle, pitch angle, and roll angle. Given that, I will only deal with yaw here in this post.

Processing Data from the MPU9250

The MPU has a 16-bit register for each of its three sensors. They temporarily store the data from the sensor before it is relayed via I2C.

Reading Data

We receive the data 8-bits at a time and then concatenate them together to form 16-bits again. As shown in the following snippet from kriswiners code:

Calibrating the Raw Data

The data that is received then must be calibrated according to the users environment. The calibration of the magnetometer is required to compensate for Magnetic Declination. The exact value of the correction depends on the location. There are two variables that have to calibrated: yaw and magbias.

The below shows yaw calibration for a specific magnetic declination (at Potheri , Chennai, India). The declination data can be obtained from different sites:

See the below snippet [the given snippet the data for calibration of magbias comes from another function ( magcalMPU9250(float * dest1, float * dest2) ):

Auto Calibration of the Magnetometer

This is one of the simplest and important part of the MPU code. The function magcalMPU9250(float * dest1, float * dest2) calibrates the magnetometer while you move the sensor in a figure eight. It stores the maximum and minimum readings and takes the average.

for more details please go to the source:

Permanent Calibration for a Particular Place

If you don’t want auto calibrate every time, then you just have to note the average values of magbias[] after it has been calculated and use this snippet:

The values 470, 120, 125 is fixed for my location, so after doing this there is no need of void magcalMPU9250(float * dest1, float * dest2) function so you can either comment it out or remove it. Also don’t forget to comment out the calling statement as well:

Because the raw data contains a lot of noise we use certain filters on the output of the sensors to convert them to Quaternions (Madgwick/Mahony/Kalman):

As the data changes very rapidly we sample for some amount of time (50 ms) and take the average.

Extracting Real-World Information

Finally we get the readings in form of yaw, pitch, and roll from the quaternions.

Getting readings from MPU9250 with Arduino Mega 2560

We have lots of libraries for MPU9250. One of the popular one is given by kriswiner: MPU-9250 Arduino Library by Kriswiner

Once you save the library to your Arduino folder you are ready to go. Open the example MPU9250BasicAHRS.ino. Also have this setup ready:

MPU9250 Breakout ——— Arduino

  • VIN ———————- 5V
  • SDA ———————– SDA (Pin 20)
  • SCL ———————– SCL (Pin 21)
  • GND ———————- GND

These wires shouldn’t be very long because I2C connections don’t work well for long wires.

Now clean up the MPU9250BasicAHRS code. It has LCD code in it, but we don’t need it, so remove unnecessary lines. Also I have added a part of auto calibration code. Here is the modified code without unnecessary code and added auto calibration: Github.

Now upload the code to your Arduino and make the connections shown above. Open the Serial Terminal and change the baud rate to 115200. You should see this output:

If you see this:

This means there is definitely a wiring problem (or in the worst case Mpu/arduino fault) try to rectify it before proceeding.

If everything goes well and you see “MPU is online” and “Mag Calibration: Wave device in a figure eight until done!” then everything is working and you should wave your mpu in figure eights until it finishes the auto calibration. After a while you should be getting yaw, pitch, and roll output as below:

Awesome! Now you have the data, and you can play with real time motion!😀

Auto Azimuth (Yaw) calibration of RTPT using P- controller

We first convert yaw from (-180 to +180) to (0 to 360) by doing:

Then we just find the error in yaw using a simple Proportional controller and then add the error back to yaw and then do the servo mappings with the new yaw:

This completes my RTPT project. Hope you have learned new things from it. 🙂


Источник: create.arduino.cc