Merge remote-tracking branch 'upstream/edge' into upstream-master
[clinton/Smoothieware.git] / src / modules / tools / temperaturecontrol / AD8495.cpp
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 #include "AD8495.h"
9 #include "libs/Kernel.h"
10 #include "libs/Pin.h"
11 #include "Config.h"
12 #include "checksumm.h"
13 #include "Adc.h"
14 #include "ConfigValue.h"
15 #include "libs/Median.h"
16 #include "utils.h"
17 #include "StreamOutputPool.h"
18
19 #include <fastmath.h>
20
21 #include "MRI_Hooks.h"
22
23 #define UNDEFINED -1
24
25 #define AD8495_pin_checksum CHECKSUM("ad8495_pin")
26 #define AD8495_offset_checksum CHECKSUM("ad8495_offset")
27
28 AD8495::AD8495()
29 {
30 min_temp= 999;
31 max_temp= 0;
32 }
33
34 AD8495::~AD8495()
35 {
36 }
37
38 // Get configuration from the config file
39 void AD8495::UpdateConfig(uint16_t module_checksum, uint16_t name_checksum)
40 {
41 // Thermistor pin for ADC readings
42 this->AD8495_pin.from_string(THEKERNEL->config->value(module_checksum, name_checksum, AD8495_pin_checksum)->required()->as_string());
43 this->AD8495_offset = THEKERNEL->config->value(module_checksum, name_checksum, AD8495_offset_checksum)->by_default(0)->as_number(); // Stated offset. For Adafruit board it is 250C. If pin 2(REF) of amplifier is connected to 0V then there is 0C offset.
44
45 THEKERNEL->adc->enable_pin(&AD8495_pin);
46 }
47
48
49 float AD8495::get_temperature()
50 {
51 float t= adc_value_to_temperature(new_AD8495_reading());
52 // keep track of min/max for M305
53 if(t > max_temp) max_temp= t;
54 if(t < min_temp) min_temp= t;
55 return t;
56 }
57
58 void AD8495::get_raw()
59 {
60
61 int adc_value= new_AD8495_reading();
62 const uint32_t max_adc_value= THEKERNEL->adc->get_max_value();
63 float t=((float)adc_value)/(((float)max_adc_value)/3.3*0.005);
64
65 t = t - this->AD8495_offset;
66
67 THEKERNEL->streams->printf("adc= %d, max_adc= %lu, temp= %f, offset = %f\n", adc_value,max_adc_value,t, this->AD8495_offset);
68
69 // reset the min/max
70 min_temp= max_temp= t;
71 }
72
73 float AD8495::adc_value_to_temperature(uint32_t adc_value)
74 {
75 const uint32_t max_adc_value= THEKERNEL->adc->get_max_value();
76 if ((adc_value >= max_adc_value))
77 return infinityf();
78
79 float t=((float)adc_value)/(((float)max_adc_value)/3.3*0.005);
80
81 t=t-this->AD8495_offset;
82
83 return t;
84 }
85
86 int AD8495::new_AD8495_reading()
87 {
88 // filtering now done in ADC
89 return THEKERNEL->adc->read(&AD8495_pin);
90 }