Store accelerometer data
응답
19. 9. 2 오전 8:36

I wrote the following code to Store accelerometer data in the sd card. Now I am facing two problems.

1. I cant stop the code to stop writing data on the sd card. I used a xTimerStop() function with a vTaskDelay() function but its not working.

2. On the text file sometimes there is a 4th data. I am attaching my code and the screenshot of the text file for your kind help.

#include "XdkAppInfo.h"
#undef BCDS_MODULE_ID
#define BCDS_MODULE_ID XDK_APP_MODULE_ID_APP_CONTROLLER
#include <stdio.h>
#include "BCDS_CmdProcessor.h"
#include <FreeRTOS.h>
#include "timers.h"

// SD Card Related Headers

#include "BCDS_SDCard_Driver.h"
#include "ff.h"

// Sensor Related Headers

#include "AppController.h"
#include "XdkSensorHandle.h"

static CmdProcessor_T * AppCmdProcessor;
xTimerHandle accelerometerHandle = NULL;

// SD Card Related Declarations

#define DEFAULT_LOGICAL_DRIVE   ""
#define DRIVE_ZERO              UINT8_C(0)
#define FORCE_MOUNT             UINT8_C(1)
#define FIRST_LOCATION          UINT8_C(0)

static FIL fileObject;

static int counter = 0;
static const char Filename[] = "Test.txt";
static char readings [100];
static int fileSize = 0;


Retcode_T searchForFileOnSdCard(const char* filename, FILINFO* fileData){

  if(FR_OK == f_stat(filename, fileData)){ // @suppress("Symbol is not resolved")
        return RETCODE_OK;
  } else {
    printf("File %s does not exist. \n\r",filename);
    return RETCODE_FAILURE;
  }
}

void writeDataIntoFileOnSdCard(const char* filename, char* dataBuffer){
    FRESULT fileSystemResult;
    char ramBufferWrite[UINT16_C(256)]; // Temporay buffer for write file
    uint16_t fileSize;
    UINT bytesWritten;
    fileSize = (uint16_t) strlen(dataBuffer);

    for(uint32_t index = 0; index < fileSize; index++){
        ramBufferWrite[index] = dataBuffer[index];
    }
    f_open(&fileObject, filename, FA_OPEN_EXISTING | FA_WRITE);
    f_lseek(&fileObject, f_size(&fileObject)); // @suppress("Field cannot be resolved")
    fileSystemResult = f_write(&fileObject, ramBufferWrite, fileSize, &bytesWritten);

    if((fileSystemResult != FR_OK) || (fileSize != bytesWritten)){ // @suppress("Symbol is not resolved")
        printf("Error: Cannot write to file %s \n\r", filename);
    }
    fileSystemResult = f_close(&fileObject);
}

static void readAccelerometer(xTimerHandle xTimer)
 {
     (void) xTimer;

	if(RETCODE_OK == searchForFileOnSdCard(Filename, NULL)){
  for (;;) {

    Retcode_T returnValue = RETCODE_FAILURE;

    Accelerometer_XyzData_T bma280 = {INT16_C(0), INT16_C(0), INT16_C(0)};
    memset(&bma280, 0, sizeof(CalibratedAccel_XyzMps2Data_T));

    returnValue = Accelerometer_readXyzGValue(xdkAccelerometers_BMA280_Handle,&bma280);

    if (RETCODE_OK == returnValue) {
    	    	    	fileSize+= snprintf(readings + fileSize, sizeof(readings) - fileSize,
    	    	    	                "%.2f ,", (float) bma280.xAxisData);
    	    	    	fileSize+= snprintf(readings + fileSize, sizeof(readings) - fileSize,
    	    	    	    	    	"%.2f ,", (float) bma280.yAxisData);
    	    	    	fileSize+= snprintf(readings + fileSize, sizeof(readings) - fileSize,
    	    	    	    	    	"%.2f \n", (float) bma280.zAxisData);
    	    	writeDataIntoFileOnSdCard(Filename, readings);
    	    	printf("%d \n\r",counter);
    		  counter = counter + 1;
    	    }
  }

}
}



//End of Task Experiment


//SD Card Related Functions

