|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Basic Math Help
Hi, this is my first post on Chief Delphi. my name is Phil Nguyen and I am on the team 1930, Delta Force.
I am having trouble with multiplying numbers and stuff. the numbers cannot be an int because we need it very precise. PHP Code:
It doesn't work, not even somthing like: PHP Code:
We've tried using current height as a double and a float, same thing. |
|
#2
|
|||
|
|||
|
Re: Basic Math Help
Quote:
If you need it in thousandths, just multiply everything by 1000 first, then deal with the decimal shift later. That way you can use only integer math and keep your precision. Remember, you only get a byte of resolution to drive your motors.. |
|
#3
|
|||
|
|||
|
Re: Basic Math Help
My first thought would be to try something like this.
Code:
current_height = Get_Analog_Value(rc_ana_in01); current_height *= .0049; current_height -= .0044; current_height = pow(current_height, -1.0895); current_height *= 29.401; |
|
#4
|
|||
|
|||
|
Re: Basic Math Help
what type is current_height?
18 thousand could be correct, maybe the analog input value is higher than what you think it is. Try printing the analog value and the value*30, and compare them. Also, I strongly recommend not using floats, they are very slow and memory intensive. |
|
#5
|
|||
|
|||
|
Re: Basic Math Help
we're using an infared sensor to detect height, I'd like the height to be close to thousanths. yeah, I did do a printf and thats where I got the number 200 from.
Last edited by CoasterFuelPhil : 14-02-2007 at 11:08. |
|
#6
|
|||||
|
|||||
|
Re: Basic Math Help
Assuming current_height is defined as a float, how are you printing the value?
This printf does not support printing of floats, so are you converting it back to an (int) to print? By the way, floats, including doubles, on this PIC actually have less accuracy than a long. |
|
#7
|
|||
|
|||
|
Re: Basic Math Help
printf("current_height: %u\r", current_height );
thats probably the problem |
|
#8
|
|||
|
|||
|
Re: Basic Math Help
Yes, printf does not support floats
Try this: printf("current_height: %u\r", (unsigned int) current_height ); I would strongly suggest not using floats, though. |
|
#9
|
|||
|
|||
|
Re: Basic Math Help
Okay, let's look at this another way.
Your analog sensor gives you a value of 0-255 Your motor takes a value of 0-255 So, why are you using a float? At the worst case, you could use a look up table. You have your complicated formula you want to use. Use your formula to make an array. Then, you just need: Code:
myvalue = LookUpTable[GetAnalogValue(....)]; Or, you can forget about inches, and deal with everything in the domain of 0-255, whatever units they happen to be ![]() |
|
#10
|
|||
|
|||
|
Re: Basic Math Help
Actually, analog inputs on the RC are 0-1023 (OI is 0-255) unless you use a different ADC. Also, they are not trying to controll a motor, they want to calculate the height of something (arm?)
|
|
#11
|
|||
|
|||
|
Re: Basic Math Help
Quote:
Still, he does not need floating point for this. (to the OP): Have you ever heard of "fixed point" math? Someone touched on it earlier - if you need .001 precision, then multiply everything by 1000. Keep in mind that at .001 precision, you can measure just one inch with your A/D.... ttyl, --buddy |
|
#12
|
|||
|
|||
|
Re: Basic Math Help
Quote:
I mean, current_height=29.401*(pow((0.0049*((double)Get_An alog_Value(rc_ana_in01)) - 0.0044),-1.0895)); Shouldn't be turned into current_height=30*(pow((0*((double)Get_Analog_Valu e(rc_ana_in01)) - 0),-1)); You'd always get a divide by zero =P! However you might be right in that floats/doubles aren't the tools for the job...I can't say; I'm not up on PIC architecture ![]() |
|
#13
|
||||
|
||||
|
Re: Basic Math Help
I've never used floats, we always use doubles. They get printed with %dl, and they operate very similarly to floats
|
|
#14
|
|||
|
|||
|
Re: Basic Math Help
Doubles are even more memory-intensive than floats on the PIC18F (well, on any architecture, but especially so on the PIC). Using them probably is a bad idea, I doubt you'd ever need that precision for FIRST. Also, double use the %lf token, for "long float." I don't think %dl has the meaning you think it does.
|
|
#15
|
|||
|
|||
|
Re: Basic Math Help
There is no reason to use floating point numbers,
Why not do this: Code:
current_height= (294010* pow( (49*(Get_Analog_Value(rc_ana_in01)) - 44),-10895) ) ) / 10000 ); to keep it from overflowing |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help with math | Termite233 | Math and Science | 4 | 12-07-2005 04:41 |
| basic pneumatic help needed | Anton | Programming | 4 | 10-02-2005 16:02 |
| Help programming in Visual Basic | xxlshortys | Programming | 0 | 06-04-2003 08:54 |
| Math Help!!! | Leon Machado IV | Chit-Chat | 7 | 16-11-2001 19:38 |