Skip to main content
Tsemr.1
Associate II
January 25, 2021
Question

Floating point error not working.

  • January 25, 2021
  • 3 replies
  • 1620 views

Dear ST Hello,

I have a big problem with floating point calculations,

I am trying to implement the IIR filter to filter a current but the result is Zero.

Sometimes the watch expression window shows me 0 but the input filter is not zero.

 /* Get the HImax */
 HimaxCurrentCons = HAL_ADC_GetValue(&hadc2);
 
 /* Get LSM current */
 l_u16AdcCurrent = HAL_ADC_GetValue(&hadc1);
 
 /* Filter the current LSM */
 Input[0] = (float_t)1E6;//l_u16AdcCurrent;
 filter_out = FilterCoefInput[0] * Input[0];
 
 for (int i = 6; i > 0; i--)
 {
	 /* adding the ai * xi i=7..1 */
	 filter_out = filter_out + (FilterCoefInput[i] * Input[i]);
 
	 /* Shifting the register input */
	 Input[i] = Input[i - 1];
 }
 
 for (int j = 6; j > 0; j--)
 {
	/* Adding the bj * yj j = 6..1 */
	filter_out = filter_out + (FilterCoefOutput[j] * Output[j-1]);
 
	/* Shifting the register output */
	Output[j] = Output[j - 1];
 }
 
 Output[0] = filter_out;
 
 /* Current compute */
 LsmCurrent = (300.0/2559) * filter_out;

Thank in advance,

S.tarik

    This topic has been closed for replies.

    3 replies

    KnarfB
    Super User
    January 25, 2021

    What MCU? And: you are not showing all types.

    Tsemr.1
    Tsemr.1Author
    Associate II
    January 25, 2021

    I am using the STM32F407 discovery board,

    const float_t FilterCoefInput [7] = {
    #if IMPULS
    		8.576557E-06f, 5.145934E-05f,
    		1.286483E-04f, 1.715311E-04f,
    		1.286483E-04f, 5.145934E-05f,
    		8.576557E-06f
    #else
    		8.576557, 51.45934,
    		128.6483, 171.5311,
    		128.6483, 51.45934,
    		8.576557
    #endif
     
    };
    const float_t FilterCoefOutput [7] = {
    #if IMPULS
    		 1.0f,			+4.787135f,
    		 -9.6495177f, +10.46907f,
    		 -6.44111188f, +2.129038f,
    		 -0.2951724f
    #else
    		 1.0f,			+4.787135f,
    		 -9.6495177f, +10.46907f,
    		 -6.44111188f, +2.129038f,
    		 -0.2951724f
    #endif
     
    };
    float_t Input[7] = {0.0};
    float_t Output[6] = {0.0};
    float_t filter_out = 0.0f;

    I am trying different configurations to solve this problem,

    thank you

    KnarfB
    Super User
    January 25, 2021

    If Output array has size 6, the first write to Output[j] with j=6 is illegal, potentially overwriting filter_out. Make the array larger.

    Tsemr.1
    Tsemr.1Author
    Associate II
    January 27, 2021

    Thank you,

    that was the problem

    waclawek.jan
    Super User
    January 25, 2021

    You define:

    > Output[6]

    but in the loop you write to Output[j] when j == 6.

    JW

    Tsemr.1
    Tsemr.1Author
    Associate II
    January 27, 2021

    yes JW, thank you for help.