STM32F4 Discover Board Strange Number Bug

Discussion in 'Embedded Systems and Microcontrollers' started by Shagas, Aug 8, 2015.

  1. Shagas

    Thread Starter Active Member

    May 13, 2013
    I've been having issues with a piece of code that I'm writing for my UAV on the STM32F4 discovery board.
    The issue is the following: For some reason an addition of a positive number gives a zero or negative result.

    Code (C):
    3. // action_potential_X is float
    4. // pitch_scaling_factor = 0.2
    5. // X_group is float
    7. if(action_potential_pitch > 0){
    8.         N_group = action_potential_pitch*pitch_scaling_factor;
    9.         S_group = 0;
    10.     }else if(action_potential_pitch < 0){
    11.         S_group = action_potential_pitch*pitch_scaling_factor;
    12.         N_group = 0;
    13.     }else{
    14.         N_group = 0;
    15.         S_group = 0;
    16.     }
    19.     // Motor outputs
    20.     uint16_t motor0_out;
    21.     uint16_t motor1_out;
    22.     uint16_t motor2_out;
    23.     uint16_t motor3_out;
    25.     // Assign individual motor outputs
    26.     motor0_out = (fabsf(CW_group) + fabsf(N_group) + fabsf(R_group));
    27.     motor1_out = (fabsf(ACW_group) + fabsf(S_group) + fabsf(R_group));
    28.     motor2_out = (fabsf(CW_group)  + fabsf(S_group) + fabsf(L_group));
    29.     motor3_out = (fabsf(ACW_group) + fabsf(N_group) + fabsf(L_group));
    31.     // Update motor outputs
    32.     writeOutput(motor0_out, motor1_out, motor2_out, motor3_out);

    You can see here that I went paranoid and put fabsf everywhere even though there are previous checks on ALL the groups for negativity, just in case the compiler was doing something strange (which I think that it is).
    The issue is that if I remove the N_group and S_group from the motorX_out assignments then all the motors react uniformly to the input. If I leave them
    as it is then motors 1 and 2 (which belong to S_group) do not work until I push the throttle up to about 60%. In other words they behave something like (throttle-60%) . It's as if the S_group is subtracting even though it's supposed to be positive.I've went through the loop with the debugger about 20 times with various values and everything is doing what it is supposed to, but in practice it's not working.

    Does anyone have an idea on what is going on? I've been debugging this for about 8 hours now with no luck. The worst part is that the EXACT same thing for roll with the exact same code works no problem, but with pitch I get this issue.

    I'm using Coocox IDE with gcc compiler
    Last edited: Aug 8, 2015
  2. Shagas

    Thread Starter Active Member

    May 13, 2013
    After hours and hours of debugging and sifting through the dissasembly I found out that the issue was not the program at all
    but the back motor ESCs which auto calibrated at the beginning to give a 50% deadzone . At least I learned a few ARM assembly instructions...
    JohnInTX likes this.
  3. atferrari

    AAC Fanatic!

    Jan 6, 2004
    with very very few exceptions, most of posts including a bug claim in the title are just a story about an error or mistake by the TS/OP.
    Good that you found the reason.