Fixed z probe averaging code.

This commit is contained in:
Adrian Bowyer 2014-04-10 14:25:09 +01:00
parent cced20f303
commit adb6ac3fc6
4 changed files with 44 additions and 40 deletions

View file

@ -222,7 +222,8 @@ void Platform::Init()
void Platform::InitZProbe() void Platform::InitZProbe()
{ {
zProbeCount = 0; // zProbeCount = 0;
zModOnThisTime = true;
zProbeOnSum = 0; zProbeOnSum = 0;
zProbeOffSum = 0; zProbeOffSum = 0;
@ -231,7 +232,7 @@ void Platform::InitZProbe()
pinMode(zProbeModulationPin, OUTPUT); pinMode(zProbeModulationPin, OUTPUT);
digitalWrite(zProbeModulationPin, HIGH); // enable the IR LED digitalWrite(zProbeModulationPin, HIGH); // enable the IR LED
} }
LastZProbeReading = GetRawZHeight(); // LastZProbeReading = GetRawZHeight();
} }

View file

@ -503,11 +503,11 @@ class Platform
float ZProbeStopHeight() const; // The height above the bed at which the probe is triggered float ZProbeStopHeight() const; // The height above the bed at which the probe is triggered
void SetZProbeStopHeight(float z); // Set the height above the bed at which the probe is triggered void SetZProbeStopHeight(float z); // Set the height above the bed at which the probe is triggered
int ZProbe() const; // Get an A->D value from the probe. This may have been averaged etc unsigned int ZProbe() const; // Get an A->D value from the probe. This may have been averaged etc
int ZProbeOnVal() const; // Returns on value for a modulated probe; total for non-modulated unsigned int ZProbeOnVal() const; // Returns on value for a modulated probe; total for non-modulated
void SetZProbe(int iZ); // Set the A->D value corresponding to SetZProbeStopHeight() void SetZProbe(unsigned int iZ); // Set the A->D value corresponding to SetZProbeStopHeight()
void SetZProbeType(int iZ); // None (0), non-modulating (1) or modulating (2) void SetZProbeType(int8_t iZ); // None (0), non-modulating (1) or modulating (2)
int GetZProbeType() const; // Get the probe type: None (0), non-modulating (1) or modulating (2) int8_t GetZProbeType() const; // Get the probe type: None (0), non-modulating (1) or modulating (2)
// Heat and temperature // Heat and temperature
@ -564,18 +564,19 @@ class Platform
// Z probe // Z probe
void InitZProbe();
void PollZHeight();
int8_t zProbePin; int8_t zProbePin;
int8_t zProbeModulationPin; int8_t zProbeModulationPin;
int8_t zProbeType; int8_t zProbeType;
uint8_t zProbeCount; // uint8_t zProbeCount;
long zProbeOnSum; // sum of readings taken when IR led is on bool zModOnThisTime;
long zProbeOffSum; // sum of readings taken when IR led is on unsigned long zProbeOnSum; // sum of readings taken when IR led is on
uint16_t LastZProbeReading; unsigned long zProbeOffSum; // sum of readings taken when IR led is on
// uint16_t LastZProbeReading;
int zProbeADValue; int zProbeADValue;
float zProbeStopHeight; float zProbeStopHeight;
bool zProbeEnable; bool zProbeEnable;
void InitZProbe();
void PollZHeight();
// AXES // AXES
@ -823,7 +824,7 @@ inline int Platform::GetRawZHeight() const
return (zProbeType != 0) ? analogRead(zProbePin) : 0; return (zProbeType != 0) ? analogRead(zProbePin) : 0;
} }
inline int Platform::ZProbe() const inline unsigned int Platform::ZProbe() const
{ {
return (zProbeType == 1) return (zProbeType == 1)
? (zProbeOnSum + zProbeOffSum)/NumZProbeReadingsAveraged // non-modulated mode ? (zProbeOnSum + zProbeOffSum)/NumZProbeReadingsAveraged // non-modulated mode
@ -832,7 +833,7 @@ inline int Platform::ZProbe() const
: 0; // z-probe disabled : 0; // z-probe disabled
} }
inline int Platform::ZProbeOnVal() const inline unsigned int Platform::ZProbeOnVal() const
{ {
return (zProbeType == 1) return (zProbeType == 1)
? (zProbeOnSum + zProbeOffSum)/NumZProbeReadingsAveraged ? (zProbeOnSum + zProbeOffSum)/NumZProbeReadingsAveraged
@ -841,6 +842,25 @@ inline int Platform::ZProbeOnVal() const
: 0; : 0;
} }
inline void Platform::PollZHeight()
{
uint16_t currentReading = GetRawZHeight();
// Compute a moving average
if (zModOnThisTime)
zProbeOnSum = zProbeOnSum + currentReading - zProbeOnSum/NumZProbeReadingsAveraged;
else
zProbeOffSum = zProbeOffSum + currentReading - zProbeOffSum/NumZProbeReadingsAveraged;
// LastZProbeReading = currentReading;
// zProbeCount = (zProbeCount + 1) % NumZProbeReadingsAveraged;
zModOnThisTime = !zModOnThisTime;
if (zProbeType == 2)
{
// Reverse the modulation, ready for next time
//digitalWrite(zProbeModulationPin, (zProbeCount & 1) ? HIGH : LOW);
digitalWrite(zProbeModulationPin, zModOnThisTime ? HIGH : LOW);
}
}
inline float Platform::ZProbeStopHeight() const inline float Platform::ZProbeStopHeight() const
{ {
return zProbeStopHeight; return zProbeStopHeight;
@ -851,41 +871,22 @@ inline void Platform::SetZProbeStopHeight(float z)
zProbeStopHeight = z; zProbeStopHeight = z;
} }
inline void Platform::SetZProbe(int iZ) inline void Platform::SetZProbe(unsigned int iZ)
{ {
zProbeADValue = iZ; zProbeADValue = iZ;
} }
inline void Platform::SetZProbeType(int pt) inline void Platform::SetZProbeType(int8_t pt)
{ {
zProbeType = (pt >= 0 && pt <= 2) ? pt : 0; zProbeType = (pt >= 0 && pt <= 2) ? pt : 0;
InitZProbe(); InitZProbe();
} }
inline int Platform::GetZProbeType() const inline int8_t Platform::GetZProbeType() const
{ {
return zProbeType; return zProbeType;
} }
inline void Platform::PollZHeight()
{
uint16_t currentReading = GetRawZHeight();
if (zProbeType == 2)
{
// Reverse the modulation, ready for next time
digitalWrite(zProbeModulationPin, (zProbeCount & 1) ? HIGH : LOW);
}
if (zProbeCount & 1)
{
zProbeOffSum = zProbeOffSum - LastZProbeReading + currentReading;
}
else
{
zProbeOnSum = zProbeOnSum - LastZProbeReading + currentReading;
}
LastZProbeReading = currentReading;
zProbeCount = (zProbeCount + 1) % NumZProbeReadingsAveraged;
}
//******************************************************************************************************** //********************************************************************************************************

