From d6ee30f5a8ea3da437be75cccc034bdaebeb5a8a Mon Sep 17 00:00:00 2001 From: Rob Giseburt Date: Fri, 17 Nov 2017 09:41:01 -0600 Subject: [PATCH] Fix temperature readings, adjusting Ultimaker2+ settings, and make MAX31865 more fault tolerant. --- g2core/device/max31865/max31865.h | 49 ++++++++++++--------- g2core/settings/settings_Ultimaker_2_Plus.h | 37 ++++++++-------- g2core/temperature.cpp | 25 ++++++++--- 3 files changed, 66 insertions(+), 45 deletions(-) diff --git a/g2core/device/max31865/max31865.h b/g2core/device/max31865/max31865.h index c4e16ae2..9908860f 100644 --- a/g2core/device/max31865/max31865.h +++ b/g2core/device/max31865/max31865.h @@ -94,7 +94,7 @@ struct MAX31865 final { template MAX31865(const Motate::PinOptions_t options, // completely ignored, but for compatibility with ADCPin - std::function &&_interrupt, + std::function &&_interrupt, SPIBus_t &spi_bus, const chipSelect_t &_cs, float pullup_resistance = 430, // 430 is the value used on the Adafruit breakout @@ -194,7 +194,7 @@ struct MAX31865 final { _fault_status_needs_read = true; } if (_interrupt_handler) { - _interrupt_handler(); + _interrupt_handler(fault_detected); } _state = NEEDS_SAMPLED; }; @@ -235,7 +235,7 @@ struct MAX31865 final { bool _fault_status_needs_read = false; void _postReadFaultStatus() { if (_interrupt_handler) { - _interrupt_handler(); + _interrupt_handler(_fault_status.value != 0); } }; @@ -339,8 +339,7 @@ struct MAX31865 final { // interface to make this a drop-in replacement (after init) for an ADCPin - float _vref = 3.3; - std::function _interrupt_handler; + std::function _interrupt_handler; void startSampling() { @@ -411,25 +410,25 @@ struct MAX31865 final { int32_t getValue() { return getRaw(); }; - int32_t getBottom() { - return 0; - }; - float getBottomVoltage() { - return 0; - }; - int32_t getTop() { - return 32767; - }; - float getTopVoltage() { - return _vref; - }; +// int32_t getBottom() { +// return 0; +// }; +// float getBottomVoltage() { +// return 0; +// }; +// int32_t getTop() { +// return 32767; +// }; +// float getTopVoltage() { +// return _vref; +// }; void setVoltageRange(const float vref, const float min_expected = 0, const float max_expected = -1, const float ideal_steps = 1) { - _vref = vref; +// _vref = vref; // All of the rest are ignored, but here for compatibility of interface }; @@ -438,19 +437,27 @@ struct MAX31865 final { if (r < 0) { return r*1000.0; } - return ((r*_pullup_resistance)/32768.0) * _vref; + return ((r*_pullup_resistance)/32768.0); }; operator float() { return getVoltage(); }; + float getResistance() { + float r = getRaw(); + if (r < 0) { + return r*1000.0; + } + return (r*_pullup_resistance)/32768.0; + } + void setInterrupts(const uint32_t interrupts) { // ignore this -- it's too dangerous to accidentally change the SPI interrupts }; // We can only support interrupt inferface option 2: a function with a closure or function pointer - void setInterruptHandler(std::function &&handler) { + void setInterruptHandler(std::function &&handler) { _interrupt_handler = std::move(handler); }; - void setInterruptHandler(const std::function &handler) { + void setInterruptHandler(const std::function &handler) { _interrupt_handler = handler; }; diff --git a/g2core/settings/settings_Ultimaker_2_Plus.h b/g2core/settings/settings_Ultimaker_2_Plus.h index d82b6c4f..56b6f88a 100644 --- a/g2core/settings/settings_Ultimaker_2_Plus.h +++ b/g2core/settings/settings_Ultimaker_2_Plus.h @@ -231,7 +231,7 @@ // *** axis settings ********************************************************************************** #define X_AXIS_MODE AXIS_STANDARD // xam see canonical_machine.h cmAxisMode for valid values -#define X_VELOCITY_MAX 8700 // xvm G0 max velocity in mm/min +#define X_VELOCITY_MAX 8700 // xvm G0 max velocity in mm/min #define X_FEEDRATE_MAX X_VELOCITY_MAX // xfr G1 max feed rate in mm/min #define X_TRAVEL_MIN 0 // xtn minimum travel - used by soft limits and homing #define X_TRAVEL_MAX 230 // xtm travel between switches or crashes @@ -377,15 +377,15 @@ M100.1 ({{ajh:144000.0}}) #define HAS_TEMPERATURE_SENSOR_1 true #if HAS_TEMPERATURE_SENSOR_1 - #define TEMPERATURE_SENSOR_1_CIRCUIT_TYPE ADCCircuitDifferentialPullup - #define TEMPERATURE_SENSOR_1_CIRCUIT_INIT { /*pullup_resistance:*/ 200 } - #define TEMPERATURE_SENSOR_1_TYPE PT100> - #define TEMPERATURE_SENSOR_1_INIT {&temperature_sensor_1_circuit} +// #define TEMPERATURE_SENSOR_1_CIRCUIT_TYPE ADCCircuitDifferentialPullup +// #define TEMPERATURE_SENSOR_1_CIRCUIT_INIT { /*pullup_resistance:*/ 200 } +// #define TEMPERATURE_SENSOR_1_TYPE PT100> +// #define TEMPERATURE_SENSOR_1_INIT {&temperature_sensor_1_circuit} -// #define TEMPERATURE_SENSOR_1_CIRCUIT_TYPE ADCCircuitRawResistance -// #define TEMPERATURE_SENSOR_1_CIRCUIT_INIT { } -// #define TEMPERATURE_SENSOR_1_TYPE PT100> -// #define TEMPERATURE_SENSOR_1_INIT {&temperature_sensor_1_circuit, spiBus, spiCSPinMux.getCS(5), /*pullup_resistance:*/ 430.0} + #define TEMPERATURE_SENSOR_1_CIRCUIT_TYPE ADCCircuitRawResistance + #define TEMPERATURE_SENSOR_1_CIRCUIT_INIT { } + #define TEMPERATURE_SENSOR_1_TYPE PT100> + #define TEMPERATURE_SENSOR_1_INIT {&temperature_sensor_1_circuit, spiBus, spiCSPinMux.getCS(5), /*pullup_resistance:*/ 430.0f} #endif // HAS_TEMPERATURE_SENSOR_1 #define EXTRUDER_1_OUTPUT_PIN kHeaterOutput1_PinNumber @@ -406,16 +406,17 @@ M100.1 ({{ajh:144000.0}}) #define EXTRUDER_2_OUTPUT_PIN kHeaterOutput2_PinNumber -#define HAS_TEMPERATURE_SENSOR_3 false +#define HAS_TEMPERATURE_SENSOR_3 true #if HAS_TEMPERATURE_SENSOR_3 - #define TEMPERATURE_SENSOR_3_CIRCUIT_TYPE ADCCircuitDifferentialPullup - #define TEMPERATURE_SENSOR_3_CIRCUIT_INIT { /*pullup_resistance:*/ 200 } - #define TEMPERATURE_SENSOR_3_TYPE PT100> - #define TEMPERATURE_SENSOR_3_INIT {&temperature_sensor_3_circuit} -// #define TEMPERATURE_SENSOR_3_CIRCUIT_TYPE ADCCircuitRawResistance -// #define TEMPERATURE_SENSOR_3_CIRCUIT_INIT { } -// #define TEMPERATURE_SENSOR_3_TYPE PT100> -// #define TEMPERATURE_SENSOR_3_INIT {&temperature_sensor_3_circuit, spiBus, spiCSPinMux.getCS(6), /*pullup_resistance:*/ 430.0} +// #define TEMPERATURE_SENSOR_3_CIRCUIT_TYPE ADCCircuitDifferentialPullup +// #define TEMPERATURE_SENSOR_3_CIRCUIT_INIT { /*pullup_resistance:*/ 200 } +// #define TEMPERATURE_SENSOR_3_TYPE PT100> +// #define TEMPERATURE_SENSOR_3_INIT {&temperature_sensor_3_circuit} + + #define TEMPERATURE_SENSOR_3_CIRCUIT_TYPE ADCCircuitRawResistance + #define TEMPERATURE_SENSOR_3_CIRCUIT_INIT { } + #define TEMPERATURE_SENSOR_3_TYPE PT100> + #define TEMPERATURE_SENSOR_3_INIT {&temperature_sensor_3_circuit, spiBus, spiCSPinMux.getCS(6), /*pullup_resistance:*/ 430.0f} #endif // HAS_TEMPERATURE_SENSOR_3 #define BED_OUTPUT_PIN kHeaterOutput11_PinNumber diff --git a/g2core/temperature.cpp b/g2core/temperature.cpp index d97a91b9..cc588029 100755 --- a/g2core/temperature.cpp +++ b/g2core/temperature.cpp @@ -266,17 +266,16 @@ struct ADCCircuitDifferentialPullup : ADCCircuit struct ADCCircuitRawResistance : ADCCircuit { - const float _vref; - ADCCircuitRawResistance(const float vref = kSystemVoltage) : _vref{vref} {}; + ADCCircuitRawResistance() {}; float get_resistance(float v) const override { - return v/_vref; + return v; }; float get_voltage(const float r) const override { - return r*_vref; + return r; }; }; @@ -413,6 +412,9 @@ struct PT100 { float raw_adc_voltage = 0.0; int32_t raw_adc_value = 0; + bool new_sample_since_read = false; + uint8_t reads_without_sample = 0; + const float variance_max = 1.1; ValueHistory<20> history {variance_max}; @@ -432,7 +434,7 @@ struct PT100 { template PT100(const ADCCircuit *_circuit, Ts&&... additional_values) : circuit{_circuit}, - adc_pin{kNormal, [&]{this->adc_has_new_value();}, additional_values...} + adc_pin{kNormal, [&](bool e){this->adc_has_new_value(e);}, additional_values...} { adc_pin.setInterrupts(kPinInterruptOnChange|kInterruptPriorityLow); adc_pin.setVoltageRange(kSystemVoltage, @@ -453,6 +455,16 @@ struct PT100 { }; float temperature_exact() { + if (!new_sample_since_read) { + reads_without_sample++; + if (reads_without_sample > 10) { + cm_alarm(STAT_TEMPERATURE_CONTROL_ERROR, "Sensor read failed 10 times."); + } + } else { + reads_without_sample = 0; + } + new_sample_since_read = false; + float r = get_resistance(); if (r < 0.0) { return -1; } @@ -514,7 +526,7 @@ struct PT100 { }; // Call back function from the ADC to tell it that the ADC has a new sample... - void adc_has_new_value() { + void adc_has_new_value(bool error = false) { raw_adc_value = adc_pin.getRaw(); float v = fabs(adc_pin.getVoltage()); // if (v < 0) { @@ -525,6 +537,7 @@ struct PT100 { // return; // } history.add_sample(v); + new_sample_since_read = true; }; };