PT100.cpp 14.7 KB
Newer Older
João Lino's avatar
v0.1  
João Lino committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
/*
  PT100.cpp - Library for measuring temperature with a PT100.
  Created by João Lino, June 24, 2015.
  Released into the public domain.
*/
#include "Arduino.h"
#include "PT100.h"

PT100::PT100(char *name,
                    int OutputPin_SensorPower, 
                    int OutputPin_ThirdLinePower, 
                    int InputPin_TemperatureReading, 
                    int InputPin_ThirdLineReading, 
					int TimeBetweenReadings, 
					float ADCVmax, 
					float Vs,
					float R1,
					float R2,
                    float m,
                    float b) {
	
    _name                              =   name;
    _OutputPin_SensorPower             =   OutputPin_SensorPower;
    _OutputPin_ThirdLinePower          =   OutputPin_ThirdLinePower;
    _InputPin_TemperatureReading       =   InputPin_TemperatureReading;
    _InputPin_ThirdLineReading         =   InputPin_ThirdLineReading;
	_TimeBetweenReadings               =   TimeBetweenReadings;
	_ADCVmax                           =   ADCVmax;
	_Vs                                =   Vs;
	_R1                                =   R1;
	_R2 	                           =   R2;
    __m = m;
    __b = b;
	
	_temperatureAverage	               =   24.0;
	_measuredTemperature               =   24.0;
	_lastTemperatureRead               =   0;
	_VoutAnalogSample                  =   -1;
	_VoutPreviousAnalogSample          =   -1.0;
	_temperatureMeasurementsMarker     =   0;
    _rPT100MeasurementsMarker          =   0;
    _rLineMeasurementsMarker           =   0;
    _measuredTemperatureDeviation      =   0.0;
    _sampleDeviation                   =   0.0;

    analogReference(INTERNAL1V1);									// EXTERNAL && INTERNAL2V56 && INTERNAL1V1
    pinMode(_OutputPin_SensorPower, OUTPUT);            // setup temperature sensor input pin
    pinMode(_OutputPin_ThirdLinePower, OUTPUT);            // setup temperature sensor input pin
    digitalWrite(_OutputPin_SensorPower, LOW);      // initialize sensor on
    digitalWrite(_OutputPin_ThirdLinePower, LOW);      // initialize sensor on
}

void PT100::setPower(float ADCVmax, float Vs) {
    _ADCVmax                           =   ADCVmax;
    _Vs                                =   Vs;
}

void PT100::safeHardwarePowerOff() {
  digitalWrite(_OutputPin_SensorPower, LOW);        // Turn temperature sensor OFF for safety
  digitalWrite(_OutputPin_ThirdLinePower, LOW);        // Turn temperature sensor OFF for safety
}

