EmbeddedRelated.com
Forums

Arithmetic on MSP430F149

Started by larstoref April 14, 2005
well im trying to do simple float operations, but i get crazy
results...a few examples...i have an accelerometer i want to use as a
tiltsensor (2-axis)

this simple code calculate the values:
float pitch, roll;
pitch = 180*asinf(Ax/807); // 807 = 1g
roll  = 180*asinf(Ay/807);

when i do this, i get -23040.000000 when Ax and Ay is close to 0 (+/-
100) whats wrong? according to crossworks documentation for math.h,
asinf will return a float in the range [-pi/2, pi/2].

also, when i try to do a simple subtraction, float x - float y, i get
simular strange results....i have to cast to ints and then do the
subtraction....is my msp crazy or is it just me? :)





Beginning Microcontrollers with the MSP430

Why are you messing around with floating point? it is toally 
unnecessary. For accuracy you should also be correcting and normalising 
the readings against calibration values.

Al

larstoref wrote:

>well im trying to do simple float operations, but i
get crazy
>results...a few examples...i have an accelerometer i want to use as a
>tiltsensor (2-axis)
>
>this simple code calculate the values:
>float pitch, roll;
>pitch = 180*asinf(Ax/807); // 807 = 1g
>roll  = 180*asinf(Ay/807);
>
>when i do this, i get -23040.000000 when Ax and Ay is close to 0 (+/-
>100) whats wrong? according to crossworks documentation for math.h,
>asinf will return a float in the range [-pi/2, pi/2].
>
>also, when i try to do a simple subtraction, float x - float y, i get
>simular strange results....i have to cast to ints and then do the
>subtraction....is my msp crazy or is it just me? :)
>
>
>
>
>
>
>.
>
> 
>Yahoo! Groups Links
>
>
>
> 
>
>
>
>
>
>  
>


Hi, 

> well im trying to do simple float operations, but
i get crazy 
> results...a few examples...i have an accelerometer i want to 
> use as a tiltsensor (2-axis)
> 
> this simple code calculate the values:
> float pitch, roll;
> pitch = 180*asinf(Ax/807); // 807 = 1g
> roll  = 180*asinf(Ay/807);
> 
> when i do this, i get -23040.000000 when Ax and Ay is close to 0 (+/-
> 100) whats wrong? according to crossworks documentation for 
> math.h, asinf will return a float in the range [-pi/2, pi/2].
> 
> also, when i try to do a simple subtraction, float x - float 
> y, i get simular strange results....i have to cast to ints 
> and then do the subtraction....is my msp crazy or is it just me? :)

I have just tried:

  a = 180 * asinf(100./807);

This sets a == 23.3623 as it should.  Tested on an F149 with both
multiplier enabled and disabled.

How are you displaying the results in order to know that you get -23040?

--
Paul Curtis, Rowley Associates Ltd  http://www.rowley.co.uk
CrossWorks for MSP430, ARM, AVR and (soon) MAXQ processors

--- In msp430@msp4..., "larstoref" <larstoref@y...> wrote:
> 
> well im trying to do simple float operations, but i get crazy
> results...a few examples...i have an accelerometer i want to use as a
> tiltsensor (2-axis)
> 
> this simple code calculate the values:
> float pitch, roll;
> pitch = 180*asinf(Ax/807); // 807 = 1g
> roll  = 180*asinf(Ay/807);
> 
> when i do this, i get -23040.000000 when Ax and Ay is close to 0 (+/-
> 100) whats wrong? according to crossworks documentation for math.h,
> asinf will return a float in the range [-pi/2, pi/2].
> 
> also, when i try to do a simple subtraction, float x - float y, i get
> simular strange results....i have to cast to ints and then do the
> subtraction....is my msp crazy or is it just me? :)

What are the types of Ax and Ay? If they are not floats, then the
result of the division by 807 is going to be an integer. I don't know
if the compiler will automagically cast this int into a float before
calling asinf or not. The results could vary from the unexpected
(asinf(0.0)) to spectacularly wrong. Like interpreting an integer as a
float.

BTW, the correct conversion from radians to degrees is 180 degrees/ PI
radians.

You could skip the whole floating point thing and just use Cordic
routines. 






I am using printf to display my results. my usart0 is connected to my
pc through rs232.

I have been testing abit more, and it seems like its printf that give
me the trouble.... im not sure why... i use %f for floats and %d for
ints..and sometimes i get wrong output and somtimes its right..