void InitSdCard(void){
  Retcode_T retVal = RETCODE_FAILURE;
  FRESULT FileSystemResult = FR_OK; // @suppress("Symbol is not resolved")
  static FATFS FatFileSystemObject;
  SDCardDriver_Initialize();
  if(SDCARD_INSERTED == SDCardDriver_GetDetectStatus()){
    retVal = SDCardDriver_DiskInitialize(DRIVE_ZERO);
    if(RETCODE_OK == retVal){
      printf("SD Card Disk initialize succeeded \n\r");
      FileSystemResult = f_mount(&FatFileSystemObject, DEFAULT_LOGICAL_DRIVE, FORCE_MOUNT);
      if (FR_OK != FileSystemResult){ // @suppress("Symbol is not resolved")
        printf("Mounting SD card failed \n\r");
      }
    }
  }
}

// Sensor Related Functions


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_500HZ);

   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);
         vTaskDelay(5000);
         xTimerStop(accelerometerHandle,timerBlockTime);
          vTaskSuspend(readAccelerometer);


}

static void AppControllerSetup(void * param1, uint32_t param2)
{
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);
    Retcode_T retcode = RETCODE_OK;

    uint32_t threeMilisecondDelay = UINT32_C(3);
    uint32_t timerAutoReloadOn = UINT32_C(1);

// SD Card Code

    InitSdCard();

    accelerometerHandle = xTimerCreate((const char *) "readAcclerometer", threeMilisecondDelay, timerAutoReloadOn, NULL, readAccelerometer);
// Sensor Code

    initAccelerometer();

// Common

    retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerEnable, NULL, UINT32_C(0));
    if (RETCODE_OK != retcode)
    {
        printf("AppControllerSetup : Failed \r\n");
        Retcode_RaiseError(retcode);
        assert(0);
    }
}

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_ERROR, RETCODE_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 투표)
RE: Store accelerometer data
응답
19. 9. 9 오전 10:42 as a reply to Md Zakaria RAhman.

Hi I am trying to add this new line. If the counter is less than or equal to 500 then the wrtie function should be called if not then it should be stopped. I tried running this code but the following error is hapening. What should i do

 

0 (0 투표)
RE: Store accelerometer data
응답
19. 9. 9 오전 11:58 as a reply to Md Zakaria RAhman.

Hi,

Can you kindly quickly explain what is your use case ?

To answer your first question regarding stopping the timer and extra data,

  1. What is the purpose of the for loop in the timer function ?
  2. If you are interested in executing the timer function for certain time duration N only, then you can initialize periodicity using below function. In the example, xTimer is not active, change its period to 500ms.  This will also cause the timer to start.  Block for a maximum of 100 ticks if the change period command cannot immediately be sent to the timer command queue.
    xTimerChangePeriod( xTimer, 500 / portTICK_PERIOD_MS, 100 ) == pdPASS
  3. Also can you explain why fileSize is added to the buffer ?

You can refer application SdCardExample for storage code reference and use SendAccelDataOverUdpandBle for sensor sampling reference. A hint is, in the second application SendAccelDataOverUdpandBle, write data to SD card in place of sending over BLE.

0 (0 투표)
RE: Store accelerometer data
응답
19. 9. 9 오후 1:29 as a reply to Padmapriya G.

Now i used a task function insted of a timer function. I have optimised the for loop to run till the cound is below 900. This time the task function stops writing data. But still i could see there are sometimes 4 colums of data .And also if someone can also help me how to modify the code so that i press button 1 the task starts and stops when i press button 2.

Sincerely,

Md Zakaria Rahman

#include "XdkAppInfo.h"
#undef BCDS_MODULE_ID
#define BCDS_MODULE_ID XDK_APP_MODULE_ID_APP_CONTROLLER
#include <stdio.h>
#include "BCDS_CmdProcessor.h"
#include <FreeRTOS.h>
#include "timers.h"

// SD Card Related Headers

#include "BCDS_SDCard_Driver.h"
#include "ff.h"

// Sensor Related Headers

#include "AppController.h"
#include "XdkSensorHandle.h"

static CmdProcessor_T * AppCmdProcessor;

// SD Card Related Declarations

#define DEFAULT_LOGICAL_DRIVE   ""
#define DRIVE_ZERO              UINT8_C(0)
#define FORCE_MOUNT             UINT8_C(1)
#define FIRST_LOCATION          UINT8_C(0)

