Go Down

Topic: results are 0.00 when adding 2 floats together (Read 1 time) previous topic - next topic


im adapting balancing robot code i got from http://www.kerrywong.com. ( i think ) i am down to one final issue. when i blend the accelerometer and gyro data i get a 0.00 result.

i can get both parts of the equation if i do them individually, but as soon as i add them my result goes to 0.00.
i can serial print all components of the formula and they are all floats...

Code: [Select]

degX = (float) ggyrox2 * (float) elapsedTimeSec;

        currAngle = 0.95 * (prevAngle + degX) + 0.05 * angle;
   //    currAngle = 0.05 * angle;
   //    currAngle = 0.95 * (prevAngle + degX);
        prevAngle = currAngle;
        return currAngle;


So print out the two values before and after you combine them.
That fragment of code does not reproduce your problem.


ok, i changed the formula.

gyroangle = 0.95 * (prevAngle + degX) comes out with a result of 0.00

if i change the line to:

currAngle=0.95 * (prevAngle + degX) i get the correct number

acclangle= 0.05 * angle; i get the correct number

when i add gyroangle and acclangle i get 0.00

Code: [Select]

degX = (float) ggyrox2 * (float) elapsedTimeSec;
      acclangle= 0.05 * angle;
      gyroangle = 0.95 * (prevAngle + degX);

      currAngle = acclangle + gyroangle;

        prevAngle = currAngle;
   Serial.print (",");
   Serial.print (",");
   Serial.print (",");
   Serial.println ("");
    return currAngle;

James C4S

Your code snippet really is not helpful to solving your problem.  You really need to post code that does two things:  1) compiles as-is and 2) reproduces the problem.  This way others can help determine (and test) solutions.  (This sometimes meaning creating "test code" to demonstrate the issue.)

It would be exceptionally helpful to know how all of the variables you are using are currently defined and their values before this code is run.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com


i have attached the complete code.  please excuse the mess.  i am a novice.  i copied one piece of code to read the accelerometers, and one for the balancing formulas.  i recently did my own code to read the accelerometers and gyro.  all this is functional except for the blending formula.  thanks for the help!!!


Just going to point out, because people tend to overlook the obvious surprisingly often:
Are you sure that a 0.00 result is incorrect?  It's entirely possible that the two angles just happen to add to 0.  If that's so, there's probably a problem somewhere in your physical system, or in your coding where you assign values to your variables.


good suggestion.  i did check that several ways as i juggled the formula around, but ill check that one more time since you mentioned it...


Probably a stupid suggestion that I have not tested but you define the offending floats all on the same line (float accAngle = 0.0, currAngle = 0.0, prevAngle = 0.0, elapsedTimeSec=0.0;) and only the first entry (accAngle) works as expected. I program in VB6 and if you define multiple variables on the same line only the last is of your defined type and the rest end up as variant types. Maybe your having a similar problem.
Don't PM me for help as I will ignore it.

Nick Gammon

You need to post all your code. I've seen people "shadow" variables before (have the same variable in different scopes). Also you might have a NaN (not a number) in there somewhere.
Please post technical questions on the forum, not by personal message. Thanks!

More info:


I program in VB6 and if you define multiple variables on the same line only the last is of your defined type and the rest end up as variant types

Well in C you can define variables on one line without problems.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131