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

DTMP-driver.h

Go to the documentation of this file.
00001 /*!@addtogroup Dexter_Industries
00002  * @{
00003  * @defgroup dTemp Temp Probe
00004  * Dexter Industries Temperature Probe Sensor driver
00005  * @{
00006  */
00007 
00008 /*
00009  * $Id: DTMP-driver.h 29 2010-06-25 12:55:41Z xander $
00010  */
00011 
00012 #ifndef __DTMP_DRIVER_H__
00013 #define __DTMP_DRIVER_H__
00014 
00015 /** \file DTMP-driver.h
00016  * \brief ROBOTC DI Temp Probe Driver
00017  *
00018  * DTMP-driver.h provides an API for the Dexter Industries Temperature Probe.
00019  *
00020  * Changelog:
00021  * - 0.1: Initial release
00022  *
00023  * Credits :
00024  * - Big thanks to Dexter Industries for providing me with the hardware necessary to write and test this.
00025  *
00026  * License: You may use this code as you wish, provided you give credit where its due.
00027  *
00028  * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 2.00 AND HIGHER.
00029  * \author Xander Soldaat (mightor@gmail.com)
00030  * \date 13 June 2010
00031  * \version 0.1
00032  * \example DTMP-test1.c
00033  */
00034 
00035 #pragma systemFile
00036 
00037 // internal lookup tables used for resistance to temp conversions
00038 const float _a[] = {0.003357042,         0.003354017,        0.0033530481,       0.0033536166};
00039 const float _b[] = {0.00025214848,       0.00025617244,      0.00025420230,      0.000253772};
00040 const float _c[] = {0.0000033743283,     0.0000021400943,    0.0000011431163,    0.00000085433271};
00041 const float _d[] = {-0.000000064957311, -0.000000072405219, -0.000000069383563, -0.000000087912262};
00042 
00043 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00044 // Function prototypes
00045 bool DTMPreadTemp(tSensors link, float &temp);
00046 bool DTMPreadTempK(tSensors link, float &temp);
00047 bool DTMPreadTempF(tSensors link, float &temp);
00048 
00049 
00050 /**
00051  * Read the temperature in degrees Celcius.
00052  * @param link the DI Temp Sensor port number
00053  * @param temp the temperature value in degrees Celcius,
00054  * @return true if no error occured, false if it did
00055  */
00056 bool DTMPreadTemp(tSensors link, float &temp) {
00057   float _tempK = 0.0;
00058   if (!DTMPreadTempK(link, _tempK))
00059     return false;
00060 
00061   temp = _tempK - 273;
00062   return true;
00063 }
00064 
00065 
00066 /**
00067  * Read the temperature in Kelvin.
00068  * @param link the DI Temp Sensor port number
00069  * @param temp the temperature value in Kelvin,
00070  * @return true if no error occured, false if it did
00071  */
00072 bool DTMPreadTempK(tSensors link, float &temp) {
00073   // local vars
00074   byte i = 0;
00075   int val = 0;
00076   float RtRt25 = 0.0;
00077   float lnRtRt25 = 0.0;
00078 
00079   // Temp sensor type must absolutely be set to sensorAnalogInactive
00080   if (SensorType[link] != sensorAnalogInactive)
00081     return false;
00082 
00083   // Get the basic factors for equation
00084   val = SensorValue[link];
00085   RtRt25 = (float)val / (1023 - val);
00086   lnRtRt25 = log(RtRt25);
00087 
00088   if (RtRt25 > 3.277)
00089     i = 0;
00090   else if (RtRt25 > 0.3599)
00091     i = 1;
00092   else if (RtRt25 > 0.06816)
00093     i = 2;
00094   else
00095     i = 3;
00096 
00097   temp =  1.0 / (_a[i] + (_b[i] * lnRtRt25) + (_c[i] * lnRtRt25 * lnRtRt25) + (_d[i] * lnRtRt25 * lnRtRt25 * lnRtRt25));
00098 
00099   return true;
00100 }
00101 
00102 
00103 /**
00104  * Read the temperature in Fahrenheit.
00105  * @param link the DI Temp Sensor port number
00106  * @param temp the temperature value in Fahrenheit,
00107  * @return true if no error occured, false if it did
00108  */
00109 bool DTMPreadTempF(tSensors link, float &temp) {
00110   float _tempK = 0.0;
00111   if (!DTMPreadTempK(link, _tempK))
00112     return false;
00113 
00114   temp = 32 + ((_tempK - 273) * 9) / 5;
00115 
00116   return true;
00117 }
00118 
00119 #endif // __DTMP_DRIVER_H__
00120 
00121 /*
00122  * $Id: DTMP-driver.h 29 2010-06-25 12:55:41Z xander $
00123  */
00124 /* @} */
00125 /* @} */