Go to Post Winning just makes your ego smile. - MooreteP [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 14-02-2007, 10:20
CoasterFuelPhil CoasterFuelPhil is offline
Registered User
FRC #1930
 
Join Date: Oct 2006
Location: Rochester
Posts: 6
CoasterFuelPhil is on a distinguished road
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:
current_height=29.401*(pow((0.0049*((double)Get_Analog_Value(rc_ana_in01)) - 0.0044),-1.0895)); 

It doesn't work, not even somthing like:
PHP Code:
current_height = ((30)*Get_Analog_Value(rc_ana_in01)); 
I am getting a number like 18 thousand when the analog info is something like 200

We've tried using current height as a double and a float, same thing.
  #2   Spotlight this post!  
Unread 14-02-2007, 10:27
Tom Bottiglieri Tom Bottiglieri is offline
Registered User
FRC #0254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Jan 2004
Rookie Year: 2003
Location: San Francisco, CA
Posts: 3,187
Tom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond repute
Re: Basic Math Help

Quote:
Originally Posted by CoasterFuelPhil View Post

I am having trouble with multiplying numbers and stuff. the numbers cannot be an int because we need it very precise.
How precise do you need this number?
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   Spotlight this post!  
Unread 14-02-2007, 10:27
Jake M Jake M is offline
void* Jake;
FRC #1178 (DURT)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Missouri
Posts: 118
Jake M has a spectacular aura aboutJake M has a spectacular aura about
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;
Working with floats can be very confusing, since the default casting is (int). I always like to try it like this, until I'm sure my logic is correct. In fact, this may even result in less assembly instructions, although I'm not sure at all.
__________________
Code:
void function(void)
 {
  function();
 }
  #4   Spotlight this post!  
Unread 14-02-2007, 10:37
tdlrali tdlrali is offline
Registered User
FRC #0469 (Las Guerrillas)
Team Role: Programmer
 
Join Date: Sep 2006
Rookie Year: 2006
Location: MI
Posts: 377
tdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud of
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   Spotlight this post!  
Unread 14-02-2007, 11:06
CoasterFuelPhil CoasterFuelPhil is offline
Registered User
FRC #1930
 
Join Date: Oct 2006
Location: Rochester
Posts: 6
CoasterFuelPhil is on a distinguished road
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   Spotlight this post!  
Unread 14-02-2007, 11:22
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,856
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
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.
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle
  #7   Spotlight this post!  
Unread 14-02-2007, 12:18
CoasterFuelPhil CoasterFuelPhil is offline
Registered User
FRC #1930
 
Join Date: Oct 2006
Location: Rochester
Posts: 6
CoasterFuelPhil is on a distinguished road
Re: Basic Math Help

printf("current_height: %u\r", current_height );

thats probably the problem
  #8   Spotlight this post!  
Unread 14-02-2007, 12:55
tdlrali tdlrali is offline
Registered User
FRC #0469 (Las Guerrillas)
Team Role: Programmer
 
Join Date: Sep 2006
Rookie Year: 2006
Location: MI
Posts: 377
tdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud of
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   Spotlight this post!  
Unread 14-02-2007, 13:39
buddy.smith buddy.smith is offline
Master Control
FRC #1795
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2007
Location: atlanta
Posts: 20
buddy.smith is an unknown quantity at this point
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(....)];
Make sense?

Or, you can forget about inches, and deal with everything in the domain of 0-255, whatever units they happen to be
  #10   Spotlight this post!  
Unread 14-02-2007, 15:30
tdlrali tdlrali is offline
Registered User
FRC #0469 (Las Guerrillas)
Team Role: Programmer
 
Join Date: Sep 2006
Rookie Year: 2006
Location: MI
Posts: 377
tdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud oftdlrali has much to be proud of
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   Spotlight this post!  
Unread 15-02-2007, 11:05
buddy.smith buddy.smith is offline
Master Control
FRC #1795
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2007
Location: atlanta
Posts: 20
buddy.smith is an unknown quantity at this point
Re: Basic Math Help

Quote:
Originally Posted by tdlrali View Post
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?)
I stand corrected.

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   Spotlight this post!  
Unread 15-02-2007, 15:18
Cheezmeister Cheezmeister is offline
Registered User
FRC #0871
 
Join Date: Jan 2007
Location: Long Island
Posts: 8
Cheezmeister is an unknown quantity at this point
Re: Basic Math Help

Quote:
Originally Posted by buddy.smith View Post
I stand corrected.

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
Well, floating point is the most direct solution to the precision needed for complex formula. The values he starts with and the values he'll wind up with ought to be ints, but that doesn't mean that everything in the formula can necessarily be rounded.

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   Spotlight this post!  
Unread 15-02-2007, 17:41
paulcd2000's Avatar
paulcd2000 paulcd2000 is offline
Accidentally speaks in C
AKA: Paul Dagnelie
FRC #1719 (The Umbrella Corp.)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Baltimore
Posts: 368
paulcd2000 is a jewel in the roughpaulcd2000 is a jewel in the roughpaulcd2000 is a jewel in the rough
Send a message via AIM to paulcd2000
Re: Basic Math Help

Quote:
Originally Posted by tdlrali View Post
I would strongly suggest not using floats, though.
I've never used floats, we always use doubles. They get printed with %dl, and they operate very similarly to floats
__________________
"People don't say 'It's just a game' when their team is winning!" -- Scott Adams

5.5 students (on average)* $7/h *210 hours/student= $8085 of labor, all volunteered (not counting mentors', who are each that much)

We have blades on our robot?! ***sweeeeeet***

There are 11 types of people in the world. Those who can read binary, those who can't, and those who say this joke is supposed to be, "There are 10 types of people in the world. Those who can read binary and those who have a life."
  #14   Spotlight this post!  
Unread 16-02-2007, 00:31
Shinigami2057 Shinigami2057 is offline
Slackware Is Your New God (Mentor)
AKA: Harry Bock
FRC #1350 (Rambots)
Team Role: Programmer
 
Join Date: Oct 2006
Rookie Year: 2006
Location: Johnston, RI
Posts: 106
Shinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really nice
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.
__________________
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs.
  #15   Spotlight this post!  
Unread 16-02-2007, 02:57
Salik Syed Salik Syed is offline
Registered User
FRC #0701 (RoboVikes)
Team Role: Alumni
 
Join Date: Jan 2003
Rookie Year: 2001
Location: Stanford CA.
Posts: 514
Salik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud ofSalik Syed has much to be proud of
Send a message via AIM to Salik Syed
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 );
You will need to use a unsigned int for the first param of the pow function
to keep it from overflowing
__________________
Team 701
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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


All times are GMT -5. The time now is 00:24.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi