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

dexterind-flex.h

Go to the documentation of this file.
00001 /*!@addtogroup Dexter_Industries
00002  * @{
00003  * @defgroup dFlex dFlex Sensor
00004  * Dexter Industries dFlex Sensor driver
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: dexterind-flex.h 133 2013-03-10 15:15:38Z xander $
00010  */
00011 
00012 #ifndef __DFLEX_H__
00013 #define __DFLEX_H__
00014 /** \file dexterind-flex.h
00015  * \brief ROBOTC Dexter Industries dFlex Sensor driver
00016  *
00017  * DFLEX-driver.h provides an API for the Dexter Industries dFlex Sensor.
00018  *
00019  * Changelog:
00020  * - 0.1: Initial release
00021  *
00022  * Credits:
00023  * - Big thanks to Dexter Industries for providing me with the hardware necessary to write and test this.
00024  *
00025  * License: You may use this code as you wish, provided you give credit where its due.
00026  *
00027  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 
00028 
00029  * \author Xander Soldaat (xander_at_botbench.com)
00030  * \date 23 June 2010
00031  * \version 0.1
00032  * \example dexterind-flex-test1.c
00033  * \example dexterind-flex-test2.c
00034  */
00035 
00036 #pragma systemFile
00037 
00038 #ifndef __COMMON_H__
00039 #include "common.h"
00040 #endif
00041 
00042 #define DFLEXDAT "DFLEX.dat"    /*!< Datafile for dFlex Sensor calibration info */
00043 
00044 // Globals
00045 int dflexlow = 0;                    /*!< Low calibration value */
00046 int dflexhigh = 1023;                /*!< High calibration value */
00047 bool DFLEX_calibrated = false;   /*!< Has the sensor been calibrated yet */
00048 
00049 // Function prototypes
00050 int DFLEXvalRaw(tSensors link);
00051 int DFLEXvalNorm(tSensors link);
00052 
00053 void DFLEXcalLow(tSensors link);
00054 void DFLEXcalLow(int lowval);
00055 void DFLEXcalHigh(tSensors link);
00056 void DFLEXcalHigh(int highval);
00057 
00058 void _DFLEXcheckSensor(tSensors link);
00059 void _DFLEXwriteCalVals(int lowval, int highval);
00060 void _DFLEXreadCalVals(int &lowval, int &highval);
00061 
00062 
00063 /**
00064  * Read the raw value of the dFlex Sensor.
00065  * @param link the dFlex Sensor port number
00066  * @return the raw value of the dFlex Sensor
00067  */
00068 int DFLEXvalRaw(tSensors link) {
00069   _DFLEXcheckSensor(link);
00070 
00071   return SensorRaw[link];
00072 }
00073 
00074 
00075 /**
00076  * Read the normalised value of the dFlex Sensor, based on the low and high values.
00077  *
00078  * Note: this is not a linear value
00079  * @param link the dFlex Sensor port number
00080  * @return the normalised value (0-100)
00081  */
00082 int DFLEXvalNorm(tSensors link) {
00083   long currval = 0;
00084 
00085   _DFLEXcheckSensor(link);
00086 
00087   if (!DFLEX_calibrated) {
00088     _DFLEXreadCalVals(dflexlow, dflexhigh);
00089   }
00090 
00091   currval = DFLEXvalRaw(link);
00092 
00093   if (currval <= dflexlow)
00094     return 0;
00095   else if (currval >= dflexhigh)
00096     return 100;
00097 
00098   return ((currval - dflexlow) * 100) / (dflexhigh - dflexlow);
00099 }
00100 
00101 
00102 /**
00103  * Calibrate the dFlex Sensor's low calibration value with the current raw sensor reading.
00104  * @param link the dFlex Sensor port number
00105  */
00106 void DFLEXcalLow(tSensors link) {
00107   _DFLEXcheckSensor(link);
00108 
00109   dflexlow = SensorRaw[link];
00110   _DFLEXwriteCalVals(dflexlow, dflexhigh);
00111 }
00112 
00113 
00114 /**
00115  * Calibrate the dFlex Sensor's low calibration value with the supplied value.
00116  * @param lowval the sensor's low calibration value
00117  */
00118 void DFLEXcalLow(int lowval) {
00119   dflexlow = lowval;
00120   _DFLEXwriteCalVals(dflexlow, dflexhigh);
00121 }
00122 
00123 
00124 /**
00125  * Calibrate the dFlex Sensor's high calibration value with the current raw sensor reading.
00126  * @param link the dFlex Sensor port number
00127  */
00128 void DFLEXcalHigh(tSensors link) {
00129   _DFLEXcheckSensor(link);
00130 
00131   dflexhigh = SensorRaw[link];
00132   _DFLEXwriteCalVals(dflexlow, dflexhigh);
00133 }
00134 
00135 
00136 /**
00137  * Calibrate the dFlex Sensor's high calibration value with the supplied value.
00138  * @param highval the sensor's high calibration value
00139  */
00140 void DFLEXcalHigh(int highval) {
00141   dflexhigh = highval;
00142   _DFLEXwriteCalVals(dflexlow, dflexhigh);
00143 }
00144 
00145 
00146 /**
00147  * Check if the sensor is set to raw and that it's been configured as a
00148  * sensorAnalogInactive. If not, reconfigure the port.
00149  *
00150  * Note: this is an internal function and should not be called directly
00151  * @param link the dFlex Sensor port number
00152  */
00153 void _DFLEXcheckSensor(tSensors link) {
00154   if (SensorMode[link] != modeRaw)
00155     SensorMode[link] = modeRaw;
00156   if (SensorType[link] != sensorAnalogInactive)
00157     SensorType[link] = sensorAnalogInactive;
00158 }
00159 
00160 
00161 /**
00162  * Write the low and high calibration values to a data file.
00163  *
00164  * Note: this is an internal function and should not be called directly
00165  * @param lowval the low calibration value
00166  * @param highval the high calibration value
00167  */
00168 void _DFLEXwriteCalVals(int lowval, int highval) {
00169   TFileHandle hFileHandle;
00170   TFileIOResult nIoResult;
00171   short nFileSize = 4;
00172 
00173   // Delete the old data file and open a new one for writing
00174   Delete(DFLEXDAT, nIoResult);
00175   OpenWrite(hFileHandle, nIoResult, DFLEXDAT, nFileSize);
00176   if (nIoResult != ioRsltSuccess) {
00177     Close(hFileHandle, nIoResult);
00178     eraseDisplay();
00179     nxtDisplayTextLine(3, "W:can't cal file");
00180     PlaySound(soundException);
00181     while(bSoundActive) EndTimeSlice();
00182     wait1Msec(5000);
00183     StopAllTasks();
00184   }
00185 
00186   // Write the low calibration value
00187   WriteShort(hFileHandle, nIoResult, lowval);
00188   if (nIoResult != ioRsltSuccess) {
00189     eraseDisplay();
00190     nxtDisplayTextLine(3, "can't write lowval");
00191     PlaySound(soundException);
00192     while(bSoundActive) EndTimeSlice();
00193     wait1Msec(5000);
00194     StopAllTasks();
00195   }
00196 
00197   // Write the high calibration value
00198   WriteShort(hFileHandle, nIoResult, highval);
00199   if (nIoResult != ioRsltSuccess) {
00200     eraseDisplay();
00201     nxtDisplayTextLine(3, "can't write highval");
00202     PlaySound(soundException);
00203     while(bSoundActive) EndTimeSlice();
00204     wait1Msec(5000);
00205     StopAllTasks();
00206   }
00207 
00208   // Close the file
00209   Close(hFileHandle, nIoResult);
00210   if (nIoResult != ioRsltSuccess) {
00211     eraseDisplay();
00212     nxtDisplayTextLine(3, "Can't close");
00213     PlaySound(soundException);
00214     while(bSoundActive) EndTimeSlice();
00215     wait1Msec(5000);
00216     StopAllTasks();
00217   }
00218 }
00219 
00220 
00221 /**
00222  * Read the low and high calibration values from a data file.
00223  *
00224  * Note: this is an internal function and should not be called directly
00225  * @param lowval the low calibration value
00226  * @param highval the high calibration value
00227  */
00228 void _DFLEXreadCalVals(int &lowval, int &highval) {
00229   TFileHandle hFileHandle;
00230   TFileIOResult nIoResult;
00231   short nFileSize;
00232 
00233   short lv = 0;
00234   short hv = 0;
00235 
00236   // Open the data file for reading
00237   DFLEX_calibrated = true;
00238   OpenRead(hFileHandle, nIoResult, DFLEXDAT, nFileSize);
00239   if (nIoResult != ioRsltSuccess) {
00240     Close(hFileHandle, nIoResult);
00241     return;
00242   }
00243 
00244   // Read the low calibration value
00245   ReadShort(hFileHandle, nIoResult, lv);
00246   if (nIoResult != ioRsltSuccess) {
00247     Close(hFileHandle, nIoResult);
00248     return;
00249   }
00250 
00251   // Read the high calibration value
00252   ReadShort(hFileHandle, nIoResult, hv);
00253   if (nIoResult != ioRsltSuccess) {
00254     Close(hFileHandle, nIoResult);
00255     return;
00256   }
00257 
00258   // Assign values and close file
00259   lowval = lv;
00260   highval = hv;
00261   Close(hFileHandle, nIoResult);
00262 }
00263 
00264 #endif // __DFLEX_H__
00265 
00266 /*
00267  * $Id: dexterind-flex.h 133 2013-03-10 15:15:38Z xander $
00268  */
00269 /* @} */
00270 /* @} */