This repository has been archived on 2025-02-01. You can view files and clone it, but cannot push or open issues or pull requests.
reprapfirmware-dc42/Heat.cpp
David Crocker 6525a46a52 Version 1.09m
New features
============
The PWM frequency for the heated bed and for any heater used as a
chamber heater is now 10Hz for bettercompatibility with DC-AC SSRs.

The PWM frequency for fans is now configurable using the F paramete ron
the M106 command. The default is 500Hz, which gives esonable control of
fans not designed for PWM. Increase it to 25000Hz when using 4-wire PWM
fans.

When a Duet 0.8.5 board is configured or detected, the fan control is
now automatically inverted. If you previously used M106 P0 I1 in
config.g to invert it, you will need to remove that.

M579 (scale Cartesian axes) is now implemented (thanks chrishamm).

M114, M119 and M573 commands can now be executed concurrently with other
commands.

When DDA debugging is enabled, the debug output now includes all active
extruders instead of just the first two.

M408 S0 now includes the fan speeds (for PanelDue).

M119 now reports the Z probe as well as the endstop switch states.

A tool can now be defined even if a tool with the same tool number
exists already. The existing tool will be shut down and deleted.

The bed heater can now be disabled using M140 S-1 (thanks chrishamm).

The chamber heater (if present) and the endstop switch states are now
reported to the web interface (thanks chrishamm).

Increased defauklt Z prove dive height to 5mm.

Increased default PID Ki to 0.2

Bug fixes
=========
On a CoreXY machine, XY speeds were too low by a factor of sqrt(2).

On a delta machine, after running auto calibration the Z=0 height could
be slightly inaccurate, depending on the difference between the X and Z
endstop corrections

When using a non-intelligent modulated Z probe on a Duet 0.8.5, the
modulation pin number was incorrect.

The M27 (Report SD card print status) response was inverted compared to
what it should be. When in Marlin mode it now includes the "byte n/m"
field that some versions of Pronterface expect.

Cold extrusion prevention did not work - an error message was generated,
but the extruder was driven anyway.

M999 PERASE is now more reliable (thanks chrishamm).

M23, M30 and M32 commands did not work when the filename parameter
passed included an absolute path.

//A T command inside a macro file did not execute the tool change macros
files.

A memory leak occurred when a tool was deleted.

All moves are now completed before switching to CoreXY mode.

Polling requests from PanelDue were not relied to when a macro was being
executed

M667 with no parameters returned an incorrect string
2015-12-06 22:12:31 +00:00

275 lines
7.2 KiB
C++

