Go to Post And while I am not that big on hero-worship, I am pretty darn proud to know Dave. However, this does not mean that I will stop picking on him - MissInformation [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 04-02-2007, 14:21
Tz0m Tz0m is offline
Registered User
FRC #0333
Team Role: Programmer
 
Join Date: Feb 2006
Rookie Year: 2006
Location: New York
Posts: 17
Tz0m is an unknown quantity at this point
Storing functions to variables

I am trying to set a variable, type double, to a function that calculates distance. This is the code I have.
Quote:
dist = 80.0/tan(tilt_angR);
If I use a printf statement to calculate and display "dist," the value caclulates and prints properly. However, I need to set this value to a variable. Any help is appreciated.
  #2   Spotlight this post!  
Unread 04-02-2007, 14:45
Bongle's Avatar
Bongle Bongle is offline
Registered User
FRC #2702 (REBotics)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Waterloo
Posts: 1,069
Bongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond repute
Send a message via MSN to Bongle
Re: Storing functions to variables

If I read this correctly, you're trying to make a function that returns a distance.

Code:
double dist(double tilt_angR)
{
  return 80.0/tan(tilt_angR);
}
That code will do what I think you want to do.
  #3   Spotlight this post!  
Unread 04-02-2007, 15:14
Tz0m Tz0m is offline
Registered User
FRC #0333
Team Role: Programmer
 
Join Date: Feb 2006
Rookie Year: 2006
Location: New York
Posts: 17
Tz0m is an unknown quantity at this point
Re: Storing functions to variables

Thanks for the help, but I have another problem. The variable tilt_angR is set to another function, and I am having the same problem as before. Here is my code.
Quote:
tilt_angD = ((((int)TILT_SERVO - 124) * 65)/124));
tilt_angR = ((tilt_angD *3.14159265)/180.0);
dist = (80.0/tan(tilt_angR));
Thanks for the help.
  #4   Spotlight this post!  
Unread 04-02-2007, 19:07
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: Storing functions to variables

Remember that in C, those aren't functions or equations. They are statements, executed once.

To assign a function to a variable would be something like:
Code:
(type omitted) myfunc = tan;
So you need to declare all three variables, like so:
Code:
int tilt_angD = ((((int)TILT_SERVO - 124) * 65)/124));
double tilt_angR = ((tilt_angD *3.14159265)/180.0);
double dist = (80.0/tan(tilt_angR));
Just watch out for integer math.

Just be careful about your floating-point expressions, the RC doesn't like them much.
  #5   Spotlight this post!  
Unread 09-02-2007, 16:06
Tz0m Tz0m is offline
Registered User
FRC #0333
Team Role: Programmer
 
Join Date: Feb 2006
Rookie Year: 2006
Location: New York
Posts: 17
Tz0m is an unknown quantity at this point
Re: Storing functions to variables

Thanks for the help, but nothing has worked so far. We still have the same problem.
  #6   Spotlight this post!  
Unread 09-02-2007, 22:25
Kevin Sevcik's Avatar
Kevin Sevcik Kevin Sevcik is offline
(Insert witty comment here)
FRC #0057 (The Leopards)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Houston, Texas
Posts: 3,745
Kevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond repute
Send a message via AIM to Kevin Sevcik Send a message via Yahoo to Kevin Sevcik
Re: Storing functions to variables

I think we're having difficulties divining what you're attempting. If you could post some larger chunks of your code, such as where you're calling these statements and an example of the code where the printfs are working. Don't be shy about cutting and pasting a vast majority of your user_routines.c or zipping up some of your source files and attaching them to a post.
__________________
The difficult we do today; the impossible we do tomorrow. Miracles by appointment only.

Lone Star Regional Troubleshooter
  #7   Spotlight this post!  
Unread 10-02-2007, 14:07
Tz0m Tz0m is offline
Registered User
FRC #0333
Team Role: Programmer
 
Join Date: Feb 2006
Rookie Year: 2006
Location: New York
Posts: 17
Tz0m is an unknown quantity at this point
Re: Storing functions to variables

Ok, I have everything setup properly now, I think. The only thing I need to do is assign the variable 'TILT_SERVO' to the variable I pass into my function, 'tiltS'. I am not sure where to declare my variable and how or where to set its value to 'TILT_SERVO'. I have attached the three files I have edited for my code.
Attached Files
File Type: c calcs.c (788 Bytes, 27 views)
File Type: h calcs.h (421 Bytes, 26 views)
File Type: c user_routines.c (17.6 KB, 21 views)
  #8   Spotlight this post!  
Unread 11-02-2007, 17:14
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: Storing functions to variables

when you call the function simply put TILT_SERVO in the parameter

i.e :
Code:
double range

void someFunction ( blah blah)
{
    range = DistanceCalc(TILT_SERVO);
}

or:
printf("The Distance is: %lf inches\n", DistanceCalc(TILT_SERVO));
if you want to assign your variable the value TILT_SERVO
just declare it (outside of the main loop, towards the beginning of the file under the #defines)
like this: double tiltS;
then insidde your main loop put this code:
tiltS = TILT_SERVO;
this is probably redundant and un-necessary since you already have TILT_SERVO holding the value of the camera tilt


also you may want to be wary of this line
Code:
tiltD = ((tiltS - 124) * 65) / 124;
you should change it to this:
Code:
tiltD = ((tiltS - 124) * 65) / 124.0;
Just to clarify that you do not in any case want to do integer division
__________________
Team 701

Last edited by Salik Syed : 11-02-2007 at 17:18.
  #9   Spotlight this post!  
Unread 18-02-2007, 11:36
Tz0m Tz0m is offline
Registered User
FRC #0333
Team Role: Programmer
 
Join Date: Feb 2006
Rookie Year: 2006
Location: New York
Posts: 17
Tz0m is an unknown quantity at this point
Re: Storing functions to variables

I do not want to do integer division, but I have to. The robot controller does not support floats or doubles. I have the function printing an int and dividing it by a number in order to get a proper answer in inches. If I use a float and type cast it to an int and divide by 100, the function prints a blank space.
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
Storing the balls Max Brin Technical Discussion 4 18-02-2006 20:34
Math.h and Functions and Variables amateurrobotguy Programming 1 26-02-2005 03:19
PDF of all functions/variables! mikesown Programming 1 01-02-2005 18:56
Storing Tetras on Robot MisterX General Forum 22 15-01-2005 19:26
SCORing vs. STORing Chris Fultz General Forum 3 15-01-2004 13:36


All times are GMT -5. The time now is 03:56.

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