[经验分享] 大赛分享二:RSL10蓝牙SoC之BHI160传感器数据采集

Justice_Gao   2021-6-13 17:36 楼主

设备:

RSL10-SENSE-GEVK:RSL10 传感器开发套件

//-----------------------------------------------------------------------------
// Copyright (c) 2018 Semiconductor Components Industries LLC
// (d/b/a "ON Semiconductor").  All rights reserved.
// This software and/or documentation is licensed by ON Semiconductor under
// limited terms and conditions.  The terms and conditions pertaining to the
// software and/or documentation are available at
// http://www.onsemi.com/site/pdf/ONSEMI_T&C.pdf ("ON Semiconductor Standard
// Terms and Conditions of Sale, Section 8 Software") and if applicable the
// software license agreement.  Do not use this software and/or documentation
// unless you have carefully read and you agree to the limited terms and
// conditions.  By using this software and/or documentation, you agree to the
// limited terms and conditions.
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <math.h>

#include <BDK.h>
#include <BSP_Components.h>
#include <BLE_Components.h>
#include <SoftwareTimer.h>

#include <bsec_integration.h>

/** Converts measured orientation data into degrees.
 *
 * GREEN LED will be turned on if board heading is within 10 degrees of
 * magnetic north (<=10 or >=350).
 */
void ao_vector_cb(bhy_data_generic_t *data, bhy_virtual_sensor_t sensor)
{
    float h = data->data_vector.x / 32768.0f * 360.0f;
    float p = data->data_vector.y / 32768.0f * 360.0f;
    float y = data->data_vector.z / 32768.0f * 360.0f;

    if (h > 350.0f || h < 10.0f)
    {
        LED_On(LED_GREEN);
    }
    else
    {
        LED_Off(LED_GREEN);
    }

    printf("Orientation: h=%.1f� p=%.1f�, y=%.1f� (%d)\r\n", h, p, y, data->data_vector.status);
}

/** Converts measured linear acceleration data based on sensors dynamic range
 * and prints it to debug terminal.
 *
 * Also turns on RED LED if total acceleration applied to the board is over 1g.
 */
void la_vector_cb(bhy_data_generic_t *data, bhy_virtual_sensor_t sensor)
{
    /* Linear Acceleration sensor values are scaled using dynamic range. */
    uint16_t dyn_range = BHI160_NDOF_GetAccelDynamicRange();

    float x = data->data_vector.x / 32768.0f * dyn_range;
    float y = data->data_vector.y / 32768.0f * dyn_range;
    float z = data->data_vector.z / 32768.0f * dyn_range;

    if (fabsf(x) + fabsf(y) + fabsf(z) >= 1.0f)
    {
        LED_On(LED_RED);
    }
    else
    {
        LED_Off(LED_RED);
    }
    printf("Linear Accel: x=%.2f g, y=%.2f g, z=%.2f g\r\n", x, y, z);
}

void gyro_vector_cb(bhy_data_generic_t *data, bhy_virtual_sensor_t sensor)
{
    uint16_t dyn_range = BHI160_NDOF_GetGyroDynamicRange();

    float x = data->data_vector.x / 32768.0f * dyn_range;
    float y = data->data_vector.y / 32768.0f * dyn_range;
    float z = data->data_vector.z / 32768.0f * dyn_range;

    if (fabsf(z) > 45.0f)
    {
        LED_On(LED_BLUE);
    }
    else
    {
        LED_Off(LED_BLUE);
    }
    printf("Rate of Rotation: x=%.2f �/s, y=%.2f �/s, z=%.2f �/s\r\n", x, y, z);
}
void CustomService_WriteInd(struct BLE_ICS_RxIndData *ind)
{
    /* Print at most ind->data_len characters from ind->data array. */
    printf("APP: Received message \'%.*s\'\r\n", ind->data_len, ind->data);

    /* Echo received message back to TX characteristic. */
    //BLE_ICS_Notify(ind->data, ind->data_len);
}



int main(void)
{
    int32_t retval;
    /* Initialize all needed BDK components. */
    BDK_Initialize();
    /* Initialize all LEDs to eliminate any currents from disabled DIO pads. */
    LED_Initialize(LED_RED);
    LED_Initialize(LED_GREEN);
    LED_Initialize(LED_BLUE);

    /* Increase I2C bus speed to 400kHz. */
    HAL_I2C_SetBusSpeed(HAL_I2C_BUS_SPEED_FAST);

    /* Initialize BHI160.
     * This may take a while depending on bus speed because RAM patch has to be
     * uploaded into BHI160 to enable support for BMM150 magnetometer.
     *
     * Set temporary timer to see how long it took to initialize and load the
     * RAM patch.
     */
    LED_On(LED_GREEN);
    {
        printf("Initializing BHI160.\r\n");

        struct SwTimer tim;
        struct SwTimer_Duration tim_dur;

        SwTimer_Initialize(&tim);
        SwTimer_Start(&tim);

        retval = BHI160_NDOF_Initialize();
        ASSERT_DEBUG(retval == BHY_SUCCESS);

        SwTimer_GetElapsed(&tim, &tim_dur);
        SwTimer_Remove(&tim);

        printf("BHI160 initialized in %lu seconds and %lu ns.\r\n",
                tim_dur.seconds, tim_dur.nanoseconds);
    }
    LED_Off(LED_GREEN);

    /* Enable desired virtual sensor outputs. */

    retval = BHI160_NDOF_EnableSensor(BHI160_NDOF_S_ORIENTATION, &ao_vector_cb, 5);
    ASSERT_DEBUG(retval == BHY_SUCCESS);

    retval = BHI160_NDOF_EnableSensor(BHI160_NDOF_S_LINEAR_ACCELERATION, &la_vector_cb, 50);
    ASSERT_DEBUG(retval == BHY_SUCCESS);

    retval = BHI160_NDOF_EnableSensor(BHI160_NDOF_S_RATE_OF_ROTATION, &gyro_vector_cb, 50);
    ASSERT_DEBUG(retval == BHY_SUCCESS);

    printf("APP: Entering main loop.\r\n");
    while (1)
    {
        BDK_Schedule();
        SYS_WAIT_FOR_INTERRUPT;
    }

    return 0;
}

烧录后可以通过J-LInk RTT viewer看打印日志

 

image.png

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复