OCCRA
Go to Post The pie is as large as you make it. - boomergeek [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Events   CD-Media   CD-Spy   FRC-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-29-2005, 02:10 PM
DanDon's Avatar
DanDon DanDon is offline
ohhh MY god
AKA: Dan Hoizner
FRC #0375 (The Robotic Plague)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Staten Island, NY
Posts: 1,432
DanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond repute
Send a message via ICQ to DanDon Send a message via AIM to DanDon Send a message via MSN to DanDon
#define and #ifdef/endif

If I have code that says (example only)
Quote:
if(rc_dig_in01==1)
#define PROG1
else if(rc_dig_in02==1)
#define PROG2

#ifdef PROG1
printf("Hello, world.");
#endif
#ifdef PROG2
printf("FIRST Robotics");
#endif
Will the printf's change when i flip either of the switches? Basically, my question is if code between #ifdef/#endif is loaded onto the robot, and whether or not variables can be defined on the fly, based on switch input.

Thank you in advance,
__________________
  #2   Spotlight this post!  
Unread 04-29-2005, 02:20 PM
Chris Hibner's Avatar Unsung FIRST Hero
Chris Hibner Chris Hibner is offline
Eschewing Obfuscation Since 1990
AKA: Lars Kamen's Roadie
FRC #0051 (Wings of Fire)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1997
Location: Canton, MI
Posts: 1,404
Chris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond reputeChris Hibner has a reputation beyond repute
Re: #define and #ifdef/endif

That should not work. #define statements, #ifdef, #endif, and anything starting with the # symbol are preprocessor directives. Therefore, they are not runtime code - they just tell the compiler how to compile. They do nothing at run time.
__________________
-
An ounce of perception is worth a pound of obscure.
  #3   Spotlight this post!  
Unread 04-29-2005, 02:23 PM
devicenull devicenull is offline
Robot? We need a robot?
AKA: Brian Rak
None #0230 (Gaelhawks)
Team Role: Programmer
 
Join Date: Sep 2004
Location: Shelton, CT
Posts: 359
devicenull is just really nicedevicenull is just really nicedevicenull is just really nicedevicenull is just really nicedevicenull is just really nice
Re: #define and #ifdef/endif

Code:
#ifdef PROG1
printf("Hello, world.");
#endif
#ifdef PROG2
printf("FIRST Robotics");
#endif

If we #define PROG1, then it puts the "Hello, world" printf in.
If we #define PROG2, then it puts the "FIRST Robotics" printf in.

I used these for a few sensor values that differed from our practice to competition robot.. I'll post the code we used when I get a chance.
__________________


Quote:
Originally Posted by GDC
If the RC is mounted in such a way that field personnel must be contorted, bent, folded, spindled or mutilated in order to see the RC, then the RC must be relocated.
  #4   Spotlight this post!  
Unread 04-29-2005, 02:27 PM
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 570
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: #define and #ifdef/endif

...Chris posted while I was typing this....

Short answer is that #define and #if switches are processed before the compiler ever sees the code. Therefore you can't do what you are trying to do. You'd be better off storing the program number into a variable and deciding what to do based on that
Code:
enum
{
  PROG_DO_NOTHING = 0,
  PROG1,
  PROG2
};
static int g_prog_num = PROG_DO_NOTHING;
....
if(rc_dig_in01==1)
{
  g_prog_num = PROG1;
}
else if(rc_dig_in02==1)
{
  g_prog_num = PROG2;
}

switch(g_prog_num)
{
  case PROG1:
    printf("Hello, world.");
    break;
  case PROG2:
    printf("FIRST Robotics");
    break;
  default:
    printf("DO NOTHING");
    break;
}
  #5   Spotlight this post!  
Unread 04-29-2005, 02:30 PM
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 570
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: #define and #ifdef/endif

Quote:
Originally Posted by devicenull
I used these for a few sensor values that differed from our practice to competition robot.
We did this as well. This is a pretty common practice.

However, the toggling of your #defines happens before you compile, not at runtime like it looks like Dan was suggesting.
  #6   Spotlight this post!  
Unread 04-29-2005, 03:48 PM
DanDon's Avatar
DanDon DanDon is offline
ohhh MY god
AKA: Dan Hoizner
FRC #0375 (The Robotic Plague)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Staten Island, NY
Posts: 1,432
DanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond repute
Send a message via ICQ to DanDon Send a message via AIM to DanDon Send a message via MSN to DanDon
Re: #define and #ifdef/endif

Quote:
Originally Posted by Dave Scheck
...Chris posted while I was typing this....

Short answer is that #define and #if switches are processed before the compiler ever sees the code. Therefore you can't do what you are trying to do. You'd be better off storing the program number into a variable and deciding what to do based on that
Code:
enum
{
PROG_DO_NOTHING = 0,
PROG1,
PROG2
};
static int g_prog_num = PROG_DO_NOTHING;
....
if(rc_dig_in01==1)
{
g_prog_num = PROG1;
}
else if(rc_dig_in02==1)
{
g_prog_num = PROG2;
}
 
switch(g_prog_num)
{
case PROG1:
printf("Hello, world.");
break;
case PROG2:
printf("FIRST Robotics");
break;
default:
printf("DO NOTHING");
break;
}
Thank you very much for all the replies.

Dave, I am unfamiliar with the 'enum' command, what does it do? Assign values to the identities inside, ascending from the value assigned to the first identity?

Again, thank you very much,
__________________
  #7   Spotlight this post!  
Unread 04-29-2005, 04:27 PM
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 570
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: #define and #ifdef/endif

Quote:
Originally Posted by dhoizner
Dave, I am unfamiliar with the 'enum' command, what does it do? Assign values to the identities inside, ascending from the value assigned to the first identity?
The enum represents an "enumeration" or "enumerated list". Your interpretation is correct. It creates a list of constants to be used later in your code. The constants are numbered by adding one to the previous value. You can also change the numbering scheme whenever you want to by doing the following
Code:
//This is equivalent to
#define STEP_A  0
#define STEP_B  (STEP_A + 1)
#define STEP_C  5
#define STEP_D  (STEP_C+1)

enum
{
  STEP_A = 0,
  STEP_B,
  STEP_C = 5,
  STEP_D,
}
One of the benefits is that you don't have to define the values for the constants as they will autoincrement. If used properly, you won't have to worry about accidentally reusing numbers. You will also be able to renumber the constants simply by rearranging them in the enum structure. Here's an example...


Say that you have 3 steps to a process defined as #defines
Code:
#define STEP_A  1
#define STEP_B  2
#define STEP_C  3
You have your constants and they work just fine. During testing, you realize that you need to add STEP_D before STEP_C. You make the following change to your code
Code:
#define STEP_A  1
#define STEP_B  2
#define STEP_C  3
#define STEP_D  4
While there is nothing wrong with this, your constants don't reflect the fact that STEP_D occurs before STEP_C.

If your original code was written with enums
Code:
enum
{
  STEP_A = 1,
  STEP_B,
  STEP_C
};
You could easilly add STEP_D and have the step order make sense
Code:
enum
{
  STEP_A = 1,
  STEP_B,
  STEP_D,
  STEP_C
};
Unlike a #define, these constants will reside in the memory space of your program, as opposed to being substituded in pre-compilation. This may be a downside if you have limited memory or speed.

In the context that I used in my original response, #defines would have been just fine, I just chose to go with the enum because I wasn't too concerned with the values that the constants took on....I was more interested in having unique constants.

You can find some more info about enums here
  #8   Spotlight this post!  
Unread 04-29-2005, 04:34 PM
DanDon's Avatar
DanDon DanDon is offline
ohhh MY god
AKA: Dan Hoizner
FRC #0375 (The Robotic Plague)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Staten Island, NY
Posts: 1,432
DanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond repute
Send a message via ICQ to DanDon Send a message via AIM to DanDon Send a message via MSN to DanDon
Re: #define and #ifdef/endif

Ok, now, I have another question. Can I use 'enum' in a header, and just include that header in all files that I need the constants declared in enum?

Thank you,

Dan
__________________
  #9   Spotlight this post!  
Unread 04-29-2005, 04:48 PM
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 570
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: #define and #ifdef/endif

Yup, we do it all the time. Remember that when you do a #include that it is the same thing as copy/pasting the included file into your current file.

One thing to be careful of is that enums have scope, so you will want to be careful with the way you name your constants.

If you ran the following
Code:
enum
{
  A=1,
  B
};

void my_function(void)
{
   enum
   {
      B = 4
   }
   printf("A: %d  B: %d\n",A,B);
}

void main(void)
{
     printf("A: %d  B: %d\n",A,B);
     my_function();
     printf("A: %d  B: %d\n",A,B);
}
Your output would be
Code:
A: 1 B: 2
A: 1 B: 4
A: 1 B: 2
  #10   Spotlight this post!  
Unread 04-29-2005, 04:59 PM
DanDon's Avatar
DanDon DanDon is offline
ohhh MY god
AKA: Dan Hoizner
FRC #0375 (The Robotic Plague)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Staten Island, NY
Posts: 1,432
DanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond repute
Send a message via ICQ to DanDon Send a message via AIM to DanDon Send a message via MSN to DanDon
Re: #define and #ifdef/endif

When using the scripting/navigation code provided by Kevin Watson, u would call a switch statement with the following parameter:
Code:
command_list[current_command].command
I want to have different scripts.

Code:
 int check_switch_box(void) 
{
if(rc_dig_in01==1) 
return command_list_1[current_command].command;
}
So theoretically, i could call
Code:
switch(check_switch_box()){}
instead of
Code:
switch(command_list[current_command].command){}
?

That should work, right? Since command_list[current_command].command really is just a value inside a struct.
__________________
  #11   Spotlight this post!  
Unread 04-29-2005, 05:35 PM
tckma's Avatar
tckma tckma is offline
Registered User
AKA: Tom C.
FRC #2849 (Ursa Major)
Team Role: Mentor
 
Join Date: Nov 2004
Rookie Year: 2002
Location: Ellicott City, MD
Posts: 90
tckma has a spectacular aura abouttckma has a spectacular aura abouttckma has a spectacular aura about
Send a message via ICQ to tckma Send a message via AIM to tckma Send a message via Yahoo to tckma
Re: #define and #ifdef/endif

Quote:
Originally Posted by dhoizner
If I have code that says (example only)

Will the printf's change when i flip either of the switches? Basically, my question is if code between #ifdef/#endif is loaded onto the robot, and whether or not variables can be defined on the fly, based on switch input.

Thank you in advance,
That won't work. #define is a preprocessor directive, i.e. it won't work during run-time. A better thing to do would be to just do this directly:

if(rc_dig_in01==1)
{
printf("Hello, world.\r");
}
else if (rc_dig_in02==1)
{
printf("FIRST Robotics\r");
}
__________________
Tom C.
Software and Controls Mentor
Team 2849
Prior team associations: 1418 ('08-'09), 1100 ('05-'08), 811 ('02, '04-'05), 246 ('03)

  #12   Spotlight this post!  
Unread 04-29-2005, 06:03 PM
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 570
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: #define and #ifdef/endif

Quote:
Originally Posted by dhoizner
So theoretically, i could call
Code:
switch(check_switch_box()){}
instead of
Code:
switch(command_list[current_command].command){}
?
I think that would work as long as check_switch_box can see current_command. Depending on where it is defined you may need an extern.

It may be a little cleaner to do it in two steps
Code:
new_cmd = check_switch_box();
switch(new_cmd)
That way you have a chance to sneak in and add debug for the return value of your function.

An even cleaner way to do it would be to get your program number up front and then use pass that in to your function.
Code:
//Outside of autonomous loop....
if(rc_dig_in01==1)
{
  g_prog_num = 1;
}
else if(rc_dig_in02==1)
{
  g_prog_num = 2;
}
...
//Inside autonomous loop....
//Assuming that the function can see current_command
new_cmd = get_auton_command(g_prog_num);
switch(new_cmd)
{
.....
}
...
// Using unsigned ints for the sake of argument
unsigned int get_auton_command(unsigned int prog_num)
{
  unsigned int command = DO_NOTHING;
  if(prog_num == 1)
  {
    command = command_list_prog1[current_command].command
  }
  else if(prog_num == 2)
  {
    command = command_list_prog2[current_command].command
  }
  return command;
}
  #13   Spotlight this post!  
Unread 04-29-2005, 06:27 PM
DanDon's Avatar
DanDon DanDon is offline
ohhh MY god
AKA: Dan Hoizner
FRC #0375 (The Robotic Plague)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Staten Island, NY
Posts: 1,432
DanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond reputeDanDon has a reputation beyond repute
Send a message via ICQ to DanDon Send a message via AIM to DanDon Send a message via MSN to DanDon
Re: #define and #ifdef/endif

Quote:
Originally Posted by Dave Scheck
I think that would work as long as check_switch_box can see current_command. Depending on where it is defined you may need an extern.

It may be a little cleaner to do it in two steps
Code:
new_cmd = check_switch_box();
switch(new_cmd)
That way you have a chance to sneak in and add debug for the return value of your function.

An even cleaner way to do it would be to get your program number up front and then use pass that in to your function.
Code:
//Outside of autonomous loop....
if(rc_dig_in01==1)
{
g_prog_num = 1;
}
else if(rc_dig_in02==1)
{
g_prog_num = 2;
}
...
//Inside autonomous loop....
//Assuming that the function can see current_command
new_cmd = get_auton_command(g_prog_num);
switch(new_cmd)
{
.....
}
...
// Using unsigned ints for the sake of argument
unsigned int get_auton_command(unsigned int prog_num)
{
unsigned int command = DO_NOTHING;
if(prog_num == 1)
{
command = command_list_prog1[current_command].command
}
else if(prog_num == 2)
{
command = command_list_prog2[current_command].command
}
return command;
}
Thanks Dave. The function robot_command() is in robot.c. That is the function that is called from user_routines_fast. Due to that fact, I am putting check_switch_box() in robot.c, so since current_command is a global declared in robot.c, check_switch_box should be able to see it, correct?

Thanks again,
__________________
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


All times are GMT -5. The time now is 07:58 AM.

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


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