Accelerometer convert to g
응답
17. 7. 4 오전 9:04

I copied the accelerometer example from the sensor guide to the XDK workbench and got the following output (XDK lies still on the table):

INFO | XDK DEVICE 1: A - M/S2    : 24.000000 m/s2    18.000000 m/s2     1018.000000 m/s2

INFO | XDK DEVICE 1: D - M/S2   : 92.000000 m/s2    63.000000 m/s2     4178.000000 m/s2

A = data is calculated in earth acceleration g

D = converted sensor data in bytes

 

How do I convert the output data to g?

Do I have to divide the A values through 1000 to get the g values? E.g:

A: 0.024 g   0.018 g      1.018 g

How do I convert the D values to g? Do I have to divide through 4000?

The provided unit m/s2 in the sensor guide example is misleading, I feel.

0 (0 투표)
RE: Accelerometer convert to g
응답
17. 7. 4 오후 2:56 as a reply to Stefan Maier.

Hello Stefan,

Unfortunately, the sensor guide has a mistake in the example code 4. The physical unit in the string is wrong. The data is read from the acceleration sensor of the XDK by using the function Accelerometer_readXyzGValue(), which calculates the physical value from the digital sensor value, and divides it by the earth acceleration (~9,81) and represent it in milli g.

To receive the correct physical value in g, you have to use the equation you already mentioned in your application code to calculate the g value of every axis. You simply divide every axis individually by 1000. You do not need to calculate the sensor data, that is represented in bytes, to g. You only need to use the function Accelerometer_readXyzGValue() instead of Accelerometer_readXyzLsbValue() to read the data calculated in g.

Additionally, I apologize for the inconvenience with the incorrect unit and thank you for the hint about the wrong unit in the example code 4 of the sensor guide. We will correct this in the next release of the sensor guide.

Please let me know, if this was helpful and do not hesitate to ask if you have further questions.

Kind regards,
Franjo

0 (0 투표)
RE: Accelerometer convert to g
응답
17. 11. 23 오전 8:48 as a reply to Franjo Stjepandic.

Hello,

I copied the code example in the Sensor Guide for the Accelerometer and after flashing to the XDK, no reading is printed on the console. I was wondering if you can help me on this. I used the XdkApplicationTemplate to insert the code


/* system header files */
#include <stdio.h>
/* additional interface header files */
#include "FreeRTOS.h"
#include "timers.h"

/* own header files */
#include "XdkApplicationTemplate.h"
#include "BCDS_CmdProcessor.h"
#include "BCDS_Assert.h"

/* constant definitions ***************************************************** */

/* local variables ********************************************************** */

/* global variables ********************************************************* */

/* inline functions ********************************************************* */

/* local functions ********************************************************** */

/* global functions ********************************************************* */

/**
 * @brief This is a template function where the user can write his custom application.
 *
 */

/* **************** User Programming starts here: **************** */

/* sensor handling header file */
#include "XdkSensorHandle.h"

/* initialize Accelerometer */
static void initAccelerometer(void)
{
    Retcode_T accelerometerReturnValue = RETCODE_FAILURE;

    accelerometerReturnValue = Accelerometer_init(xdkAccelerometers_BMA280_Handle);

    if (RETCODE_OK != accelerometerReturnValue)
    {
        printf("Accelerometer initialization Failed\n\r");
    }

}

/* set Accelerometer Bandwith */
static void returnBandwith (void)
{
    Retcode_T returnBandwithValue = RETCODE_FAILURE;

    returnBandwithValue = Accelerometer_setBandwidth(xdkAccelerometers_BMA280_Handle, ACCELEROMETER_BMA280_BANDWIDTH_125HZ);

    if (RETCODE_OK != returnBandwithValue)
    {
        printf("Accelerometer Bandwith not set");
    }

}

/* set Accelerometer Range */
static void returnRange (void)
{
    Retcode_T returnRangeValue = RETCODE_FAILURE;

    returnRangeValue = Accelerometer_setRange(xdkAccelerometers_BMA280_Handle, ACCELEROMETER_BMA280_RANGE_2G);

    if (RETCODE_OK != returnRangeValue)
    {
        printf("Accelerometer Range not set");
    }
}

