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

MHTS

Guru

Joined: Sun Nov 15, 2009 5:46 amPosts: 1523

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.

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:

Who is online

Users browsing this forum: No registered users and 2 guests

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