static FIL fileObject;

static int counter = 0;
static const char Filename[] = "Test.txt";
static char readings [100];
static int fileSize = 0;


Retcode_T searchForFileOnSdCard(const char* filename, FILINFO* fileData){

  if(FR_OK == f_stat(filename, fileData)){ // @suppress("Symbol is not resolved")
        return RETCODE_OK;
  } else {
    printf("File %s does not exist. \n\r",filename);
    return RETCODE_FAILURE;
  }
}

void writeDataIntoFileOnSdCard(const char* filename, char* dataBuffer){
    FRESULT fileSystemResult;
    char ramBufferWrite[UINT16_C(512)]; // Temporay buffer for write file
    uint16_t fileSize;
    UINT bytesWritten;
    fileSize = (uint16_t) strlen(dataBuffer);

    for(uint32_t index = 0; index < fileSize; index++){
        ramBufferWrite[index] = dataBuffer[index];
    }
    f_open(&fileObject, filename, FA_OPEN_EXISTING | FA_WRITE);
    f_lseek(&fileObject, f_size(&fileObject)); // @suppress("Field cannot be resolved")
    fileSystemResult = f_write(&fileObject, ramBufferWrite, fileSize, &bytesWritten);

    if((fileSystemResult != FR_OK) || (fileSize != bytesWritten)){ // @suppress("Symbol is not resolved")
        printf("Error: Cannot write to file %s \n\r", filename);
    }
    fileSystemResult = f_close(&fileObject);
}

void readwrite()
{
	if(RETCODE_OK == searchForFileOnSdCard(Filename, NULL)){
  for (counter= 0;counter<= 900;counter = counter + 1) {

    Retcode_T returnValue = RETCODE_FAILURE;

    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) {
        	    	    	fileSize+= snprintf(readings + fileSize, sizeof(readings) - fileSize,
        	    	    	                "%ld ,", (long int) bma280.xAxisData);
        	    	    	fileSize+= snprintf(readings + fileSize, sizeof(readings) - fileSize,
        	    	    	    	    	"%ld ,", (long int) bma280.yAxisData);
        	    	    	fileSize+= snprintf(readings + fileSize, sizeof(readings) - fileSize,
        	    	    	    	    	"%ld \n", (long int) bma280.zAxisData);
        	    	writeDataIntoFileOnSdCard(Filename, readings);
        	    	printf("%d \n\r",counter);
        		     	    }
  }

}
}

void createnewtask(void)
{
  xTaskHandle taskHandle = NULL;

  xTaskCreate(
    readwrite,                 // function that implements the task
    (const char * const) "My Task", // a name for the task
	900,       // depth of the task stack
    NULL,                           // parameters passed to the function
    4,               // task priority
    taskHandle                      // pointer to a task handle for late reference
  );
}

//End of Task Experiment


//SD Card Related Functions

void InitSdCard(void){
  Retcode_T retVal = RETCODE_FAILURE;
  FRESULT FileSystemResult = FR_OK; // @suppress("Symbol is not resolved")
  static FATFS FatFileSystemObject;
  SDCardDriver_Initialize();
  if(SDCARD_INSERTED == SDCardDriver_GetDetectStatus()){
    retVal = SDCardDriver_DiskInitialize(DRIVE_ZERO);
    if(RETCODE_OK == retVal){
      printf("SD Card Disk initialize succeeded \n\r");
      FileSystemResult = f_mount(&FatFileSystemObject, DEFAULT_LOGICAL_DRIVE, FORCE_MOUNT);
      if (FR_OK != FileSystemResult){ // @suppress("Symbol is not resolved")
        printf("Mounting SD card failed \n\r");
      }
    }
  }
}

// Sensor Related Functions


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_500HZ);

   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);

    createnewtask();

}

static void AppControllerSetup(void * param1, uint32_t param2)
{
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);
    Retcode_T retcode = RETCODE_OK;

// SD Card Code

    InitSdCard();

// Sensor Code

    initAccelerometer();

// Common

    retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerEnable, NULL, UINT32_C(0));
    if (RETCODE_OK != retcode)
    {
        printf("AppControllerSetup : Failed \r\n");
        Retcode_RaiseError(retcode);
        assert(0);
    }
}

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_ERROR, RETCODE_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 투표)