Math Functions
Up to Bugs, Known Issues, & Requests
Hi,
I need to execute the asin function in math.h but the function can't be found when I do a build. I included the math.h file. Am I doing something wrong?
Thanks,
Paul
Previously Liam Staskawicz wrote:
I think these are not built into the toolchain, in an attempt to keep it as lightweight as possible. You may need to find/write your own implementation of it.
Under mcbuilder/resources/arm-elf/include, there is a math.h file with math functions. Where is the object or library for this header file? Can't it be linked in?
The math functions (which are usually contained in libm.a, by the way, when it has been built, so you link with -lm as an argument after your object files) tend to use floating point math. This chip has no FPU. The mere presence of a header file does not necessarily mean that the library has been built.
There are two ways to do floating-point on a chip such as this. Those are to build the toolchain to do all floating-point ops in its own library functions, and to build an exception handler for undefined instructions and try to emulate an FPU that way. To my knowledge, neither has been done here.
One of those two things would have to be done to have any hope of libm being able to work. This is not a shortcoming of the Make Controller in particular. I have not seen any open project which does the exception handler thing.
I have, myself, built such a toolchain (--with-float=soft). It was a huge pain. I wouldn't advise it. It makes my object files incompatible with anybody else's. It is generally frowned upon by people who know more about this stuff than I do. The next toolchain I build will not be like that. It will try to use the non-existent FPU, which will throw an exception.
There are ways to do trig functions using fixed-point math and/or lookup tables, thus avoiding the need for an FPU. Depending on your problem space (as embedded work so often does), you might be able to get by with just a small lookup table, or you might have to find an actual fixed-point trig library.
There are ARM chips with FPUs. These small, microcontroller-targeted ones, tend not to be among them, in general.
I'm trying to take an accelerometer analog output (limited to +-1 in each axis) and covert it to an angle. I know its not a linear function and the examples I've seen use asin. Any ideas?
I am working on a project that entails an accelerometer/XBee in my trailer and a controller/Xbee/LCD in my truck to help me in leveling the trailer when I set up when I go camping. Later I plan on proximity sensors to help line the trailer gooseneck up with the pin in the bed of the truck.
If you want to go the fixed-point math route, google comes to the rescue. If nothing else, I did find that a graphics library called Allegro has fixed-point routines. Also, there is a fixed-point FAQ. Both of those come up in the first page of google results for "fixed point trig".
But the lookup table is easy. It is also a commonly used technique when there is sufficient memory for the table.