View file

@ -193,9 +193,8 @@ void RepRap::Init()
platform->Message(HOST_MESSAGE, "\n"); platform->Message(HOST_MESSAGE, "\n");
platform->Message(HOST_MESSAGE, NAME); platform->Message(HOST_MESSAGE, NAME);
platform->Message(HOST_MESSAGE, " is up and running.\n"); platform->Message(HOST_MESSAGE, " is up and running.\n");
ResetLoopTimers();
lastTime = platform->Time(); lastTime = platform->Time();
fastTime = FLT_MAX;
slowTime = 0;
} }
void RepRap::Exit() void RepRap::Exit()
@ -223,7 +222,7 @@ void RepRap::Spin()
// Track the loop time // Track the loop time
float t = platform->Time(); float t = platform->Time();
float d = t = lastTime; float d = t - lastTime;
if(d < fastTime) if(d < fastTime)
fastTime = d; fastTime = d;
if(d > slowTime) if(d > slowTime)
@ -240,6 +239,7 @@ void RepRap::Diagnostics()
webserver->Diagnostics(); webserver->Diagnostics();
snprintf(scratchString, STRING_LENGTH, "Slowest loop time was %f secs; fastest was %f secs.\n", slowTime, fastTime); snprintf(scratchString, STRING_LENGTH, "Slowest loop time was %f secs; fastest was %f secs.\n", slowTime, fastTime);
platform->Message(HOST_MESSAGE, scratchString); platform->Message(HOST_MESSAGE, scratchString);
ResetLoopTimers();
} }
// Turn off the heaters, disable the motors, and // Turn off the heaters, disable the motors, and

View file

@ -39,6 +39,7 @@ class RepRap
Heat* GetHeat() const; // Get the instance of the class that handles all heat and temperature Heat* GetHeat() const; // Get the instance of the class that handles all heat and temperature
GCodes* GetGCodes() const; // Get the instance of the class that handles G Codes from all sources GCodes* GetGCodes() const; // Get the instance of the class that handles G Codes from all sources
Webserver* GetWebserver() const; // Get the instance of the class that handles Web traffic Webserver* GetWebserver() const; // Get the instance of the class that handles Web traffic
void ResetLoopTimers(); // Reset the max and min for loop diagnostics
private: private:
@ -58,6 +59,7 @@ inline Heat* RepRap::GetHeat() const { return heat; }
inline GCodes* RepRap::GetGCodes() const { return gCodes; } inline GCodes* RepRap::GetGCodes() const { return gCodes; }
inline Webserver* RepRap::GetWebserver() const { return webserver; } inline Webserver* RepRap::GetWebserver() const { return webserver; }
inline bool RepRap::Debug() const { return debug; } inline bool RepRap::Debug() const { return debug; }
inline void RepRap::ResetLoopTimers() { fastTime = FLT_MAX; slowTime = 0.0; }
inline void RepRap::SetDebug(bool d) inline void RepRap::SetDebug(bool d)
{ {