Skip to main content
kriscicluna
Associate
February 7, 2018
Question

STM32F4 Discovery Multi-dimensional Array Problem

  • February 7, 2018
  • 1 reply
  • 1574 views
Posted on February 07, 2018 at 09:08

I have a control algorithm which operates within a Timer Interrupt of 200 us. Basically my code generates an output and compares it to a look up table. When I have a single array defined:

const float Adm_Re_array[720] = {...values...}

And search for a match in the LUT which has the minimum distance from the array using this code:

for (i=1;i<=8;i=i++)

{

k = act_element+i-4;

if (k < 0) k = k + 720;

if (k >(719)) k = k - 720;

dist_Re = Adm_Re-Adm_Re_array[k];

dist_T = (dist_Re*dist_Re);

if ((dist_T <= dist_T_min)||(dist_T_min == -1))

{

dist_T_min = dist_T;

act_element = k;

}

}

My control algorithm works well and I have used only 124 us of the 200 us.

When I change from a single dimension to a multi-dimensional array of form:

const float Adm_Re_array[7][720] = {...values...}

And search for a match in the LUT which has the minimum distance from the array using this code:

for (i=1;i<=8;i=i++)

{

k = act_element+i-4;

if (k < 0) k = k + 720; 

if (k >(719)) k = k - 720 

dist_Re = Adm_Re-Adm_Re_array[4][k];

dist_T = (dist_Re*dist_Re);

if ((dist_T <= dist_T_min)||(dist_T_min == -1))

{

dist_T_min = dist_T;

act_element = k;

}

}

My control code goes unstable while the loop time does not change significantly from the 124 us as I am only looking at one row of the multidimensional array in my search. Are there any known issues with the STM32 using multidimensional arrays similar to what I am observing. 

Regards,

K

    This topic has been closed for replies.

    1 reply

    AvaTar
    Senior III
    February 7, 2018
    Posted on February 07, 2018 at 09:18

    You are not on a PC here, with virtually unlimited ressources.

    Can you tell where this array is located (stack or global) ?

    My control code goes unstable ...

    What exactly does this mean ?

    Are there any known issues with the STM32 using multidimensional arrays similar to what I am observing.

    One thing has nothing to do with the other.

    Organizing memory for your variables is the sole responsibility of the compiler (toolchain).

    BTW, I am pretty sure your algorithm could use integer types and scaled math instead of floating point.

    kriscicluna
    Associate
    February 7, 2018
    Posted on February 07, 2018 at 09:36

    I am generating a sinusoidal current (controlled by the microcontroller) and comparing it to the fixed LUT in memory. When I compare it to a single dimension array the LUT and real time current are exactly the same. When I introduce multi-dimensional arrays the generated sinusoidal current (real-time) is not controlled according to the reference (hence not stable).

    Yes I can easily use integer and scale accordingly. The size of the array is constant hence i defined it const float. Could you direct me towards documentation on how to optimize my array definitions?

    AvaTar
    Senior III
    February 7, 2018
    Posted on February 07, 2018 at 10:16

    When I introduce multi-dimensional arrays the generated sinusoidal current (real-time) is not controlled according to the reference (hence not stable).

    Still not sure what you mean with unstable.

    Deviation too high, ringing, or does your code take too long ?

    Yes I can easily use integer and scale accordingly. The size of the array is constant hence i defined it const float.

    Do you use the FPU at all in your project ?

    Adding floating point code does not mean the FPU is actually used.

    And have you realized that single precision floating point has only 24 bit resolution ?

    Algorithms designed with double in mind tend to become unstable with float.

    if (k < 0) k = k + 720; 

    if (k >(719)) k = k - 720

    I would replace that with something like

    k = (uint32_t) (k%720);

    Generally, the involved datatypes and some surrounding code would clarify things, too.

    As a general advice, you can test your algorithms extensively with proper datatypes on a PC before.

    Instrumentation, debugging and validation of results is much easier.

    We use a (not so cheap) testing tool and a simulation environment for that purpose.