Go to Post Don't just assume that because you can modify an existing design that you are an engineer. - Daniel_LaFleur [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
  #16   Spotlight this post!  
Unread 06-01-2007, 07:32
Mark Pierce Mark Pierce is offline
Registered User
FRC #0085 (B. O. B.)
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 1999
Location: Zeeland, MI
Posts: 239
Mark Pierce has a brilliant futureMark Pierce has a brilliant futureMark Pierce has a brilliant futureMark Pierce has a brilliant futureMark Pierce has a brilliant futureMark Pierce has a brilliant futureMark Pierce has a brilliant futureMark Pierce has a brilliant futureMark Pierce has a brilliant futureMark Pierce has a brilliant futureMark Pierce has a brilliant future
Re: Why it works with an underscore?

Quote:
Originally Posted by Generalx5 View Post
Something like this?

#define Setting_1 ((solenoid1 = 1) && (Solenoid2 = 0) && (solenoid3 = 1) && (solenoid4 = 0) && (solenoid5 = 0) && (solenoid6 = 0));
I don't think this is what you want to do. If you want Setting_1 to turn on Solenoid1 and solenoid3, with the others off, you would write the following:

Code:
 /* Setting_1 - Turn on solenoids 1&3 only */
#define Setting_1 solenoid1 = 1;Solenoid2 = 0; solenoid3 = 1;solenoid4 = 0; solenoid5 = 0; solenoid6 = 0
This makes it clear you are doing assignments. You could put braces around the assignment statements to make the command look like a single statement to the compiler. This is not needed if you always use braces for the executable statements in if(), while() and for() statements.

Some additional advice for you and others reading this thread:
  • Writing code with comments that explain what is intended is one of the best ways to prevent and debug mistakes. Including these comments of what you intend to do when you ask for advice really helps us help you.
  • Routine names like Setting_1 are great for examples to illustrate a point, but in your robot code, use names like "Extend_L_R_arms_only"
  • In programming there are often dozens of ways to accomplish the task. Often the choice should be made by which will be easiest to understand, debug, and modify as the program develops.
  #17   Spotlight this post!  
Unread 06-01-2007, 09:00
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?

I'll also point out that #defines, although pretty useful in some cases, can make code very hard to read, which is important if you ever have someone helping you with it.

For example, your Setting_1 thing affects a LOT of variables, but someone coming along and reading the code would have no idea. They'd see "Setting_1;". It wouldn't look like a function and it wouldn't have any apparent assignments, but because of the #define it actually affects 6 different variables. At the very least, you should have the #define named such that it is apparent it changes something.

One of my coworkers at Sony really likes making complicated #defines. They make code REALLY hard to read until he explains what each one does.

Code:
((solenoid1 = 1) && (Solenoid2 = 0) && (solenoid3 = 1) && (solenoid4 = 0) && (solenoid5 = 0) && (solenoid6 = 0));
Although this actually may* do what you want it to, it does it in a pretty nonsensical way. As posted above, you want each of those assignment statements to have a semicolon after them, rather than a logical and. That way, each of them is a seperate statement that is guaranteed to execute, rather than a wierd condition statement that may stop halfway through (See below).
What follows below is completely tangential and is only for added knowledge
*Depending on the compiler, it may short-circuit the condition statement if one of the arguments in an && block is false. Since ALL conditions must be true for your condition to work out (because they are all anded together), then as soon as it encounters one that returns zero (Solenoid2 = 0) it may exit the condition and return zero.

Basically, when your program is run, it will do this:
Assigns solenoid1 = 1 (result: 1)
Assigns solenoid2 = 0 (result: 0)
**ANDS the first two expressions together (result: 0 && 1 is 0)
Assigns solenoid3 = 1 (result: 1)
ANDS the third expression with result from first 2 (result: 0 && 1 is 0)
Assigns solenoid4 = 0 (result: 0)
ANDS the fourth expression with result from first 3 (result: 0 && 0 is 0)
Assigns solenoid5 = 0 (result: 0)
ANDS the fifth expression with result from first 4 (result: 0 && 0 is 0)
Assigns solenoid6 = 0 (result: 0)
ANDS the sixth expression with result from first 5 (result: 0 && 0 is 0)

At **, the program KNOWS that the final result is going to be zero because one condition in a series of logical ands resulted in zero, so depending on the compiler, it might just save time and skip doing the rest.

Last edited by Bongle : 06-01-2007 at 09:05.
  #18   Spotlight this post!  
Unread 06-01-2007, 15:24
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?

It compiles now, but its still not responding to the changes in the sensors. If one of the sensors changed signals and this setting is defined as setting_2 below, it does not change the settings for the solenoids.

This is how im using the codes:


#define Setting_1 solenoid1 = 1;solenoid2 = 0; solenoid3 = 1;solenoid4 = 0; solenoid5 = 0; solenoid6 = 1
#define Setting_2 solenoid1 = 0;solenoid2 = 1; solenoid3 = 0;solenoid4 = 1; solenoid5 = 1; solenoid6 = 0



if ((Senor1 = 1) && (Sensor2 = 1) && (Sensor3 = 1) && (Sensor4 = 1) && (Sensor5 = 1))
{
Setting_1;
}
if ((Senor1 = 1) && (Sensor2 = 1) && (Sensor3 = 1) && (Sensor4 = 1) && (Sensor5 = 0))
{
Setting_2;
}

  #19   Spotlight this post!  
Unread 06-01-2007, 15:57
Noah Kleinberg Noah Kleinberg is offline
Registered User
FRC #0395 (2TrainRobotics)
Team Role: Driver
 
Join Date: Jan 2006
Rookie Year: 2006
Location: New York
Posts: 196
Noah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to beholdNoah Kleinberg is a splendid one to behold
Send a message via AIM to Noah Kleinberg
Re: Why it works with an underscore?

Quote:
Originally Posted by Generalx5 View Post
It compiles now, but its still not responding to the changes in the sensors. If one of the sensors changed signals and this setting is defined as setting_2 below, it does not change the settings for the solenoids.

This is how im using the codes:


#define Setting_1 solenoid1 = 1;solenoid2 = 0; solenoid3 = 1;solenoid4 = 0; solenoid5 = 0; solenoid6 = 1
#define Setting_2 solenoid1 = 0;solenoid2 = 1; solenoid3 = 0;solenoid4 = 1; solenoid5 = 1; solenoid6 = 0



if ((Senor1 = 1) && (Sensor2 = 1) && (Sensor3 = 1) && (Sensor4 = 1) && (Sensor5 = 1))
{
Setting_1;
}
if ((Senor1 = 1) && (Sensor2 = 1) && (Sensor3 = 1) && (Sensor4 = 1) && (Sensor5 = 0))
{
Setting_2;
}

Your problem here is that you're using "=" instead of "==" inside your if statement. "=" sets a variable's value, while "==" tests for equality.

(x==y) returns a true or false value based on the equality of x and y, but (x=y), like you have here, returns whatever y is equal to, which in this case is 1 (or 0 for one of them) (0 represents false, and anything besides 0 represents true). Therefore you're always entering the first if statement because all of the conditions always return "1", whereas the second one is never reached because "sensor5=0" always returns 0.

Hope this was helpful.
  #20   Spotlight this post!  
Unread 06-01-2007, 18:14
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?

Wonderful! That makes sense, I've been reminded of this before and again have forgotten. Thanks for the help!
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 02:07.

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