Problem running out of SRAM

Hi, all!
I am using Pixhawk and recently I’ve been developing a module/function to read a large size (around 1.5MB) 4-D matrix of float type from a binary file in the SD card and store the read matrix into the flash memory, 2MB on Pixhawk. The matrix is used for controller gain scheduling, so it should be accessed every time the main loop runs.
As I am starting from scratch, I tried reading a 2*4 test matrix of float type and it went well. However, when I tried with the large matrix, I could not build because I ran out of SRAM, as shown in the error message “region `sram’ overflowed by 524024 bytes”. So I suppose that once the large matrix is read in, it’s stored in the RAM, 256KB on pixhawk, yet it’s way less than enough. Therefore, I was wondering how can I read the matrix from the SD card and store it directly into the flash memory, in order not to overflow the RAM?

Here’s a snippet of my coding, it’s rather simple:

#include <AP_HAL/AP_HAL.h>
#include <stdio.h>

void setup();
void loop();

const AP_HAL::HAL& hal = AP_HAL::get_HAL();

float T_hat_in[103][103][4][4];

void setup(void)
{
    hal.scheduler->delay(5000);
    hal.console->begin(57600);
    hal.console->printf("SD Card reading test startup...\n");
    FILE *fp;
    fp = fopen("/fs/microsd/table/T_pixhawk.bin", "rb");
    fread(&T_hat_in, sizeof(float), 103*103*16, fp);
    fclose(fp);
}

void loop(void)
{
    hal.console->printf("Read %f \n",T_hat_in[1][1][1][1]);
    hal.scheduler->delay(500);
}

AP_HAL_MAIN();

Well, in theory you can define your data table as a volatile const, and set a predetermined address for it in the flash by the #pragma location=address. Then you can fill it up with Flash write during runtime.
However I doubt that you have 1.5Mbyte free flash beside the pixhawk code…

Thanks, although I kind of solve the problem by shrinking the lookup table size for gain scheduling instead to let it fit inside the RAM. So basically leaving the flash memory intact.

yeah, main problem is the pixhawk hardware is a small embedded cpu with limited memory. Have you considered developing your algorithm using a Linux flight controller? You’ll have tons of memory resources there. Take a look at the BeagleBone Blue or Emlid products

Thanks much, I will look into that.