Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Why it works with an underscore? (http://www.chiefdelphi.com/forums/showthread.php?t=50954)

Generalx5 05-01-2007 01:12

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.

Astronouth7303 05-01-2007 01:17

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.

Stuart 05-01-2007 02:48

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

Donut 05-01-2007 10:29

Re: Why it works with an underscore?
 
Quote:

Originally Posted by Stuart (Post 548198)
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.

Bongle 05-01-2007 11:07

Re: Why it works with an underscore?
 
Quote:

Originally Posted by Stuart (Post 548198)
#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


mluckham 05-01-2007 11:26

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.

Generalx5 05-01-2007 14:07

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.

Generalx5 05-01-2007 18:30

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.

Bongle 05-01-2007 20:19

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.

Generalx5 05-01-2007 23:27

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.

prograid 06-01-2007 00:14

Re: Why it works with an underscore?
 
Quote:

Originally Posted by Generalx5 (Post 548652)
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.

Generalx5 06-01-2007 00:18

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:eek: 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.

John Gutmann 06-01-2007 03:59

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........:eek:

John Gutmann 06-01-2007 04:01

Re: Why it works with an underscore?
 
Quote:

Originally Posted by Generalx5 (Post 548673)
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:eek: 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.

Alan Anderson 06-01-2007 07:21

Re: Why it works with an underscore?
 
Quote:

Originally Posted by Generalx5 (Post 548652)
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?


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

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