TRC Library for FTC (2011) 2011
TRC Library Code Project

C:/Users/Michael/Ftc/2011/code/trclib/sensor.h

Go to the documentation of this file.
00001 #if 0
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 #endif
00014 
00015 #ifndef _SENSOR_H
00016 #define _SENSOR_H
00017 
00018 #pragma systemFile
00019 
00020 #ifdef MOD_ID
00021     #undef MOD_ID
00022 #endif
00023 #define MOD_ID                  MOD_SENSOR
00024 
00025 //
00026 // Constants.
00027 //
00028 #define SENSORZONE_LO           0
00029 #define SENSORZONE_MID          1
00030 #define SENSORZONE_HI           2
00031 #define NUM_SENSOR_ZONES        3
00032 
00033 #define SENSORF_USER_MASK       0x00ff
00034 #define SENSORF_ENABLE_EVENTS   0x0001
00035 #define SENSORF_INVERSE         0x0002
00036 #ifdef HTSMUX_STATUS
00037   #define SENSORF_HTSMUX        0x0080
00038 #endif
00039 #define SENSORF_CALIBRATING     0x0100
00040 
00041 #define SensorCalibrating(s)    (s.sensorFlags & SENSORF_CALIBRATING)
00042 
00043 //
00044 // Type definitions.
00045 //
00046 typedef struct
00047 {
00048     int sensorID;
00049     int lowThreshold;
00050     int highThreshold;
00051     int sensorFlags;
00052     int sensorValue;
00053     int sensorZone;
00054     int rawMin;
00055     int rawMax;
00056 } SENSOR;
00057 
00058 //
00059 // Import function prototypes.
00060 //
00061 void
00062 SensorEvent(
00063     __in SENSOR &sensor
00064     );
00065 
00075 void
00076 SensorInit(
00077     __out SENSOR &sensor,
00078     __in  int sensorID,
00079     __in  int lowThreshold,
00080     __in  int highThreshold,
00081     __in  int sensorFlags
00082     )
00083 {
00084     TFuncName("SensorInit");
00085     TLevel(INIT);
00086     TEnter();
00087 
00088     sensor.sensorID = sensorID;
00089     sensor.lowThreshold = lowThreshold;
00090     sensor.highThreshold = highThreshold;
00091     sensor.sensorFlags = sensorFlags & SENSORF_USER_MASK;
00092     sensor.sensorValue = 0;
00093     sensor.sensorZone = SENSORZONE_LO;
00094 
00095     TExit();
00096     return;
00097 }   //SensorInit
00098 
00105 void
00106 SensorCal(
00107     __inout SENSOR &sensor,
00108     __in    bool fStart
00109     )
00110 {
00111     TFuncName("SensorCal");
00112     TLevel(API);
00113     TEnterMsg(("fStart=%d", (byte)fStart));
00114 
00115     if (fStart)
00116     {
00117         sensor.sensorFlags |= SENSORF_CALIBRATING;
00118         sensor.rawMin = 1023;
00119         sensor.rawMax = 0;
00120     }
00121     else
00122     {
00123         int zoneRange = (sensor.rawMax - sensor.rawMin)/3;
00124 
00125         sensor.sensorFlags &= ~SENSORF_CALIBRATING;
00126         sensor.lowThreshold = sensor.rawMin + zoneRange;
00127         sensor.highThreshold = sensor.rawMax - zoneRange;
00128         TInfo(("LoTh=%d,HiTh=%d",
00129                sensor.lowThreshold, sensor.highThreshold));
00130     }
00131 
00132     TExit();
00133     return;
00134 }   //SensorCal
00135 
00142 void
00143 SensorTask(
00144     __inout SENSOR &sensor
00145     )
00146 {
00147     int flags = sensor.sensorFlags;
00148 
00149     TFuncName("SensorTask");
00150     TLevel(TASK);
00151     TEnter();
00152 
00153 //    nxtDisplayTextLine(sensor.sensorID + 3,
00154 //                       "CalFlags=%d,%x",
00155 //                       sensor.sensorID, sensor.sensorFlags);
00156 #ifdef HTSMUX_STATUS
00157     if (flags & SENSORF_HTSMUX)
00158     {
00159         sensor.sensorValue = 1023 -
00160                              HTSMUXreadAnalogue((tMUXSensor)sensor.sensorID);
00161     }
00162     else
00163     {
00164         sensor.sensorValue = SensorRaw[sensor.sensorID];
00165     }
00166 #else
00167     sensor.sensorValue = SensorRaw[sensor.sensorID];
00168 #endif
00169     if (flags & SENSORF_CALIBRATING)
00170     {
00171         //
00172         // We are in calibration mode.
00173         //
00174         if (sensor.sensorValue < sensor.rawMin)
00175         {
00176             sensor.rawMin = sensor.sensorValue;
00177         }
00178         else if (sensor.sensorValue > sensor.rawMax)
00179         {
00180             sensor.rawMax = sensor.sensorValue;
00181         }
00182     }
00183     else
00184     {
00185         int zone;
00186 
00187         if (sensor.sensorValue <= sensor.lowThreshold)
00188         {
00189             zone = (flags & SENSORF_INVERSE)?
00190                    SENSORZONE_HI: SENSORZONE_LO;
00191         }
00192         else if (sensor.sensorValue <= sensor.highThreshold)
00193         {
00194             zone = SENSORZONE_MID;
00195         }
00196         else
00197         {
00198             zone = (flags & SENSORF_INVERSE)?
00199                    SENSORZONE_LO: SENSORZONE_HI;
00200         }
00201 
00202         if (zone != sensor.sensorZone)
00203         {
00204             //
00205             // We have crossed to another zone, let's send a sensor event.
00206             //
00207             sensor.sensorZone = zone;
00208             if (flags & SENSORF_ENABLE_EVENTS)
00209             {
00210                 SensorEvent(sensor);
00211             }
00212         }
00213     }
00214 
00215     TExit();
00216     return;
00217 }   //SensorTask
00218 
00219 #endif  //ifndef _SENSOR_H
 All Data Structures Files Functions Variables Defines