Skip to content

Commit

Permalink
Removed max temperature check when validating model parameters
Browse files Browse the repository at this point in the history
Also give details in the error message when validating model parameters
fails
  • Loading branch information
dc42 committed Mar 18, 2024
1 parent 1cafa22 commit 4d405fa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 deletions.
50 changes: 24 additions & 26 deletions src/Heating/FOPDT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,19 @@ FopDt::FopDt() noexcept
Reset();
}

// Check the model parameters are sensible, if they are then save them and return true.
bool FopDt::SetParameters(float phr, float pbcr, float pfcr, float pcrExponent, float pdt, float pMaxPwm, float temperatureLimit, float pVoltage, bool pUsePid, bool pInverted) noexcept
// Check the model parameters are sensible, if they are then save them and return true. If not then write an error message to reply and return false.
bool FopDt::SetParameters(float phr, float pbcr, float pfcr, float pcrExponent, float pdt, float pMaxPwm, float pVoltage, bool pUsePid, bool pInverted, const StringRef& reply) noexcept
{
// DC 2017-06-20: allow S down to 0.01 for one of our OEMs (use > 0.0099 because >= 0.01 doesn't work due to rounding error)
const float maxTempIncrease = max<float>(1500.0, temperatureLimit + 500.0);
if ( phr/pbcr > 0.1 // minimum 10C temperature rise (same as with earlier heater model)
&& EstimateMaxTemperatureRise(phr, pbcr, pcrExponent) <= maxTempIncrease // max temperature increase within limits
&& pfcr >= 0.0
&& pcrExponent >= 1.0
&& pcrExponent <= 1.6
&& pdt > 0.099
&& pdt * pbcr <= 50.0 // dead time less than half cooling time constant
&& pMaxPwm > 0.0099
&& pMaxPwm <= 1.0
)
const char *const err =
(phr/pbcr < 0.1) ? "estimated temperature rise too small" // minimum 10C temperature rise (same as with earlier heater model)
: (pfcr < 0.0) ? "fan reduces cooling rate"
: (pcrExponent < 1.0 || pcrExponent > 1.6) ? "cooling rate exponent out of range"
: (pdt <= 0.0099) ? "dead time too small"
: (pdt * pbcr > 50.0) ? "dead time too close to cooling time constant" // dead time less than half the cooling time constant
: (pMaxPwm <= 0.0099 || pMaxPwm > 1.0) ? "PWM out of range"
: nullptr;
if (err == nullptr)
{
heatingRate = phr;
basicCoolingRate = pbcr;
Expand All @@ -89,26 +87,25 @@ bool FopDt::SetParameters(float phr, float pbcr, float pfcr, float pcrExponent,
CalcPidConstants(100.0);
return true;
}
reply.printf("bad model parameters: %s", err);
return false;
}

#if SUPPORT_REMOTE_COMMANDS

bool FopDt::SetParameters(const CanMessageHeaterModelNewNew& msg, float temperatureLimit) noexcept
// Check the model parameters are sensible, if they are then save them and return true. If not then write an error message to reply and return false.
bool FopDt::SetParameters(const CanMessageHeaterModelNewNew& msg, const StringRef& reply) noexcept
{
// DC 2017-06-20: allow S down to 0.01 for one of our OEMs (use > 0.0099 because >= 0.01 doesn't work due to rounding error)
const float maxTempIncrease = max<float>(1500.0, temperatureLimit + 500.0);
if ( msg.heatingRate/msg.basicCoolingRate > 0.1 // minimum 10C temperature rise (same as with earlier heater model)
&& EstimateMaxTemperatureRise(msg.heatingRate, msg.basicCoolingRate, msg.coolingRateExponent) <= maxTempIncrease
// max temperature increase within limits
&& msg.fanCoolingRate >= 0.0
&& msg.coolingRateExponent >= 1.0
&& msg.coolingRateExponent <= 1.6
&& msg.deadTime > 0.099
&& msg.deadTime * msg.basicCoolingRate <= 50.0 // dead time less than half the cooling time constant
&& msg.maxPwm > 0.0099
&& msg.maxPwm <= 1.0
)
const char *const err =
(msg.heatingRate/msg.basicCoolingRate < 0.1) ? "estimated temperature rise too small" // minimum 10C temperature rise (same as with earlier heater model)
: (msg.fanCoolingRate < 0.0) ? "fan reduces cooling rate"
: (msg.coolingRateExponent < 1.0 || msg.coolingRateExponent > 1.6) ? "cooling rate exponent out of range"
: (msg.deadTime <= 0.0099) ? "dead time too small"
: (msg.deadTime * msg.basicCoolingRate > 50.0) ? "dead time too close to cooling time constant" // dead time less than half the cooling time constant
: (msg.maxPwm <= 0.0099 || msg.maxPwm > 1.0) ? "PWM out of range"
: nullptr;
if (err == nullptr)
{
heatingRate = msg.heatingRate;
basicCoolingRate = msg.basicCoolingRate;
Expand All @@ -132,6 +129,7 @@ bool FopDt::SetParameters(const CanMessageHeaterModelNewNew& msg, float temperat
enabled = true;
return true;
}
reply.printf("bad model parameters: %s", err);
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Heating/FOPDT.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ class FopDt INHERIT_OBJECT_MODEL
FopDt() noexcept;

void Reset() noexcept;
bool SetParameters(float phr, float pbcr, float pfcr, float pcrExponent, float pdt, float pMaxPwm, float temperatureLimit, float pVoltage, bool pUsePid, bool pInverted) noexcept;
bool SetParameters(float phr, float pbcr, float pfcr, float pcrExponent, float pdt, float pMaxPwm, float pVoltage, bool pUsePid, bool pInverted, const StringRef& reply) noexcept;
void SetDefaultToolParameters() noexcept;
void SetDefaultBedOrChamberParameters() noexcept;
#if SUPPORT_REMOTE_COMMANDS
bool SetParameters(const CanMessageHeaterModelNewNew& msg, float temperatureLimit) noexcept;
bool SetParameters(const CanMessageHeaterModelNewNew& msg, const StringRef& reply) noexcept;
#endif

// Stored parameters
Expand Down
7 changes: 2 additions & 5 deletions src/Heating/Heater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ GCodeResult Heater::SetOrReportModel(unsigned int heater, GCodeBuffer& gb, const
GCodeResult Heater::SetModel(float hr, float bcr, float fcr, float coolingRateExponent, float td, float maxPwm, float voltage, bool usePid, bool inverted, const StringRef& reply) noexcept
{
GCodeResult rslt;
if (model.SetParameters(hr, bcr, fcr, coolingRateExponent, td, maxPwm, GetHighestTemperatureLimit(), voltage, usePid, inverted))
if (model.SetParameters(hr, bcr, fcr, coolingRateExponent, td, maxPwm, voltage, usePid, inverted, reply))
{
if (model.IsEnabled())
{
Expand All @@ -251,7 +251,6 @@ GCodeResult Heater::SetModel(float hr, float bcr, float fcr, float coolingRateEx
}
else
{
reply.copy("bad model parameters");
rslt = GCodeResult::error;
}

Expand Down Expand Up @@ -704,8 +703,7 @@ GCodeResult Heater::SetFaultDetectionParameters(const CanMessageSetHeaterFaultDe

GCodeResult Heater::SetModel(unsigned int heater, const CanMessageHeaterModelNewNew& msg, const StringRef& reply) noexcept
{
const float temperatureLimit = GetHighestTemperatureLimit();
const bool rslt = model.SetParameters(msg, temperatureLimit);
const bool rslt = model.SetParameters(msg, reply);
if (rslt)
{
if (model.IsEnabled())
Expand All @@ -719,7 +717,6 @@ GCodeResult Heater::SetModel(unsigned int heater, const CanMessageHeaterModelNew
return GCodeResult::ok;
}

reply.copy("bad model parameters");
return GCodeResult::error;
}

Expand Down

0 comments on commit 4d405fa

Please sign in to comment.