|
|
View previous topic :: View next topic |
Author |
Message |
s_mack
Joined: 04 Jun 2009 Posts: 107
|
18F4480 as a math student.Can it keep up to these equations? |
Posted: Thu Jun 04, 2009 10:06 pm |
|
|
New to the PIC world.
Can a microcontroller such as an 18F4480 handle "complex" math? My device is firing every 4.4ms (approx) and I'm wondering if it can safely keep up when having to do this calculation:
Code: | var = 2.005 * var ^ 2.027 |
I don't yet know proper syntax so the symbols may very well mean something different to you. What I'm getting at is 2.005 multiplied by a variable to the 2.027th power.
I have a potentiometer input and output and my output is the input modified to that equation.
Is it better for me to devise a 255 row table with the values and do a switch or something to that effect? Or am I worrying for nothing and it will reliably keep up?
Thanks
- Steven |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Thu Jun 04, 2009 10:15 pm |
|
|
Can we assume the input from the pot will be to the ADC...?
What will be the CPU speed?
What will be firing, a timer, an external interrrupt?
What will be the output? What do you mean by potentiometer output? What interface will you use?
All these will have to be taken into consideration as well. You might be able do the math in 4.4ms but some of the above may be an issue. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Jun 04, 2009 11:27 pm |
|
|
The below code is executing in 830 us on a 48 MHz clocked PIC18.
Code: | x=2.005*pow(x,2.027); |
|
|
|
s_mack
Joined: 04 Jun 2009 Posts: 107
|
|
Posted: Thu Jun 04, 2009 11:40 pm |
|
|
dyeatman wrote: | Can we assume the input from the pot will be to the ADC...?
What will be the CPU speed?
What will be firing, a timer, an external interrrupt?
What will be the output? What do you mean by potentiometer output? What interface will you use?
All these will have to be taken into consideration as well. You might be able do the math in 4.4ms but some of the above may be an issue. |
Hmm... that's all very good questions I'm sure. Unfortunately I probably don't know the answers. Yes to the ADC, not sure on CPU speed (its a PIC 18F4480 - if that doesn't answer the Q then I don't know), a timer is firing to keep it at 4.4ms, output I believe is a PWM (when device is "off" it approximates the input and when "on" it modifies the input as per the equation), and I don't know what you mean by an "interface".
I didn't design the board and I can't say I know the finer details of its operation. I have the source code and I'm making a few adjustments by I'm not super familiar with it. I'm... well... a businessman playing engineer for the moment and trying not to get into too much trouble along the way. I'm not trained in any of this... forgive me!
Thanks for your help.
FvM wrote: | The below code is executing in 830 us on a 48 MHz clocked PIC18. Code: | x=2.005*pow(x,2.027); |
|
Thanks! That is most helpful. It tells me not only that I may be in the right ballpark (thorough testing required) but also shows me the proper syntax! Appreciated.
- Steven |
|
|
s_mack
Joined: 04 Jun 2009 Posts: 107
|
|
Posted: Thu Jun 04, 2009 11:48 pm |
|
|
Further to the above in case it clears anything up at all...
The code was originally designed to simply double the input when the device was on.
So if the POT input was at say 25% of its range, the output would be set to 50%. I've since found that linear correlation to be too simplistic. The new equation much better approximates what we wish to achieve. I originally wanted that, but the engineer that designed it said we should stay away from math beyond addition and subtraction for fear it may run too slow. At the time though, he was working remotely. He would code and I would test. Initially it seemed the simple addition would suffice so we never took it further. Now he's out of the picture and I'd rather not fork over big $ for things I think I can hopefully handle on my own.
With guidance
Cheers.
- Steven |
|
|
Ttelmah Guest
|
|
Posted: Fri Jun 05, 2009 9:13 am |
|
|
No, 18F4480, does not answer the speed question.
The chip itself, has a _maximum_ speed it is rated to run at, but can be operated at any speed below this. The actual speed depends on the components used to generate the clock.
The actual time needed to perform operations depends directly on the actual clock rate.
If you have existing code that runs on the chip, this should have the clock speed near the top of the source.
Otherwise you are going to need to get more 'personal' with the board, an read the clock frequency on the crystal if present....
Best Wishes |
|
|
s_mack
Joined: 04 Jun 2009 Posts: 107
|
|
Posted: Fri Jun 05, 2009 9:27 am |
|
|
Skimming through the code, I see:
Code: | // Setup the Node BAUD rate as configured in EEPROM using a 40MHz clock |
Code: | set_rtcc(0x66); //reset the timer for a 10Hz update rate |
Code: | #DEVICE HIGH_INTS=TRUE, ADC=8
#use delay(clock=40000000) |
That's it for anything that hints (to me) at a speed.
Thanks for your help. I think I'll just give it a try and thoroughly test. If it works it works, right? |
|
|
Ttelmah Guest
|
|
Posted: Fri Jun 05, 2009 9:45 am |
|
|
Yes.
Two of these lines say the chip is being run at 40MHz. For the original example, the time would go up to 996uSec, from the 830uSec Fvm gave. Still time to do quite a lot more.
In the compiler manual, under 'common questions and answers', there is a topic entitled 'how much time to maths operations take', with a table for 40MHz, covering multiplication, division, log, exp etc.. Gives you a better idea of how long each operation will take.
Best Wsihes |
|
|
s_mack
Joined: 04 Jun 2009 Posts: 107
|
|
Posted: Fri Jun 05, 2009 9:54 am |
|
|
That's helpful, thanks!
Curious how you figured out 996uSec. From that table I'd figure we have one * and one pow so why isn't that 510.4 + 35.8 = 546.2?
I also see on that help page that it starts by saying Quote: | For trig formulas consider a lookup table instead of real time calculations | which I suspected might be faster.
- Steven |
|
|
Ttelmah Guest
|
|
Posted: Fri Jun 05, 2009 2:13 pm |
|
|
The table doesn't have the power function. Fvm posted the time taken for a single power, and multiplication, as 830uSec at 48MHz. Scale by 48/40, and you get the time that would be needed at 40MHz.
Best Wishes |
|
|
Franck26
Joined: 29 Dec 2007 Posts: 122 Location: Ireland
|
|
Posted: Fri Jun 05, 2009 2:56 pm |
|
|
Hi s_mack,
As you suggested earlier, if var is an int (or int8), there is only 255 possibility, so you should use a table. Like that you would be able to implement any kind of equation and it would always take the same processor time (few micro second).
Good luck,
Franck. |
|
|
s_mack
Joined: 04 Jun 2009 Posts: 107
|
|
Posted: Fri Jun 05, 2009 3:27 pm |
|
|
That probably is the way to go, yes. Especially since I'd have to round and convert the float results of the equation to int8 aproximations anyway.
Thanks everyone. |
|
|
s_mack
Joined: 04 Jun 2009 Posts: 107
|
|
Posted: Sat Jun 06, 2009 8:52 pm |
|
|
Back to this...
So now I have a performance vs. space issue looming. And I wonder what your thoughts are.
I have a "complex" equation like previous discussed and I have confirmed it is too slow for this application. I built a table like this: Code: | int8 CONST ECO_MODE[256] = { //giving a table saves the processer from having to do heavy trig.
14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,15,15,15,15,16,16,16,16,16,17,17,17,17,18,18,18,18,
19,19,19,19,20,20,20,20,21,21,21,22,22,22,23,23,23,23,24,24,24,25,25,26,26,26,27,27,27,28,28,28,29,29,30,30,
30,31,31,32,32,33,33,34,34,34,35,35,36,36,37,37,38,38,39,39,40,41,41,42,42,43,43,44,45,45,46,47,47,48,48,49,
50,50,51,52,53,53,54,55,55,56,57,58,59,59,60,61,62,63,64,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,
81,82,83,84,86,87,88,89,90,92,93,94,95,97,98,99,101,102,104,105,106,108,109,111,112,114,115,117,118,120,122,
123,125,127,128,130,132,134,136,137,139,141,143,145,147,149,151,153,155,157,159,162,164,166,168,171,173,175,
178,180,183,185,188,190,193,195,198,201,203,206,209,212,215,218,218,218,218,218,218,218,218,218,218,218,218,
218,218,218,218,218,218,218,218,218,239,239,239,239,239,239,239,239,239,239,239,239,239,239,239,239,239,239}; |
and that works fine.
The problem now is that I need a different table depending on user input. In fact, I have 200 tables. I don't mind creating them but now I'm running out of ROM space!! So I started to wonder if I could dynamically create the the 200 tables at startup before the mission critical action is taking place. In other words, go back to the equation and put it inside a for 1 to 200 loop and have the code build my 200 tables when the power is first turned on? Then I might run out of RAM space. So is there a way to dynamically build just the one table that is needed based on user input?
Maybe I can make this more clear by building a scenario. The user turns on the device and enters "programming" mode (by holding down a button - they would only do this very infrequently) and enters in their choice of the tables (basically puts X into the equation) which is stored into EEPROM and then they shut it off. From now on they turn it on without entering programming mode. Before the device can be used, the first half a second or so is spent crunching the inputed variable through our complex equation and the code builds (into RAM) the table like above. After "crunch time" is done, the normal routine kicks in, which is a tight loop that can't handle the equation each time but can handle a table lookup.
Sound feasible? Any thoughts on how I'd go about that?
- Steven |
|
|
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Sat Jun 06, 2009 11:02 pm |
|
|
200 tables sounds a bit excessive. If it depends on user input, then just recalculate the one table at the time of input. _________________ Andrew |
|
|
s_mack
Joined: 04 Jun 2009 Posts: 107
|
|
Posted: Sat Jun 06, 2009 11:07 pm |
|
|
That's my question. How does one do that? The table is declared as a CONST. Can I fill that at run time? |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|