Society of Robots - Robot Forum

Software => Software => Topic started by: frank26080115 on March 10, 2008, 08:09:36 PM

Title: double assigned to double gives error
Post by: frank26080115 on March 10, 2008, 08:09:36 PM
Code: [Select]
int accelToAngle(int accel_)
{
double accel = accel_;
double accelZ_ = accelZ;
double angleRad = atan2(accel, accelZ_);
double angleDeg = (angleRad * 180) / 3.14159;
int angleDeg_ = angleDeg;
return angleDeg_;
}

won't compile, error not shown, only .elf file was never generated

Code: [Select]
int accelToAngle(int accel_)
{
double accel = accel_;
double accelZ_ = accelZ;
double angleRad = atan2(accel, accelZ_);
double angleDeg = 0;
int angleDeg_ = angleDeg;
return angleDeg_;
}

this will compile with no errors.

how do I do double calculations?
I'm using avr studio.
Title: Re: double assigned to double gives error
Post by: Tsukubadaisei on March 10, 2008, 08:27:17 PM
Where is the accelZ variable comming from? is it a global variable?
And what is atan2()? If you are using math.h then I think it is atan().  If it is a custom function show us the code or try using atan().
What is the difference between accel and accel_, accelZ and accelZ_, angleDeg and angleDeg_?

Finally, in the 7th line, it is written:

Code: [Select]
int angleDeg_ = angleDeg;instead try this
Code: [Select]
int angleDeg_ = (int)angleDeg;
Title: Re: double assigned to double gives error
Post by: frank26080115 on March 10, 2008, 10:11:56 PM
the differences between the names are their type

atan2:
The atan2() function computes the principal value of the arc tangent of __y / __x, using the signs of both arguments to determine the quadrant of the return value. The returned value is in the range [-pi, +pi] radians.

that doesn't cause the compile error

accelZ is global, it's an int

basically, I can't assign angleDeg to anything else but a constant

my chip is a atmega644 with PLENTY of room left.
Title: Re: double assigned to double gives error
Post by: Tsukubadaisei on March 11, 2008, 03:05:10 AM
From your code I could see you do some redundant things. And one of them is creating and assigning this angleDeg constant. Since it is just a instance for this function you wont be able to use it elsewhere. So try this code:

Code: [Select]
int accelToAngle(int accel_)
{
float accel = accel_;
float accelZ_ = accelZ;
float angleRad = atan2(accel, accelZ_);
return (int)((angleRad * 180) / 3.14159);
}
That is assuming that you have no trouble with angleRad.
If you have trouble with angleRad try this:

Code: [Select]
int accelToAngle(int accel_)
{
float accel = accel_;
float accelZ_ = (float)accelZ;
return (int)((atan2(accel, accelZ_) * 180) / 3.14159);
}

I am not an Atmega expert(I have just used the Atmega32 and 128) so there might be a remote possibility that "casting" is not implemented in your compiler. So should be a good idea try another compliler if everything else fails.
But since both Atmega32 and 128 have "casting" implemented  I doubt that.

If you still have trouble please post the source of atan2() and the type of accel_ (int or double or float).
By the way I used float instead of double, just in case. Try with that and see what happens. If you want try with double (but I hardly believe you need that pecision).
Title: Re: double assigned to double gives error
Post by: frank26080115 on March 11, 2008, 05:40:29 PM
I've been trying out different ways all day, but i've resorted to generating lookup tables

I think the problem is that math.h doesn't work at all, it's supposed to compute constants on the computer because of the optimization my compiler does. If I actually try to use them for variables, no dice
Title: Re: double assigned to double gives error
Post by: Tsukubadaisei on March 11, 2008, 10:28:49 PM
that is strange because I use math.h with atmega32 and atmega128 and it works(though I dont remember using atan()). My compiler is the gcc. Maybe you should try using gcc if you are not using it already.
Anyway, If you still want to try using a function there is one last resource I could recomend: Taylor expansion.
http://en.wikipedia.org/wiki/Taylor_series#List_of_Taylor_series_of_some_common_functions (http://en.wikipedia.org/wiki/Taylor_series#List_of_Taylor_series_of_some_common_functions) <- search here for arctan.
Just implement the function until the fourth term and you will have enough precision.
That is everything I have to say. That is what I whould have done as my last resource. Good luck!
Title: Re: double assigned to double gives error
Post by: JesseWelling on March 11, 2008, 10:51:08 PM
Code: [Select]
double angleDeg = (angleRad * 180) / 3.14159;
I'm guessing the constant needs a qualifier.
Code: [Select]
double angleDeg = (angleRad * 180) / 3.14159f;The f would qualify it as a float, but I can't seem to remember what the double qualifier is at the moment. But any ways, you get the idea  ;)

By the way this is also a good place to start with weird AVR specific problems: http://www.nongnu.org/avr-libc/


Title: Re: double assigned to double gives error
Post by: JonHylands on March 12, 2008, 06:44:27 AM
Code: [Select]
double angleDeg = (angleRad * 180) / 3.14159;
Try this:

double angleDeg = (angleRad * 180.0) / 3.14159;

- Jon