/* read and print BMA280 accelerometer data */
void readAccelerometer (void)
{
    Retcode_T returnValue = RETCODE_FAILURE;

    returnValue = Accelerometer_init(xdkAccelerometers_BMA280_Handle);

    Accelerometer_XyzData_T bma280 = {INT32_C(0), INT32_C(0), INT32_C(0)};
    memset(&bma280, 0, sizeof(Accelerometer_XyzData_T));
    returnValue = Accelerometer_readXyzGValue(xdkAccelerometers_BMA280_Handle,&bma280);

    if (RETCODE_OK == returnValue)
    {
        printf("BMA280 Acceleration Data \n\r: %f \n\r %f \n\r: %f \n\r",
                (float) bma280.xAxisData,(float) bma280.yAxisData,(float) bma280.zAxisData);
    }
}

void appInitSystem(void * CmdProcessorHandle, uint32_t param2)
{
    if (CmdProcessorHandle == NULL)
    {
        printf("Command processor handle is null \n\r");
        assert(false);
    }
    BCDS_UNUSED(param2);
      
}

0 (0 투표)
RE: Accelerometer convert to g
응답
17. 11. 23 오후 6:37 as a reply to Ahmad Khairi Hamzah.

Hello Ahmad,

First, I'd like to welcome you to the XDK community.

Your code is not working because you never called the functions you declared in appInitSystem(). appInitSystem() is the entry function of the application. It is called when the XDK boots the application, and as such, every code you want to run has to be called via appInitSystem().

To solve this your appInitSystem() could be modified as follows:

void appInitSystem(void * CmdProcessorHandle, uint32_t param2) {

if (CmdProcessorHandle == NULL) {

printf("Command processor handle is null \n\r");

assert(false); }

BCDS_UNUSED(param2);

vTaskDelay(5000);

initAccelerometer();

returnBandwith();

returnRange();

readAccelerometer(); }

Please note, that I inserted a small delay of ~5 seconds, so that you are able to see the output in the XDK-Workbench's console. Without this delay, the printf() could be get lost because the application may already be running before the XDK has established a USB connection to your Laptop/computer.

Additionally, please remove the line returnValue = Accelerometer_init(xdkAccelerometers_BMA280_Handle); from the function readAccelerometer(). You already call the initialization of the accelerometer in the function initAccelerometer.

Please let me know if that was helpful and do not hesitate to ask if you have further questions.

Kind regards,
Franjo

0 (0 투표)
RE: Accelerometer convert to g
응답
17. 11. 26 오전 8:58 as a reply to Franjo Stjepandic.

Hello Franjo,

Thank you so much for your help. Now the code works! I adapted the use of Timer from the "Hello World!" program to print the accelerometer reading every 3 seconds as below. It works but is this the right way to do it?

/* system header files */
#include <stdio.h>
/* additional interface header files */
#include "FreeRTOS.h"
#include "timers.h"

/* own header files */
#include "XdkApplicationTemplate.h"
#include "BCDS_CmdProcessor.h"
#include "BCDS_Assert.h"

/* constant definitions ***************************************************** */

/* local variables ********************************************************** */

/* global variables ********************************************************* */

/* inline functions ********************************************************* */

/* local functions ********************************************************** */

/* global functions ********************************************************* */

/**
 * @brief This is a template function where the user can write his custom application.
 *
 */

/* **************** User Programming starts here: **************** */

/* sensor handling header file */
#include "XdkSensorHandle.h"

/* Macro used to define blocktime of a timer */
#define TIMERBLOCKTIME UINT32_C(0xffff)
#define TIMER_AUTORELOAD_ON pdTRUE
#define SECONDS(x) ((portTickType) (x * 1000) / portTICK_RATE_MS)

/* initialize Accelerometer */
static void initAccelerometer(void)
{
    Retcode_T accelerometerReturnValue = RETCODE_FAILURE;

    accelerometerReturnValue = Accelerometer_init(xdkAccelerometers_BMA280_Handle);

    if (RETCODE_OK != accelerometerReturnValue)
    {
        printf("Accelerometer initialization Failed\n\r");
    }

}

/* set Accelerometer Bandwith */
static void returnBandwith (void)
{
    Retcode_T returnBandwithValue = RETCODE_FAILURE;

    returnBandwithValue = Accelerometer_setBandwidth(xdkAccelerometers_BMA280_Handle, ACCELEROMETER_BMA280_BANDWIDTH_125HZ);

    if (RETCODE_OK != returnBandwithValue)
    {
        printf("Accelerometer Bandwith not set");
    }

}

