diff --git a/Platform.cpp b/Platform.cpp index 769b568..d46d329 100644 --- a/Platform.cpp +++ b/Platform.cpp @@ -204,6 +204,7 @@ void Platform::Init() if(heatOnPins[i] >= 0) pinModeNonDue(heatOnPins[i], OUTPUT); thermistorInfRs[i] = ( thermistorInfRs[i]*exp(-thermistorBetas[i]/(25.0 - ABS_ZERO)) ); + tempSum[i] = 0; } if(coolingFanPin >= 0) @@ -212,6 +213,8 @@ void Platform::Init() analogWrite(coolingFanPin, 0); } + NumAtoDReadingsAveraged = 8; // must be an even number, preferably a power of 2 for performance, and no greater than 64 + InitialiseInterrupts(); addToTime = 0.0; @@ -251,6 +254,7 @@ void Platform::Spin() if(Time() - lastTime < 0.006) return; PollZHeight(); + PollTemperatures(); lastTime = Time(); ClassReport("Platform", longWait); @@ -359,15 +363,18 @@ void Platform::ClassReport(char* className, float &lastTime) float Platform::GetTemperature(int8_t heater) { // If the ADC reading is N then for an ideal ADC, the input voltage is at least N/(AD_RANGE + 1) and less than (N + 1)/(AD_RANGE + 1), times the analog reference. - // So we add 0.5 to to the reading to get a better estimate of the input. But first, recognise the special case of thermistor disconnected. - int rawTemp = GetRawTemperature(heater); - if (rawTemp == AD_RANGE) - { - // Thermistor is disconnected - return ABS_ZERO; - } + // So we add 0.5 to to the reading to get a better estimate of the input. + int rawTemp = tempSum[heater]/NumAtoDReadingsAveraged; //GetRawTemperature(heater); + + // First, recognise the special case of thermistor disconnected. +// if (rawTemp == AD_RANGE) +// { +// // Thermistor is disconnected +// return ABS_ZERO; +// } float r = (float)rawTemp + 0.5; - return ABS_ZERO + thermistorBetas[heater]/log( (r*thermistorSeriesRs[heater]/((AD_RANGE + 1) - r))/thermistorInfRs[heater] ); + r = ABS_ZERO + thermistorBetas[heater]/log( (r*thermistorSeriesRs[heater]/((AD_RANGE + 1) - r))/thermistorInfRs[heater] ); + return r; } diff --git a/Platform.h b/Platform.h index aa59a41..9143049 100644 --- a/Platform.h +++ b/Platform.h @@ -189,7 +189,6 @@ const unsigned int httpOutputBufferSize = 2 * 1432; #define BAUD_RATE 115200 // Communication speed of the USB if needed. const uint16_t lineBufsize = 256; // use a power of 2 for good performance -const uint16_t NumZProbeReadingsAveraged = 8; // must be an even number, preferably a power of 2 for performance, and no greater than 64 /****************************************************************************************************/ @@ -549,6 +548,7 @@ class Platform int zProbeADValue; float zProbeStopHeight; bool zProbeEnable; + // AXES void InitZProbe(); @@ -557,14 +557,13 @@ class Platform float axisLengths[AXES]; float homeFeedrates[AXES]; float headOffsets[AXES]; // FIXME - needs a 2D array -// bool zProbeStarting; -// float zProbeHigh; -// float zProbeLow; // HEATERS - Bed is assumed to be the first int GetRawTemperature(byte heater) const; + void PollTemperatures(); + long tempSum[HEATERS]; int8_t tempSensePins[HEATERS]; int8_t heatOnPins[HEATERS]; float thermistorBetas[HEATERS]; @@ -582,7 +581,8 @@ class Platform float standbyTemperatures[HEATERS]; float activeTemperatures[HEATERS]; int8_t coolingFanPin; - //int8_t turnHeatOn; + + uint16_t NumAtoDReadingsAveraged; // Smoothing filter on A to D readings // Serial/USB @@ -815,10 +815,12 @@ inline void Platform::PollZHeight() { uint16_t currentReading = GetRawZHeight(); + // We do a moving average of the probe's A to D readings to smooth out noise + if (zModOnThisTime) - zProbeOnSum = zProbeOnSum + currentReading - zProbeOnSum/NumZProbeReadingsAveraged; + zProbeOnSum = zProbeOnSum + currentReading - zProbeOnSum/NumAtoDReadingsAveraged; else - zProbeOffSum = zProbeOffSum + currentReading - zProbeOffSum/NumZProbeReadingsAveraged; + zProbeOffSum = zProbeOffSum + currentReading - zProbeOffSum/NumAtoDReadingsAveraged; zModOnThisTime = !zModOnThisTime; @@ -832,18 +834,18 @@ inline void Platform::PollZHeight() inline int Platform::ZProbe() const { return (zProbeType == 1) - ? (zProbeOnSum + zProbeOffSum)/NumZProbeReadingsAveraged // non-modulated mode + ? (zProbeOnSum + zProbeOffSum)/NumAtoDReadingsAveraged // non-modulated mode : (zProbeType == 2) - ? (zProbeOnSum - zProbeOffSum)/(NumZProbeReadingsAveraged/2) // modulated mode + ? (zProbeOnSum - zProbeOffSum)/(NumAtoDReadingsAveraged/2) // modulated mode : 0; // z-probe disabled } inline int Platform::ZProbeOnVal() const { return (zProbeType == 1) - ? (zProbeOnSum + zProbeOffSum)/NumZProbeReadingsAveraged + ? (zProbeOnSum + zProbeOffSum)/NumAtoDReadingsAveraged : (zProbeType == 2) - ? zProbeOnSum/(NumZProbeReadingsAveraged/2) + ? zProbeOnSum/(NumAtoDReadingsAveraged/2) : 0; } @@ -887,6 +889,14 @@ inline int Platform::GetRawTemperature(byte heater) const return 0; } +inline void Platform::PollTemperatures() +{ + // We do a moving average of each thermometer's A to D readings to smooth out noise + + for(int8_t heater = 0; heater < HEATERS; heater++) + tempSum[heater] = tempSum[heater] + GetRawTemperature(heater) - tempSum[heater]/NumAtoDReadingsAveraged; +} + inline float Platform::HeatSampleTime() const { return heatSampleTime;