Prusa3d Marlin fork
temperature.h
1 /*
2  temperature.h - temperature controller
3  Part of Marlin
4 
5  Copyright (c) 2011 Erik van der Zalm
6 
7  Grbl is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Grbl is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Grbl. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef temperature_h
22 #define temperature_h
23 
24 #include "Marlin.h"
25 #include "config.h"
26 
27 // public functions
28 void soft_pwm_init(); //initialize the soft pwm isr
29 void temp_mgr_init(); //initialize the temperature handler
30 void manage_heater(); //it is critical that this is called periodically.
31 bool get_temp_error(); //return true if any thermal error is set
32 
33 // low level conversion routines
34 // do not use these routines and variables outside of temperature.cpp
35 extern int target_temperature[EXTRUDERS];
36 extern float current_temperature[EXTRUDERS];
37 #ifdef SHOW_TEMP_ADC_VALUES
38  extern int current_temperature_raw[EXTRUDERS];
39  extern int current_temperature_bed_raw;
40 #endif
41 extern int target_temperature_bed;
42 extern float current_temperature_bed;
43 
44 #ifdef PINDA_THERMISTOR
45 extern uint16_t current_temperature_raw_pinda;
46 extern float current_temperature_pinda;
47 bool has_temperature_compensation();
48 #endif
49 
50 #ifdef AMBIENT_THERMISTOR
51 extern int current_temperature_raw_ambient;
52 extern float current_temperature_ambient;
53 #endif
54 
55 #ifdef VOLT_PWR_PIN
56 extern int current_voltage_raw_pwr;
57 #endif
58 
59 #ifdef VOLT_BED_PIN
60 extern int current_voltage_raw_bed;
61 #endif
62 
63 extern bool bedPWMDisabled;
64 
65 #ifdef PIDTEMP
66  extern int pid_cycle, pid_number_of_cycles;
67  extern float _Kp,_Ki,_Kd;
68  float scalePID_i(float i);
69  float scalePID_d(float d);
70  float unscalePID_i(float i);
71  float unscalePID_d(float d);
72 
73  bool pidTuningRunning(); // returns true if PID tuning is still running
74  void preparePidTuning(); // non-blocking call to set "pidTuningRunning" to true immediately
75 #endif
76 
77 
78 #ifdef BABYSTEPPING
79 extern volatile int babystepsTodo[3];
80 
81 inline void babystepsTodoZadd(int n)
82 {
83  if (n != 0) {
84  CRITICAL_SECTION_START
85  babystepsTodo[Z_AXIS] += n;
86  CRITICAL_SECTION_END
87  }
88 }
89 #endif
90 
91 void resetPID(uint8_t extruder);
92 
93 //high level conversion routines, for use outside of temperature.cpp
94 //inline so that there is no performance decrease.
95 //deg=degreeCelsius
96 
97 // Doesn't save FLASH when FORCE_INLINE removed.
98 FORCE_INLINE float degHotend(uint8_t extruder) {
99  return current_temperature[extruder];
100 };
101 
102 #ifdef SHOW_TEMP_ADC_VALUES
103  FORCE_INLINE float rawHotendTemp(uint8_t extruder) {
104  return current_temperature_raw[extruder];
105  };
106 
107  FORCE_INLINE float rawBedTemp() {
108  return current_temperature_bed_raw;
109  };
110 #endif
111 
112 FORCE_INLINE float degBed() {
113  return current_temperature_bed;
114 };
115 
116 // Doesn't save FLASH when FORCE_INLINE removed.
117 FORCE_INLINE float degTargetHotend(uint8_t extruder) {
118  return target_temperature[extruder];
119 };
120 
121 FORCE_INLINE float degTargetBed() {
122  return target_temperature_bed;
123 };
124 
125 // Doesn't save FLASH when FORCE_INLINE removed.
126 FORCE_INLINE void setTargetHotend(const float &celsius) {
127  target_temperature[0] = celsius;
128  resetPID(0);
129 };
130 
131 FORCE_INLINE void setTargetBed(const float &celsius) {
132  target_temperature_bed = celsius;
133 };
134 
135 FORCE_INLINE bool isHeatingHotend(uint8_t extruder){
136  return target_temperature[extruder] > current_temperature[extruder];
137 };
138 
139 FORCE_INLINE bool isHeatingBed() {
140  return target_temperature_bed > current_temperature_bed;
141 };
142 
143 FORCE_INLINE bool isCoolingHotend(uint8_t extruder) {
144  return target_temperature[extruder] < current_temperature[extruder];
145 };
146 
147 FORCE_INLINE bool isCoolingBed() {
148  return target_temperature_bed < current_temperature_bed;
149 };
150 
151 #define degHotend0() degHotend(0)
152 #define degTargetHotend0() degTargetHotend(0)
153 #define isHeatingHotend0() isHeatingHotend(0)
154 #define isCoolingHotend0() isCoolingHotend(0)
155 
156 // return "false", if all heaters are 'off' (ie. "true", if any heater is 'on')
157 #define CHECK_ALL_HEATERS ((target_temperature[0] != 0) || (target_temperature_bed != 0))
158 
159 int getHeaterPower(int heater);
160 void disable_heater(); // Disable all heaters *instantaneously*
161 void updatePID();
162 
163 
164 FORCE_INLINE void autotempShutdown(){
165  #ifdef AUTOTEMP
166  if(autotemp_enabled)
167  {
168  autotemp_enabled=false;
169  if(degTargetHotend(active_extruder)>autotemp_min)
170  setTargetHotend(0);
171  }
172  #endif
173 }
174 
175 void PID_autotune(float temp, int extruder, int ncycles);
176 
177 #ifdef THERMAL_MODEL
178 bool thermal_model_enabled(); // return thermal model state
179 void thermal_model_set_enabled(bool enabled);
180 void thermal_model_set_warn_beep(bool enabled);
181 void thermal_model_set_params(float P=NAN, float U=NAN, float V=NAN, float C=NAN, float D=NAN,
182  int16_t L=-1, float Ta_corr=NAN, float warn=NAN, float err=NAN);
183 void thermal_model_set_resistance(uint8_t index, float R);
184 
185 void thermal_model_report_settings();
186 void thermal_model_reset_settings();
187 void thermal_model_load_settings();
188 void thermal_model_save_settings();
189 
190 void thermal_model_autotune(int16_t temp = 0, bool selftest = false);
191 bool thermal_model_autotune_result(); // return true if the last autotune was complete and successful
192 
193 #ifdef THERMAL_MODEL_DEBUG
194 void thermal_model_log_enable(bool enable);
195 #endif
196 #endif
197 
198 #ifdef FAN_SOFT_PWM
199 extern unsigned char fanSpeedSoftPwm;
200 #endif
201 extern uint8_t fanSpeedBckp;
202 
203 #endif