Skip to content

Instantly share code, notes, and snippets.

@dahmadjid
Created September 22, 2022 15:31
Show Gist options
  • Select an option

  • Save dahmadjid/c86181c5153f0d192c3de0f00a0d4bf1 to your computer and use it in GitHub Desktop.

Select an option

Save dahmadjid/c86181c5153f0d192c3de0f00a0d4bf1 to your computer and use it in GitHub Desktop.

Revisions

  1. dahmadjid created this gist Sep 22, 2022.
    202 changes: 202 additions & 0 deletions mpu_useful_stuff.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,202 @@


    /**
    * @brief MPU6050 constructor.
    */
    void mpu6050_init();

    /**
    * @brief: Get digital low-pass filter configuration. The DLPF_CFG parameter
    * sets the digital low pass filter configuration. It also determines the
    * internal sampling rate used by the device as shown in the table below.
    * Note: The accelerometer output rate is 1kHz. This means that for a Sample
    * Rate greater than 1kHz, the same accelerometer sample may be output to the
    * FIFO, DMP, and sensor registers more than once.
    *
    * | ACCELEROMETER | GYROSCOPE
    * DLPF_CFG | Bandwidth | Delay | Bandwidth | Delay | Sample Rate
    * ---------+-----------+--------+-----------+--------+-------------
    * 0 | 260Hz | 0ms | 256Hz | 0.98ms | 8kHz
    * 1 | 184Hz | 2.0ms | 188Hz | 1.9ms | 1kHz
    * 2 | 94Hz | 3.0ms | 98Hz | 2.8ms | 1kHz
    * 3 | 44Hz | 4.9ms | 42Hz | 4.8ms | 1kHz
    * 4 | 21Hz | 8.5ms | 20Hz | 8.3ms | 1kHz
    * 5 | 10Hz | 13.8ms | 10Hz | 13.4ms | 1kHz
    * 6 | 5Hz | 19.0ms | 5Hz | 18.6ms | 1kHz
    * 7 | Reserved | Reserved | Reserved
    *
    * @return DLFP configuration.
    */
    uint8_t mpu6050_get_dlpf_mode();

    /**
    * @brief Set digital low-pass filter configuration.
    *
    * @param mode New DLFP configuration setting.
    */
    void mpu6050_set_dlpf_mode(uint8_t mode);

    /**
    * @brief Get full-scale gyroscope range. The FS_SEL parameter allows setting
    * the full-scale range of the gyro sensors, as described below:
    *
    * 0 = +/- 250 degrees/sec
    * 1 = +/- 500 degrees/sec
    * 2 = +/- 1000 degrees/sec
    * 3 = +/- 2000 degrees/sec
    *
    * @return Current full-scale gyroscope range setting.
    */
    uint8_t mpu6050_get_full_scale_gyro_range();

    /**
    * @brief Set full-scale gyroscope range.
    *
    * @param range New full-scale gyroscope range value.
    */
    void mpu6050_set_full_scale_gyro_range(uint8_t range);

    /**
    * @brief Get self-test factory trim value for accelerometer X axis.
    *
    * @return Factory trim value.
    */




    /**
    * @brief Get full-scale accelerometer range.
    * The FS_SEL parameter allows setting the full-scale range of the accelerometer
    * sensors, as described below:
    *
    * 0 = +/- 2g
    * 1 = +/- 4g
    * 2 = +/- 8g
    * 3 = +/- 16g
    *
    * @return Current full-scale accelerometer range setting.
    */
    uint8_t mpu6050_get_full_scale_accel_range();

    /**
    * @brief Set full-scale accelerometer range.
    *
    * @param range New full-scale accelerometer range setting.
    */
    void mpu6050_set_full_scale_accel_range(uint8_t range);



    /**
    * @brief Get gyroscope output rate divider. The sensor register output,
    * FIFO output, DMP sampling, Motion Detection, Zero Motion Detection,
    * and Free Fall Detection are all based on the Sample Rate.
    * The Sample Rate is generated by dividing the gyroscope output rate by
    * SMPLRT_DIV:
    *
    * Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV)
    *
    * Where Gyroscope Output Rate = 8kHz when the DLPF is disabled (DLPF_CFG = 0 or
    * 7), and 1kHz when the DLPF is enabled.
    *
    * Note: The accelerometer output rate is 1kHz. This means that for a Sample
    * Rate greater than 1kHz, the same accelerometer sample may be output to the
    * FIFO, DMP, and sensor registers more than once.
    *
    * @return Current sample rate.
    */
    uint8_t mpu6050_get_rate();

    /**
    * @brief Set gyroscope output rate divider.
    *
    * @param rate New sample rate divider.
    */
    void mpu6050_set_rate(uint8_t rate);



    /**
    * @brief Calculates acceleration resolution.
    *
    * @param accel_scale Acceleration scale. The scale range values are described
    * below:
    *
    * 0 = +/- 2g
    * 1 = +/- 4g
    * 2 = +/- 8g
    * 3 = +/- 16g
    *
    * @return Resolution of the acceleration.
    */
    float mpu6050_get_accel_res(uint8_t accel_scale);

    /**
    * @brief Calculates rotation resolution.
    *
    * @param accel_scale Rotation scale. The scale range values are described
    * below:
    *
    * 0 = +/- 250 degrees/sec
    * 1 = +/- 500 degrees/sec
    * 2 = +/- 1000 degrees/sec
    * 3 = +/- 2000 degrees/sec
    *
    * @return Resolution of the acceleration.
    */
    float mpu6050_get_gyro_res(uint8_t gyro_scale);

    /**
    * @brief Function which accumulates gyro and accelerometer data after device
    * initialization. It calculates the average of the at-rest readings and then
    * loads the resulting offsets into accelerometer and gyro bias registers.
    *
    * @param accel_bias_res Acceleration bias resolution.
    * @param gyro_bias_res Rotation bias resolution.
    */

    /**
    * @brief Get raw 6-axis motion sensor readings (accel/gyro).
    * Retrieves all currently available motion sensor values.
    *
    * @param data_accel pointer to acceleration struct.
    * @param data_gyro pointer to rotation struct.
    */
    void mpu6050_get_motion
    (
    mpu6050_acceleration_t* data_accel,
    mpu6050_rotation_t* data_gyro
    );




















    // OUMBA3D edhorbou 3liha talla


    /**
    * @brief Function which accumulates gyro and accelerometer data after device
    * initialization. It calculates the average of the at-rest readings and then
    * loads the resulting offsets into accelerometer and gyro bias registers.
    *
    * @param accel_bias_res Acceleration bias resolution.
    * @param gyro_bias_res Rotation bias resolution.
    */
    void mpu6050_calibrate(float *accel_bias_res, float *gyro_bias_res);