eka sinambela / Mbed 2 deprecated g3_waterplay

Dependencies:   mbed

Fork of g3_waterplay by Mario Simaremare

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 {
00021     this->reload();
00022 }
00023 
00024 void TemperatureSensor::reload()
00025 {
00026     double readings[SAMPLING_NUMBER];
00027     for(int counter = 0; counter < SAMPLING_NUMBER; ++counter){
00028         readings[counter] = _analog_in.read();
00029     }
00030     
00031     sort(readings, readings + SAMPLING_NUMBER);
00032     
00033     this->_reading = readings[SAMPLING_NUMBER / 2];
00034     this->_voltage = this->_reading * VIN * CONVERTER;
00035     double RT = (this->_voltage * RESISTANCE) / (VIN - this->_voltage);
00036     double logRT = log(RT);
00037     double k0 = K0; 
00038     double k1 = K1 * logRT;
00039     double k2 = K2 * pow(logRT, 3.0);
00040     double kelvin = 1.0 / (k0 + k1 + k2);
00041     this->_temperature = (kelvin + KELVIN_TO_CELCIUS);
00042     
00043     if(this->_temperature > KELVIN_TO_CELCIUS){
00044         this->_temperature += VARIANCE;
00045         this->_temperature += 0.21*this->_temperature - 6.09;
00046     }
00047     
00048     this->_status = 0.0;
00049     this->_strStatus = "OK";
00050     if(this->_temperature < LOWER_BOUNDARY){
00051         this->_status = this->_temperature - LOWER_BOUNDARY;
00052         this->_strStatus = "LW";
00053     } else if(this->_temperature > UPPER_BOUNDARY){
00054         this->_status = this->_temperature - UPPER_BOUNDARY;
00055         this->_strStatus = "HI";
00056     }
00057 }
00058 
00059 double TemperatureSensor::getReading()
00060 {
00061     return(this->_reading);
00062 }
00063 
00064 
00065 double TemperatureSensor::getVoltage()
00066 {
00067     return(this->_voltage);
00068 }
00069 
00070 double TemperatureSensor::getTemperature()
00071 {
00072     return(this->_temperature);
00073 }
00074 
00075 double TemperatureSensor::getStatus()
00076 {
00077     return(this->_status);
00078 }
00079 
00080 char* TemperatureSensor::getStrStatus()
00081 {
00082     return(this->_strStatus);
00083 }