/* set Accelerometer Range */
static void returnRange (void)
{
    Retcode_T returnRangeValue = RETCODE_FAILURE;

    returnRangeValue = Accelerometer_setRange(xdkAccelerometers_BMA280_Handle, ACCELEROMETER_BMA280_RANGE_2G);

    if (RETCODE_OK != returnRangeValue)
    {
        printf("Accelerometer Range not set");
    }
}

/* read and print BMA280 accelerometer data */
void readAccelerometer (void)
{
    Retcode_T returnValue = RETCODE_FAILURE;

    Accelerometer_XyzData_T bma280 = {INT32_C(0), INT32_C(0), INT32_C(0)};
    memset(&bma280, 0, sizeof(Accelerometer_XyzData_T));
    returnValue = Accelerometer_readXyzGValue(xdkAccelerometers_BMA280_Handle,&bma280);

    if (RETCODE_OK == returnValue)
    {
        printf("BMA280 Acceleration Data \n\r: %f \n\r %f \n\r: %f \n\r",
                (float) bma280.xAxisData,(float) bma280.yAxisData,(float) bma280.zAxisData);
    }
}

void timerInit(void)
{
    xTimerHandle applicationTimer;
    /* create timer task to print Accelerometer reading in the output console every three
    seconds */
    /* Validated for portMAX_DELAY to assist the task to wait Infinitely
    (without timing out) and ticks cannot be 0 in FreeRTOS timer. So ticks is
    assigned to 1 */

    applicationTimer = xTimerCreate(
            (char * const) "Test Application to print Accelerometer reading",
            SECONDS(3),
            TIMER_AUTORELOAD_ON,
            NULL,
            readAccelerometer);
    /*start the timer*/
    xTimerStart(applicationTimer, TIMERBLOCKTIME);
}

void appInitSystem(void * CmdProcessorHandle, uint32_t param2)
{
    if (CmdProcessorHandle == NULL)
    {
        printf("Command processor handle is null \n\r");
        assert(false);
    }
    BCDS_UNUSED(param2);

    /*Call up the functions that has been declared
    Every code you want to run has to be called up here */
    vTaskDelay(5000);        //delays output in XDK console for 5s
    initAccelerometer();
    returnBandwith();
    returnRange();
    readAccelerometer();
    timerInit();
}
/**@} */
/** ************************************************************************* */

 

0 (0 투표)
RE: Accelerometer convert to g
응답
17. 11. 27 오후 5:02 as a reply to Ahmad Khairi Hamzah.

Hello Ahmad,

nearly everything is correct, except for one issue with your usage / declaration of readAccelerometer().

Because you used it in a timer, it has to have the following signature:

void readAccelerometer (xTimerHandle timer)

instead of the input being just void.

If you make this change, you should either remove the call of readAccelerometer() inside appInitSystem(), or change the call to

readAccelerometer(null);

But since you use this function in a timer, deleting this line would be completely fine.

Please tell me if this was helpful, and do not hesitate to ask further questions.

Kind regards,
Franjo

0 (0 투표)
RE: Accelerometer convert to g
응답
19. 1. 20 오후 3:20 as a reply to Franjo Stjepandic.

May I know that this code should be placed in AppController.c, AppController.h, XdkAppInfo.h or Main.c for 3.5.0 version?

Thank you and Best Regards!

0 (0 투표)
RE: Accelerometer convert to g
응답
19. 1. 21 오후 5:02 as a reply to l Kent.
Dear I Kent,

The code should be placed in the implementation file AppController.c . Additionally, I recommend taking a look at your other thread Running code of Accelerometer for further instructions.

Please let me know if this was helpful and feel free to ask if you have further questions.

Kind regards,
Franjo
0 (0 투표)
RE: Accelerometer convert to g
응답
19. 7. 30 오후 12:09 as a reply to Franjo Stjepandic.

Hey everyone. I wrote the following code to get the accelerometer data. I used a timer function there. Caan someone please tell me how to write it in a way so the xdk only gives the accelerometer data for 5 seconds or any other fixed time.

#include "XdkAppInfo.h"

#undef BCDS_MODULE_ID  // [i] Module ID define before including Basics package

#define BCDS_MODULE_ID XDK_APP_MODULE_ID_APP_CONTROLLER

#include "AppController.h"

#include <stdio.h>

#include "BCDS_CmdProcessor.h"

#include "FreeRTOS.h"

#include "XdkSensorHandle.h"

#include "timers.h"

 

/* --------------------------------------------------------------------------- |

 * HANDLES ******************************************************************* |

 * -------------------------------------------------------------------------- */

 