--- In msp430@msp4..., "Paul Curtis" <plc@r...> wrote:
> Hi, 
> 
> > well im trying to do simple float operations, but i get crazy 
> > results...a few examples...i have an accelerometer i want to 
> > use as a tiltsensor (2-axis)
> > 
> > this simple code calculate the values:
> > float pitch, roll;
> > pitch = 180*asinf(Ax/807); // 807 = 1g
> > roll  = 180*asinf(Ay/807);
> > 
> > when i do this, i get -23040.000000 when Ax and Ay is close to 0
(+/-
> > 100) whats wrong? according to crossworks documentation for 
> > math.h, asinf will return a float in the range [-pi/2, pi/2].
> > 
> > also, when i try to do a simple subtraction, float x - float 
> > y, i get simular strange results....i have to cast to ints 
> > and then do the subtraction....is my msp crazy or is it just me? :)
> 
> I have just tried:
> 
>   a = 180 * asinf(100./807);
> 
> This sets a == 23.3623 as it should.  Tested on an F149 with both
> multiplier enabled and disabled.
> 
> How are you displaying the results in order to know that you get -23040?
> 
> --
> Paul Curtis, Rowley Associates Ltd  http://www.rowley.co.uk
> CrossWorks for MSP430, ARM, AVR and (soon) MAXQ processors




Have you:

(a) #included <math.h>?
(b) #included <stdio.h>?

What warnings does the compiler give you?  I tried replicating your
code, but I can't, I get the right results.

-- Paul.

> -----Original Message-----
> From: larstoref [mailto:larstoref@lars...]
> Sent: 16 April 2005 19:16
> To: msp430@msp4...
> Subject: [msp430] Re: Arithmetic on MSP430F149
> 
> 
> 
> 
> I am using printf to display my results. my usart0 is connected to my
> pc through rs232.
> 
> I have been testing abit more, and it seems like its printf that give
> me the trouble.... im not sure why... i use %f for floats and %d for
> ints..and sometimes i get wrong output and somtimes its right..
> 
> 
> --- In msp430@msp4..., "Paul Curtis" <plc@r...> wrote:
> > Hi, 
> > 
> > > well im trying to do simple float operations, but i get crazy 
> > > results...a few examples...i have an accelerometer i want to 
> > > use as a tiltsensor (2-axis)
> > > 
> > > this simple code calculate the values:
> > > float pitch, roll;
> > > pitch = 180*asinf(Ax/807); // 807 = 1g
> > > roll  = 180*asinf(Ay/807);
> > > 
> > > when i do this, i get -23040.000000 when Ax and Ay is 
> close to 0 (+/-
> > > 100) whats wrong? according to crossworks documentation for 
> > > math.h, asinf will return a float in the range [-pi/2, pi/2].
> > > 
> > > also, when i try to do a simple subtraction, float x - float 
> > > y, i get simular strange results....i have to cast to ints 
> > > and then do the subtraction....is my msp crazy or is it 
> just me? :)
> > 
> > I have just tried:
> > 
> >   a = 180 * asinf(100./807);
> > 
> > This sets a == 23.3623 as it should.  Tested on an F149 with both
> > multiplier enabled and disabled.
> > 
> > How are you displaying the results in order to know that 
> you get -23040?
> > 
> > --
> > Paul Curtis, Rowley Associates Ltd  http://www.rowley.co.uk
> > CrossWorks for MSP430, ARM, AVR and (soon) MAXQ processors
> 
> 
> 
> 
> 
> .
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
> 

YES!! that was it! the stdio.h was not included in the file i used for
calculating the results. Thanks alot :)

Lars

--- In msp430@msp4..., "Paul Curtis" <plc@r...> wrote:
> Have you:
> 
> (a) #included <math.h>?
> (b) #included <stdio.h>?
> 
> What warnings does the compiler give you?  I tried replicating your
> code, but I can't, I get the right results.
> 
> -- Paul.
> 
> > -----Original Message-----
> > From: larstoref [mailto:larstoref@y...]
> > Sent: 16 April 2005 19:16
> > To: msp430@msp4...
> > Subject: [msp430] Re: Arithmetic on MSP430F149
> > 
> > 
> > 
> > 
> > I am using printf to display my results. my usart0 is connected to my
> > pc through rs232.
> > 
> > I have been testing abit more, and it seems like its printf that give
> > me the trouble.... im not sure why... i use %f for floats and %d for
> > ints..and sometimes i get wrong output and somtimes its right..
> > 
> > 
> > --- In msp430@msp4..., "Paul Curtis" <plc@r...> wrote:
> > > Hi, 
> > > 
> > > > well im trying to do simple float operations, but i get
crazy 
> > > > results...a few examples...i have an accelerometer i want to

> > > > use as a tiltsensor (2-axis)
> > > > 
> > > > this simple code calculate the values:
> > > > float pitch, roll;
> > > > pitch = 180*asinf(Ax/807); // 807 = 1g
> > > > roll  = 180*asinf(Ay/807);
> > > > 
> > > > when i do this, i get -23040.000000 when Ax and Ay is 
> > close to 0 (+/-
> > > > 100) whats wrong? according to crossworks documentation for 
> > > > math.h, asinf will return a float in the range [-pi/2,
pi/2].
> > > > 
> > > > also, when i try to do a simple subtraction, float x - float

