Mindstorms 3rd Party ROBOTC Drivers RobotC
[Home] [Download] [Submit a bug/suggestion] [ROBOTC Forums] [Blog] [Support this project]

LEGOLS-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup Lego
00002  * @{
00003  * @defgroup legols Light Sensor
00004  * Light Sensor
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: LEGOLS-driver.h 47 2011-01-17 19:47:22Z xander $
00010  */
00011 
00012 #ifndef __LEGOLS_H__
00013 #define __LEGOLS_H__
00014 /** \file LEGOLS-driver.h
00015  * \brief Lego Light Sensor driver
00016  *
00017  * LEGOLS-driver.h provides an API for the Lego Light Sensor.
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  * - 0.2: Make use of new calls for analogue SMUX sensors in common.h
00022  *
00023  * License: You may use this code as you wish, provided you give credit where its due.
00024  *
00025  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER.
00026  * \author Xander Soldaat (mightor_at_gmail.com)
00027  * \date 25 November 2009
00028  * \version 0.2
00029  * \example LEGOLS-test1.c
00030  * \example LEGOLS-test2.c
00031  * \example LEGOLS-SMUX-test1.c
00032  * \example LEGOLS-SMUX-test2.c
00033  */
00034 
00035 #pragma systemFile
00036 
00037 #ifndef __COMMON_H__
00038 #include "common.h"
00039 #endif
00040 
00041 #define LEGOLSDAT "legols.dat"    /*!< Datafile for Light Sensor calibration info */
00042 
00043 // Globals
00044 int lslow = 0;                    /*!< Low calibration value */
00045 int lshigh = 1023;                /*!< High calibration value */
00046 bool legols_calibrated = false;   /*!< Has the sensor been calibrated yet */
00047 
00048 // Function prototypes
00049 int LSvalRaw(tSensors link);
00050 int LSvalNorm(tSensors link);
00051 void LScalLow(tSensors link);
00052 void LScalLow(int lowval);
00053 void LScalHigh(tSensors link);
00054 void LScalHigh(int highval);
00055 void LSsetActive(tSensors link);
00056 void LSsetInactive(tSensors link);
00057 
00058 #ifdef __HTSMUX_SUPPORT__
00059 int LSvalNorm(tMUXSensor muxsensor);
00060 void LScalLow(tMUXSensor muxsensor);
00061 int LSvalRaw(tMUXSensor muxsensor);
00062 void LScalHigh(tMUXSensor muxsensor);
00063 void LSsetActive(tMUXSensor muxsensor);
00064 void LSsetInactive(tMUXSensor muxsensor);
00065 #endif // __HTSMUX_SUPPORT__
00066 
00067 void _LScheckSensor(tSensors link);
00068 void _LSwriteCalVals(int lowval, int highval);
00069 void _LSreadCalVals(int &lowval, int &highval);
00070 
00071 
00072 /**
00073  * Read the raw value of the Light Sensor.
00074  * @param link the Light Sensor port number
00075  * @return the raw value of the Light Sensor
00076  */
00077 int LSvalRaw(tSensors link) {
00078   _LScheckSensor(link);
00079 
00080   return SensorRaw[link];
00081 }
00082 
00083 
00084 /**
00085  * Read the raw value of the Light Sensor.
00086  * @param muxsensor the SMUX sensor port number
00087  * @return the raw value of the Light Sensor
00088  */
00089 #ifdef __HTSMUX_SUPPORT__
00090 int LSvalRaw(tMUXSensor muxsensor) {
00091   return 1023 - HTSMUXreadAnalogue(muxsensor);
00092 }
00093 #endif // __HTSMUX_SUPPORT__
00094 
00095 /**
00096  * Read the normalised value of the Light Sensor, based on the low and high values.
00097  * @param link the Light Sensor port number
00098  * @return the normalised value
00099  */
00100 int LSvalNorm(tSensors link) {
00101   long currval = 0;
00102 
00103   _LScheckSensor(link);
00104 
00105   if (!legols_calibrated) {
00106     _LSreadCalVals(lslow, lshigh);
00107   }
00108 
00109   currval = LSvalRaw(link);
00110 
00111   if (currval <= lslow)
00112     return 0;
00113   else if (currval >= lshigh)
00114     return 100;
00115 
00116   return ((currval - lslow) * 100) / (lshigh - lslow);
00117 }
00118 
00119 
00120 /**
00121  * Read the normalised value of the Light Sensor, based on the low and high values.
00122  * @param muxsensor the SMUX sensor port number
00123  * @return the normalised value
00124  */
00125  #ifdef __HTSMUX_SUPPORT__
00126 int LSvalNorm(tMUXSensor muxsensor) {
00127   long currval = 0;
00128 
00129   if (!legols_calibrated) {
00130     _LSreadCalVals(lslow, lshigh);
00131   }
00132 
00133   currval = LSvalRaw(muxsensor);
00134 
00135   if (currval <= lslow)
00136     return 0;
00137   else if (currval >= lshigh)
00138     return 100;
00139 
00140   return ((currval - lslow) * 100) / (lshigh - lslow);
00141 }
00142 #endif // __HTSMUX_SUPPORT__
00143 
00144 /**
00145  * Calibrate the Light Sensor's low calibration value with the current raw sensor reading.
00146  * @param link the Light Sensor port number
00147  */
00148 void LScalLow(tSensors link) {
00149   _LScheckSensor(link);
00150 
00151   lslow = SensorRaw[link];
00152   _LSwriteCalVals(lslow, lshigh);
00153 }
00154 
00155 
00156 /**
00157  * Calibrate the Light Sensor's low calibration value with the current raw sensor reading.
00158  * @param muxsensor the SMUX sensor port number
00159  */
00160 #ifdef __HTSMUX_SUPPORT__
00161 void LScalLow(tMUXSensor muxsensor) {
00162   lslow = LSvalRaw(muxsensor);
00163   _LSwriteCalVals(lslow, lshigh);
00164 }
00165 #endif // __HTSMUX_SUPPORT__
00166 
00167 
00168 /**
00169  * Calibrate the Light Sensor's low calibration value with the supplied value.
00170  * @param lowval the sensor's low calibration value
00171  */
00172 void LScalLow(int lowval) {
00173   lslow = lowval;
00174   _LSwriteCalVals(lslow, lshigh);
00175 }
00176 
00177 
00178 /**
00179  * Calibrate the Light Sensor's high calibration value with the current raw sensor reading.
00180  * @param link the Light Sensor port number
00181  */
00182 void LScalHigh(tSensors link) {
00183   _LScheckSensor(link);
00184 
00185   lshigh = SensorRaw[link];
00186   _LSwriteCalVals(lslow, lshigh);
00187 }
00188 
00189 
00190 /**
00191  * Calibrate the Light Sensor's high calibration value with the current raw sensor reading.
00192  * @param muxsensor the SMUX sensor port number
00193  */
00194 #ifdef __HTSMUX_SUPPORT__
00195 void LScalHigh(tMUXSensor muxsensor) {
00196   lshigh = LSvalRaw(muxsensor);
00197   _LSwriteCalVals(lslow, lshigh);
00198 }
00199 #endif // __HTSMUX_SUPPORT__
00200 
00201 
00202 /**
00203  * Calibrate the Light Sensor's high calibration value with the supplied value.
00204  * @param highval the sensor's high calibration value
00205  */
00206 void LScalHigh(int highval) {
00207   lshigh = highval;
00208   _LSwriteCalVals(lslow, lshigh);
00209 }
00210 
00211 
00212 /**
00213  * Configure the sensor as a LightActive sensor
00214  * @param link the Light Sensor port number
00215  */
00216 void LSsetActive(tSensors link) {
00217   SensorType[link] = sensorLightActive;
00218   SensorMode[link] = modeRaw;
00219   wait1Msec(5);
00220 }
00221 
00222 
00223 /**
00224  * Configure the sensor as a LightActive sensor
00225  * @param muxsensor the SMUX sensor port number
00226  */
00227 #ifdef __HTSMUX_SUPPORT__
00228 void LSsetActive(tMUXSensor muxsensor) {
00229   HTSMUXsetAnalogueActive(muxsensor);
00230 }
00231 #endif // __HTSMUX_SUPPORT__
00232 
00233 
00234 /**
00235  * Configure the sensor as a LightInactive sensor
00236  * @param link the Light Sensor port number
00237  */
00238 void LSsetInactive(tSensors link) {
00239   SensorType[link] = sensorLightInactive;
00240   SensorMode[link] = modeRaw;
00241   wait1Msec(5);
00242 }
00243 
00244 
00245 /**
00246  * Configure the sensor as a LightInactive sensor
00247  * @param muxsensor the SMUX sensor port number
00248  */
00249 #ifdef __HTSMUX_SUPPORT__
00250 void LSsetInactive(tMUXSensor muxsensor) {
00251   HTSMUXsetAnalogueInactive(muxsensor);
00252 }
00253 #endif // __HTSMUX_SUPPORT__
00254 
00255 
00256 /**
00257  * Check if the sensor is set to raw and that it's been configured as a
00258  * LightActive or Inactive sensor.  If not, set the default to sensorLightInActive.
00259  *
00260  * Note: this is an internal function and should not be called directly
00261  * @param link the Light Sensor port number
00262  */
00263 void _LScheckSensor(tSensors link) {
00264   if (SensorMode[link] != modeRaw &&
00265     ((SensorType[link] != sensorLightActive) ||
00266      (SensorType[link] != sensorLightInactive))) {
00267       LSsetInactive(link);
00268     }
00269 }
00270 
00271 
00272 /**
00273  * Write the low and high calibration values to a data file.
00274  *
00275  * Note: this is an internal function and should not be called directly
00276  * @param lowval the low calibration value
00277  * @param highval the high calibration value
00278  */
00279 void _LSwriteCalVals(int lowval, int highval) {
00280   TFileHandle hFileHandle;
00281   TFileIOResult nIoResult;
00282   short nFileSize = 4;
00283 
00284   // Delete the old data file and open a new one for writing
00285   Delete(LEGOLSDAT, nIoResult);
00286   OpenWrite(hFileHandle, nIoResult, LEGOLSDAT, nFileSize);
00287   if (nIoResult != ioRsltSuccess) {
00288     Close(hFileHandle, nIoResult);
00289     eraseDisplay();
00290     nxtDisplayTextLine(3, "W:can't cal file");
00291     PlaySound(soundException);
00292     while(bSoundActive);
00293     wait1Msec(5000);
00294     StopAllTasks();
00295   }
00296 
00297   // Write the low calibration value
00298   WriteShort(hFileHandle, nIoResult, lowval);
00299   if (nIoResult != ioRsltSuccess) {
00300     eraseDisplay();
00301     nxtDisplayTextLine(3, "can't write lowval");
00302     PlaySound(soundException);
00303     while(bSoundActive);
00304     wait1Msec(5000);
00305     StopAllTasks();
00306   }
00307 
00308   // Write the high calibration value
00309   WriteShort(hFileHandle, nIoResult, highval);
00310   if (nIoResult != ioRsltSuccess) {
00311     eraseDisplay();
00312     nxtDisplayTextLine(3, "can't write highval");
00313     PlaySound(soundException);
00314     while(bSoundActive);
00315     wait1Msec(5000);
00316     StopAllTasks();
00317   }
00318 
00319   // Close the file
00320   Close(hFileHandle, nIoResult);
00321   if (nIoResult != ioRsltSuccess) {
00322     eraseDisplay();
00323     nxtDisplayTextLine(3, "Can't close");
00324     PlaySound(soundException);
00325     while(bSoundActive);
00326     wait1Msec(5000);
00327     StopAllTasks();
00328   }
00329 }
00330 
00331 /**
00332  * Read the low and high calibration values from a data file.
00333  *
00334  * Note: this is an internal function and should not be called directly
00335  * @param lowval the low calibration value
00336  * @param highval the high calibration value
00337  */
00338 void _LSreadCalVals(int &lowval, int &highval) {
00339   TFileHandle hFileHandle;
00340   TFileIOResult nIoResult;
00341   short nFileSize;
00342 
00343   short lv = 0;
00344   short hv = 0;
00345 
00346   // Open the data file for reading
00347   legols_calibrated = true;
00348   OpenRead(hFileHandle, nIoResult, LEGOLSDAT, nFileSize);
00349   if (nIoResult != ioRsltSuccess) {
00350     Close(hFileHandle, nIoResult);
00351     return;
00352   }
00353 
00354   // Read the low calibration value
00355   ReadShort(hFileHandle, nIoResult, lv);
00356   if (nIoResult != ioRsltSuccess) {
00357     Close(hFileHandle, nIoResult);
00358     return;
00359   }
00360 
00361   // Read the high calibration value
00362   ReadShort(hFileHandle, nIoResult, hv);
00363   if (nIoResult != ioRsltSuccess) {
00364     Close(hFileHandle, nIoResult);
00365     return;
00366   }
00367 
00368   // Assign values and close file
00369   lowval = lv;
00370   highval = hv;
00371   Close(hFileHandle, nIoResult);
00372 }
00373 
00374 #endif // __LEGOLS_H__
00375 
00376 /*
00377  * $Id: LEGOLS-driver.h 47 2011-01-17 19:47:22Z xander $
00378  */
00379 /* @} */
00380 /* @} */