Merge branch 'edge' into debugbreak
[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 #define thermistor_checksum CHECKSUM("thermistor")
20 #define r0_checksum CHECKSUM("r0")
21 #define readings_per_second_checksum CHECKSUM("readings_per_second")
22 #define max_pwm_checksum CHECKSUM("max_pwm")
23 #define t0_checksum CHECKSUM("t0")
24 #define beta_checksum CHECKSUM("beta")
25 #define vadc_checksum CHECKSUM("vadc")
26 #define vcc_checksum CHECKSUM("vcc")
27 #define r1_checksum CHECKSUM("r1")
28 #define r2_checksum CHECKSUM("r2")
29 #define temperature_control_checksum CHECKSUM("temperature_control")
30 #define thermistor_pin_checksum CHECKSUM("thermistor_pin")
31 #define heater_pin_checksum CHECKSUM("heater_pin")
32
33 #define get_m_code_checksum CHECKSUM("get_m_code")
34 #define set_m_code_checksum CHECKSUM("set_m_code")
35 #define set_and_wait_m_code_checksum CHECKSUM("set_and_wait_m_code")
36
37 #define designator_checksum CHECKSUM("designator")
38
39 #define p_factor_checksum CHECKSUM("p_factor")
40 #define i_factor_checksum CHECKSUM("i_factor")
41 #define d_factor_checksum CHECKSUM("d_factor")
42
43 #define i_max_checksum CHECKSUM("i_max")
44
45 #define QUEUE_LEN 8
46
47 class TemperatureControlPool;
48
49 class TemperatureControl : public Module {
50 public:
51 TemperatureControl();
52 TemperatureControl(uint16_t name);
53
54 void on_module_loaded();
55 void on_main_loop(void* argument);
56 void on_gcode_execute(void* argument);
57 void on_gcode_received(void* argument);
58 void on_config_reload(void* argument);
59 void on_second_tick(void* argument);
60
61 void set_desired_temperature(double desired_temperature);
62 double get_temperature();
63 double adc_value_to_temperature(int adc_value);
64 uint32_t thermistor_read_tick(uint32_t dummy);
65 int new_thermistor_reading();
66
67 void pid_process(double);
68
69 double target_temperature;
70
71 // Thermistor computation settings
72 double r0;
73 double t0;
74 int r1;
75 int r2;
76 double beta;
77 double j;
78 double k;
79
80 int max_pwm;
81
82 // PID settings
83 double p_factor;
84 double i_factor;
85 double d_factor;
86
87 // PID runtime
88 double i_max;
89
90 double p, i, d;
91 int o;
92
93 double last_reading;
94
95 double acceleration_factor;
96 double readings_per_second;
97
98 RingBuffer<uint16_t,QUEUE_LEN> queue; // Queue of readings
99 uint16_t median_buffer[QUEUE_LEN];
100 int running_total;
101
102 uint16_t name_checksum;
103
104 Pin thermistor_pin;
105 Pwm heater_pin;
106
107 bool waiting;
108
109 uint16_t set_m_code;
110 uint16_t set_and_wait_m_code;
111 uint16_t get_m_code;
112
113 string designator;
114
115 TemperatureControlPool *pool;
116 int pool_index;
117 };
118
119 #endif