> > > > y, i get simular strange results....i have to cast to ints 
> > > > and then do the subtraction....is my msp crazy or is it 
> > just me? :)
> > > 
> > > I have just tried:
> > > 
> > >   a = 180 * asinf(100./807);
> > > 
> > > This sets a == 23.3623 as it should.  Tested on an F149 with both
> > > multiplier enabled and disabled.
> > > 
> > > How are you displaying the results in order to know that 
> > you get -23040?
> > > 
> > > --
> > > Paul Curtis, Rowley Associates Ltd  http://www.rowley.co.uk
> > > CrossWorks for MSP430, ARM, AVR and (soon) MAXQ processors
> > 
> > 
> > 
> > 
> > 
> > .
> > 
> >  
> > Yahoo! Groups Links
> > 
> > 
> > 
> >  
> > 
> > 
> > 
> >




Hi Lars, 

Good to know that we simple vendors have a use in this forum.  Without
the #include you will get a warning from the compiler that there is no
prototype for that function.  Once you include the header file, the
prototype is available and then the compiler can generate the correct
calling sequence.

Good luck with your project!

Regards,

--
Paul Curtis, Rowley Associates Ltd  http://www.rowley.co.uk
CrossWorks for MSP430, ARM, AVR and (soon) MAXQ processors

> -----Original Message-----
> From: larstoref [mailto:larstoref@lars...] 
> Sent: 16 April 2005 19:07
> To: msp430@msp4...
> Subject: [msp430] Re: Arithmetic on MSP430F149
> 
> 
> 
> YES!! that was it! the stdio.h was not included in the file i 
> used for calculating the results. Thanks alot :)
> 
> Lars
> 
> --- In msp430@msp4..., "Paul Curtis" <plc@r...> wrote:
> > Have you:
> > 
> > (a) #included <math.h>?
> > (b) #included <stdio.h>?
> > 
> > What warnings does the compiler give you?  I tried replicating your 
> > code, but I can't, I get the right results.
> > 
> > -- Paul.
> > 
> > > -----Original Message-----
> > > From: larstoref [mailto:larstoref@y...]
> > > Sent: 16 April 2005 19:16
> > > To: msp430@msp4...
> > > Subject: [msp430] Re: Arithmetic on MSP430F149
> > > 
> > > 
> > > 
> > > 
> > > I am using printf to display my results. my usart0 is 
> connected to 
> > > my pc through rs232.
> > > 
> > > I have been testing abit more, and it seems like its printf that 
> > > give me the trouble.... im not sure why... i use %f for 
> floats and 
> > > %d for ints..and sometimes i get wrong output and 
> somtimes its right..
> > > 
> > > 
> > > --- In msp430@msp4..., "Paul Curtis" <plc@r...>
wrote:
> > > > Hi,
> > > > 
> > > > > well im trying to do simple float operations, but i get
crazy 
> > > > > results...a few examples...i have an accelerometer i 
> want to use 
> > > > > as a tiltsensor (2-axis)
> > > > > 
> > > > > this simple code calculate the values:
> > > > > float pitch, roll;
> > > > > pitch = 180*asinf(Ax/807); // 807 = 1g roll  > >
> > > 180*asinf(Ay/807);
> > > > > 
> > > > > when i do this, i get -23040.000000 when Ax and Ay is
> > > close to 0 (+/-
> > > > > 100) whats wrong? according to crossworks documentation
for 
> > > > > math.h, asinf will return a float in the range [-pi/2,
pi/2].
> > > > > 
> > > > > also, when i try to do a simple subtraction, float x 
> - float y, 
> > > > > i get simular strange results....i have to cast to 
> ints and then 
> > > > > do the subtraction....is my msp crazy or is it
> > > just me? :)
> > > > 
> > > > I have just tried:
> > > > 
> > > >   a = 180 * asinf(100./807);
> > > > 
> > > > This sets a == 23.3623 as it should.  Tested on an F149 
> with both 
> > > > multiplier enabled and disabled.
> > > > 
> > > > How are you displaying the results in order to know that
> > > you get -23040?
> > > > 
> > > > --
> > > > Paul Curtis, Rowley Associates Ltd  http://www.rowley.co.uk 
> > > > CrossWorks for MSP430, ARM, AVR and (soon) MAXQ processors
> > > 
> > > 
> > > 
> > > 
> > > 
> > > .
> > > 
> > >  
> > > Yahoo! Groups Links
> > > 
> > > 
> > > 
> > >  
> > > 
> > > 
> > > 
> > >
> 
> 
> 
> 
> 
> .
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
> 
>