Change Conveyor,Planner,Block to use HeapRing
[clinton/Smoothieware.git] / src / modules / tools / temperaturecontrol / TemperatureControl.h
1 /*
2 this file is part of smoothie (http://smoothieware.org/). the motion control part is heavily based on grbl (https://github.com/simen/grbl).
3 smoothie 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.
4 smoothie 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.
5 you should have received a copy of the gnu general public license along with smoothie. if not, see <http://www.gnu.org/licenses/>.
6 */
7
8 #ifndef temperaturecontrol_h
9 #define temperaturecontrol_h
10
11 #include "libs/Pin.h"
12 #include "Pwm.h"
13 #include <math.h>
14
15 #include "RingBuffer.h"
16
17 #define UNDEFINED -1
18
19 #include "TemperatureControlPublicAccess.h"
20 #define thermistor_checksum CHECKSUM("thermistor")
21 #define r0_checksum CHECKSUM("r0")
22 #define readings_per_second_checksum CHECKSUM("readings_per_second")
23 #define max_pwm_checksum CHECKSUM("max_pwm")
24 #define pwm_frequency_checksum CHECKSUM("pwm_frequency")
25 #define t0_checksum CHECKSUM("t0")
26 #define beta_checksum CHECKSUM("beta")
27 #define vadc_checksum CHECKSUM("vadc")
28 #define vcc_checksum CHECKSUM("vcc")
29 #define r1_checksum CHECKSUM("r1")
30 #define r2_checksum CHECKSUM("r2")
31 #define thermistor_pin_checksum CHECKSUM("thermistor_pin")
32 #define heater_pin_checksum CHECKSUM("heater_pin")
33
34 #define get_m_code_checksum CHECKSUM("get_m_code")
35 #define set_m_code_checksum CHECKSUM("set_m_code")
36 #define set_and_wait_m_code_checksum CHECKSUM("set_and_wait_m_code")
37
38 #define designator_checksum CHECKSUM("designator")
39
40 #define p_factor_checksum CHECKSUM("p_factor")
41 #define i_factor_checksum CHECKSUM("i_factor")
42 #define d_factor_checksum CHECKSUM("d_factor")
43
44 #define i_max_checksum CHECKSUM("i_max")
45
46 #define preset1_checksum CHECKSUM("preset1")
47 #define preset2_checksum CHECKSUM("preset2")
48
49
50 #define QUEUE_LEN 8
51
52 class TemperatureControlPool;
53
54 class TemperatureControl : public Module {
55
56 public:
57 TemperatureControl(uint16_t name);
58
59 void on_module_loaded();
60 void on_main_loop(void* argument);
61 void on_gcode_execute(void* argument);
62 void on_gcode_received(void* argument);
63 void on_config_reload(void* argument);
64 void on_second_tick(void* argument);
65 void on_get_public_data(void* argument);
66 void on_set_public_data(void* argument);
67
68 void set_desired_temperature(float desired_temperature);
69 float get_temperature();
70 float adc_value_to_temperature(int adc_value);
71 uint32_t thermistor_read_tick(uint32_t dummy);
72 int new_thermistor_reading();
73
74
75 int pool_index;
76 TemperatureControlPool *pool;
77 friend class PID_Autotuner;
78
79 private:
80 void pid_process(float);
81
82 float target_temperature;
83
84 float preset1;
85 float preset2;
86
87 // Thermistor computation settings
88 float r0;
89 float t0;
90 int r1;
91 int r2;
92 float beta;
93 float j;
94 float k;
95
96
97 // PID runtime
98 float i_max;
99
100 int o;
101
102 float last_reading;
103
104 float acceleration_factor;
105 float readings_per_second;
106
107 RingBuffer<uint16_t,QUEUE_LEN> queue; // Queue of readings
108 uint16_t median_buffer[QUEUE_LEN];
109 int running_total;
110
111 uint16_t name_checksum;
112
113 Pin thermistor_pin;
114 Pwm heater_pin;
115
116 bool waiting;
117 bool min_temp_violated;
118
119 uint16_t set_m_code;
120 uint16_t set_and_wait_m_code;
121 uint16_t get_m_code;
122
123 string designator;
124
125
126 void setPIDp(float p);
127 void setPIDi(float i);
128 void setPIDd(float d);
129
130 float iTerm;
131 float lastInput;
132 // PID settings
133 float p_factor;
134 float i_factor;
135 float d_factor;
136 float PIDdt;
137 };
138
139 #endif