1
0
mirror of https://github.com/MarlinFirmware/Marlin.git synced 2024-11-23 20:18:52 +00:00
MarlinFirmware/Marlin/temperature.h

510 lines
14 KiB
C
Raw Normal View History

/**
2016-03-24 18:01:20 +00:00
* Marlin 3D Printer Firmware
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
2016-04-29 01:18:13 +00:00
* temperature.h - temperature controller
*/
#ifndef TEMPERATURE_H
#define TEMPERATURE_H
#include "planner.h"
#include "thermistortables.h"
2016-04-29 01:18:13 +00:00
#include "MarlinConfig.h"
#if ENABLED(PID_EXTRUSION_SCALING)
#include "stepper.h"
#endif
2016-04-29 01:18:13 +00:00
#ifndef SOFT_PWM_SCALE
#define SOFT_PWM_SCALE 0
#endif
#define HOTEND_LOOP() for (int8_t e = 0; e < HOTENDS; e++)
#if HOTENDS == 1
2016-07-10 04:11:17 +00:00
#define HOTEND_INDEX 0
#define EXTRUDER_IDX 0
#else
2016-07-10 04:11:17 +00:00
#define HOTEND_INDEX e
#define EXTRUDER_IDX active_extruder
#endif
/**
* States for ADC reading in the ISR
*/
enum ADCSensorState {
#if HAS_TEMP_0
PrepareTemp_0,
MeasureTemp_0,
#endif
#if HAS_TEMP_1
PrepareTemp_1,
MeasureTemp_1,
#endif
#if HAS_TEMP_2
PrepareTemp_2,
MeasureTemp_2,
#endif
#if HAS_TEMP_3
PrepareTemp_3,
MeasureTemp_3,
#endif
#if HAS_TEMP_4
PrepareTemp_4,
MeasureTemp_4,
#endif
#if HAS_TEMP_BED
PrepareTemp_BED,
MeasureTemp_BED,
#endif
#if ENABLED(FILAMENT_WIDTH_SENSOR)
Prepare_FILWIDTH,
Measure_FILWIDTH,
#endif
SensorsReady, // Temperatures ready. Delay the next round of readings to let ADC pins settle.
StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
};
// Minimum number of Temperature::ISR loops between sensor readings.
// Multiplied by 16 (OVERSAMPLENR) to obtain the total time to
// get all oversampled sensor readings
#define MIN_ADC_ISR_LOOPS 10
#define ACTUAL_ADC_SAMPLES max(int(MIN_ADC_ISR_LOOPS), int(SensorsReady))
2016-04-29 01:18:13 +00:00
class Temperature {
2016-04-29 01:18:13 +00:00
public:
2016-07-06 15:50:41 +00:00
static float current_temperature[HOTENDS],
current_temperature_bed;
static int current_temperature_raw[HOTENDS],
target_temperature[HOTENDS],
current_temperature_bed_raw,
target_temperature_bed;
static volatile bool in_temp_isr;
2016-04-29 01:18:13 +00:00
#if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
2016-05-26 18:58:38 +00:00
static float redundant_temperature;
2016-04-29 01:18:13 +00:00
#endif
2016-04-28 01:06:32 +00:00
2016-09-24 05:51:21 +00:00
static uint8_t soft_pwm_bed;
2016-04-29 01:18:13 +00:00
#if ENABLED(FAN_SOFT_PWM)
2016-09-24 05:51:21 +00:00
static uint8_t fanSpeedSoftPwm[FAN_COUNT];
#endif
2016-04-29 01:18:13 +00:00
#if ENABLED(PIDTEMP) || ENABLED(PIDTEMPBED)
#define PID_dT ((OVERSAMPLENR * float(ACTUAL_ADC_SAMPLES)) / (F_CPU / 64.0 / 256.0))
#endif
2016-04-29 01:18:13 +00:00
#if ENABLED(PIDTEMP)
2015-04-03 23:38:05 +00:00
2016-07-12 05:08:14 +00:00
#if ENABLED(PID_PARAMS_PER_HOTEND) && HOTENDS > 1
2016-05-27 00:43:20 +00:00
static float Kp[HOTENDS], Ki[HOTENDS], Kd[HOTENDS];
#if ENABLED(PID_EXTRUSION_SCALING)
2016-05-27 00:43:20 +00:00
static float Kc[HOTENDS];
2016-04-29 01:18:13 +00:00
#endif
2016-07-12 05:08:14 +00:00
#define PID_PARAM(param, h) Temperature::param[h]
2016-04-29 01:18:13 +00:00
#else
2016-04-29 01:18:13 +00:00
static float Kp, Ki, Kd;
#if ENABLED(PID_EXTRUSION_SCALING)
2016-04-29 01:18:13 +00:00
static float Kc;
#endif
2016-07-12 05:08:14 +00:00
#define PID_PARAM(param, h) Temperature::param
2016-05-27 00:43:20 +00:00
#endif // PID_PARAMS_PER_HOTEND
2016-04-29 01:18:13 +00:00
// Apply the scale factors to the PID values
#define scalePID_i(i) ( (i) * PID_dT )
#define unscalePID_i(i) ( (i) / PID_dT )
#define scalePID_d(d) ( (d) / PID_dT )
#define unscalePID_d(d) ( (d) * PID_dT )
2016-04-29 01:18:13 +00:00
#endif
2016-04-29 01:18:13 +00:00
#if ENABLED(PIDTEMPBED)
2016-05-26 18:58:38 +00:00
static float bedKp, bedKi, bedKd;
2016-04-29 01:18:13 +00:00
#endif
2016-04-29 01:18:13 +00:00
#if ENABLED(BABYSTEPPING)
2016-05-26 18:58:38 +00:00
static volatile int babystepsTodo[3];
2016-04-29 01:18:13 +00:00
#endif
#if WATCH_HOTENDS
2016-05-27 00:43:20 +00:00
static int watch_target_temp[HOTENDS];
static millis_t watch_heater_next_ms[HOTENDS];
2016-04-29 01:18:13 +00:00
#endif
#if WATCH_THE_BED
2016-05-26 18:58:38 +00:00
static int watch_target_bed_temp;
static millis_t watch_bed_next_ms;
2016-04-29 01:18:13 +00:00
#endif
#if ENABLED(PREVENT_COLD_EXTRUSION)
2016-07-06 15:28:09 +00:00
static bool allow_cold_extrude;
2016-05-26 18:58:38 +00:00
static float extrude_min_temp;
static bool tooColdToExtrude(uint8_t e) {
#if HOTENDS == 1
UNUSED(e);
#endif
2016-07-06 15:28:09 +00:00
return allow_cold_extrude ? false : degHotend(HOTEND_INDEX) < extrude_min_temp;
}
2016-04-29 01:18:13 +00:00
#else
static bool tooColdToExtrude(uint8_t e) { UNUSED(e); return false; }
2016-04-29 01:18:13 +00:00
#endif
private:
#if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
2016-05-26 18:58:38 +00:00
static int redundant_temperature_raw;
static float redundant_temperature;
2016-04-29 01:18:13 +00:00
#endif
2016-05-26 18:58:38 +00:00
static volatile bool temp_meas_ready;
2016-04-29 01:18:13 +00:00
#if ENABLED(PIDTEMP)
2016-07-06 15:50:41 +00:00
static float temp_iState[HOTENDS],
temp_dState[HOTENDS],
pTerm[HOTENDS],
iTerm[HOTENDS],
dTerm[HOTENDS];
2016-04-29 01:18:13 +00:00
#if ENABLED(PID_EXTRUSION_SCALING)
2016-05-27 00:43:20 +00:00
static float cTerm[HOTENDS];
static long last_e_position;
2016-05-26 18:58:38 +00:00
static long lpq[LPQ_MAX_LEN];
static int lpq_ptr;
2016-04-29 01:18:13 +00:00
#endif
static float pid_error[HOTENDS];
static bool pid_reset[HOTENDS];
2016-04-29 01:18:13 +00:00
#endif
#if ENABLED(PIDTEMPBED)
2016-07-06 15:50:41 +00:00
static float temp_iState_bed,
temp_dState_bed,
pTerm_bed,
iTerm_bed,
dTerm_bed,
pid_error_bed;
2016-04-29 01:18:13 +00:00
#else
2016-05-26 18:58:38 +00:00
static millis_t next_bed_check_ms;
2016-04-29 01:18:13 +00:00
#endif
static unsigned long raw_temp_value[MAX_EXTRUDERS],
2016-07-06 15:50:41 +00:00
raw_temp_bed_value;
2016-04-29 01:18:13 +00:00
// Init min and max temp with extreme values to prevent false errors during startup
2016-07-06 15:50:41 +00:00
static int minttemp_raw[HOTENDS],
maxttemp_raw[HOTENDS],
minttemp[HOTENDS],
maxttemp[HOTENDS];
2016-04-29 01:18:13 +00:00
2016-07-09 02:28:37 +00:00
#ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
static int consecutive_low_temperature_error[HOTENDS];
#endif
#ifdef MILLISECONDS_PREHEAT_TIME
static unsigned long preheat_end_time[HOTENDS];
#endif
2016-04-29 01:18:13 +00:00
#ifdef BED_MINTEMP
2016-05-26 18:58:38 +00:00
static int bed_minttemp_raw;
2016-04-29 01:18:13 +00:00
#endif
#ifdef BED_MAXTEMP
2016-05-26 18:58:38 +00:00
static int bed_maxttemp_raw;
2016-04-29 01:18:13 +00:00
#endif
#if ENABLED(FILAMENT_WIDTH_SENSOR)
2016-05-26 18:58:38 +00:00
static int meas_shift_index; // Index of a delayed sample in buffer
2016-04-29 01:18:13 +00:00
#endif
#if HAS_AUTO_FAN
2016-05-26 18:58:38 +00:00
static millis_t next_auto_fan_check_ms;
2016-04-29 01:18:13 +00:00
#endif
2016-09-24 05:51:21 +00:00
static uint8_t soft_pwm[HOTENDS];
2016-04-29 01:18:13 +00:00
#if ENABLED(FAN_SOFT_PWM)
2016-09-24 05:51:21 +00:00
static uint8_t soft_pwm_fan[FAN_COUNT];
2016-04-29 01:18:13 +00:00
#endif
2016-04-29 01:18:13 +00:00
#if ENABLED(FILAMENT_WIDTH_SENSOR)
2016-05-26 18:58:38 +00:00
static int current_raw_filwidth; //Holds measured filament diameter - one extruder only
2016-04-29 01:18:13 +00:00
#endif
public:
/**
* Instance Methods
*/
Temperature();
void init();
2016-05-26 18:58:38 +00:00
/**
* Static (class) methods
*/
static float analog2temp(int raw, uint8_t e);
static float analog2tempBed(int raw);
2016-04-29 01:18:13 +00:00
/**
* Called from the Temperature ISR
*/
2016-05-26 18:58:38 +00:00
static void isr();
2016-04-29 01:18:13 +00:00
/**
* Call periodically to manage heaters
*/
//static void manage_heater(); // changed to address compiler error
static void manage_heater() __attribute__((__optimize__("O2")));
2016-04-29 01:18:13 +00:00
2016-07-09 02:28:37 +00:00
/**
* Preheating hotends
*/
#ifdef MILLISECONDS_PREHEAT_TIME
2016-07-12 05:08:47 +00:00
static bool is_preheating(uint8_t e) {
#if HOTENDS == 1
UNUSED(e);
#endif
return preheat_end_time[HOTEND_INDEX] && PENDING(millis(), preheat_end_time[HOTEND_INDEX]);
}
static void start_preheat_time(uint8_t e) {
#if HOTENDS == 1
UNUSED(e);
#endif
preheat_end_time[HOTEND_INDEX] = millis() + MILLISECONDS_PREHEAT_TIME;
}
static void reset_preheat_time(uint8_t e) {
#if HOTENDS == 1
UNUSED(e);
#endif
preheat_end_time[HOTEND_INDEX] = 0;
2016-07-09 02:28:37 +00:00
}
#else
#define is_preheating(n) (false)
#endif
2016-04-29 01:18:13 +00:00
#if ENABLED(FILAMENT_WIDTH_SENSOR)
2016-05-26 18:58:38 +00:00
static float analog2widthFil(); // Convert raw Filament Width to millimeters
static int widthFil_to_size_ratio(); // Convert raw Filament Width to an extrusion ratio
2016-04-29 01:18:13 +00:00
#endif
//high level conversion routines, for use outside of temperature.cpp
//inline so that there is no performance decrease.
//deg=degreeCelsius
2016-07-10 04:11:17 +00:00
static float degHotend(uint8_t e) {
2016-05-27 00:43:20 +00:00
#if HOTENDS == 1
2016-07-10 04:11:17 +00:00
UNUSED(e);
2016-05-27 00:43:20 +00:00
#endif
2016-07-10 04:11:17 +00:00
return current_temperature[HOTEND_INDEX];
2016-05-27 00:43:20 +00:00
}
static float degBed() { return current_temperature_bed; }
2016-04-29 01:18:13 +00:00
#if ENABLED(SHOW_TEMP_ADC_VALUES)
2016-07-10 04:11:17 +00:00
static float rawHotendTemp(uint8_t e) {
2016-05-27 00:43:20 +00:00
#if HOTENDS == 1
2016-07-10 04:11:17 +00:00
UNUSED(e);
2016-05-27 00:43:20 +00:00
#endif
2016-07-10 04:11:17 +00:00
return current_temperature_raw[HOTEND_INDEX];
2016-05-27 00:43:20 +00:00
}
static float rawBedTemp() { return current_temperature_bed_raw; }
2016-04-29 01:18:13 +00:00
#endif
2016-07-10 04:11:17 +00:00
static float degTargetHotend(uint8_t e) {
2016-05-27 00:43:20 +00:00
#if HOTENDS == 1
2016-07-10 04:11:17 +00:00
UNUSED(e);
2016-05-27 00:43:20 +00:00
#endif
2016-07-10 04:11:17 +00:00
return target_temperature[HOTEND_INDEX];
2016-05-27 00:43:20 +00:00
}
static float degTargetBed() { return target_temperature_bed; }
2016-04-29 01:18:13 +00:00
#if WATCH_HOTENDS
2016-07-10 04:11:17 +00:00
static void start_watching_heater(uint8_t e = 0);
2016-04-29 01:18:13 +00:00
#endif
#if WATCH_THE_BED
2016-05-26 18:58:38 +00:00
static void start_watching_bed();
2016-04-29 01:18:13 +00:00
#endif
2016-07-10 04:11:17 +00:00
static void setTargetHotend(const float& celsius, uint8_t e) {
2016-05-27 00:43:20 +00:00
#if HOTENDS == 1
2016-07-10 04:11:17 +00:00
UNUSED(e);
2016-05-27 00:43:20 +00:00
#endif
2016-07-09 02:28:37 +00:00
#ifdef MILLISECONDS_PREHEAT_TIME
if (celsius == 0.0f)
reset_preheat_time(HOTEND_INDEX);
else if (target_temperature[HOTEND_INDEX] == 0.0f)
start_preheat_time(HOTEND_INDEX);
2016-07-09 02:28:37 +00:00
#endif
2016-07-10 04:11:17 +00:00
target_temperature[HOTEND_INDEX] = celsius;
#if WATCH_HOTENDS
2016-07-10 04:11:17 +00:00
start_watching_heater(HOTEND_INDEX);
2016-04-29 01:18:13 +00:00
#endif
}
static void setTargetBed(const float& celsius) {
2016-04-29 01:18:13 +00:00
target_temperature_bed = celsius;
#if WATCH_THE_BED
2016-04-29 01:18:13 +00:00
start_watching_bed();
#endif
}
2016-04-29 01:18:13 +00:00
2016-07-10 04:11:17 +00:00
static bool isHeatingHotend(uint8_t e) {
2016-05-27 00:43:20 +00:00
#if HOTENDS == 1
2016-07-10 04:11:17 +00:00
UNUSED(e);
2016-05-27 00:43:20 +00:00
#endif
2016-07-10 04:11:17 +00:00
return target_temperature[HOTEND_INDEX] > current_temperature[HOTEND_INDEX];
2016-05-27 00:43:20 +00:00
}
static bool isHeatingBed() { return target_temperature_bed > current_temperature_bed; }
2016-04-29 01:18:13 +00:00
2016-07-10 04:11:17 +00:00
static bool isCoolingHotend(uint8_t e) {
2016-05-27 00:43:20 +00:00
#if HOTENDS == 1
2016-07-10 04:11:17 +00:00
UNUSED(e);
2016-05-27 00:43:20 +00:00
#endif
2016-07-10 04:11:17 +00:00
return target_temperature[HOTEND_INDEX] < current_temperature[HOTEND_INDEX];
2016-05-27 00:43:20 +00:00
}
static bool isCoolingBed() { return target_temperature_bed < current_temperature_bed; }
2016-04-29 01:18:13 +00:00
/**
* The software PWM power for a heater
*/
2016-05-26 18:58:38 +00:00
static int getHeaterPower(int heater);
2016-04-29 01:18:13 +00:00
/**
* Switch off all heaters, set all target temperatures to 0
*/
2016-05-26 18:58:38 +00:00
static void disable_all_heaters();
2016-04-29 01:18:13 +00:00
/**
* Perform auto-tuning for hotend or bed in response to M303
*/
#if HAS_PID_HEATING
2016-05-27 00:43:20 +00:00
static void PID_autotune(float temp, int hotend, int ncycles, bool set_result=false);
2016-04-29 01:18:13 +00:00
#endif
/**
* Update the temp manager when PID values change
*/
2016-05-26 18:58:38 +00:00
static void updatePID();
2016-04-29 01:18:13 +00:00
2016-10-10 20:34:35 +00:00
#if ENABLED(AUTOTEMP)
static void autotempShutdown() {
2016-04-29 01:18:13 +00:00
if (planner.autotemp_enabled) {
planner.autotemp_enabled = false;
2016-07-10 04:11:17 +00:00
if (degTargetHotend(EXTRUDER_IDX) > planner.autotemp_min)
setTargetHotend(0, EXTRUDER_IDX);
2016-04-29 01:18:13 +00:00
}
2016-10-10 20:34:35 +00:00
}
#endif
2016-04-29 01:18:13 +00:00
#if ENABLED(BABYSTEPPING)
2016-11-03 23:23:31 +00:00
static void babystep_axis(const AxisEnum axis, const int distance) {
if (axis_known_position[axis]) {
#if IS_CORE
#if ENABLED(BABYSTEP_XY)
switch (axis) {
case CORE_AXIS_1: // X on CoreXY and CoreXZ, Y on CoreYZ
babystepsTodo[CORE_AXIS_1] += distance * 2;
babystepsTodo[CORE_AXIS_2] += distance * 2;
break;
case CORE_AXIS_2: // Y on CoreXY, Z on CoreXZ and CoreYZ
babystepsTodo[CORE_AXIS_1] += CORESIGN(distance * 2);
babystepsTodo[CORE_AXIS_2] -= CORESIGN(distance * 2);
break;
case NORMAL_AXIS: // Z on CoreXY, Y on CoreXZ, X on CoreYZ
babystepsTodo[NORMAL_AXIS] += distance;
break;
}
#elif CORE_IS_XZ || CORE_IS_YZ
// Only Z stepping needs to be handled here
babystepsTodo[CORE_AXIS_1] += CORESIGN(distance * 2);
babystepsTodo[CORE_AXIS_2] -= CORESIGN(distance * 2);
#else
babystepsTodo[Z_AXIS] += distance;
#endif
#else
babystepsTodo[axis] += distance;
#endif
}
}
#endif // BABYSTEPPING
2016-04-29 01:18:13 +00:00
private:
2016-05-26 18:58:38 +00:00
static void set_current_temp_raw();
2016-04-29 01:18:13 +00:00
2016-05-26 18:58:38 +00:00
static void updateTemperaturesFromRawValues();
2016-04-29 01:18:13 +00:00
#if ENABLED(HEATER_0_USES_MAX6675)
2016-05-26 18:58:38 +00:00
static int read_max6675();
2016-04-29 01:18:13 +00:00
#endif
2016-05-26 18:58:38 +00:00
static void checkExtruderAutoFans();
2016-04-29 01:18:13 +00:00
2016-05-26 18:58:38 +00:00
static float get_pid_output(int e);
2016-04-29 01:18:13 +00:00
#if ENABLED(PIDTEMPBED)
2016-05-26 18:58:38 +00:00
static float get_pid_output_bed();
2016-04-29 01:18:13 +00:00
#endif
2016-05-26 18:58:38 +00:00
static void _temp_error(int e, const char* serial_msg, const char* lcd_msg);
2016-08-09 05:21:01 +00:00
static void min_temp_error(int8_t e);
static void max_temp_error(int8_t e);
2016-04-29 01:18:13 +00:00
#if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
typedef enum TRState { TRInactive, TRFirstHeating, TRStable, TRRunaway } TRstate;
2016-04-29 01:18:13 +00:00
2016-05-26 18:58:38 +00:00
static void thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc);
2016-04-29 01:18:13 +00:00
#if ENABLED(THERMAL_PROTECTION_HOTENDS)
2016-05-27 00:43:20 +00:00
static TRState thermal_runaway_state_machine[HOTENDS];
static millis_t thermal_runaway_timer[HOTENDS];
2016-04-29 01:18:13 +00:00
#endif
#if HAS_THERMALLY_PROTECTED_BED
2016-05-26 18:58:38 +00:00
static TRState thermal_runaway_bed_state_machine;
static millis_t thermal_runaway_bed_timer;
2016-04-29 01:18:13 +00:00
#endif
#endif // THERMAL_PROTECTION
};
extern Temperature thermalManager;
#endif // TEMPERATURE_H