void PT100::measure(boolean ln) {
    if(millis() - _lastTemperatureRead >= _TimeBetweenReadings) {                           //time to measure temperature
    
    /** measure Vout analog sample */
    _VoutPreviousAnalogSample   =   _VoutAnalogSample;                              // Store previous
    //digitalWrite(_OutputPin_SensorPower, HIGH);                                         // Turn on sensor
    //delay(10);
    _VoutAnalogSample               = analogRead(_InputPin_TemperatureReading);     // Get a reading
    //float test = analogRead(A7);
    //digitalWrite(_OutputPin_SensorPower, LOW);                                          // Turn off sensor
    
    _lastTemperatureRead            = millis();                                             // Mark time of temperature reading

    /** Calculate temperature value */
    float Vout = (_VoutAnalogSample + _VoutPreviousAnalogSample) / 2 * _ADCVmax / CONSTANT_ADC_STEP_COUNT;
    //float Rtest = 608 / ( _Vs / (test * _ADCVmax / CONSTANT_ADC_STEP_COUNT) - 1.0);
    float Rx = _R1 / ( _Vs / Vout - 1.0);// - Rtest;
    float measuredTemperatureNow = 1.08271 * pow(10.0, -13.0) * (3.12508 * pow(10.0, 16.0) - 5.65566 * pow(10.0, 6.0) * sqrt(3.51501 * pow(10.0, 19.0) - 4.61805 * pow(10.0, 16.0) * Rx));

    float temperatureDiff = measuredTemperatureNow - _temperatureAverage;                                                                           // Calculate deviation between temperature reading and average temperature
    _temperatureMeasurementsMarker++;                                                                                                                                                   // Position reading buffer marker at the last updated position
    if(_temperatureMeasurementsMarker >= TEMPERATURE_AVERAGE_VALUE_I) _temperatureMeasurementsMarker = 0;           // Check that it has not gone out of the buffer range

    if( _temperatureMeasurements[_temperatureMeasurementsMarker] == 0.0 ) {
        // First Run
        if ( measuredTemperatureNow > 12.0 ) {
            // First Run with good data
            _temperatureMeasurements[_temperatureMeasurementsMarker] = measuredTemperatureNow;
        }
        return;
    }
    /*
    if(temperatureDiff > 5 || temperatureDiff < 5) {
        _temperatureMeasurements[_temperatureMeasurementsMarker] = measuredTemperatureNow;   // Write the average temp + one third of the measured temperature deviation
    }
    else {
        _temperatureMeasurements[_temperatureMeasurementsMarker] = _temperatureAverage + (temperatureDiff / 3.0);   // Write the average temp + one third of the measured temperature deviation
    }
    */
    //_temperatureMeasurements[_temperatureMeasurementsMarker] = measuredTemperatureNow; 
    _temperatureMeasurements[_temperatureMeasurementsMarker] = _temperatureAverage + (temperatureDiff / 3.0);   // Write the average temp + one third of the measured temperature deviation

    _temperatureAverage = 0.0;                                                                                      // Zero out the average, it is time to calculate the new one
    for(int markerCounter = 0; markerCounter < TEMPERATURE_AVERAGE_VALUE_I; markerCounter++) {                  // Iterate over the temperature values in the buffer
      _temperatureAverage += _temperatureMeasurements[markerCounter];                                           // Sum all the temperature values
    }
    _temperatureAverage = _temperatureAverage / TEMPERATURE_AVERAGE_VALUE_F;                                    // Divide the sum by the number of values summed.
    _measuredTemperature = _temperatureAverage;                                                                 // Set the measured temperature to the calculated average value


    xFilterNoise(_temperatureMeasurementsMarker);
    /*
    Serial.print("PT100 : [");
    Serial.print(_name);
    Serial.print("]\tVoutSample: [");
    Serial.print(_VoutAnalogSample);


    //    Serial.print("]\tVout[");
    //    Serial.print(Vout,6);
    //    Serial.print("]\tRx[");
    //    Serial.print(Rx,6);

    
    Serial.print("]\tTNow[");
    Serial.print(measuredTemperatureNow,6);
    Serial.print("]\tTCalc[");
    Serial.print(_measuredTemperature,6);
    Serial.println("] ");
    */


    //Serial.print("PT100 : [");
    #ifdef DEBUG
    Serial.print(_name);
    Serial.print(",");
    Serial.print(_VoutAnalogSample);


    Serial.print(",");

    /*Serial.print(test);
    Serial.print(",");
    Serial.print(Rtest);
    Serial.print(",");*/
    /*Serial.print(Vout,6);
    Serial.print(",");
    Serial.print(Rx,6);

    
    Serial.print(",");
    Serial.print(measuredTemperatureNow,6);
    Serial.print(",");
    Serial.print(_measuredTemperature,6);
    Serial.print(",");*/
    if(ln) Serial.println("");
    #endif
  }
}

