Pages: [1]   Go Down
Author Topic: results are 0.00 when adding 2 floats together  (Read 606 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
   
     }   

Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34168
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

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

Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!!

* rev3a.ino (18.4 KB - downloaded 9 times.)
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 135
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Norfolk UK
Offline Offline
Faraday Member
**
Karma: 69
Posts: 2557
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18794
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 626
Posts: 34168
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pages: [1]   Go Up
Jump to: