Go to Post Give yourself the best chance to win at the event you're at by picking the best possible alliance partners available - always. - Rich Kressly [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 24-01-2004, 13:58
Ryan Cumings's Avatar
Ryan Cumings Ryan Cumings is offline
Programmer and University Rejected
#0134 (Team Discovery)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2000
Location: Pembroke, NH
Posts: 65
Ryan Cumings is an unknown quantity at this point
Send a message via AIM to Ryan Cumings
Global Constants

I was wondering how I could make a global constant for a trig lookup table.
  #2   Spotlight this post!  
Unread 24-01-2004, 16:28
Adam Shapiro's Avatar
Adam Shapiro Adam Shapiro is offline
Registered User
FRC #0555 (Montclair Robotics)
Team Role: Alumni
 
Join Date: Jan 2002
Rookie Year: 2001
Location: Montclair, NJ
Posts: 401
Adam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud of
Send a message via AIM to Adam Shapiro
Re: Global Constants

You can use a #define statement in a globally-included header file to create global constants. You can also use extern to define global variables.
__________________
Mentor to Teams 555, 1929, and 2070!
Currently working in hardware design at Cisco.
Cornell University DARPA Urban Challenge - http://www.cornellracing.com
Co-Captain Team 555 - 2003,2004,2005
Trust, Love, and Magic
  #3   Spotlight this post!  
Unread 07-02-2004, 10:51
Darkman_X000 Darkman_X000 is offline
Registered User
#0612
 
Join Date: Feb 2004
Location: Chantilly, VA
Posts: 8
Darkman_X000 is an unknown quantity at this point
Re: Global Constants

Quote:
Originally Posted by Adam Shapiro
You can use a #define statement in a globally-included header file to create global constants. You can also use extern to define global variables.
What exactly do you mean by "globally-included header file"? When you use #define to create a global constant, it does not have a type. Do you have to typecast it whenever you use it?

For example:
Code:
user_routines.c
#define axelWidth 1234
...
void fn(void)
{
...
x*=axelWidth;      //Can it be this?
x*=(int)axelWidth; //Or must it be this?
x=x*(axelWidth);   //This seems to be the only form that the compiler likes
...
}
...
In C++ you can declare global constants using const varType varName = initialValue; Is this acceptable in C?

Please clarify!!!
  #4   Spotlight this post!  
Unread 07-02-2004, 11:40
Unsung FIRST Hero
Mike Betts Mike Betts is offline
Electrical Engineer
no team
Team Role: Engineer
 
Join Date: Dec 2001
Rookie Year: 1995
Location: Homosassa, FL
Posts: 1,442
Mike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond repute
Re: Global Constants

Quote:
Originally Posted by Darkman_X000
In C++ you can declare global constants using const varType varName = initialValue; Is this acceptable in C?

Please clarify!!!
Please read the pdf document MPLAB® C18 C COMPILER USER’S GUIDE which was included on the CDROM that had MPLAB. All your questions will be answered.

Beware comparing C++ with C. C has been around since 1974 and has changed very little in all these years. ANSI complient C is a standard and C++ is a different animal.
__________________
Mike Betts

Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013

As easy as 355/113...
  #5   Spotlight this post!  
Unread 08-02-2004, 11:34
Adam Shapiro's Avatar
Adam Shapiro Adam Shapiro is offline
Registered User
FRC #0555 (Montclair Robotics)
Team Role: Alumni
 
Join Date: Jan 2002
Rookie Year: 2001
Location: Montclair, NJ
Posts: 401
Adam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud of
Send a message via AIM to Adam Shapiro
Re: Global Constants

Quote:
Originally Posted by Darkman_X000
What exactly do you mean by "globally-included header file"? When you use #define to create a global constant, it does not have a type. Do you have to typecast it whenever you use it?
I meant that you can use a #define statement in a header (for example "globals.h") that is included in any file that needs that variable. When you use a #define statement the variable name that you create will be replaced later (not sure of which but either by the compiler or the linker..probably compiler). Hence, the variable that you create with #define would be constant, you can not modify it. You can, however define variables using extern which would be globally accessable (see 'Global Variables, anyone?').

Quote:
Originally Posted by Darkman_X000
In C++ you can declare global constants using const varType varName = initialValue; Is this acceptable in C?
Unfortunately I am more of a C++ programmer, myself, so I do not really know an answer to this one. Perhaps somebody else can field this question?'Global Variables, anyone?'
__________________
Mentor to Teams 555, 1929, and 2070!
Currently working in hardware design at Cisco.
Cornell University DARPA Urban Challenge - http://www.cornellracing.com
Co-Captain Team 555 - 2003,2004,2005
Trust, Love, and Magic

Last edited by Adam Shapiro : 08-02-2004 at 11:38.
  #6   Spotlight this post!  
Unread 08-02-2004, 19:05
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: Global Constants

If it is in a header, then you can is a const in any file that includes the header
  #7   Spotlight this post!  
Unread 24-01-2004, 16:44
Ryan Cumings's Avatar
Ryan Cumings Ryan Cumings is offline
Programmer and University Rejected
#0134 (Team Discovery)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2000
Location: Pembroke, NH
Posts: 65
Ryan Cumings is an unknown quantity at this point
Send a message via AIM to Ryan Cumings
Re: Global Constants

When I use extern would the variable be placed in user memory? I have 2k of data for the lookup tables in the trig (this is very little for the 32k of rom space available) and don't want to put these non changing variables in the ram. A define doesn't help because this is an actual table containing data not just one macro that can be quickly changed.
  #8   Spotlight this post!  
Unread 24-01-2004, 16:49
Adam Shapiro's Avatar
Adam Shapiro Adam Shapiro is offline
Registered User
FRC #0555 (Montclair Robotics)
Team Role: Alumni
 
Join Date: Jan 2002
Rookie Year: 2001
Location: Montclair, NJ
Posts: 401
Adam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud ofAdam Shapiro has much to be proud of
Send a message via AIM to Adam Shapiro
Re: Global Constants

Unfortunately the extern will be placed in the user memory so, if there is no other way, you might have to try to minimize your table .
__________________
Mentor to Teams 555, 1929, and 2070!
Currently working in hardware design at Cisco.
Cornell University DARPA Urban Challenge - http://www.cornellracing.com
Co-Captain Team 555 - 2003,2004,2005
Trust, Love, and Magic
  #9   Spotlight this post!  
Unread 24-01-2004, 17:08
doy's Avatar
doy doy is offline
#1208 Ex-Lead Programmer
AKA: Jesse Luehrs
#1208 (The Super Critical Axe Men)
Team Role: Alumni
 
Join Date: Jan 2004
Location: O'Fallon, IL
Posts: 27
doy is on a distinguished road
Send a message via AIM to doy
Re: Global Constants

couldnt you declare the table rom? look in the c18 compiler reference guide, i think you could just declare
Code:
const rom int trig_lookup[] = {/*info here*/}
and it will put it in rom... you should probably check on that though.
  #10   Spotlight this post!  
Unread 24-01-2004, 17:28
Ryan Cumings's Avatar
Ryan Cumings Ryan Cumings is offline
Programmer and University Rejected
#0134 (Team Discovery)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2000
Location: Pembroke, NH
Posts: 65
Ryan Cumings is an unknown quantity at this point
Send a message via AIM to Ryan Cumings
Re: Global Constants

Ahhh thank you, it appears that should work out well, now the table won't have to be minimized. Seeings as how I don't have the C Reference available and am not able to attend meetings for now due to Academic problems, would you declare this in the header or the C file?
  #11   Spotlight this post!  
Unread 24-01-2004, 17:41
doy's Avatar
doy doy is offline
#1208 Ex-Lead Programmer
AKA: Jesse Luehrs
#1208 (The Super Critical Axe Men)
Team Role: Alumni
 
Join Date: Jan 2004
Location: O'Fallon, IL
Posts: 27
doy is on a distinguished road
Send a message via AIM to doy
Re: Global Constants

you would declare it and define it without extern in one .c file, and declare it extern in all other .c files that you want to use it in.
  #12   Spotlight this post!  
Unread 24-01-2004, 17:42
Ryan Cumings's Avatar
Ryan Cumings Ryan Cumings is offline
Programmer and University Rejected
#0134 (Team Discovery)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2000
Location: Pembroke, NH
Posts: 65
Ryan Cumings is an unknown quantity at this point
Send a message via AIM to Ryan Cumings
Re: Global Constants

Thank you for your time.
  #13   Spotlight this post!  
Unread 26-01-2004, 20:48
Guest
 
Posts: n/a
Re: Global Constants

deltacoder already wrote some excellent trig lookup code:

Check the repository: http://nrg.chaosnet.org/repository/i...ral%20C%20Code
  #14   Spotlight this post!  
Unread 26-01-2004, 21:02
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
Re: Global Constants

note - the code in the repository does not currently utilize the "rom" keyword - it's meant to be compiler/platform independent. I would recommend the addition of the rom and const keywords to the definition of the table array, like so:

const rom unsigned char SIN_LOOKUP[90]
__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)
  #15   Spotlight this post!  
Unread 26-01-2004, 21:08
Ryan Cumings's Avatar
Ryan Cumings Ryan Cumings is offline
Programmer and University Rejected
#0134 (Team Discovery)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2000
Location: Pembroke, NH
Posts: 65
Ryan Cumings is an unknown quantity at this point
Send a message via AIM to Ryan Cumings
Re: Global Constants

I think my trig code will run fine,
It has a lookup table with 1440 16-bit values and my sine and cosine functions take very little hit to performance. The angle I use is also in 16-bit straight from the gyro which makes this whole thing very high precision which will be increadably useful for the new drive style (Arcade) I developed and hopefully will get to test Wednesday.

The reason for so many values is that we have 32k of rom sitting there doing nothing. Infact it would be a much more efficient way to calculate out 8-bit values for your angles and multiply it by a 8-bit number for your hypontenuse then shift right by 8. That essentially does floating point for you without the performance hit. Doing it with 16-bits can be a bit trickier but the same principle applies.
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
Slightly humorous story about global warming & space explor. archiver 2001 3 24-06-2002 04:01
'IT' info... archiver 2001 19 24-06-2002 01:58


All times are GMT -5. The time now is 22:23.

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