Society of Robots - Robot Forum

Software => Software => Topic started by: Asellith on December 10, 2007, 10:57:17 AM

Title: Float calcs in a microcontroller
Post by: Asellith on December 10, 2007, 10:57:17 AM
I am using a z8 encore as a temp sensor interface and noticed that when I do the float calcs to determine the temp in an int value without lossing resolution it slows way now. Now sure if that is a problem on this project the information doesn't need to update that fast because it is just being output to an LED display. However for future projects I have in mind I may need to speed this process up is the best way to just build a lookup table as long as I have memory? I guess I could just corespond each ADC value directly with a temp and look it up in a table or something like that.

Here is the code
Code: [Select]
int ADC_to_Temp(int ADCvalue)
{
float temperature;
float voltage;
int temp_int;


voltage = ADCvalue * (3.3/1023); // converts the input ADC value on a 3.3 volt scale at 10 bit resolution
temperature = voltage / 0.01;     // LM35 uses 10 mv per degree celsius
temp_int = temperature;
if ((temperature - temp_int) > 0.5)
return(temp_int + 1);
else
return(temp_int);

}
Title: Re: Float calcs in a microcontroller
Post by: Admin on December 10, 2007, 01:23:44 PM
Yea, this is why I don't recommend floating point on mcu's . . .

Do you have it running at the full 20MHz?

A few simple changes . . . try it out and let us know if it's still not fast enough . . .

instead of
Code: [Select]
temperature = voltage / 0.01;do this
Code: [Select]
temperature = voltage * 100;
instead of
Code: [Select]
voltage = ADCvalue * (3.3/1023)do this
Code: [Select]
voltage = ADCvalue / 310
Title: Re: Float calcs in a microcontroller
Post by: DomoArigato on December 10, 2007, 04:50:19 PM
Often the reason it slows down is that the micro controller uses software to simulate floating point calculations.  You should be able to use the ADC value as an integer without any major loss of information.  Instead of using degrees in your calculations just use something like milli-degrees as your basic unit and leave it as an integer  (basically what Admin suggested).  A lookup table is also a great idea, especially if you only have one sensor your working with.  Lookup tables can be a problem if you have a large range of values of need a very high resolution.  They are generally faster than making a complicated conversion though.  Sometimes lookup tables are a problem because you can't change your conversion without re-programming, sometimes thats important to be able to do.
Title: Re: Float calcs in a microcontroller
Post by: JesseWelling on December 11, 2007, 12:51:24 AM
You can also make linear interpolation tables.
For my project at work we fit arctan into a little over 1k of data with .01 degrees accuracy.
Title: Re: Float calcs in a microcontroller
Post by: Admin on December 11, 2007, 05:54:24 AM
Anyone know of any math lookup table source code libraries out there for say AVR?
Title: Re: Float calcs in a microcontroller
Post by: Asellith on December 11, 2007, 11:05:49 AM
Ok so I changed some of the math ops admin suggested but in order to still get the float values to work and give me the resolution I need I had to put a .0 after each integer to force C to do a float calc instead of some weird int math forced into a float variable. New code works faster and looks better

Code: [Select]
int ADC_to_Temp(int ADCvalue)
{
float temperature;
float voltage;
int temp_int;


voltage = ADCvalue / 310.0; // converts the input ADC value on a 3.3 volt scale at 10 bit resolution
temperature = voltage * 100.0;     // LM35 uses 10 mv per degree celsius
temp_int = temperature;
if ((temperature - temp_int) > 0.5)
return(temp_int + 1);
else
return(temp_int);
}

I could probably do more with the int conversion and change this a bit but it runs fine and fast enough now so why mess with it.

Oh Admin I will be running the 20 MHZ right now I am using the dev board which is at 18.432 Mhz. The code works good just have to add a Celsius to Fahrenheit conversion and I'm done

Thanks for the help
Jonathan Bowen
Title: Re: Float calcs in a microcontroller
Post by: derrick_chi on December 11, 2007, 11:49:00 AM
Floating point calculations on a low level processor like a mcu can have horrible performance, it has to do with the fact that the floating point unit is usually HUGE and takes up alot of space on the die, some times I really wonder exactly why they are even necessary for RISC processors that are not intended to handle mathematical calculations which require a convergence to zero or infinity, ie calculus.

  Try changing your unit of representation for the number from volts to mv or something smaller your accuracy will go up and you won't need floating point calculations and if the processor runs only at 20 mhz, geeeeezzzz you want to avoid floating point calculations as much as you can. 

I use this method of changing the unit representation all the time to avoid using floating point numbers, and trust me the accuracy is just as good (depending on what unit you chose to use ) or in plenty of cases I've seen better and the performance is as well.
Title: Re: Float calcs in a microcontroller
Post by: JesseWelling on December 11, 2007, 11:10:18 PM
Quote
Anyone know of any math lookup table source code libraries out there for say AVR?

I can't post the code from work directly but I can borrow the book that the code is based on...
I can't remember if it has code in it or not but I'll see what I can do and maybe write my own for you guys.
Title: Re: Float calcs in a microcontroller
Post by: paulstreats on December 12, 2007, 11:49:23 AM
If you desperately need to use floats, and keep accuracy then you could un external fp co-processor