View unanswered posts | View active topics It is currently Sat Dec 07, 2019 2:36 am

Reply to topic  [ 4 posts ] 
Questions for MHTS! Please help :) 
Author Message

Joined: Thu Nov 29, 2012 9:04 pm
Posts: 7
Post Questions for MHTS! Please help :)
I've noticed that you always define these terms, but I don't get these. So would you please explain these to me?

#define DEADBAND(x) ((abs(x) >= DEADBAND_THRESHOLD)? x: 0)
#define BOUND(x) (((x) > 100)? 100: ((x) < -100)? -100: (x))

Thank you so much :)

Sun Dec 02, 2012 1:46 pm

Joined: Sun Oct 21, 2012 10:01 pm
Posts: 76
Post Re: Questions for MHTS! Please help :)
The #define pre-processor command tells the RobotC compiler to replace every DEADBAND_THRESHOLD in your code with the constant 15. It's different than declaring a constant variable, since the value isn't actually stored in the NXT memory, but as part of the program. In this case, DEADBAND_THRESHOLD represents the lowest acceptable joystick value (to prevent motors from running when joysticks are released, but not at 0). DEADBAND(x) is a bit like a function; it returns a value of x if the absolute value of x is greater than DEADBAND_THRESHOLD; otherwise, it returns 0. BOUND(x) keeps x within a [-100, 100] range. JOYSTICK_POWER(x) combines DEADBAND and BOUND to return a value that is neither too low nor too high.

Sun Dec 02, 2012 8:33 pm
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Post Re: Questions for MHTS! Please help :)
These are call macros. Like what amcerbu said, #define is a pre-processor command. It allows you to associate a name to anything. For example, when I defined DEADBAND_THRESHOLD as 15, it means whenever the pre-processor finds DEADBAND_THRESHOLD in the code, it is going to replace it with whatever it is associated with, in this case the constant 15. The benefit of using "defined constants" is that you may use the constant in a lot of places. If, for example, you decided to change the deadband threshold to say 20, you need to search and find all instances of the value 15 and replace them with 20. However, if you use the DEADBAND_THRESHOLD name everywhere in your code, you can just change the define line to 20 and it will be replaced everywhere. Giving it a meaningful name also makes the code a lot easier to understand. There are many creative ways of using macros. For example, you can make macros look like functions.
#define BOUND(x) (((x) > 100)? 100: ((x) < -100)? -100: (x))
int drivePower = BOUND(joystick.joy1_y1);

Since we have defined BOUND(x) as a macro, it means when the pre-processor sees BOUND(joystick.joy1_y1), it is going to replace the statement with what BOUND is defined to and substituting x with joystick.joy1_y1. So it becomes:
int drivePower = (((joystick.joy1_y1) > 100)? 100: ((joystick.joy1_y1) < -100)? -100: (joystick.joy1_y1));

It makes the code a lot cleaner and easier to understand.

Sun Dec 02, 2012 11:17 pm

Joined: Thu Nov 29, 2012 9:04 pm
Posts: 7
Post Re: Questions for MHTS! Please help :)
OMG that's brilliant!! Thank you guys!! :)

Tue Dec 04, 2012 11:59 pm
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.