static CmdProcessor_T * AppCmdProcessor;

xTimerHandle accelerometerHandle = NULL;

 

/* --------------------------------------------------------------------------- |

 * EXECUTING FUNCTIONS ******************************************************* |

 * -------------------------------------------------------------------------- */

 

 static void readAccelerometer(xTimerHandle xTimer)

 {

     (void) xTimer;

 

     Retcode_T returnValue = RETCODE_FAILURE;

 

     /* read and print BMA280 accelerometer data */

 

     Accelerometer_XyzData_T bma280 = {INT32_C(0), INT32_C(0), INT32_C(0)};

     memset(&bma280, 0, sizeof(CalibratedAccel_XyzMps2Data_T));

 

     returnValue = Accelerometer_readXyzGValue(xdkAccelerometers_BMA280_Handle,&bma280);

 

     if (RETCODE_OK == returnValue) {

         printf("BMA280 Acceleration Data - M/S2 %f m/s2 %f m/s2 %f m/s2\n\r",

             (float) bma280.xAxisData, (float) bma280.yAxisData, (float) bma280.zAxisData);

     }

 }

 

static void initAccelerometer(void)

{

    Retcode_T returnValue = RETCODE_FAILURE;

    Retcode_T returnBandwidthValue = RETCODE_FAILURE;

    Retcode_T returnRangeValue = RETCODE_FAILURE;

 

    /* initialize accelerometer */

 

    returnValue = Accelerometer_init(xdkAccelerometers_BMA280_Handle);

 

    if ( RETCODE_OK != returnValue) {

        printf("BMA280 Accelerometer initialization failed\n\r");

    }

 

    returnBandwidthValue = Accelerometer_setBandwidth(xdkAccelerometers_BMA280_Handle,ACCELEROMETER_BMA280_BANDWIDTH_125HZ);

 

    if (RETCODE_OK != returnBandwidthValue) {

        printf("Configuring bandwidth failed \n\r");

    }

    returnRangeValue = Accelerometer_setRange(xdkAccelerometers_BMA280_Handle,ACCELEROMETER_BMA280_RANGE_2G);

 

    if (RETCODE_OK != returnRangeValue) {

        printf("Configuring range failed \n\r");

    }

}

 

 

/* --------------------------------------------------------------------------- |

 * BOOTING- AND SETUP FUNCTIONS ********************************************** |

 * -------------------------------------------------------------------------- */

 

 static void AppControllerEnable(void * param1, uint32_t param2)

 {

     BCDS_UNUSED(param1);

     BCDS_UNUSED(param2);

 

     uint32_t timerBlockTime = UINT32_MAX;

 

     xTimerStart(accelerometerHandle,timerBlockTime);

 }

 

 

 static void AppControllerSetup(void * param1, uint32_t param2)

 {

     BCDS_UNUSED(param1);

     BCDS_UNUSED(param2);

     Retcode_T retcode = RETCODE_OK;

 

     uint32_t oneMilisecondDelay = UINT32_C(1);

     uint32_t timerAutoReloadOn = UINT32_C(1);

 

     initAccelerometer();

 

     /* Creation and start of the timer task */

     accelerometerHandle = xTimerCreate((const char *) "readAcclerometer", oneMilisecondDelay,timerAutoReloadOn, NULL, readAccelerometer);

 

     retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerEnable, NULL, UINT32_C(0));

     if (RETCODE_OK != retcode)

     {

        printf("AppControllerSetup : Failed \r\n");

        Retcode_RaiseError(retcode);

        assert(0);

 

        /* To provide LED indication for the user */

     }

 }

 

 

 void AppController_Init(void * cmdProcessorHandle, uint32_t param2)

 {

     BCDS_UNUSED(param2);

 

     Retcode_T retcode = RETCODE_OK;

 

     if (cmdProcessorHandle == NULL)

     {

         printf("AppController_Init : Command processor handle is NULL \r\n");

         retcode = RETCODE(RETCODE_SEVERITY_ERRORRETCODE_NULL_POINTER);

     }

     else

     {

         AppCmdProcessor = (CmdProcessor_T *) cmdProcessorHandle;

         retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerSetup, NULL, UINT32_C(0));

     }

 

     if (RETCODE_OK != retcode)

     {

         Retcode_RaiseError(retcode);

         assert(0); /* To provide LED indication for the user */

     }

 }

0 (0 투표)