karpavicius.linas

float math saturating to u16 midscale

Discussion created by karpavicius.linas on Dec 9, 2016
Latest reply on Dec 10, 2016 by S. Marsanne

Hello.
I was making BLDC motor program and my ramp for current for no good reason is saturating.

 

Ramp idea is simple, i have u32 current, and float current_f.

 

When i change set point (current) my current_f start to add or subtract 0.001 froam itself. But it is saturating at 32768.

volatile uint32_t current = 2000;
volatile float current_f = 5000.0f;

 

 

    if(current_f<current)
    {
      current_f+=0.001f;
    }
    if(current_f>current)
    {
      current_f-=0.001f;
    }

 

If i change float do double, it works as it should. When i switch back to float and software FPU( no Hardware FPU) same problem happens, it is saturating to 32768. What do i missing ? Is it normal, or my set size is to low for float ?

Thank you for your answers

Outcomes