Developer Portal Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 
    SOLVED

    XDK Mita Write Accelerometer to SD Card

    Stallion
    Member

    XDK Mita Write Accelerometer to SD Card

    Hi all, 

    I am trying to write some code on XDK Mita which writes accelerometer measumerents to the SD Card as a .csv file every 50 milliseconds (20 times a second). I have copied and pasted my code below. The problem is, no matter if I run my code for 10 minutes or an hour, I only have 3800-4300 (3-4 minutes of data), written to the SD card. I have attached one of my results to show what I mean. If anyone knows what might be causing this and how I can fix this so I can write to the SD every 50 millisecond, I would appreciate any help 🙂 Thanks, Stallion

     

    package main;
    import platforms.xdk110;
    var x_temp : int32;
    var y_temp : int32;
    var z_temp : int32;

    setup accelerometer{
    bandwidth = BW_62_50Hz;
    }

    setup storage : SDCard {
    var writeOut = appendingTextWrite("SensorData.csv"); 
    }

    every 50 milliseconds
    {
    x_temp = accelerometer.x_axis.read();
    y_temp = accelerometer.y_axis.read();
    z_temp = accelerometer.z_axis.read();

    //This command allows for making columns how I want

    storage.writeOut.write(`${x_temp}, ${y_temp}, ${z_temp},`);
    println(`Magnitude x-accel is: ${x_temp} `);
    println(`Magnitude y-accel is: ${y_temp} `);
    println(`Magnitude z-accel is: ${z_temp} `);
    } // Every 50 milliseconds write to the SD card and terminal

    7 REPLIES 7
    LlobetAtWork
    Long-established Member

    Re: XDK Mita Write Accelerometer to SD Card

    Hi Stallion,

    this could be due performance issues. An average new (out of the box) class 10 SD Card takes around 6.25ms (min: 4.26ms, max 8.25ms) to initialize a file write operation and the write speed is around 1.25ms/kB for a  FatFS file. A simple file write operation like yours takes between 7.5ms and 10ms on a fresh card. Performance will degrade with time and especially the setup time will rise with usage.

    I think that the code inside the timer function may take slightly longer to execute than 50ms. The runtime will enqueue the execution of a timer until the timer pending queue overflows after 4 minutes and the XDK stops sampling and storing into the SD card.

    Usually the more perfoming solution for mid-to high data-rate logging is to have a timer or interrupt generating data and enqueueing it to a strean buffer, after the stream buffer reaches a certain size, it can be stored or sent out. 

    Best regards,

    Francisco Llobet

    Stallion
    Member

    Re: XDK Mita Write Accelerometer to SD Card

    Hi Francisco,

    Thanks for your reply on this topic. Going back to the main C architecture now, if I wanted to created a function that executes every 50 milliseconds, how would I do that? So basically these are the things I am wondering,

    The information that appears to contain information about how to use interrupts on the XDK can be found here:

    https://developer.bosch.com/web/xdk/timer-and-countern

    This information is in the 'XDK Extension bus' topic. Does that mean that the interrupts can only be used to external peripherals to the XDK? Or can it be used with the peripherals within the XDK? Such as the BMA280 Accelerometer.

    The other question is, if I want to achieve 50 ms interrupts, the clock is 48 MHz (CLOCK0) and the largest prescale is 1024. that means that the slowest interrupts I can achieve is 21 micro seconds - way too fast. Am I missing something or is there another way I can achieve 50 ms interrupts?

    LlobetAtWork
    Long-established Member

    Re: XDK Mita Write Accelerometer to SD Card

    Hi Stallion,

    before I make a mess, I wanted to clarify some technical topics. It is OK to use the SW timers (Timers in Mita are translated to timers in FreeRTOS) for the required period (50ms +/- 1ms). A HW timer is better suited for periods smaller than 1ms or for generating PWM signals or some other use cases.
    Using a HW timer and executing write SD card or SPI operations in interrupt context (outside the runtime governed by the schedule) would get the system into a deadlock anyways.

    Basically the point needed for a fluid execution is that the function run for each timer be shorter than the timer period. For this it might be necessary to measure or benchmark that portion of the code.

    For this, in C you can embed some benchmarking code like

    #include "task.h"
    #include "timer.h"  // Hope those are the correct FreeRTOS API headers
    
    void myTimerFunction(TimerHandle_t xTimer)
    {
        (void)xTimer; // If unused 
    
        uint32_t startTime = (uint32_t)xTaskGetTickCount(); // Time at start
      
    
       /* Your code doing things in less than 50ms*/  
     
      uin32_t stopTime = (uint32_t)xTaskGetTickCount(); // Time at stop
        if(stopTime - startTime > 50)
        {
            printf("ERROR: Time issue. Code takes longer to execute than 50Ticks\n\r");
         }
    }

    best regards,

    Francisco Llobet

    Stallion
    Member

    Re: XDK Mita Write Accelerometer to SD Card

    Hi Francisco,

    Yes, soft timers are acceptable. Thank you for your help and I am looking forward to your next reply on how I can set up a 50 ms timer with soft timing. Please clarify if your suggestions are to be implemented in the C environment or the Mita environment.

    Stallion

    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