Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    How to Convert BMI160 Data into roll, pitch and yaw

    How to Convert BMI160 Data into roll, pitch and yaw

    amar
    Established Member

    Hi everyone,

    Present i am working with BMX160, Till i am able to read Three sensor's raw data(Acc, Gyro, Mag) and convert them into Gravity, Radiuns/sec,uT respectively and To converting these readings iinto roll, pitch, yaw i am using below section of code(i got this from google)like below.

    #define   ACCELEROMETER_SENSITIVITY       16384.0
    #define  GYROSCOPE_SENSITIVITY                  16.4 //from bmx160 datasheet pg no:9,it is the value form range FS2000
    #define   M_PI                                                                3.14159265359
    #define   dt                                                                      0.01   // 10 ms sample rate!

    void ComplementaryFilter(int16_t *accData, int16_t *gyrData, float *pitch, float *roll)
    {
    float pitchAcc, rollAcc, yaw = 0.0;
    float *yaw_ptr;

    // Integrate the gyroscope data -> int(angularSpeed) = angle
    *pitch = ( (float)gyro.x / GYROSCOPE_SENSITIVITY ) * dt; // Angle around the X-axis
    *roll = ( (float)gyro.y / GYROSCOPE_SENSITIVITY ) * dt; // Angle around the Y-axis

    yaw = yaw + ((float)gyro.z) * dt; // Angle around the Z-axis
    yaw_ptr = &yaw;

    // Compensate for drift with accelerometer data if !bull**bleep**
    // Sensitivity = -2 to 2 G at 16Bit -> 2G = 32768 && 0.5G = 8192
    int forceMagnitudeApprox = abs(accel.x) + abs(accel.y) + abs(accel.z);

    if (forceMagnitudeApprox > 8192 && forceMagnitudeApprox < 32768)
    {
    // Turning around the X axis results in a vector on the Y-axis
    pitchAcc = atan2f((float)accel.y, (float)accel.z) * 180 / M_PI;
    *pitch = *pitch * 0.98 + pitchAcc * 0.02;

    // Turning around the Y axis results in a vector on the X-axis
    rollAcc = atan2f((float)accel.x, (float)accel.z) * 180 / M_PI;
    *roll = *roll * 0.98 + rollAcc * 0.02;
    }

    PRINTF("pitch\t%0.5f\troll\t%0.5f\tyaw\t%0.5f\t\r\n", *pitch,*roll, yaw);
    }

    Can you please review it once.

    If it is wrong to proceede so please correct me and suggeat me right  formula for converting it.

    Thank you inadvance,

    amarr 

     

    1 REPLY 1

    Yanchao
    Community Moderator
    Community Moderator

    Hi,

    We have formula as bellow:

    #define GRAVITY_EARTH   (9.80665f) /* Earth's gravity in m/s^2 */
    #define RAD             (57.2957805f)
    #define INV_RAD         (0.01745329f)
    
    /*!
     *  @brief This internal API is used to convert lsb to ms2 
     *
     *  @param[in] val: value
     *  @param[in] range: range value
     *  @param[in] bit_width
     *
     *  @return float value in ms2
     *
     */
    float lsb_to_ms2(int16_t val, float g_range, uint8_t bit_width)
    {
    	float half_scale = (float)(1 << bit_width) / 2.0f;
    
    	return GRAVITY_EARTH * val * g_range / half_scale;
    }
    float lsb_to_mg(int16_t val, float g_range, uint8_t bit_width)
    {
    	float half_scale = (float)(1 << bit_width) / 2.0f;
    
    	return 1000.0f * val * g_range / half_scale;
    }
    
    float lsb_to_dps(int16_t val, float g_range, uint8_t bit_width)
    {
    	float half_scale = (float)(1 << bit_width) / 2.0f;
    
    	return val * g_range / half_scale;
    }
    
    example: 
    accel_x = lsb_to_ms2(accel_data[i].x, 8, 16);
    gyro_x = lsb_to_dps(gyro_data[i].x, 2000, 16);
    
    #define MAG_FRAMES	20
    struct bmm150_mag_data mag_data[MAG_FRAMES];
    rslt = bmm150_aux_mag_data(aux_data[i].data, &bmm150dev);
    mag_data[i].x = bmm150dev.data.x;
    mag_data[i].y = bmm150dev.data.y;
    mag_data[i].z = bmm150dev.data.z;
    

     

    Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist