STM32F446VET7 Two Float Value Difference Problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-26 7:55 AM
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
Solved! Go to Solution.
- Labels:
-
STM32F4 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-26 9:44 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-26 8:07 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-26 8:22 AM - edited ‎2024-08-26 8:30 AM
Hi TDK,
Thank you for answering
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 :
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-26 9:44 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-26 9:52 AM
You change tail after the subtraction. When the difference was calculated, tail was 8.
JW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-26 10:09 AM
@TDK wrote:The processor is unlikely to be performing math incorrectly.
Subject to the inherent limitations of binary floating-point maths:
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
#FP #FloatingPoint
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-26 11:35 AM
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
I am gonna try to fix that
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2024-08-26 11:58 AM
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.
