Go to Post Often heard exchange in the shop: "The physics don't allow that." "But it's so cool!" - MBF [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 05-01-2007, 01:12
Generalx5's Avatar
Generalx5 Generalx5 is offline
Hard Core Inventor
AKA: Jun(John) Zheng
FRC #1346 (Trobotics)
Team Role: Student
 
Join Date: Nov 2005
Rookie Year: 2004
Location: CANADA
Posts: 94
Generalx5 will become famous soon enough
Talking Why it works with an underscore?

I have a code written up for my own custom encoder involving optical sensors, yes its a optical encoder, using quadrature.

My question is why the compiler will find a syntax error if I didn't use an underscore in my variables.

For instance, I have this before:

-------------------------------------------------------------
/*** DEFINE USER VARIABLES AND INITIALIZE THEM HERE ***/

unsigned char Sen1;
unsigned char Sen2;
unsigned char Sen3;
unsigned char Sen4;
unsigned char Sen5;


-------------------------------------------------------------
along with

-------------------------------------------------------------
void Process_Data_From_Local_IO(void)
{

Sen1 = rc_dig_in05;
Sen2 = rc_dig_in06;
Sen3 = rc_dig_in07;
Sen4 = rc_dig_in08;
Sen5 = rc_dig_in09;


after I changed it to:

-------------------------------------------------------------
/*** DEFINE USER VARIABLES AND INITIALIZE THEM HERE ***/

unsigned char Sen_1;
unsigned char Sen_2;
unsigned char Sen_3;
unsigned char Sen_4;
unsigned char Sen_5;

-------------------------------------------------------------
void Process_Data_From_Local_IO(void)
{

Sen_1 = rc_dig_in05;
Sen_2 = rc_dig_in06;
Sen_3 = rc_dig_in07;
Sen_4 = rc_dig_in08;
Sen_5 = rc_dig_in09;


-------------------------------------------------------------
Im new at programming so im open to harassment.

************************************************** ***************
Another question is how do you set a value under certan conditions in programming?

For instance, I have 5 sensors like the above. When all 5 sensors reads a certain encoding for example all 0's how do I set this reading to a specific digit, so I can later call up on again. This makes it easier for changing later due to so many sensor reading combinations.

The idea im getting at is something like:

if (rc_dig_in01 = 0,rc_dig_in02 = 0,rc_dig_in03 = 0,rc_dig_in04 = 0,rc_dig_in05 = 0) == 10
if (rc_dig_in01 = 0,rc_dig_in02 = 0,rc_dig_in03 = 0,rc_dig_in14 = 0,rc_dig_in15 = 1) == 20

and after all these are set, I can have another routine that can use those numbers:

if (20) turn on solenoid 3 etc etc.

Last edited by Generalx5 : 05-01-2007 at 01:37. Reason: Added the second part under the * line
  #2   Spotlight this post!  
Unread 05-01-2007, 01:17
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: Why it works with an underscore?

Welcome to the quirkiness of the MCC18 compiler!

The compiler is known to choke for no apparent reason. Personally, I think it's a catch-all. Something goes wrong and it tosses a syntax error.
  #3   Spotlight this post!  
Unread 05-01-2007, 02:48
Stuart's Avatar
Stuart Stuart is offline
#include coffee.h
FRC #1745 (P51- Mustangs)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2004
Location: Dallas, TX
Posts: 414
Stuart has a reputation beyond reputeStuart has a reputation beyond reputeStuart has a reputation beyond reputeStuart has a reputation beyond reputeStuart has a reputation beyond reputeStuart has a reputation beyond reputeStuart has a reputation beyond reputeStuart has a reputation beyond reputeStuart has a reputation beyond reputeStuart has a reputation beyond reputeStuart has a reputation beyond repute
Send a message via AIM to Stuart
Re: Why it works with an underscore?

#define is your best friend in MPLAB . . or at least your cool kinda crazy uncle

as in
#define optical1 rc_dig_in05

as for your if statements commas are wrong you want ANDs(&&)

so its if(blah == 0 && other == 0 && batman == 0)

ohh also remember that = is assignment and == is compare
__________________
Proud mentor of Team #1745 the P-51 Mustangs

If at first it doesn't work, use a hammer.
If that doesn't work, use a bigger hammer.

Last edited by Stuart : 05-01-2007 at 02:50.
  #4   Spotlight this post!  
Unread 05-01-2007, 10:29
Donut Donut is offline
The Arizona Mentor
AKA: Andrew
FRC #2662 (RoboKrew)
Team Role: Engineer
 
Join Date: Mar 2005
Rookie Year: 2004
Location: Goodyear, AZ
Posts: 1,287
Donut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond reputeDonut has a reputation beyond repute
Re: Why it works with an underscore?

Quote:
Originally Posted by Stuart View Post
ohh also remember that = is assignment and == is compare
Remember this or you will have an infinite number of problems everytime you load code to your robot.

Nothing hurts more than to discover that a single = in an if statement actual assigns that value to that variable, rather than comparing it.

I don't know why the underscore thing would be an issue; I know MPLAB tends to blow up on variables with spaces, but single words have always worked fine for me.

It might have something to do with the fact you're using numbers in the name; you might need so many letters in your variable before you can use a number.
__________________
FRC Team 498 (Peoria, AZ), Student: 2004 - 2007
FRC Team 498 (Peoria, AZ), Mentor: 2008 - 2011
FRC Team 167 (Iowa City, IA), Mentor: 2012 - 2014
FRC Team 2662 (Tolleson, AZ), Mentor: 2014 - Present
  #5   Spotlight this post!  
Unread 05-01-2007, 11:07
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: Why it works with an underscore?

Quote:
Originally Posted by Stuart View Post
#define is your best friend in MPLAB . . or at least your cool kinda crazy uncle

as in
#define optical1 rc_dig_in05
To expand on this, there are many good reasons you should do it this way.

What a #define does is essentially does a find-replace on your code before it actually compiles, replacing all of 'optical1' (in this example) with 'rc_dig_in05'. It makes your code more readable because reading 'optical1' makes more sense than 'rc_dig_in05'. It uses no extra memory because you're still ACTUALLY using the already-declared rc_dig_in05 variable instead of making a new one.

Further, if you #define all your stuff in the same place, it makes for a VERY useful section of code that definitively tells you what all your outputs/inputs are. Before I started using #defines to rename all my inputs/outputs, I dreaded the day when someone would ask "hey Bongle, what does the first optical sensor plug into?". You might answer something, then find out during a match you were wrong. This way, you just go to the .h where you #defined everything and say "oh, optical1 is defined to rc_dig_in05, so it goes on digital input 5".

Finally, doing it that way might get around the silly MCC18 error you have right now

As an example in real life, here's our #define section from 2006. We put it all in ifi_aliases.h at the bottom.
Code:
#define turretpan pwm04 // turret left/right motion
#define leftdrive1 pwm05
#define leftdrive2 pwm06
#define rightdrive1 pwm07 // drive motors
#define rightdrive2 pwm08 
#define turrettilt pwm09 // turret up/down
#define shooter pwm10 // shooter motor

#define potentInput rc_ana_in03 // potentiometer input
#define gyroInput rc_ana_in01 // gyro input
#define firingPin relay2_fwd  // the motor that drove a ball into the shooter wheels
#define limitSwitch rc_dig_in01 // limit switch that detected when the ball-pushing motor had come around

Last edited by Bongle : 05-01-2007 at 11:10.
  #6   Spotlight this post!  
Unread 05-01-2007, 11:26
mluckham's Avatar
mluckham mluckham is offline
Registered User
FRC #0758 (Sky Robotics)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2006
Location: Ontario, Canada
Posts: 116
mluckham will become famous soon enoughmluckham will become famous soon enough
Re: Why it works with an underscore?

Quote:
SenX versus Sen_X
I would search your project to see if variables like SenX are already defined. While it is certainly possible for compilers to have bugs, your code is always the first thing to suspect.


Quote:
if (rc_dig_in01 = 0,rc_dig_in02 = 0,rc_dig_in03 = 0,rc_dig_in04 = 0,rc_dig_in05 = 0) == 10
I suggest you write this as:

if ((rc_dig_in01 == 0) && (rc_dig_in02 == 0) && (rc_dig_in03 == 0) && (rc_dig_in04 == 0) && (rc_dig_in05 == 0))
{
outputvariable = 10;
}

This follows the C syntax rule:

if (expression) statement; (SINGLE statement)
if (expression) { statement; statement; ... } (BLOCK of statements)


There are other ways of writing this as well, but unless you are very experienced with C and the rules of operator precedence, liberal use of round brackets around each sub-expression will guarantee the multiple statements are evaluated in the manner and the order you expect.

As Stuart said, it is a very common error to use if (rc_dig_01 = 0) rather than (rc_dig_01 == 0). The first form ASSIGNS the value 0 to rc_dig_01, the second COMPARES rc_dig_01 with 0.
  #7   Spotlight this post!  
Unread 05-01-2007, 14:07
Generalx5's Avatar
Generalx5 Generalx5 is offline
Hard Core Inventor
AKA: Jun(John) Zheng
FRC #1346 (Trobotics)
Team Role: Student
 
Join Date: Nov 2005
Rookie Year: 2004
Location: CANADA
Posts: 94
Generalx5 will become famous soon enough
Thumbs up Re: Why it works with an underscore?

Ok, I see where my mistakes are, thanks a lot for all the help =D. I'll go edit my code right now.
  #8   Spotlight this post!  
Unread 05-01-2007, 18:30
Generalx5's Avatar
Generalx5 Generalx5 is offline
Hard Core Inventor
AKA: Jun(John) Zheng
FRC #1346 (Trobotics)
Team Role: Student
 
Join Date: Nov 2005
Rookie Year: 2004
Location: CANADA
Posts: 94
Generalx5 will become famous soon enough
Question Re: Why it works with an underscore?

Do I #define a name that I want to use and then in the routines use them there?

Can I:

#define *Sensor_variable read* results in turning on solenoid 1 and 4 and 5


then later if this happens:

if ((Sen_1 == 1) && (Sen_2 == 1) && (Sen_3 == 1) && (Sen_4 == 1) && (Sen_5 == 1))
{
Sensor_variable read;
}


This way I can activate certain solenoids when my 5 sensors reads all 1's

its soooo confusing...still trying to make the encoder work.
  #9   Spotlight this post!  
Unread 05-01-2007, 20:19
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: Why it works with an underscore?

#defines are very simple.

At their simplest (you can do a lot of wierd stuff with them, but that's for another day), you have:

#define <text to replace> <text to replace with>

So if you did
#define *Sensor_variable read*

Then anytime you typed "*Sensor_variable", it would replace it with "read*". Unless you didn't mean the asterisks to be there, then it would do the same thing without the asterisks.

This means you can do things like
#define test pwm01;

The preprocessor (thing that processes everything that starts with #) doesn't care WHAT you put after the first argument to a define, it will go to the end of the line and replace all instances of "test" with "pwm01;". Note that the semicolon is in there. That means that if you wrote

test = 5;

Then after the preprocessor was done, it'd look like

pwm01; = 5; // compile error woo-hoo

because it replaced "test" with "pwm01;". So be careful of what you put as the second argument to #define, or else it might not make sense.

For a good example of #defines, take a look at the given code in ifi_aliases.h. The entire file is IFI just re-defining big ugly expressions as easy-to-use things like pwm01, pwm02, rc_dig_in01, etc. You just need to do the same thing.

Last edited by Bongle : 05-01-2007 at 20:23.
  #10   Spotlight this post!  
Unread 05-01-2007, 23:27
Generalx5's Avatar
Generalx5 Generalx5 is offline
Hard Core Inventor
AKA: Jun(John) Zheng
FRC #1346 (Trobotics)
Team Role: Student
 
Join Date: Nov 2005
Rookie Year: 2004
Location: CANADA
Posts: 94
Generalx5 will become famous soon enough
Lightbulb Re: Why it works with an underscore?

Is it possible to define something and have a set of commands built into it?

Something like this?

#define Setting_1 ((solenoid1 = 1) && (Solenoid2 = 0) && (solenoid3 = 1) && (solenoid4 = 0) && (solenoid5 = 0) && (solenoid6 = 0));

will this allow me to do....

if (Sensors_are_online);
{
Setting_1
}


basically if the sensor receives a signal, it would turn on setting_1 which is to turn solenoid 1 and solenoid 3 on.
  #11   Spotlight this post!  
Unread 06-01-2007, 00:14
prograid's Avatar
prograid prograid is offline
Registered User
AKA: Ben Cherian
FRC #0254 (The Cheesy Poofs)
Team Role: Alumni
 
Join Date: Oct 2004
Rookie Year: 2004
Location: San Jose
Posts: 80
prograid will become famous soon enough
Re: Why it works with an underscore?

Quote:
Originally Posted by Generalx5 View Post
Is it possible to define something and have a set of commands built into it?

Something like this?

#define Setting_1 ((solenoid1 = 1) && (Solenoid2 = 0) && (solenoid3 = 1) && (solenoid4 = 0) && (solenoid5 = 0) && (solenoid6 = 0));

will this allow me to do....

if (Sensors_are_online);
{
Setting_1
}


basically if the sensor receives a signal, it would turn on setting_1 which is to turn solenoid 1 and solenoid 3 on.
Yes, #define basically does a find & replace. However, it is a good practice to not have a semicolon at the end of a macro, that way, when you use the macro, it has a semicolon at the end of the line and keeps you from wondering whether you are missing any semicolons.

By the way, you don't need a semicolon after the if (Sensors_are_online) statement.

In general, any sort of construct that uses curly braces does not have a semicolon for itself. Rather, the individual statements within the braces have semicolons at the end of them.
  #12   Spotlight this post!  
Unread 06-01-2007, 00:18
Generalx5's Avatar
Generalx5 Generalx5 is offline
Hard Core Inventor
AKA: Jun(John) Zheng
FRC #1346 (Trobotics)
Team Role: Student
 
Join Date: Nov 2005
Rookie Year: 2004
Location: CANADA
Posts: 94
Generalx5 will become famous soon enough
Re: Why it works with an underscore?

NVM that last one I got it figured out, it was just a simple ";" mistake, I added an extra ; at the end of the text to replace.

Alright heres the new problem...

I have a bunch of if statements and they rely on a bunch of sensors,

when these sensors read a certain set of digital values it would turn on certain solenoids, correct, but! if these digital values from the sensors changed (knowing this from printf), the solenoids stay the same. They do not change with the input so I'm guessing that I'm doing something wrong with the if statements.

I have a lot of if's and in the very bottom on all the if statements, there would be an else if statement to eliminate any other undefined sensor readings.

any suggestions to what I'm doing wrong?

************************************************** ************

Ah! yes, I found the mistakes there, I've been reading the powerpoint tutorials from IFI and it got me confused. I start putting ; and brackets all over the place....I'll remember it for next time.

Last edited by Generalx5 : 06-01-2007 at 00:48. Reason: Added part under the * line.
  #13   Spotlight this post!  
Unread 06-01-2007, 03:59
John Gutmann John Gutmann is offline
I'm right here
AKA: sparksandtabs
FRC #0340 (GRR)
Team Role: Mechanical
 
Join Date: Feb 2005
Rookie Year: 2004
Location: rochester
Posts: 804
John Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant future
Send a message via AIM to John Gutmann Send a message via MSN to John Gutmann Send a message via Yahoo to John Gutmann
Re: Why it works with an underscore?

You can also use #define in your header files

#ifndef header_h__
#define header_h_

stuff stuff stuff

#endif

Then if you put #include "header.h" more then once it will say hey! header_h__ is already defined stupid! you can't define it again!! and just exit........
  #14   Spotlight this post!  
Unread 06-01-2007, 04:01
John Gutmann John Gutmann is offline
I'm right here
AKA: sparksandtabs
FRC #0340 (GRR)
Team Role: Mechanical
 
Join Date: Feb 2005
Rookie Year: 2004
Location: rochester
Posts: 804
John Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant futureJohn Gutmann has a brilliant future
Send a message via AIM to John Gutmann Send a message via MSN to John Gutmann Send a message via Yahoo to John Gutmann
Re: Why it works with an underscore?

Quote:
Originally Posted by Generalx5 View Post
NVM that last one I got it figured out, it was just a simple ";" mistake, I added an extra ; at the end of the text to replace.

Alright heres the new problem...

I have a bunch of if statements and they rely on a bunch of sensors,

when these sensors read a certain set of digital values it would turn on certain solenoids, correct, but! if these digital values from the sensors changed (knowing this from printf), the solenoids stay the same. They do not change with the input so I'm guessing that I'm doing something wrong with the if statements.

I have a lot of if's and in the very bottom on all the if statements, there would be an else if statement to eliminate any other undefined sensor readings.

any suggestions to what I'm doing wrong?

************************************************** ************

Ah! yes, I found the mistakes there, I've been reading the powerpoint tutorials from IFI and it got me confused. I start putting ; and brackets all over the place....I'll remember it for next time.

You could use a switch loop.

**Somebody please correct me if I am wrong, I don't remember is switch is only C++ or c too.
  #15   Spotlight this post!  
Unread 06-01-2007, 07:21
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,112
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Why it works with an underscore?

Quote:
Originally Posted by Generalx5 View Post
Is it possible to define something and have a set of commands built into it?
Yes, it is possible to do that, though what you posted isn't even close to the right syntax.

But don't do it with a #define. What you're probably looking for is a function that will do what you want.

Code:
void Setting_1(void)
{
  solenoid1 = 1;
  solenoid2 = 0;
  solenoid3 = 1;
  solenoid4 = 0;
  solenoid5 = 0;
  solenoid6 = 0;
}

if (Sensors_are_online)
{
  Setting_1();
}
Generalx5, it looks like you could use a beginning tutorial in C syntax. Does anyone here have any suggestions for one?
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
A poximity sensor that works with IFI gear Validius Control System 10 16-03-2005 21:56
A poximity sensor that works with IFI gear Validius Electrical 10 16-03-2005 21:56
Why is Dave hanging out with these guys? JohnBoucher NASA Discussion 1 17-12-2004 10:38
RoboEmu 1.06 (works with XP and 2k) rbayer Programming 4 20-10-2002 21:47
Are you a team with very little money, why/ mnkysp6353 General Forum 6 09-10-2001 21:30


All times are GMT -5. The time now is 09: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