/****************************************************************************************************
RepRapFirmware - Heat
This is all the code to deal with heat and temperature.
-----------------------------------------------------------------------------------------------------
Version 0.1
18 November 2012
Adrian Bowyer
RepRap Professional Ltd
http://reprappro.com
Licence: GPL
****************************************************************************************************/
#include "RepRapFirmware.h"
const float invHeatPwmAverageCount = HEAT_SAMPLE_TIME/HEAT_PWM_AVERAGE_TIME;
Heat::Heat(Platform* p) : platform(p), active(false), bedHeater(0), chamberHeater(-1)
{
for (size_t heater = 0; heater < HEATERS; heater++)
{
pids[heater] = new PID(platform, heater);
}
}
void Heat::Init()
{
for (size_t heater = 0; heater < HEATERS; heater++)
{
pids[heater]->Init();
}
lastTime = platform->Time();
longWait = lastTime;
active = true;
}
void Heat::Exit()
{
for (size_t heater = 0; heater < HEATERS; heater++)
{
pids[heater]->SwitchOff();
}
platform->Message(HOST_MESSAGE, "Heat class exited.\n");
active = false;
}
void Heat::Spin()
{
if (active)
{
float t = platform->Time();
if (t - lastTime >= platform->HeatSampleTime())
{
lastTime = t;
for (size_t heater=0; heater < HEATERS; heater++)
{
pids[heater]->Spin();
}
platform->ClassReport(longWait);
}
}
}
void Heat::Diagnostics()
{
platform->AppendMessage(BOTH_MESSAGE, "Heat Diagnostics:\n");
for (size_t heater=0; heater < HEATERS; heater++)
{
if (pids[heater]->active)
{
platform->AppendMessage(BOTH_MESSAGE, "Heater %d: I-accumulator = %.1f\n", heater, pids[heater]->temp_iState);
}
}
}
bool Heat::AllHeatersAtSetTemperatures(bool includingBed) const
{
for (int8_t heater = (includingBed) ? 0 : 1; heater < HEATERS; heater++)
{
if (!HeaterAtSetTemperature(heater))
return false;
}
return true;
}
//query an individual heater
bool Heat::HeaterAtSetTemperature(int8_t heater) const
{
if (pids[heater]->SwitchedOff()) // If it hasn't anything to do, it must be right wherever it is...
return true;
float dt = GetTemperature(heater);
float target = (pids[heater]->Active()) ? GetActiveTemperature(heater) : GetStandbyTemperature(heater);
return (target < TEMPERATURE_LOW_SO_DONT_CARE) || (fabs(dt - target) <= TEMPERATURE_CLOSE_ENOUGH);
}
//******************************************************************************************************
PID::PID(Platform* p, int8_t h) : platform(p), heater(h)
{
}
void PID::Init()
{
SetHeater(0.0);
temperature = platform->GetTemperature(heater);
activeTemperature = ABS_ZERO;
standbyTemperature = ABS_ZERO;
lastTemperature = temperature;
temp_iState = 0.0;
badTemperatureCount = 0;
temperatureFault = false;
active = false; // Default to standby temperature
switchedOff = true;
heatingUp = false;
averagePWM = 0.0;
}
void PID::SwitchOn()
{
// if(reprap.Debug())
// {
// snprintf(scratchString, STRING_LENGTH, "Heater %d switched on.\n", heater);
// platform->Message(BOTH_MESSAGE, scratchString);
// }
switchedOff = false;
}
void PID::SetHeater(float power) const
{
platform->SetHeater(heater, power);
}
void PID::Spin()
{
// Always know our temperature, regardless of whether we have been switched on or not
temperature = platform->GetTemperature(heater);
// If we're not switched on, or there's a fault, turn the power off and go home.
// If we're not switched on, then nothing is using us. This probably means that
// we don't even have a thermistor connected. So don't even check for faults if we
// are not switched on. This is safe, as the next bit of code always turns our
// heater off in that case anyway.
if (temperatureFault || switchedOff)
{
SetHeater(0.0); // Make sure...
averagePWM *= (1.0 - invHeatPwmAverageCount);
return;
}
// We are switched on. Check for faults. Temperature silly-low or silly-high mean open-circuit
// or shorted thermistor respectively.
if (temperature < BAD_LOW_TEMPERATURE || temperature > BAD_HIGH_TEMPERATURE)
{
badTemperatureCount++;
if (badTemperatureCount > MAX_BAD_TEMPERATURE_COUNT)
{
SetHeater(0.0);
temperatureFault = true;
//switchedOff = true;
platform->Message(BOTH_MESSAGE, "Temperature fault on heater %d, T = %.1f\n", heater, temperature);
reprap.FlagTemperatureFault(heater);
}
}
else
{
badTemperatureCount = 0;
}
// Now check how long it takes to warm up. If too long, maybe the thermistor is not in contact with the heater
if (heatingUp && heater != BED_HEATER) // FIXME - also check bed warmup time?
{
float tmp = (active) ? activeTemperature : standbyTemperature;
if (temperature < tmp - TEMPERATURE_CLOSE_ENOUGH)
{
float tim = platform->Time() - timeSetHeating;
float limit = platform->TimeToHot();
if (tim > limit && limit > 0.0)
{
SetHeater(0.0);
temperatureFault = true;
//switchedOff = true;
platform->Message(BOTH_MESSAGE, "Heating fault on heater %d, T = %.1f C; still not at temperature %.1f after %f seconds.\n", heater, temperature, tmp, tim);
reprap.FlagTemperatureFault(heater);
}
}
else
{
heatingUp = false;
}
}
float targetTemperature = (active) ? activeTemperature : standbyTemperature;
float error = targetTemperature - temperature;
const PidParameters& pp = platform->GetPidParameters(heater);
if (!pp.UsePID())
{
float heaterValue = (error > 0.0) ? min<float>(pp.kS, 1.0) : 0.0;
SetHeater(heaterValue);
averagePWM = averagePWM * (1.0 - invHeatPwmAverageCount) + heaterValue;
return;
}
if(error < -pp.fullBand)
{
// actual temperature is well above target
temp_iState = (targetTemperature + pp.fullBand - 25.0) * pp.kT; // set the I term to our estimate of what will be needed ready for the switch to PID
SetHeater(0.0);
averagePWM *= (1.0 - invHeatPwmAverageCount);
lastTemperature = temperature;
return;
}
if(error > pp.fullBand)
{
// actual temperature is well below target
temp_iState = (targetTemperature - pp.fullBand - 25.0) * pp.kT; // set the I term to our estimate of what will be needed ready for the switch to PID
float heaterValue = min<float>(pp.kS, 1.0);
SetHeater(heaterValue);
averagePWM = averagePWM * (1.0 - invHeatPwmAverageCount) + heaterValue;
lastTemperature = temperature;
return;
}
float sampleInterval = platform->HeatSampleTime();
temp_iState += error * pp.kI * sampleInterval;
if (temp_iState < pp.pidMin)
{
temp_iState = pp.pidMin;
}
else if (temp_iState > pp.pidMax)
{
temp_iState = pp.pidMax;
}
float temp_dState = pp.kD * (temperature - lastTemperature) / sampleInterval;
float result = (pp.kP * error + temp_iState - temp_dState) * pp.kS / 255.0;
lastTemperature = temperature;
if (result < 0.0)
{
result = 0.0;
}
else if (result > 1.0)
{
result = 1.0;
}
if (!temperatureFault)
{
SetHeater(result);
}
averagePWM = averagePWM * (1.0 - invHeatPwmAverageCount) + result;
// debugPrintf("Heater %d: e=%f, P=%f, I=%f, d=%f, r=%f\n", heater, error, pp.kP*error, temp_iState, temp_dState, result);
}
float PID::GetAveragePWM() const
{
return averagePWM * invHeatPwmAverageCount;
}
// End