Skip to main content
Graduate
August 26, 2024
Solved

STM32F446VET7 Two Float Value Difference Problem

  • August 26, 2024
  • 3 replies
  • 1625 views

 

void AltitudeBuffer_Write(AltitudeBuffer_t* AltitudeBuffer, float altitude)
{
 static int i=0;
 i++;
 AltitudeBuffer->readTime[i%2] = FlightFlagTime.CurrentTime;
 uint32_t timeDifference = ( AltitudeBuffer->readTime[i%2] - AltitudeBuffer->readTime[(1 - (i%2))]);
 if( ((AltitudeBuffer->head+1)%ALTITUDE_BUFFER_SIZE ) == AltitudeBuffer->tail )
 {
 float tempValue = 0;
 for(int i=0; i<10; i++)
 {
 tempValue += AltitudeBuffer->buffer[i];
 }
 tempValue /= 10;
 AltitudeBuffer->avarageAltitude[AltitudeBuffer->tail % 2] = tempValue;
 AltitudeBuffer->avarageAltitude[2] = ( (AltitudeBuffer->avarageAltitude[AltitudeBuffer->tail % 2]) - (AltitudeBuffer->avarageAltitude[1-((AltitudeBuffer->tail) % 2)]) );
 AltitudeBuffer->VerticalSpeed = abs((AltitudeBuffer->avarageAltitude[2] / timeDifference)*1000);
 AltitudeBuffer->tail = (AltitudeBuffer->tail + 1)%ALTITUDE_BUFFER_SIZE;
 }
 AltitudeBuffer->buffer[AltitudeBuffer->head] = altitude;
 AltitudeBuffer->head = ( (AltitudeBuffer->head + 1) % ALTITUDE_BUFFER_SIZE );
}

 

in line 16 ; 

when (tail%2) = 0 

AltitudeBuffer->avarageAltitude[2] = ( (AltitudeBuffer->avarageAltitude[AltitudeBuffer->tail % 2]) - (AltitudeBuffer->avarageAltitude[1-((AltitudeBuffer->tail) % 2)]) );  is negative.

For example I got this values in debug session ; 

(AltitudeBuffer->avarageAltitude[AltitudeBuffer->tail % 2]) is = 897.060547

(AltitudeBuffer->avarageAltitude[1-((AltitudeBuffer->tail) % 2)]) is = 868.588684

so

AltitudeBuffer->avarageAltitude[2] should be = 897.060547 - 868.588684 = 28.471863

but it is oppiste of this value. I got ;

AltitudeBuffer->avarageAltitude[2] = -28.4718628

 

When tail % 2 = 0 its negative When tail % 2 = 0 its positive

but eitherway (AltitudeBuffer->avarageAltitude[AltitudeBuffer->tail % 2])

is bigger than

(AltitudeBuffer->avarageAltitude[1-((AltitudeBuffer->tail) % 2)]);

 

or example when tail % 2 = 1

(AltitudeBuffer->avarageAltitude[AltitudeBuffer->tail % 2]) is = 1.18015742

and

(AltitudeBuffer->avarageAltitude[1-((AltitudeBuffer->tail) % 2)]) is = 1.11249995

AltitudeBuffer->avarageAltitude[2] = 0.0676574707

Can u help me please?

I tried with double types instead of float but its not worked. I cannot define the problem exactly

 

 

    This topic has been closed for replies.
    Best answer by TDK

    Okay, showing all variables, good, but you've cropped it so we can't see where execution is at when those expressions are evaluated. Note that "tail" is also modified in the given code. Set a breakpoint at the "AltitudeBuffer->avarageAltitude[2] =" line, and step over. If you do that, only the "AltitudeBuffer->avarageAltitude[2]" will show up with a red background, instead of everything, indicating it (and only it) was changed since the last break.

    3 replies

    Super User
    August 26, 2024

    Let's be objective here. The processor is unlikely to be performing math incorrectly. It's handled in hardware by the core, which is used by millions of people and doesn't have glaring issues. Similarly, the compiler is also widely used and is unlikely to be creating the incorrect assembly code. So what's left?

    • Perhaps you are mistaken in what values are what. Try to capture everything in a screenshot rather than cut/paste which is subject to translation errors.
    • Perhaps this is a temporal issue. Put a breakpoint after the "invalid" statements are executed and show the values in the expression window.
    Graduate
    August 26, 2024

    Hi TDK, 

    Thank you for answering

    testbenchmark_0-1724685185209.png

    Tail is = 9 

    it means Tail%2 = 1

    AltitudeBuffer->avarageAltitude[AltitudeBuffer->tail % 2]

    means = AltitudeBuffer->avarageAltitude[1]

    and 

    AltitudeBuffer->avarageAltitude[1-((AltitudeBuffer->tail) % 2)] 

    means = AltitudeBuffer->avarageAltitude[0] 

     

    AltitudeBuffer->avarageAltitude[0] = 962.237976

    AltitudeBuffer->avarageAltitude[1] = 976.578308

     

    AltitudeBuffer->avarageAltitude[2] = AltitudeBuffer->avarageAltitude[1] = 976.578308 - 962.237976 = 14.340332

    But I got -14.340332

     

    With screen shots :

    testbenchmark_0-1724685656746.png

    testbenchmark_1-1724685677203.png

    testbenchmark_2-1724685696283.png

     

    TDKAnswer
    Super User
    August 26, 2024

    Okay, showing all variables, good, but you've cropped it so we can't see where execution is at when those expressions are evaluated. Note that "tail" is also modified in the given code. Set a breakpoint at the "AltitudeBuffer->avarageAltitude[2] =" line, and step over. If you do that, only the "AltitudeBuffer->avarageAltitude[2]" will show up with a red background, instead of everything, indicating it (and only it) was changed since the last break.

    Graduate
    August 26, 2024

    Hi TDK, Thank you for answering

    I did what you said and you are right. When I add a breakpoint at this 3 lines I see it clearly.

    Now tail is 7 and tail%2 = 1

    its gonna be >> 0.19411011 - 0.251983643 = -0.0578735322

     

    testbenchmark_1-1724697130675.png

    testbenchmark_2-1724697278973.png

    I am gonna try to fix that

    Super User
    August 26, 2024

    Glad you figured it out.

    Always good to simplify things when debugging to isolate the error. As a general rule, it's incredibly unlikely that the core or the compiler is the source of the issue. If you suspect one of those, try to prove it with a simple program and screenshots. Usually that convinces you otherwise and helps you find the misunderstanding. Works well for me anyway.

    Super User
    August 26, 2024

    You change tail after the subtraction. When the difference was calculated, tail was 8.

    JW