Skip to content

Interacting With The MPU9250

Pymata_rh provides the following API methods to allow you to control and monitor the MPU9250:

  • mpu_9250_initialize
  • mpu_9250_read_data
  • mpu_9250_read_saved_data
  • mpu_9250_calibrate

Let's look at each method in detail.

mpu_9250_initialize

 def mpu_9250_initialize(self, address_ak=12, address_mpu=104, g_fs=1, a_fs=0, 
                         m_fs=1, mode=2, a_bias=(0, 0, 0), g_bias=(0, 0, 0), 
                         m_bias=(0, 0, 0), mag_scale=(1, 1, 1), callback=None)

    This method instantiates an mpu_9250 object. 
    It also creates the thread to manage the mpu_9250 data retrieval.

    :param address_ak: AK8963 I2C address (default:AK8963_ADDRESS[0x0C]).

    :param address_mpu: MPU-9250 I2C address (default:MPU9050_ADDRESS_68[0x68]).

    :param g_fs: Gyroscope full scale select (default:GFS_2000[2000dps]).

    :param a_fs: Accelerometer full scale select (default:AFS_16G[16g]).

    :param m_fs: Magnetometer scale select (default:AK8963_BIT_16[16bit])

    :param mode: Magnetometer mode select (default:AK8963_MODE_C8HZ)

    :param a_bias: Accelerometer Bias

    :param g_bias: Gyroscope Bias

    :param m_bias: Magnetometer Hard Iron Distortion

    :param mag_scale: Magnetometer Soft Iron Distortion

    :param callback: Callback method that will receive mpu data frames

This method must be called before calling the mpu_9250_read_data method. Typically, you can accept all of the default values. If you wish to have your application notified of all MPU9250 reads, you must specify a callback function to handle the callback data.

Using a callback is optional but highly recommended. A callback function ensures that your application receives the latest MPU9250 data automatically without any polling interaction. If you do not specify a callback method, you may still poll for the latest reported values using the mpu_9250_read_saved_data method.

For a discussion on callbacks and polling, please refer to this section of the document.

Example:

read_mpu.py

mpu_9250_read_data

 def mpu_9250_read_data(self, mode=self.mpu_constants.MPU9250_READ_CONTINUOUS_ON, 
                        continuous_delay=0.3)

    Read and report mpu_9250 data for accelerometer, gyroscope, 
                    magnetometer, and device temperature.

    :param mode: MPU9250_READ_CONTINUOUS_ON  
                 Data is read continuously.
                 Or
                 MPU9250_READ_CONTINUOUS_OFF
                 Data read is halted.

    :param continuous_delay: Minimum delay between reads

    :return: For MPU9250_READ_CONTINUOUS_ON. 
             If a callback was specified in mpu_9250_initialize(), 
             then data returned is returned via callback.

         Callback data is a list with format:
         index[0] = pin type - for mpu9250 the value is 16
         index[1] = mpu address
         index[2] = accelerometer x axis
         index[3] = accelerometer y axis
         index[4] = accelerometer z axis
         index[5] = gyroscope x axis
         index[6] = gyroscope y axis
         index[7] = gyroscope z axis
         index[8] = magnetometer x axis
         index[9] = magnetometer y axis
         index[10] = magnetometer z axis
         index[11] = temperature
         index[12] = timestamp

         If no callback was specified, then data is stored and can be
         retrieved using mpu_9250_read_saved_data().

This method either starts a continuous read of data from the MPU9250 device if the mode is specified self.mpu_constants.MPU9250_READ_CONTINUOUS_ON or halts a continuous read if the mode is specified as self.mpu_constants.MPU9250_READ_CONTINUOUS_OFF

The units of data for each item returned is as follows:

Sensor Unit of Measurement Axis Reported
Accelerometer g (1g = 9.80665 m/s²) x, y and z axes
Gyroscope degrees per second (°/s) x, y and z axes
Magnetometer microtesla (μT) x, y and z axes
Temperature celsius degrees (°C) Not Appplicaable

The data for all of the sensors of the MPU9250 are queried at the same time so that you may track things as accurately as possible.

Example:

read_mpu.py

mpu_9250_read_saved_data

 def mpu_9250_read_saved_data(self)

    Retrieve and return the last data set read from the mpu_9250

    :return: The last read data frame in list form. An empty list,
             or a value of None may be returned as well.
             The format for the data is the same as that of
             the callback data.

You may poll the MPU9250 for the latest data by calling this method.

You may call mpu_9250_read_saved_data, whether you specified a callback or not.

Example:

read_mpu.py

mpu_9250_calibrate

 def mpu_9250_calibrate(self, log=True)

    This method will calibrate the device. This is a lengthy 
    process and may take about a minute to complete.

    This method will: 1. Test if the mpu_9250 was initialized and 
                         if not, it will initialize with the default values. 
                      2. Print the current device settings.
                      3. Run the calibrations for both the calibrate_ak8963 and MPU6050. 
                      4. Configure the device with the new settings. 
                      5. Print the new device settings.
                      6. If the log parameter is True, the calibrations will be saved to 
                         a log file.

    :param: logging - If set to True calibration data is logged to a file named mpu_calibration.log

    :return: If an error is encountered a RunTimeError exception is raised.

You need not initialize the MPU9250 after calling mpu_9250_calibrate.

Here is a sample of console output after a calibration completes. If log is enabled, each run is appended to the file so that you may have a running history of calibrations.



Configuration data being logged to mpu_calibration.log
Initializing mpu9250
Settings Before Calibration:

MPU Address: 0x68
AK Address: 0xc
Accel Resolution: 6.103515625e-05
Gyro Resolution: 0.0152587890625
Mag Resolution: 0.14993894993894993
Accel Bias: (0, 0, 0)
Gyro Bias: (0, 0, 0)
Mag Bias: (0, 0, 0)
Mag Calibration: (1.16796875, 1.171875, 1.125)
Mag Scale: (1, 1, 1)

Calibrating ...

Calibrating 0x68 - AK8963
Iterations to go: 1
ak8963 complete.
Calibrating 0x68 - MPU6500
Iterations to go: 1
mpu_6500 complete.
Configuring with calibrated values...


Settings After Calibration:

MPU Address: 0x68
AK Address: 0xc
Accel Resolution: 6.103515625e-05
Gyro Resolution: 0.0152587890625
Mag Resolution: 0.14993894993894993
Accel Bias: (-0.020819769965277776, 0.023098415798611112, 0.00850423177083326)
Gyro Bias: (-2.617306179470486, 2.636379665798611, -0.698301527235243)
Mag Bias: (45.00687003968254, 42.78531364468865, 27.326373626373623)
Mag Calibration: (1.16796875, 1.171875, 1.125)
Mag Scale: (0.9848484848484849, 1.0317460317460319, 0.9848484848484849)

Example:

calibrate_mpu9250.py



Copyright (C) 2020 Alan Yorinks. All Rights Reserved.