Questions for MHTS! Please help :)
Page 1 of 1

Author:  vincent6liu [ Sun Dec 02, 2012 1:46 pm ]
Post subject:  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 :)

Author:  amcerbu [ Sun Dec 02, 2012 8:33 pm ]
Post subject:  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.

Author:  MHTS [ Sun Dec 02, 2012 11:17 pm ]
Post subject:  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.

Author:  vincent6liu [ Tue Dec 04, 2012 11:59 pm ]
Post subject:  Re: Questions for MHTS! Please help :)

OMG that's brilliant!! Thank you guys!! :)

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group