diff --git a/README.md b/README.md index 9b1adf50b..f40e2ddff 100644 --- a/README.md +++ b/README.md @@ -26,15 +26,17 @@ See the [Danger Features document](https://dangerklipper.io/Danger_Features.html - [fan: reverse FAN](https://github.com/DangerKlippers/danger-klipper/pull/51) ([klipper#4983](https://github.com/Klipper3d/klipper/pull/4983)) -- [heater: modify PID without reload](https://github.com/DangerKlippers/danger-klipper/pull/35) +- [heaters: modify PID without reload](https://github.com/DangerKlippers/danger-klipper/pull/35) -- [heater: velocity PID](https://github.com/DangerKlippers/danger-klipper/pull/47) ([klipper#6272](https://github.com/Klipper3d/klipper/pull/6272)) +- [heaters: MPC temperature control](https://github.com/DangerKlippers/danger-klipper/pull/333) -- [heater: PID-Profiles](https://github.com/DangerKlippers/danger-klipper/pull/162) +- [heaters: velocity PID](https://github.com/DangerKlippers/danger-klipper/pull/47) ([klipper#6272](https://github.com/Klipper3d/klipper/pull/6272)) -- [heater: expose heater thermistor out of min/max](https://github.com/DangerKlippers/danger-klipper/pull/182) +- [heaters: PID-Profiles](https://github.com/DangerKlippers/danger-klipper/pull/162) -- [heater/fan: new heated_fan module](https://github.com/DangerKlippers/danger-klipper/pull/259) +- [heaters: expose heater thermistor out of min/max](https://github.com/DangerKlippers/danger-klipper/pull/182) + +- [heaters/fan: new heated_fan module](https://github.com/DangerKlippers/danger-klipper/pull/259) - [gcode: jinja2.ext.do extension](https://github.com/DangerKlippers/danger-klipper/pull/26) ([klipper#5149](https://github.com/Klipper3d/klipper/pull/5149)) @@ -116,11 +118,11 @@ See the [Danger Features document](https://dangerklipper.io/Danger_Features.html - [mcu: support for AT32F403](https://github.com/DangerKlippers/danger-klipper/pull/284) +- [z_tilt, quad_gantry_level: adaptive horizontal move z](https://github.com/DangerKlippers/danger-klipper/pull/336) + If you're feeling adventurous, take a peek at the extra features in the bleeding-edge-v2 branch [feature documentation](docs/Bleeding_Edge.md) and [feature configuration reference](docs/Config_Reference_Bleeding_Edge.md): -- [heaters: mpc temperature control](https://github.com/DangerKlippers/danger-klipper/pull/268) - - [extruder/pa: do not smooth base extruder position, only advance](https://github.com/DangerKlippers/danger-klipper/pull/266) - [core: non-critical-mcus](https://github.com/DangerKlippers/danger-klipper/pull/265) diff --git a/docs/Config_Reference.md b/docs/Config_Reference.md index 7d87cc644..259357c1d 100644 --- a/docs/Config_Reference.md +++ b/docs/Config_Reference.md @@ -94,11 +94,10 @@ A collection of DangerKlipper-specific system options # Tolerance (in mm) for distance moved in the second homing. Ensures the # second homing distance closely matches the `min_home_dist` when using # sensorless homing. The default is 0.5mm. -#temp_ignore_limits: False -# When set to true, this parameter ignores the min_value and max_value -# limits for temperature sensors. It prevents shutdowns due to -# 'ADC out of range' and similar errors by allowing readings outside the -# specified range without triggering a shutdown. The default is False. +#temp_ignore_limits: +# A list of temperature sensors which min and max temps should be ignored. +# This feature prevents shutdowns due to 'ADC out of range' and similar errors +# and thus should be used with extreme caution. #autosave_includes: False # When set to true, SAVE_CONFIG will recursively read [include ...] blocks # for conflicts to autosave data. Any configurations updated will be backed @@ -935,7 +934,8 @@ The extruder section is used to describe the heater parameters for the nozzle hotend along with the stepper controlling the extruder. See the [command reference](G-Codes.md#extruder) for additional information. See the [pressure advance guide](Pressure_Advance.md) for information -on tuning pressure advance. +on tuning pressure advance. See [PID](PID.md) or [MPC](MPC.md) for more +detailed information about the control methods. ``` [extruder] @@ -1022,12 +1022,14 @@ sensor_pin: # be smoothed to reduce the impact of measurement noise. The default # is 1 seconds. control: -# Control algorithm (either pid, pid_v or watermark). This parameter must -# be provided. pid_v should only be used on well calibrated heaters with -# low to moderate noise. -pid_Kp: -pid_Ki: -pid_Kd: +# Control algorithm (either pid, pid_v, pid_p, watermark or mpc). This +# parameter must be provided. pid_v should only be used on well calibrated +# heaters with low to moderate noise. +# +# If control: pid, pid_v or pid_p +#pid_Kp: +#pid_Ki: +#pid_Kd: # The proportional (pid_Kp), integral (pid_Ki), and derivative # (pid_Kd) settings for the PID feedback control system. Klipper # evaluates the PID settings with the following general formula: @@ -1037,11 +1039,23 @@ pid_Kd: # off and 1.0 being full on. Consider using the PID_CALIBRATE # command to obtain these parameters. The pid_Kp, pid_Ki, and pid_Kd # parameters must be provided for PID heaters. +# +# If control: watermark #max_delta: 2.0 # On 'watermark' controlled heaters this is the number of degrees in # Celsius above the target temperature before disabling the heater # as well as the number of degrees below the target before # re-enabling the heater. The default is 2 degrees Celsius. +# +# If control: mpc +# See MPC.md for details about these parameters. +#heater_power: +#cooling_fan: +#ambient_temp_sensor: +#filament_diameter: 1.75 +#filament_density: 1.2 +#filament_heat_capacity: 1.8 +# #pwm_cycle_time: 0.100 # Time in seconds for each software PWM cycle of the heater. It is # not recommended to set this unless there is an electrical diff --git a/docs/Danger_Features.md b/docs/Danger_Features.md index 70dadc5c4..cff859907 100644 --- a/docs/Danger_Features.md +++ b/docs/Danger_Features.md @@ -32,9 +32,9 @@ - [`[z_calibration]`](./Config_Reference.md#⚠️-z_calibration) enables automatic probe Z offset calibration using a reference endstop like the Voron 2.4 nozzle endstop. - [`[z_tilt_ng]`](./Config_Reference.md#z_tilt_ng) adds enforced 3-point z tilt calibration - [`[z_tilt/quad_gantry_level] increasing_threshold`](./Config_Reference.md#z_tilt) allows you to customize the allowed variation when probing multiple times - +- [`[z_tilt/quad_gantry_level] adaptive_horizontal_move_z`](./Config_Reference.md#z_tilt) adaptively decrease horizontal_move_z based on resulting error - z_tilt and QGL faster and safer! ## Heaters, Fans, and PID changes - +- [Model Predictive Control](./MPC.md) is an advanced temperature control method that offers an alternative to traditional PID control. - [Velocity PID](./PID.md) can be more accurate than positional PID, but is more susceptible to noisy sensors and may require larger smoothing times - [`PID_PROFILE [LOAD/SAVE]`](./G-Codes.md#pid_profile) allows you to calibrate and save PID profiles at multiple temperatures and fan speeds, and later restore them. With some clever macros, automatic per-material pid tuning is within reach! - [`SET_HEATER_PID HEATER= KP= KI= KD=`](./G-Codes.md#set_heater_pid) can update your PID parameters without a reload. diff --git a/docs/MPC.md b/docs/MPC.md index 17497f874..b051c267b 100644 --- a/docs/MPC.md +++ b/docs/MPC.md @@ -2,82 +2,123 @@ Model Predictive Control (MPC) is an advanced temperature control method that offers an alternative to traditional PID control. MPC leverages a system model to simulate the temperature of the hotend and adjusts the heater power to align with the target temperature. -Unlike reactive methods, MPC operates proactively, making adjustments in anticipation of temperature fluctuations. It utilizes a model of the hotend, taking into account factors such as the thermal masses of the system, heater power, heat loss to ambient air, and fans, and heat transfer into the filament. This model allows MPC to predict the amount of heat energy that will be dissipated from the hotend over a given duration, and it compensates for this by adjusting the heater power accordingly. As a result, MPC can accurately calculate the necessary heat energy input to maintain a steady temperature or to transition to a new temperature. +Unlike reactive methods, MPC operates proactively, adjusting in anticipation of temperature fluctuations. It utilizes a model of the hotend, considering factors such as the thermal masses of the system, heater power, heat loss to ambient air, and fans, and heat transfer into the filament. This model allows MPC to predict the amount of heat energy that will be dissipated from the hotend over a given duration, and it compensates for this by adjusting the heater power accordingly. As a result, MPC can accurately calculate the necessary heat energy input to maintain a steady temperature or to transition to a new temperature. MPC offers several advantages over PID control: -- **Faster and more responsive temperature control:** MPC’s proactive approach allows it to respond more quickly and accurately to changes in temperature. +- **Faster and more responsive temperature control:** MPC’s proactive approach allows it to respond more quickly and accurately to changes in temperature from fans or flow rate changes. - **Broad functionality with single calibration:** Once calibrated, MPC functions effectively across a wide range of printing temperatures. - **Simplified calibration process:** MPC is easier to calibrate compared to traditional PID control. - **Compatibility with all hotend sensor types:** MPC works with all types of hotend sensors, including those that produce noisy temperature readings. -- **Versatility with heater types:** MPC performs equally well with standard cartridge heaters and PTC heaters. -- **Applicable to both hotends and beds:** MPC can be used to control the temperature of both hotends and beds. +- **Versatility with heater types:** MPC performs well with standard cartridge heaters and PTC heaters. - **Effective for high and low flow hotends:** Regardless of the flow rate of the hotend, MPC maintains effective temperature control. > [!CAUTION] > This feature controls the portions of the 3D printer that can get very hot. All standard Danger Klipper warnings apply. Please report all issues and bugs to github or discord. -# Configuration +# Basic Configuration -To use MPC as the temperature controller set the following configuration parameters in the appropriate heater section of the config. -Currently only [extruder] and [heater_bed] heater types are supported. +To use MPC as the temperature controller for the extruder use the following basic configuration block. ``` -[extruder] OR [heater_bed] +[extruder] control: mpc -heater_power: -# Nameplate heater power in watts. - -# Note that for a PTC, a non-linear heater, MPC may not work optimally due -# to the change in power output relative to temperature for this style of -# heater. Setting heater_power to the power output at the expected printing -# temperature is recommended. -cooling_fan: fan -# This is the fan that is cooling extruded filament and the hotend. -# cooling_fan is supported for [heater_bed] but accurate performance has -# not been verified. -# Specifying "fan" will automatically use the part cooling fan. -# Bed fans could be used for the [heater_bed] by specifying -# for example. -#ambient_temp_sensor: -# Optional. If this is not given MPC will estimate this parameter -# (recommended). -# It can use any sensor, but it should be a temperature sensor in proximity -# to the hotend or measuring the ambient air surrounding the hotend such as -# a chamber sensor. -# This is used for initial state temperature and calibration but not for -# actual control. -# Example sensor: temperature_sensor beacon_coil +heater_power: 50 +cooling_fan: fan +filament_diameter: 1.75 +filament_density: 1.20 +filament_heat_capacity: 1.8 ``` -## Filament Feed Forward Configuration - - -MPC can look forward to changes in extrusion rates which could require more or less heat input to maintain target temperatures. This substantially improves the accuracy and responsiveness of the model during printing. +- `control: mpc` + *Required* + The temperature control method. + +- `heater_power: 50` + *Required* + The nameplate heater power in watts. + For a PTC, a non-linear heater, MPC may not work optimally due + to the change in power output relative to heater temperature for this style of + heater. Setting heater_power to the power output at the expected printing + temperature is recommended. + +- `cooling_fan: fan` + _Default Value: fan_ + This is the fan that is cooling extruded filament and the hotend. + Specifying "fan" will automatically use the part cooling fan. + +- `filament_diameter: fan` + _Default Value: 1.75 (mm)_ + This is the filament diameter. + +- `filament_density: 1.20` + _Default Value: 1.20 (g/mm^3)_ + This is the material density of the filament being printed. + +- `filament_heat_capacity: 1.80` + _Default Value: 1.80 (J/g/K)_ + This is the material specific heat capacity of the filament being printed. + +## Optional Config Parameters + +These can be specified in the config but should not need to be changed from the default values for most users. + +- `maximum_retract:` + _Default Value: 2.0 (mm)_ + This value clamps how much the extruder is allowed to go backwards in a single period during MPC FFF calculations. This lets the filament power go negative and add a small amount of energy to the system. + +- `target_reach_time:` + _Default Value: 2.0 (sec)_ + +- `smoothing:` + _Default Value: 0.83 (sec)_ + This parameter affects how quickly the model learns and it represents the ratio of temperature difference applied per second. A value of 1.0 represents no smoothing used in the model. + +- `min_ambient_change:` + _Default Value: 1.0 (deg C/s)_ + Larger values of MIN_AMBIENT_CHANGE will result in faster convergence but will also cause the simulated ambient temperature to flutter somewhat chaotically around the ideal value. + +- `steady_state_rate:` + _Default Value: 0.5 (deg C/s)_ + +- `ambient_temp_sensor: temperature_sensor ` + _Default Value: MPC ESTIMATE_ + It is recommended not to specify this parameter and let MPC will estimate. This is used for initial state temperature and calibration but not for actual control. + Any temperature sensor could be used, but the sensor should be in proximity to the hotend or measuring the ambient air surrounding the hotend. + +## PTC Heater Power + +The `heater power:` for PTC style heaters is recommended to be set at the normal print temperature for the printer. Some common PTC heaters are given below for reference. If your heater is not listed the manufacturer should be able to provide a temperature and power curve. + +| Heater Temp (C) | Rapido 2 (W) | Rapido 1 (W) | Dragon Ace (W) | Revo 40 (W) |Revo 60 (W) | +|:---------------:|:------------:|:------------:|:--------------:|:-----------:|:----------:| +| 180 | 72 | 52 | 51 | 30 |45 | +| 200 | 70 | 51 | 48 | 29 |44 | +| 220 | 67 | 50 | 46 | 28 |43 | +| 240 | 65 | 49 | 44 | 28 |42 | +| 260 | 64 | 48 | 43 | 27 |40 | +| 280 | 62 | 47 | 41 | 27 |39 | +| 300 | 60 | 46 | 39 | 26 |38 | +## Filament Feed Forward Configuration -These should only be set under [extruder] and are not valid for [heater_bed]. - -``` -#filament_diameter: 1.75 -# (mm) -#filament_density: 1.2 -# 1.2 (g/mm^3) is the default to cover a wide range of standard -# materials including ABS, ASA, PLA, PETG. -#filament_heat_capacity: 1.8 -# 1.8 (J/g/K) is the default to cover a wide range of standard -# materials including ABS, ASA, PLA, PETG. -``` - -Filament feed forward parameters can be set, for the printer session, via the command line or custom G-Code with the following command. - -`MPC_SET HEATER= FILAMENT_DENSITY= FILAMENT_HEAT_CAPACITY=` +The filament feed forward (FFF) feature allows MPC to look forward and see changes in extrusion rates which could require more or less heat input to maintain target temperature. This feature substantially improves the accuracy and responsiveness of the model during printing. It is enabled by default and can be defined is more detail with the `filament_density` and `filament_heat_capacity` config parameters. The default values are set to cover a wide range of standard materials including ABS, ASA, PLA, PETG. -`HEATER=`: Only [extruder] is supported. + FFF parameters can be set, for the printer session, via the `MPC_SET` G-Code command: -`FILAMENT_DENSITY= `: Filament density in g/mm^3 +`MPC_SET HEATER= FILAMENT_DENSITY= FILAMENT_HEAT_CAPACITY= [FILAMENT_TEMP=>]` -`FILAMENT_HEAT_CAPACITY=`: Filament heat capacity in J/g/K +- `HEATER`: + Only extruder is supported + +- `FILAMENT_DENSITY`: + Filament density in g/mm^3 + +- `FILAMENT_HEAT_CAPACITY`: + Filament heat capacity in J/g/K + +- `FILAMENT_TEMP`: + This can be set to either `sensor`, `ambient`, or a set temperature value. FFF will use the specific energy required to heat the filament and the power loss will be calculated based on the temperature delta. For example, updating the filament material properties for ASA would be: @@ -85,163 +126,145 @@ For example, updating the filament material properties for ASA would be: MPC_SET HEATER=extruder FILAMENT_DENSITY=1.07 FILAMENT_HEAT_CAPACITY=1.7 ``` -## Optional model parameters +## Filament Physical Properties -These can be tuned but should not need changing from the default values. +MPC works best knowing how much energy (in Joules) it takes to heat 1mm of filament by 1°C. The material values from the tables below have been curated from popular filament manufacturers and material data references. These values are sufficient for MPC to implement the FFF feature. Advanced users could tune the `filament_density` and `filament_heat_capacity` parameters based on manufacturers datasheets. -``` -#target_reach_time: 2.0 -# (sec) -#smoothing: 0.83 -# (sec) -# This parameter affects how quickly the model learns and it represents -# the ratio of temperature difference applied per second. -# A value of 1.0 represents no smoothing used in the model. -#min_ambient_change: 1.0 -# (deg C) -# Larger values of MIN_AMBIENT_CHANGE will result in faster convergence -# but will also cause the simulated ambient temperature to flutter -# somewhat chaotically around the ideal value. -#steady_state_rate: 0.5 -# (deg C/s) -``` +### Common Materials -## Example configuration block +| Material | Density [g/cm³] | Specific heat [J/g/K] | +| -------- |:---------------:|:---------------------:| +| PLA | 1.25 | 1.8 - 2.2 | +| PETG | 1.27 | 1.7 - 2.2 | +| PC+ABS | 1.15 | 1.5 - 2.2 | +| ABS | 1.06 | 1.25 - 2.4 | +| ASA | 1.07 | 1.3 - 2.1 | +| PA6 | 1.12 | 2 - 2.5 | +| PA | 1.15 | 2 - 2.5 | +| PC | 1.20 | 1.1 - 1.9 | +| TPU | 1.21 | 1.5 - 2 | +| TPU-90A | 1.15 | 1.5 - 2 | +| TPU-95A | 1.22 | 1.5 - 2 | -``` -[extruder] -control: mpc -heater_power: 50 -cooling_fan: fan -filament_density: 1.20 -filament_heat_capacity: 1.8 +### Common Carbon Fiber Filled Materials -[heater_bed] -control: mpc -heater_power: 400 -``` +| Material | Density [g/cm³] | Specific heat [J/g/K] | +| -------------------------------------------- |:---------------:|:---------------------:| +| ABS-CF | 1.11 | ^ | +| ASA-CF | 1.11 | ^ | +| PA6-CF | 1.19 | ^ | +| PC+ABS-CF | 1.22 | ^ | +| PC+CF | 1.36 | ^ | +| PLA-CF | 1.29 | ^ | +| PETG-CF | 1.30 | ^ | -> [!IMPORTANT] -> Restart the firmware to enable MPC and proceed to calibration. +^ Use the specific heat from the base polymer # Calibration The MPC default calibration routine takes the following steps: -- Cool to ambient: The calibration routine needs to know the approximate ambient temperature. It switches the part cooling fan on and waits until the hotend temperature stops decreasing relative to ambient. -- Heat past 200°C: Measure the point where the temperature is increasing most rapidly, and the time and temperature at that point. Also, three temperature measurements are needed at some point after the initial latency has taken effect. The tuning algorithm heats the hotend to over 200°C. -- Hold temperature while measuring ambient heat-loss: At this point enough is known for the MPC algorithm to engage. The calibration routine makes a best guess at the overshoot past 200°C which will occur and targets this temperature for about a minute while ambient heat-loss is measured without (and optionally with) the fan. -- MPC calibration routine creates the appropriate model constants and saves them for use. At this time the model parameters are temporary and not yet saved to the printer configuration via SAVE_CONFIG. - -## Hotend or Bed Calibration - -The MPC calibration routine has to be run initially for each heater to be controlled using MPC. In order for MPC to be functional an extruder must be able to reach 200C and a bed to reach 90C. - -`MPC_CALIBRATE HEATER= [TARGET=] [FAN_BREAKPOINTS=]` - -`HEATER=` :The heater to be calibrated. [extruder] or [heater_bed] supported. - -`[TARGET=]` : Sets the calibration temperature in degrees C. TARGET default is 200C for extruder and 90C for beds. MPC calibration is temperature independent so calibration the extruder at higher temperatures will not necessarily produce better model parameters. This is an area of exploration for advanced users - -`[FAN_BREAKPOINTS=]` : Sets the number off fan setpoint to test during calibration. Three fan powers (0%, 50%, 100%) are tested by default. An arbitrary number breakpoints can be specified e.g. 7 breakpoints would result in (0, 16%, 33%, 50%, 66%, 83%, 100%) fan speeds. Each breakpoint adds about 20s to the calibration. - -> [!NOTE] -> Ensure that the part cooling fan is off before starting calibration. - -Default calibration of the hotend: - -``` -MPC_CALIBRATE HEATER=extruder -``` - -Default calibration of the bed: - +> 1. Cool to ambient: The calibration routine needs to know the approximate ambient temperature and waits until the hotend temperature stabilises and stops decreasing relative to ambient. +> 2. Heat past 200°C: Measure the point where the temperature is increasing most rapidly, and the time and temperature at that point. Also, three temperature measurements are needed at some point after the initial latency has taken effect. +> 3. Hold temperature while measuring ambient heat-loss: At this point enough is known for the MPC algorithm to engage. The calibration routine makes a best guess at the overshoot past 200°C which will occur and targets this temperature for about a minute while ambient heat-loss is measured without and with the fan engaged (if a `cooling_fan` is specified). +> 4. The MPC calibration routine creates the appropriate model constants. At this time the model parameters are temporary and not yet saved to the printer configuration. + +The MPC calibration routine must be run for each heater, to be controlled by MPC, in order to determine the model parameters. For an MPC calibration to be successful an extruder must be able to reach 200C. Calibration is performed with the following G-code command. + +`MPC_CALIBRATE HEATER= [TARGET=] [FAN_BREAKPOINTS=]` + +- `HEATER=`: + The extruder heater to be calibrated. + +- `TARGET=`: + _Default Value: 200 (deg C)_ + Sets the calibration temperature. The default of 200C is a good target for the extruder. MPC calibration is temperature independent, so calibrating the extruder at higher temperatures will not necessarily produce better model parameters. This is an area of exploration for advanced users. + +- `FAN_BREAKPOINTS=`: + _Default Value: 3_ + Sets the number off fan setpoint to test during calibration. An arbitrary number of breakpoints can be specified e.g. 7 breakpoints would result in (0, 16%, 33%, 50%, 66%, 83%, 100%) fan speeds. + It is recommended to use a number that will capture one or more test points below the lowest level of fan normally used. For example, if 20% fan is the lowest commonly used speed, using 11 break points is recommended to test 10% and 20% fan at the low range. + +Default calibration of the hotend with seven fan breakpoints: ``` -MPC_CALIBRATE HEATER=heater_bed TARGET=100 +MPC_CALIBRATE HEATER=extruder FAN_BREAKPOINTS=7 ``` +> [!NOTE] +> Ensure that the part cooling fan is off before starting calibration. -After calibration the routine will generate the key model parameters which will be available for use in that printer session and are available in the log for future reference. +After successful calibration the method will generate the key model parameters into the log for future reference. ![Calibration Parameter Output](/docs/img/MPC_calibration_output.png) -A **SAVE_CONFIG** command is then required to commit these calibrated parameters to the printer config. The save config block should then look similar to: +A `SAVE_CONFIG` command is then required to commit these calibrated model parameters to the printer config or the user can manually update the values. The _SAVE_CONFIG_ block should then look like: ``` +#*# <----------- SAVE_CONFIG -----------> +#*# DO NOT EDIT THIS BLOCK OR BELOW. The contents are auto-generated. #*# [extruder] #*# control = mpc #*# block_heat_capacity = 22.3110 #*# sensor_responsiveness = 0.0998635 #*# ambient_transfer = 0.155082 #*# fan_ambient_transfer=0.155082, 0.20156, 0.216441 -#*# -#*# [heater_bed] -#*# control = mpc -#*# block_heat_capacity = 2078.86 -#*# sensor_responsiveness = 0.0139945 -#*# ambient_transfer = 15.6868 ``` > [!NOTE] -> If the [extruder] and [heater_bed] sections are located in a cfg file other than printer.cfg the SAVE_CONFIG command may not be able to write the calibration parameters and klippy will provide an error. - -The calibrated parameters are not suitable for pre-configuration or are not explicitly determinable. Advanced users could tweak these post calibration based on the following guidance: Slightly increasing these values will increase the temperature where MPC settles and slightly decreasing them will decrease the settling temperature. +> If the [extruder] section is in a .cfg file other than printer.cfg the `SAVE_CONFIG` command may not be able to write the calibration parameters and klippy will provide an error. + +These model parameters are not suitable for pre-configuration or are not explicitly determinable. Advanced users could tweak these post calibration based on the following guidance: Slightly increasing these values will increase the temperature where MPC settles and slightly decreasing them will decrease the settling temperature. + +- `block_heat_capacity:` + Heat capacity of the heater block in (J/K). + +- `ambient_transfer:` + Heat transfer from heater block to ambient in (W/K). + +- `sensor_responsiveness:` + A single constant representing the coefficient of heat transfer from heater block to sensor and heat capacity of the sensor in (K/s/K). + +- `fan_ambient_transfer:` + Heat transfer from heater block to ambient in with fan enabled in (W/K). + +# Support Macros + +## Temperature Wait + +The following macro can be used to replace `M109` hotend temperature set and `M190` bed temperature set G-code commands with a macro utilizing `temperature_wait` G-codes. This can be utilized in systems where the sensor temperature takes an extended time to converge on the set temperature. +> [!NOTE] +> This behaviour occurs primarily because MPC controls the modelled block temperature and not the hotend temperature sensor. For almost all cases, when temperature sensor overshoot/undershoot occurs, the block modelled temperature will be correctly at the set temperature. However, the Klipper system performs actions based on the sensor temperature only which can lead to undesirable delays in print actions with stock `M109` and `M190` commands. ``` -#block_heat_capacity: -# Heat capacity of the heater block in (J/K). -#ambient_transfer: -# Heat transfer from heater block to ambient in (W/K). -#sensor_responsiveness: -# A single constant representing the coefficient of heat transfer from -# heater block to sensor and heat capacity of the sensor in (K/s/K). -#fan_ambient_transfer: -# Heat transfer from heater block to ambient in with fan enabled in (W/K). -``` - -## Filament Feed Forward Physical Properties - -MPC works best knowing how much energy (in Joules) it takes to heat 1mm of filament by 1°C. The material values from the tables below have been curated from popular filament manufacturers and material data references. These values are sufficient for MPC to implement the filament feed forward feature. Advanced users could tune the filament_density and filament_heat_capacity parameters based on manufacturers datasheets. - -### Common Materials - -| Material | Density [g/cm³] | Specific heat [J/g/K] | -| -------- | --------------- | --------------------- | -| PLA | 1.25 | 1.8 - 2.2 | -| PETG | 1.27 | 1.7 - 2.2 | -| PC+ABS | 1.15 | 1.5 - 2.2 | -| ABS | 1.06 | 1.25 - 2.4 | -| ASA | 1.07 | 1.3 - 2.1 | -| PA6 | 1.12 | 2 - 2.5 | -| PA | 1.15 | 2 - 2.5 | -| PC | 1.20 | 1.1 - 1.9 | -| TPU | 1.21 | 1.5 - 2 | -| TPU-90A | 1.15 | 1.5 - 2 | -| TPU-95A | 1.22 | 1.5 - 2 | - -### Common Carbon Fiber Filled Materials +[gcode_macro M109] # Wait Hotend Temp +rename_existing: M109.1 +gcode: + #Parameters + {% set s = params.S|float %} -| Material | Density [g/cm³] | Specific heat [J/g/K] | -| --------- | --------------- | --------------------- | -| ABS-CF | 1.11 | ** | -| ASA-CF | 1.11 | ** | -| PA6-CF | 1.19 | ** | -| PC+ABS-CF | 1.22 | ** | -| PC+CF | 1.36 | ** | -| PLA-CF | 1.29 | ** | -| PETG-CF | 1.30 | ** | + M104 {% for p in params %}{'%s%s' % (p, params[p])}{% endfor %} # Set hotend temp + {% if s != 0 %} + TEMPERATURE_WAIT SENSOR=extruder MINIMUM={s-2} MAXIMUM={s+5} # Wait for hotend temp (within n degrees) + {% endif %} -**Use the specific heat from the base polymer -### An example macro for automatically setting these values +[gcode_macro M190] # Wait Bed Temp +rename_existing: M190.1 +gcode: + #Parameters + {% set s = params.S|float %} -These values are copied from the above tables, heat capacities are the middle of the range. + M140 {% for p in params %}{'%s%s' % (p, params[p])}{% endfor %} # Set bed temp + {% if s != 0 %} + TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM={s-2} MAXIMUM={s+5} # Wait for bed temp (within n degrees) + {% endif %} +``` -Your slicer must be configured to pass the current material type to your `PRINT_START`, for PrusaSlicer and family you may use `PRINT_START MATERIAL=[filament_type[initial_extruder]] # and other values...` +### Setting FFF Parameters From The Slicer -Then, in your `PRINT_START` macro, call `_SET_MPC_MATERIAL MATERIAL={params.MATERIAL}` +This macro will set FFF parameters automatically when the material type is passed from the slicer. ```ini -[gcode_macro SET_MPC_MATERIAL] +[gcode_macro _SET_MPC_MATERIAL] description: Set heater MPC parameters for a given material variable_filament_table: ## Update this table to adjust material settings @@ -285,6 +308,24 @@ gcode: MPC_SET HEATER={heater} FILAMENT_DENSITY={density} FILAMENT_HEAT_CAPACITY={heat_capacity} ``` +The slicer must be configured to pass the current material type to your `PRINT_START` macro. For PrusaSlicer you should add the following parameter line to `print_start` in the Start G-Code section: + +``` +MATERIAL=[filament_type[initial_extruder]] +``` + +The print_start line, in PrusaSlicer, would look like: + +``` +start_print MATERIAL=[filament_type[initial_extruder]] EXTRUDER_TEMP={first_layer_temperature[initial_extruder]} BED_TEMP={first_layer_bed_temperature[initial_extruder]} CHAMBER_TEMP={chamber_temperature} +``` + +Then, in your `PRINT_START` macro include the following macro call: + +``` +_SET_MPC_MATERIAL MATERIAL={params.MATERIAL} +``` + # Real-Time Model State The real-time temperatures and model states can be viewed from a browser by entering the following local address for your computer. @@ -295,6 +336,52 @@ https://192.168.xxx.xxx:7125/printer/objects/query?extruder ![Calibration](/docs/img/MPC_realtime_output.png) +# EXPERIMENTAL FEATURES + +## Bed Heater + +Using MPC for bed heater control is functional but the performance is not guaranteed or currently supported. MPC for the bed can be configured simply. + +``` +[heater_bed] +control: mpc +heater_power: 400 +``` + +- `control: mpc` + *Required* + The temperature control method. + +- `heater_power: 50` + *Required* + The nameplate heater power in watts. + +- `cooling_fan: fan_generic ` + _No Default Value_ + This is the fan cooling the bed. Optional parameter to support bed fans. + +The bed should be able to reach at least 90C to perform calibration with the following G-code. + +`MPC_CALIBRATE HEATER= [TARGET=] [FAN_BREAKPOINTS=]` + +- `HEATER=`: + The bed heater to be calibrated. + +- `TARGET=`: + _Default Value: 90 (deg C)_ + Sets the calibration temperature. The default of 90C is a good target for the bed. + +- `FAN_BREAKPOINTS=`: + _Default Value: 3_ + Sets the number of fan setpoint to test during calibration. + +Default calibration of the hotend with five fan breakpoints: +``` +MPC_CALIBRATE HEATER=heater_bed FAN_BREAKPOINTS=5 +``` + +These calibrated model parameters need to be saved to the _SAVE_CONFIG_ block manually or by using the `SAVE_CONFIG` command. + # BACKGROUND ## MPC Algorithm @@ -316,7 +403,7 @@ No simulation is perfect and, anyway, real life ambient temperature changes. So Steady_state_rate is used to recognize the asymptotic condition. Whenever the simulated hotend temperature changes at an absolute rate less than steady_state_rate between two successive runs of the algorithm, the steady state logic is applied. Since the algorithm runs frequently, even a small amount of noise can result in a fairly high instantaneous rate of change of hotend temperature. In practice 1°C/s seems to work well for steady_state_rate. -When in steady state, the difference between real and simulated sensor temperatures is used to drive the changes to ambient temperature. However when the temperatures are really close min_ambient_change ensures that the simulated ambient temperature converges relatively quickly. Larger values of min_ambient_change will result in faster convergence but will also cause the simulated ambient temperature to flutter somewhat chaotically around the ideal value. This is not a problem because the effect of ambient temperature is fairly small and short-term variations of even 10°C or more will not have a noticeable effect. +When in steady state, the difference between real and simulated sensor temperatures is used to drive the changes to ambient temperature. However, when the temperatures are really close min_ambient_change ensures that the simulated ambient temperature converges relatively quickly. Larger values of min_ambient_change will result in faster convergence but will also cause the simulated ambient temperature to flutter somewhat chaotically around the ideal value. This is not a problem because the effect of ambient temperature is fairly small and short-term variations of even 10°C or more will not have a noticeable effect. It is important to note that the simulated ambient temperature will only converge on real world ambient temperature if the ambient heat transfer coefficients are exactly accurate. In practice this will not be the case and the simulated ambient temperature therefore also acts a correction to these inaccuracies. diff --git a/klippy/extras/probe.py b/klippy/extras/probe.py index c8ac081ce..025cc6304 100644 --- a/klippy/extras/probe.py +++ b/klippy/extras/probe.py @@ -559,8 +559,8 @@ def _lift_toolhead(self): def _move_next(self): toolhead = self.printer.lookup_object("toolhead") - done = False # Check if done probing + done = False if len(self.results) >= len(self.probe_points): toolhead.get_last_move_time() res = self.finalize_callback(self.probe_offsets, self.results) diff --git a/klippy/klippy.py b/klippy/klippy.py index 656063ac9..05c42b962 100644 --- a/klippy/klippy.py +++ b/klippy/klippy.py @@ -384,7 +384,7 @@ def request_exit(self, result): wait_interrupted = WaitInterruption - def wait_while(self, condition_cb, error_on_cancel=True): + def wait_while(self, condition_cb, error_on_cancel=True, interval=1.0): """ receives a callback waits until callback returns False @@ -399,7 +399,7 @@ def wait_while(self, condition_cb, error_on_cancel=True): raise WaitInterruption("Command interrupted") else: return - eventtime = self.reactor.pause(eventtime + 1.0) + eventtime = self.reactor.pause(eventtime + interval) ###################################################################### diff --git a/test/klippy/mpc.cfg b/test/klippy/mpc.cfg new file mode 100644 index 000000000..5fc29574e --- /dev/null +++ b/test/klippy/mpc.cfg @@ -0,0 +1,76 @@ +# Config for extruder testing +[stepper_x] +step_pin: PF0 +dir_pin: PF1 +enable_pin: !PD7 +microsteps: 16 +rotation_distance: 40 +endstop_pin: ^PE5 +position_endstop: 0 +position_max: 200 +homing_speed: 50 + +[stepper_y] +step_pin: PF6 +dir_pin: !PF7 +enable_pin: !PF2 +microsteps: 16 +rotation_distance: 40 +endstop_pin: ^PJ1 +position_endstop: 0 +position_max: 200 +homing_speed: 50 + +[stepper_z] +step_pin: PL3 +dir_pin: PL1 +enable_pin: !PK0 +microsteps: 16 +rotation_distance: 8 +endstop_pin: ^PD3 +position_endstop: 0.5 +position_max: 200 + +[fan] +pin: PD6 + +[extruder] +step_pin: PA4 +dir_pin: PA6 +enable_pin: !PA2 +microsteps: 16 +rotation_distance: 33.5 +nozzle_diameter: 0.500 +filament_diameter: 3.500 +heater_pin: PB4 +sensor_type: EPCOS 100K B57560G104F +sensor_pin: PK5 +control: mpc +heater_power: 50 +cooling_fan: fan +filament_density: 1.20 +filament_heat_capacity: 1.8 +block_heat_capacity: 22.3110 +sensor_responsiveness: 0.0998635 +ambient_transfer: 0.155082 +fan_ambient_transfer: 0.155082, 0.20156, 0.216441 +min_temp: 0 +max_temp: 210 + +[extruder_stepper my_extra_stepper] +extruder: extruder +step_pin: PH5 +dir_pin: PH6 +enable_pin: !PB5 +microsteps: 16 +rotation_distance: 28.2 + +[mcu] +serial: /dev/ttyACM0 + +[printer] +kinematics: cartesian +max_velocity: 300 +max_accel: 3000 +max_z_velocity: 5 +max_z_accel: 100 diff --git a/test/klippy/mpc.test b/test/klippy/mpc.test new file mode 100644 index 000000000..0b9a297dc --- /dev/null +++ b/test/klippy/mpc.test @@ -0,0 +1,15 @@ +# mpc +DICTIONARY atmega2560.dict +CONFIG mpc.cfg + +# Extrude only +G1 E5 +G1 E-2 +G1 E7 + +# Home and extrusion moves +G28 +G1 X20 Y20 Z1 +G1 X25 Y25 E7.5 + +MPC_SET HEATER=extruder FILAMENT_DENSITY=1.15 FILAMENT_HEAT_CAPACITY=2.20