void PT100::measure1(boolean ln, boolean rline) {
  if(millis() - _lastTemperatureRead >= _TimeBetweenReadings) {                           //time to measure temperature
    
    /** measure Vout analog sample */
    digitalWrite(_OutputPin_SensorPower, HIGH);      // initialize sensor on
    digitalWrite(_OutputPin_ThirdLinePower, HIGH);      // initialize sensor on
    delay(10);
    _VoutAnalogSample = analogRead(_InputPin_TemperatureReading) + _sampleDeviation;     // Get a reading
    _VoutRAnalogSample = analogRead(_InputPin_ThirdLineReading) + _sampleDeviation;     // Get a reading
    digitalWrite(_OutputPin_SensorPower, LOW);      // initialize sensor on
    digitalWrite(_OutputPin_ThirdLinePower, LOW);      // initialize sensor on

    _lastTemperatureRead            = millis();                                             // Mark time of temperature reading

    _rPT100MeasurementsMarker++;                                                                                                                                                   // Position reading buffer marker at the last updated position
    if(_rPT100MeasurementsMarker >= TEMPERATURE_AVERAGE_VALUE_I) _rPT100MeasurementsMarker = 0;           // Check that it has not gone out of the buffer range
    _rPT100Measurements[_rPT100MeasurementsMarker] = _VoutAnalogSample;
    float Vout = GetMedian(_rPT100Measurements) * _ADCVmax / CONSTANT_ADC_STEP_COUNT;
    float Rx = _R1 / ( _Vs / Vout - 1.0);

    if(rline) {
        _rLineMeasurementsMarker++;                                                                                                                                                   // Position reading buffer marker at the last updated position
        if(_rLineMeasurementsMarker >= TEMPERATURE_AVERAGE_VALUE_I) _rLineMeasurementsMarker = 0;           // Check that it has not gone out of the buffer range
        _rLineMeasurements[_rLineMeasurementsMarker] = _VoutRAnalogSample;

        /** Calculate temperature value */
        float VoutR = GetMedian(_rLineMeasurements) * _ADCVmax / CONSTANT_ADC_STEP_COUNT;
        float Rline = _R2 / ( _Vs / VoutR - 1.0);
        _measuredTemperature = 1.08271 * pow(10.0, -13.0) * (3.12508 * pow(10.0, 16.0) - 5.65566 * pow(10.0, 6.0) * sqrt(3.51501 * pow(10.0, 19.0) - 4.61805 * pow(10.0, 16.0) * (Rx - Rline)));
    }
    else {
        /** Calculate temperature value */
        _measuredTemperature = 1.08271 * pow(10.0, -13.0) * (3.12508 * pow(10.0, 16.0) - 5.65566 * pow(10.0, 6.0) * sqrt(3.51501 * pow(10.0, 19.0) - 4.61805 * pow(10.0, 16.0) * Rx));
    }
    

    //xFilterNoise(_temperatureMeasurementsMarker);
/*
    Serial.print("PT100 : [");
    Serial.print(_name);
    Serial.print("]\tVoutSample: [");
    Serial.print(_VoutAnalogSample);


//    Serial.print("]\tVout[");
//    Serial.print(Vout,6);
//    Serial.print("]\tRx[");
//    Serial.print(Rx,6);

    
    Serial.print("]\tTNow[");
    Serial.print(measuredTemperatureNow,6);
    Serial.print("]\tTCalc[");
    Serial.print(_measuredTemperature,6);
    Serial.println("] ");
*/


    //Serial.print("PT100 : [");
    #ifdef DEBUG
    Serial.print(_name);
    Serial.print(",");
    Serial.print(_VoutAnalogSample);
    Serial.print(",");
    //Serial.print(_VoutRAnalogSample);
    //Serial.print(",");

    /*Serial.print(test);
    Serial.print(",");
    Serial.print(Rtest);
    Serial.print(",");*/
    /*Serial.print(Vout,6);
    Serial.print(",");
    Serial.print(Rx,6);

    
    Serial.print(",");
    Serial.print(measuredTemperatureNow,6);
    Serial.print(",");
    Serial.print(_measuredTemperature,6);
    Serial.print(",");*/
    if(ln) Serial.println("");
    #endif
  }
}

float PT100::GetMedian(int array[]){
    int sorted[TEMPERATURE_AVERAGE_VALUE_I];
    float value = 0.0;

    for(int x = 0; x < TEMPERATURE_AVERAGE_VALUE_I; x++) {
        sorted[x] = array[x];
    }

     //ARRANGE VALUES
    for(int x = 0; x < TEMPERATURE_AVERAGE_VALUE_I; x++){
        for(int y = 0; y < TEMPERATURE_AVERAGE_VALUE_I - 1; y++){
            if(sorted[y]>sorted[y+1]){
                int temp = sorted[y+1];
                sorted[y+1] = sorted[y];
                sorted[y] = temp;
            }
        }
    }

    //CALCULATE THE MEDIAN (middle number)
    if(TEMPERATURE_AVERAGE_VALUE_I % 2 != 0){// is the # of elements odd?
        int temp = ((TEMPERATURE_AVERAGE_VALUE_I+1)/2)-1;
        value = (float) sorted[temp];
    }
    else{// then it's even! :)
        value = ((float) ( sorted[(TEMPERATURE_AVERAGE_VALUE_I/2)-1] + sorted[TEMPERATURE_AVERAGE_VALUE_I/2] )) / 2.0;
    }

    return value;
}

