Mario Simaremare / Mbed 2 deprecated g3_waterplay

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TemperatureSensor.cpp Source File

TemperatureSensor.cpp

00001 /*
00002 * G3: WATERPLAY
00003 */
00004 
00005 #include "TemperatureSensor.h"
00006 #include <algorithm>
00007 #include "mbed.h"
00008 
00009 TemperatureSensor::TemperatureSensor(
00010     Printer &printer,
00011     PinName pin
00012 ):
00013     _printer(printer),
00014     _analog_in(pin),
00015     _reading(0.0),
00016     _voltage(0.0),
00017     _temperature(0.0),
00018     _status(0.0),
00019     _strStatus("OK"),
00020     _in_boundary(false)
00021 {
00022     this->reload();
00023 }
00024 
00025 void TemperatureSensor::reload()
00026 {
00027     double readings[SAMPLING_NUMBER];
00028     for(int counter = 0; counter < SAMPLING_NUMBER; ++counter){
00029         readings[counter] = _analog_in.read();
00030     }
00031     
00032     sort(readings, readings + SAMPLING_NUMBER);
00033     
00034     this->_reading = readings[SAMPLING_NUMBER / 2];
00035     this->_voltage = this->_reading * VIN * CONVERTER;
00036     double RT = (this->_voltage * RESISTANCE) / (VIN - this->_voltage);
00037     double logRT = log(RT);
00038     double k0 = K0; 
00039     double k1 = K1 * logRT;
00040     double k2 = K2 * pow(logRT, 3.0);
00041     double kelvin = 1.0 / (k0 + k1 + k2);
00042     this->_temperature = (kelvin + KELVIN_TO_CELCIUS);
00043     
00044     if(this->_temperature > KELVIN_TO_CELCIUS){
00045         this->_temperature += VARIANCE;
00046         this->_temperature += 0.21*this->_temperature - 6.09;
00047     }
00048     
00049     this->_in_boundary = false;
00050     if(this->_temperature > 0.0 && this->_temperature < 100.0){
00051         this->_in_boundary = true;
00052     }
00053     
00054     this->_status = 0.0;
00055     this->_strStatus = "OK";
00056     if(this->_temperature < LOWER_BOUNDARY){
00057         this->_status = this->_temperature - LOWER_BOUNDARY;
00058         this->_strStatus = "LW";
00059     } else if(this->_temperature > UPPER_BOUNDARY){
00060         this->_status = this->_temperature - UPPER_BOUNDARY;
00061         this->_strStatus = "HI";
00062     }
00063     
00064     this->_in_boundary = false;
00065     if(this->_temperature > 0.0 && this->_temperature < 100.0){
00066         this->_in_boundary = true;
00067     }
00068 }
00069 
00070 double TemperatureSensor::getReading()
00071 {
00072     return(this->_reading);
00073 }
00074 
00075 
00076 double TemperatureSensor::getVoltage()
00077 {
00078     return(this->_voltage);
00079 }
00080 
00081 double TemperatureSensor::getTemperature()
00082 {
00083     return(this->_temperature);
00084 }
00085 
00086 double TemperatureSensor::getStatus()
00087 {
00088     return(this->_status);
00089 }
00090 
00091 char* TemperatureSensor::getStrStatus()
00092 {
00093     return(this->_strStatus);
00094 }
00095 
00096 bool TemperatureSensor::inBoundary(){
00097     return(this->_in_boundary);
00098 }
00099 
00100 bool TemperatureSensor::inDanger(){
00101     bool in_danger = false;
00102     if(this->_in_boundary && this->_status != 0.0){
00103         in_danger = true;
00104     }
00105     return(in_danger);
00106 }