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

There is a problem with the calculation of angles.

Hello

I am trying to get the angle of the sensor using the value of mpu6050.

I used a formula to obtain an angle using sensor values.  However, if I calculate this expression and print it out, the value will only be zero.

static void send_data() //send to app
{
    uint32_t err_code;
    double angle_x, angle_y, angle_z;

    accel_values_t accel_values; //mpu6050
    app_mpu_read_accel(&accel_values);          
    err_code = app_mpu_read_accel(&accel_values);
    APP_ERROR_CHECK(err_code);
    
    //Angle test
    const double RADIAN_TO_DEGREE = 180 / 3.14159; //Pi

    angle_y = atan(-accel_values.x / sqrt(pow(accel_values.y,2) + pow(accel_values.z, 2)));
    angle_y *= RADIAN_TO_DEGREE;

    angle_x = atan(accel_values.y / sqrt(pow(accel_values.x,2) + pow(accel_values.z, 2)));
    angle_x *= RADIAN_TO_DEGREE;

    printf("Angle_X : %lf\n", angle_x); 
    printf("Angle_Y : %lf\n", angle_y); 

    nrf_delay_ms(500);
}
    

Can I know the problem with this?

Thank you.

  • Hello,

    I would recommend you to debug out with breakpoints to figure out the actual reason.

    Without details, difficult to answer your question.

  • I don't know the reason yet, but I've divided the formula little by little and it's printed out properly.

    I think the angle value is being calculated slightly wrong, but I'll try this part more.

    Thank you.

        double accel_x = accel_values.x;
        double accel_y = accel_values.y;
        double accel_z = accel_values.z;
        
        double test1 = sqrt(pow(accel_x,2) + pow(accel_z, 2)); //OK
        double test2 = atan(accel_y / test1); //OK
        double test3 = test2 * RADIAN_TO_DEGREE; //OK
        printf("Angle_X : %lf\n", test3); 
    
        double test4 = sqrt(pow(accel_y,2) + pow(accel_z, 2)); //OK
        double test5 = atan(-accel_x / test4); //OK
        double test6 = test5 * RADIAN_TO_DEGREE; //OK
        printf("Angle_Y : %lf\n", test6); 

Related