float PT100::GetMode(float new_array[]) {
    int ipRepetition[TEMPERATURE_AVERAGE_VALUE_I];
    for (int i = 0; i < TEMPERATURE_AVERAGE_VALUE_I; i++) {
        ipRepetition[i] = 0;//initialize each element to 0
        int j = 0;//
        while ((j < i) && (new_array[i] != new_array[j])) {
            if (new_array[i] != new_array[j]) {
                j++;
            }
        }
        (ipRepetition[j])++;
    }
    int iMaxRepeat = 0;
    for (int i = 1; i < TEMPERATURE_AVERAGE_VALUE_I; i++) {
        if (ipRepetition[i] > ipRepetition[iMaxRepeat]) {
            iMaxRepeat = i;
        }
    }

    return new_array[iMaxRepeat];
}
/*
void PT100::mean(int new_array[], int num){
 //GET TOTAL & CALCULATE MEAN
    float total;
    for(int i=0;i<num; i++){
        total += new_array[i];
    }
    cout << "The mean is " << total/num << endl;
    mode(new_array, num);
}
void PT100::median(int new_array[], int num){
    //CALCULATE THE MEDIAN (middle number)
    if(num % 2 != 0){// is the # of elements odd?
        int temp = ((num+1)/2)-1;
        cout << "The median is " << new_array[temp] << endl;
    }
    else{// then it's even! :)
        cout << "The median is "<< new_array[(num/2)-1] << " and " << new_array[num/2] << endl;
    }
    mean(new_array, num);
}*/

void PT100::xFilterNoise( int position ) {
    for( int i = 0; i < 10 ; i++ ) {
        int first = (position - 2 - i < 0)?(position - 2 - i + TEMPERATURE_AVERAGE_VALUE_I):(position - 2 - i);
        int second = (position - 1 - i < 0)?(position - 1 - i + TEMPERATURE_AVERAGE_VALUE_I):(position - 1 - i);
        int third = (position - i < 0)?(position - i + TEMPERATURE_AVERAGE_VALUE_I):(position - i);

        if( 
            ( ( _temperatureMeasurements[second] > _temperatureMeasurements[first] ) && ( _temperatureMeasurements[second] > _temperatureMeasurements[third] ) ) 
            ||
            ( ( _temperatureMeasurements[second] < _temperatureMeasurements[first] ) && ( _temperatureMeasurements[second] < _temperatureMeasurements[third] ) ) 
            )
        {
            _temperatureMeasurements[second] = ( _temperatureMeasurements[first] + _temperatureMeasurements[third] ) / 2;
        }
    }

}

float PT100::getCurrentTemperature() {
    float y = __m * _measuredTemperature + __b;
    return _measuredTemperature + y + _measuredTemperatureDeviation; // - 4.41;
}

float PT100::getMeasuredTemperatureDeviation() {
    return _measuredTemperatureDeviation; // - 4.41;
}
float PT100::setMeasuredTemperatureDeviation( float measuredTemperatureDeviation) {
    if( _measuredTemperatureDeviation != measuredTemperatureDeviation ) {
        _measuredTemperatureDeviation = measuredTemperatureDeviation;

        for( int i = 0; i < TEMPERATURE_AVERAGE_VALUE_I; i++ ) {
            _temperatureMeasurements[i] = _temperatureMeasurements[i] + ( _measuredTemperatureDeviation * -1 );
        }
    }
    
    return _measuredTemperatureDeviation; // - 4.41;
}

float PT100::setSampleDeviation( float sampleDeviation) {
    _sampleDeviation = sampleDeviation;

    return _sampleDeviation;
}