Bosch Sensortec Community

    Problem with running "orientation.ino" sample code in BHI160 Sensor Hub Arduino library

    Highlighted
    New Poster

    Problem with running "orientation.ino" sample code in BHI160 Sensor Hub Arduino library

    Hi,

    My team is in the process of building a wearable product, and are currently testing using the BHI160 Sensor Hub with the nRF52832 Adafruit Feather. For some contextual information between the sensor hub and the Adafruit nRF boards, respectively, both VDD and VDDIO are tied to 3.3V, GND is tied to GND, SDA is tied to SDA, SCL is tied to SCL, and INTC/IO7 is tied to pin 7. In addition, the appropriate 2.2 KOhm resistors are being used as pullups on the I2C bus.

    We are having trouble running the orientation.ino sample code provided in the library. The application code being tested can be found below. You will notice that there is one change made to the original "orientation.ino" example found in the library. The firmware hex file was changed to "Bosch_PCB_7183_di01_BMI160-7183_di01.2.1.10836_170103.h". This was because we were getting the following error message when using the original one: "Error code: (-22). RAM-Patch Mismatch". 

    """""""""""""""""""""""""""""""

    #include <Wire.h>

    #include "bhy.h"
    #include "Bosch_PCB_7183_di01_BMI160-7183_di01.2.1.10836_170103.h"

    #define BHY_INT_PIN 7

    BHYSensor bhi160;

    volatile bool intrToggled = false;
    bool newOrientationData = false;
    float heading, roll, pitch;
    uint8_t status;

    bool checkSensorStatus(void);

    void bhyInterruptHandler(void)
    {
    intrToggled = true;
    }
    void waitForBhyInterrupt(void)
    {
    while (!intrToggled)
    ;
    intrToggled = false;
    }
    void orientationHandler(bhyVector data, bhyVirtualSensor type)
    {
    heading = data.x;
    roll = data.z;
    pitch = data.y;
    status = data.status;
    newOrientationData = true;
    }

    void setup()
    {
    Serial.begin(115200);
    Wire.begin();

    if (Serial)
    {
    Serial.println("Serial working");
    }

    attachInterrupt(BHY_INT_PIN, bhyInterruptHandler, RISING);

    bhi160.begin(BHY_I2C_ADDR);

    /* Check to see if something went wrong. */
    if (!checkSensorStatus())
    return;

    Serial.println("Sensor found over I2C! Product ID: 0x" + String(bhi160.productId, HEX));

    Serial.println("Uploading Firmware.");
    bhi160.loadFirmware(bhy1_fw);

    if (!checkSensorStatus())
    return;

    intrToggled = false; /* Clear interrupt status received during firmware upload */
    waitForBhyInterrupt(); /* Wait for meta events from boot up */
    Serial.println("Firmware booted");

    /* Install a metaevent callback handler and a timestamp callback handler here if required before the first run */
    bhi160.run(); /* The first run processes all boot events */

    /* Install a vector callback function to process the data received from the wake up Orientation sensor */
    if (bhi160.installSensorCallback(BHY_VS_ORIENTATION, true, orientationHandler))
    {
    checkSensorStatus();

    return;
    }
    else
    Serial.println("Orientation callback installed");

    /* Enable the Orientation virtual sensor that gives you the heading, roll, pitch
    based of data from the accelerometer, gyroscope and magnetometer.
    The sensor is set into wake up mode so as to interrupt the host when a new sample is available
    Additionally, the FIFO buffer of the sensor is flushed for all previous data
    The maximum report latency of the sensor sample, the sensitivity and the dynamic range
    are set to 0
    */
    if (bhi160.configVirtualSensor(BHY_VS_ORIENTATION, true, BHY_FLUSH_ALL, 200, 0, 0, 0))
    {
    Serial.println("Failed to enable virtual sensor (" + bhi160.getSensorName(
    BHY_VS_ORIENTATION) + "). Loaded firmware may not support requested sensor id.");
    }
    else
    Serial.println(bhi160.getSensorName(BHY_VS_ORIENTATION) + " virtual sensor enabled");

    }

    void loop()
    {
    if (intrToggled)
    {
    intrToggled = false;
    bhi160.run();
    checkSensorStatus();
    if (newOrientationData)
    {
    /* Can also be viewed using the plotter */
    Serial.println(String(heading) + "," + String(pitch) + "," + String(roll) + "," + String(status));
    newOrientationData = false;
    }
    }
    }

    bool checkSensorStatus(void)
    {
    if (bhi160.status == BHY_OK)
    return true;

    if (bhi160.status < BHY_OK) /* All error codes are negative */
    {
    Serial.println("Error code: (" + String(bhi160.status) + "). " + bhi160.getErrorString(bhi160.status));

    return false; /* Something has gone wrong */
    }
    else /* All warning codes are positive */
    {
    Serial.println("Warning code: (" + String(bhi160.status) + ").");

    return true;
    }

    return true;
    }

    """""""""""""""""""""""""""""""

    The sketch compiles and runs but does not return any sensor data. We get the following Serial monitor output (notice the warning code 1):

    Serial working
    Sensor found over I2C! Product ID: 0x83
    Uploading Firmware.
    Firmware booted
    Orientation callback installed
    Orientation (Non Wake-up) virtual sensor enabled
    Warning code: (1)

    One observation that we have made is that the warning code (1) is because of line 2088 in bhy.cpp, where the acknowledgement bit is being read as 0x80 due to an invalid parameter number (227) being written. According to the documentation, only numbers between 1-127 (for Parameter Page 3) are valid.

    Another observation we made was that the "orientationHandler" ISR is never being called, and therefore, newOrientationData is never being set to true. We are not too sure of what is happening in the following vector callback function: "bhi160.installSensorCallback(BHY_VS_ORIENTATION, true, orientationHandler)". We believe that there is something weird going on in its function definition located in the bhy.cpp file.

    We are not sure what is going on here, and any help would be greatly appreciated.

    1 REPLY 1
    Highlighted
    Community Moderator
    Community Moderator

    Re: Problem with running "orientation.ino" sample code in BHI160 Sensor Hub Arduino librar

    Hi Arjun96G,

    The orientation sensor is defined by Google as requiring Accelerometer, Magnetometer and (optionally) Gyroscope. https://source.android.com/devices/sensors/sensor-types#orientation_deprecated

    The RAM patch your are using does not include Magnetometer support, as hinted in the lack of "BMM150" in the filename, and therefore does not support Orientation.

    I believe that you are looking for Game Rotation Vector : https://source.android.com/devices/sensors/sensor-types#game_rotation_vector

    o_o
    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