This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

I have a problem with the angle output of mpu6050 sensor.

Hello

I am working on mpu6050 sensor in sdk v14.
I succeeded in obtaining the acceleration value of this. I'm trying to get the angle of the sensor using this value.

I searched the Internet and used a formula to obtain angles using acceleration values.  However, as shown in the picture below, the value of the angle is only zero.

This is my main.c code.

#include <stdbool.h>
#include <stdint.h>
#include <string.h>

#include "nordic_common.h"
#include "nrf.h"
#include "app_error.h"
#include "app_timer.h"
#include "nrf_delay.h"

#include "app_mpu.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"


volatile bool start_accel_update_flag = false;




void timer_accel_update_handler(void * p_context)
{
    start_accel_update_flag = true;
}


/**@brief Function for initializing the nrf log module.
 */
static void log_init(void)
{
    ret_code_t err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_DEFAULT_BACKENDS_INIT();
}


void mpu_init(void)
{
    ret_code_t ret_code;
    // Initiate MPU driver
    ret_code = app_mpu_init();
    APP_ERROR_CHECK(ret_code); // Check for errors in return value
    
    // Setup and configure the MPU with intial values
    app_mpu_config_t p_mpu_config = MPU_DEFAULT_CONFIG(); // Load default values
    p_mpu_config.smplrt_div = 19;   // Change sampelrate. Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV). 19 gives a sample rate of 50Hz
    p_mpu_config.accel_config.afs_sel = AFS_2G; // Set accelerometer full scale range to 2G
    p_mpu_config.gyro_config.fs_sel = AFS_2G;
    ret_code = app_mpu_config(&p_mpu_config); // Configure the MPU with above values
    APP_ERROR_CHECK(ret_code); // Check for errors in return value 
}


/**@brief Function for application main entry.
 */
int main(void)
{
    uint32_t err_code;
    bool erase_bonds;

    // Initialize.
    log_init();
    NRF_LOG_INFO("\033[2J\033[;H"); // Clear screen

    mpu_init();
    
    // Start execution.
    NRF_LOG_INFO("MPU simple example.");


    accel_values_t accel_values;
    gyro_values_t gyro_values;
        
    // Enter main loop.
    for (;;)
    {
        if (NRF_LOG_PROCESS() == false)
        {
            app_mpu_read_accel(&accel_values);
            app_mpu_read_gyro(&gyro_values);
            
            err_code = app_mpu_read_accel(&accel_values);
            APP_ERROR_CHECK(err_code);
            err_code = app_mpu_read_accel(&gyro_values);
            APP_ERROR_CHECK(err_code);

            //float accel_x = accel_values.x * 4.0 / 32768.0; //same nano33ble lsm9ds1 sensor value
            //float accel_y = accel_values.y * 4.0 / 32768.0;
            //float accel_z = accel_values.z * 4.0 / 32768.0;

            float accel_x = accel_values.x;
            float accel_y = accel_values.y;
            float accel_z = accel_values.z;

            //Angle formula
            float RADIAN_TO_DEGREES = 180/3.14139;
            float val_x = atan(accel_y/sqrt(pow(accel_x,2) + pow(accel_z,2))) * RADIAN_TO_DEGREES; //X slope
            float val_y = atan(accel_x/sqrt(pow(accel_y,2) + pow(accel_z,2))) * RADIAN_TO_DEGREES; //Y slope


            //Accel value
            NRF_LOG_INFO("Accel_X : "NRF_LOG_FLOAT_MARKER"", NRF_LOG_FLOAT(accel_x)); //float print
            NRF_LOG_INFO("Accel_Y : "NRF_LOG_FLOAT_MARKER"", NRF_LOG_FLOAT(accel_y));
            NRF_LOG_INFO("Accel_Z : "NRF_LOG_FLOAT_MARKER"", NRF_LOG_FLOAT(accel_z));

            //Angle value
            NRF_LOG_INFO("X-Angle : "NRF_LOG_FLOAT_MARKER"", NRF_LOG_FLOAT(val_x)); //float print
            NRF_LOG_INFO("Y-Angle : "NRF_LOG_FLOAT_MARKER"", NRF_LOG_FLOAT(val_y));

            nrf_delay_ms(1000);

            start_accel_update_flag = false;
        }
   }
}

I tried debugging, but I couldn't find the cause of the problem.
Can I know the problem with this?

Thank you.

Parents
  • Hello,

    I tried debugging, but I couldn't find the cause of the problem.

    Could you elaborate how you went about debugging this? What have you done to debug, and what was the results?

    Can I know the problem with this?

    Could you try to write out the raw value returned by your app_mpi_read_accel, to see if the function call does in fact return any values?

    I would also like to point out that you are calling app_mpu_read_accel three times in the supplied code, first without an app error check, then with.
    Why are you not always checking the returned error codes the first two times, and is it intentional that you are calling app_mpu_read_accel(&gyro_values) in line 87? - Should it not be app_mpu_read_gyro?

    Looking forward to resolving this issue together!

    Best regards,
    Karl

Reply
  • Hello,

    I tried debugging, but I couldn't find the cause of the problem.

    Could you elaborate how you went about debugging this? What have you done to debug, and what was the results?

    Can I know the problem with this?

    Could you try to write out the raw value returned by your app_mpi_read_accel, to see if the function call does in fact return any values?

    I would also like to point out that you are calling app_mpu_read_accel three times in the supplied code, first without an app error check, then with.
    Why are you not always checking the returned error codes the first two times, and is it intentional that you are calling app_mpu_read_accel(&gyro_values) in line 87? - Should it not be app_mpu_read_gyro?

    Looking forward to resolving this issue together!

    Best regards,
    Karl

Children
No Data
Related