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

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