ftclib/trcdefs.h
author Trc492/3543
Sun Dec 16 22:58:56 2012 -0800 (2012-12-16)
changeset 107 fb1661a7bcdf
parent 61 8aded05af66e
permissions -rw-r--r--
Final code from East District Championship
     1 #if 0
     2 /// Copyright (c) Titan Robotics Club. All rights reserved.
     3 ///
     4 /// <module name="trcdefs.h" />
     5 ///
     6 /// <summary>
     7 ///     This module contains common definitions that can be used anywhere.
     8 /// </summary>
     9 ///
    10 /// <remarks>
    11 ///     Environment: RobotC for Lego Mindstorms NXT.
    12 /// </remarks>
    13 #endif
    14 
    15 #ifndef _TRCDEFS_H
    16 #define _TRCDEFS_H
    17 
    18 //
    19 // Macros.
    20 //
    21 
    22 /**
    23  *  This macro determines the number of elements in an array.
    24  */
    25 #define ARRAYSIZE(a)            (sizeof(a)/sizeof(a[0]))
    26 
    27 /**
    28  *  This macro determines the max of the two values.
    29  */
    30 #define max(a, b)               (((a) > (b))? (a): (b))
    31 
    32 /**
    33  *  This macro determines the min of the two values.
    34  */
    35 #define min(a, b)               (((a) < (b))? (a): (b))
    36 
    37 /**
    38  *  This macro returns a value with the corresponding bit set.
    39  */
    40 #define Bit(n)                  (1 << (n))
    41 
    42 /**
    43  *  The BOUND macro limits the value (n) within the bounds between the given
    44  *  low (l) and high (h).
    45  */
    46 #define BOUND(n,l,h)            (((n) < (l))? (l): ((n) > (h))? (h): (n))
    47 
    48 //
    49 // Joystick input macros.
    50 //
    51 #ifndef DEADBAND_INPUT_THRESHOLD
    52     #define DEADBAND_INPUT_THRESHOLD 20
    53 #endif
    54 
    55 /**
    56  *  These macros ignore input value (n) that is within the DEADBAND_THRESHOLD.
    57  *  This is necessary because analog joysticks do not always centered at zero.
    58  *  So if the joystick is at the rest position, we will consider it zero even
    59  *  though the value is non-zero but within DEADBAND_THRESHOLD.
    60  */
    61 #define DEADBAND(n,t)           ((abs(n) > (t))? (n): 0)
    62 #define DEADBAND_INPUT(n)       DEADBAND(n, DEADBAND_INPUT_THRESHOLD)
    63 
    64 /**
    65  *  This macro limits the input value (n) to the range between -128 and 127.
    66  *  This is useful when calculations on the input value may bring the result
    67  *  outside of the valid range. This macro will make sure the result is within
    68  *  bounds.
    69  */
    70 #define BOUND_INPUT(n)          BOUND(n, -128, 127)
    71 
    72 /**
    73  *  The NORMALIZE macro transforms a value (n) in the range between (sl) and
    74  *  (sh) to the range between (tl) and (th).
    75  */
    76 #define MOTOR_MIN_VALUE         -100
    77 #define MOTOR_MAX_VALUE         100
    78 
    79 #define NORMALIZE(n,sl,sh,tl,th) (int)(((long)(n) - (sl))*((th) - (tl))/((sh) - (sl)) + (tl))
    80 #define NORMALIZE_DRIVE(x,m,n)  NORMALIZE(x, m, n, \
    81                                           MOTOR_MIN_VALUE, MOTOR_MAX_VALUE)
    82 #define JOYSTICK_POWER(n)       NORMALIZE_DRIVE(DEADBAND_INPUT(n), -128, 127)
    83 
    84 #define PGMMODE_DISABLED        0
    85 #define PGMMODE_AUTONOMOUS      1
    86 #define PGMMODE_TELEOP          2
    87 
    88 #ifndef LOOP_PERIOD
    89     #define LOOP_PERIOD         20      //20-msec
    90 #endif
    91 
    92 #define INCHES_PER_METER        39.370079
    93 #define INCHES_PER_CM           (1.0/2.54)
    94 
    95 #define NXTBTN_DEBOUNCE_TIME    20
    96 
    97 #endif  //ifndef _TRCDEFS_H