Merge pull request #3571 from thinkyhead/rc_m303_with_pidtempbed

Allow setting PIDTEMP and PIDTEMPBED together or apart
This commit is contained in:
Scott Lahteine 2016-04-19 18:26:59 -07:00
commit 3b0bf88233
5 changed files with 101 additions and 35 deletions

View file

@ -505,6 +505,12 @@
#define BED_USES_THERMISTOR #define BED_USES_THERMISTOR
#endif #endif
/**
* Flags for PID handling
*/
#define HAS_PID_HEATING (ENABLED(PIDTEMP) || ENABLED(PIDTEMPBED))
#define HAS_PID_FOR_BOTH (ENABLED(PIDTEMP) && ENABLED(PIDTEMPBED))
/** /**
* ARRAY_BY_EXTRUDERS based on EXTRUDERS * ARRAY_BY_EXTRUDERS based on EXTRUDERS
*/ */

View file

@ -5559,7 +5559,7 @@ inline void gcode_M226() {
* U<bool> with a non-zero value will apply the result to current settings * U<bool> with a non-zero value will apply the result to current settings
*/ */
inline void gcode_M303() { inline void gcode_M303() {
#if ENABLED(PIDTEMP) #if HAS_PID_HEATING
int e = code_seen('E') ? code_value_short() : 0; int e = code_seen('E') ? code_value_short() : 0;
int c = code_seen('C') ? code_value_short() : 5; int c = code_seen('C') ? code_value_short() : 5;
bool u = code_seen('U') && code_value_short() != 0; bool u = code_seen('U') && code_value_short() != 0;

View file

@ -794,7 +794,7 @@ void Config_PrintSettings(bool forReplay) {
SERIAL_EOL; SERIAL_EOL;
#endif // ULTIPANEL #endif // ULTIPANEL
#if ENABLED(PIDTEMP) || ENABLED(PIDTEMPBED) #if HAS_PID_HEATING
CONFIG_ECHO_START; CONFIG_ECHO_START;
if (!forReplay) { if (!forReplay) {

View file

@ -221,7 +221,7 @@ static void updateTemperaturesFromRawValues();
//================================ Functions ================================ //================================ Functions ================================
//=========================================================================== //===========================================================================
#if ENABLED(PIDTEMP) #if HAS_PID_HEATING
void PID_autotune(float temp, int extruder, int ncycles, bool set_result/*=false*/) { void PID_autotune(float temp, int extruder, int ncycles, bool set_result/*=false*/) {
float input = 0.0; float input = 0.0;
@ -240,8 +240,13 @@ static void updateTemperaturesFromRawValues();
millis_t next_auto_fan_check_ms = temp_ms + 2500UL; millis_t next_auto_fan_check_ms = temp_ms + 2500UL;
#endif #endif
if (extruder >= EXTRUDERS if (false
#if !HAS_TEMP_BED #if ENABLED(PIDTEMP)
|| extruder >= EXTRUDERS
#else
|| extruder >= 0
#endif
#if DISABLED(PIDTEMPBED)
|| extruder < 0 || extruder < 0
#endif #endif
) { ) {
@ -253,10 +258,16 @@ static void updateTemperaturesFromRawValues();
disable_all_heaters(); // switch off all heaters. disable_all_heaters(); // switch off all heaters.
#if HAS_PID_FOR_BOTH
if (extruder < 0) if (extruder < 0)
soft_pwm_bed = bias = d = (MAX_BED_POWER) / 2; soft_pwm_bed = bias = d = (MAX_BED_POWER) / 2;
else else
soft_pwm[extruder] = bias = d = (PID_MAX) / 2; soft_pwm[extruder] = bias = d = (PID_MAX) / 2;
#elif ENABLED(PIDTEMP)
soft_pwm[extruder] = bias = d = (PID_MAX) / 2;
#else
soft_pwm_bed = bias = d = (MAX_BED_POWER) / 2;
#endif
// PID Tuning loop // PID Tuning loop
for (;;) { for (;;) {
@ -266,7 +277,15 @@ static void updateTemperaturesFromRawValues();
if (temp_meas_ready) { // temp sample ready if (temp_meas_ready) { // temp sample ready
updateTemperaturesFromRawValues(); updateTemperaturesFromRawValues();
input = (extruder < 0) ? current_temperature_bed : current_temperature[extruder]; input =
#if HAS_PID_FOR_BOTH
extruder < 0 ? current_temperature_bed : current_temperature[extruder]
#elif ENABLED(PIDTEMP)
current_temperature[extruder]
#else
current_temperature_bed
#endif
;
max = max(max, input); max = max(max, input);
min = min(min, input); min = min(min, input);
@ -281,10 +300,16 @@ static void updateTemperaturesFromRawValues();
if (heating && input > temp) { if (heating && input > temp) {
if (ELAPSED(ms, t2 + 5000UL)) { if (ELAPSED(ms, t2 + 5000UL)) {
heating = false; heating = false;
#if HAS_PID_FOR_BOTH
if (extruder < 0) if (extruder < 0)
soft_pwm_bed = (bias - d) >> 1; soft_pwm_bed = (bias - d) >> 1;
else else
soft_pwm[extruder] = (bias - d) >> 1; soft_pwm[extruder] = (bias - d) >> 1;
#elif ENABLED(PIDTEMP)
soft_pwm[extruder] = (bias - d) >> 1;
#elif ENABLED(PIDTEMPBED)
soft_pwm_bed = (bias - d) >> 1;
#endif
t1 = ms; t1 = ms;
t_high = t1 - t2; t_high = t1 - t2;
max = temp; max = temp;
@ -297,7 +322,15 @@ static void updateTemperaturesFromRawValues();
t2 = ms; t2 = ms;
t_low = t2 - t1; t_low = t2 - t1;
if (cycles > 0) { if (cycles > 0) {
long max_pow = extruder < 0 ? MAX_BED_POWER : PID_MAX; long max_pow =
#if HAS_PID_FOR_BOTH
extruder < 0 ? MAX_BED_POWER : PID_MAX
#elif ENABLED(PIDTEMP)
PID_MAX
#else
MAX_BED_POWER
#endif
;
bias += (d * (t_high - t_low)) / (t_low + t_high); bias += (d * (t_high - t_low)) / (t_low + t_high);
bias = constrain(bias, 20, max_pow - 20); bias = constrain(bias, 20, max_pow - 20);
d = (bias > max_pow / 2) ? max_pow - 1 - bias : bias; d = (bias > max_pow / 2) ? max_pow - 1 - bias : bias;
@ -336,10 +369,16 @@ static void updateTemperaturesFromRawValues();
*/ */
} }
} }
#if HAS_PID_FOR_BOTH
if (extruder < 0) if (extruder < 0)
soft_pwm_bed = (bias + d) >> 1; soft_pwm_bed = (bias + d) >> 1;
else else
soft_pwm[extruder] = (bias + d) >> 1; soft_pwm[extruder] = (bias + d) >> 1;
#elif ENABLED(PIDTEMP)
soft_pwm[extruder] = (bias + d) >> 1;
#else
soft_pwm_bed = (bias + d) >> 1;
#endif
cycles++; cycles++;
min = temp; min = temp;
} }
@ -366,27 +405,48 @@ static void updateTemperaturesFromRawValues();
} }
if (cycles > ncycles) { if (cycles > ncycles) {
SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED); SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
#if HAS_PID_FOR_BOTH
const char* estring = extruder < 0 ? "bed" : ""; const char* estring = extruder < 0 ? "bed" : "";
SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Kp "); SERIAL_PROTOCOLLN(workKp); SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Kp "); SERIAL_PROTOCOLLN(workKp);
SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Ki "); SERIAL_PROTOCOLLN(workKi); SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Ki "); SERIAL_PROTOCOLLN(workKi);
SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Kd "); SERIAL_PROTOCOLLN(workKd); SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Kd "); SERIAL_PROTOCOLLN(workKd);
#elif ENABLED(PIDTEMP)
SERIAL_PROTOCOLPGM("#define DEFAULT_Kp "); SERIAL_PROTOCOLLN(workKp);
SERIAL_PROTOCOLPGM("#define DEFAULT_Ki "); SERIAL_PROTOCOLLN(workKi);
SERIAL_PROTOCOLPGM("#define DEFAULT_Kd "); SERIAL_PROTOCOLLN(workKd);
#else
SERIAL_PROTOCOLPGM("#define DEFAULT_bedKp "); SERIAL_PROTOCOLLN(workKp);
SERIAL_PROTOCOLPGM("#define DEFAULT_bedKi "); SERIAL_PROTOCOLLN(workKi);
SERIAL_PROTOCOLPGM("#define DEFAULT_bedKd "); SERIAL_PROTOCOLLN(workKd);
#endif
#define _SET_BED_PID() \
bedKp = workKp; \
bedKi = scalePID_i(workKi); \
bedKd = scalePID_d(workKd); \
updatePID()
#define _SET_EXTRUDER_PID() \
PID_PARAM(Kp, extruder) = workKp; \
PID_PARAM(Ki, extruder) = scalePID_i(workKi); \
PID_PARAM(Kd, extruder) = scalePID_d(workKd); \
updatePID()
// Use the result? (As with "M303 U1") // Use the result? (As with "M303 U1")
if (set_result) { if (set_result) {
#if HAS_PID_FOR_BOTH
if (extruder < 0) { if (extruder < 0) {
#if ENABLED(PIDTEMPBED) _SET_BED_PID();
bedKp = workKp;
bedKi = scalePID_i(workKi);
bedKd = scalePID_d(workKd);
updatePID();
#endif
} }
else { else {
PID_PARAM(Kp, extruder) = workKp; _SET_EXTRUDER_PID();
PID_PARAM(Ki, extruder) = scalePID_i(workKi);
PID_PARAM(Kd, extruder) = scalePID_d(workKd);
updatePID();
} }
#elif ENABLED(PIDTEMP)
_SET_EXTRUDER_PID();
#else
_SET_BED_PID();
#endif
} }
return; return;
} }

View file

@ -1343,7 +1343,7 @@ static void lcd_control_menu() {
static void _lcd_autotune(int e) { static void _lcd_autotune(int e) {
char cmd[30]; char cmd[30];
sprintf_P(cmd, PSTR("M303 U1 E%d S%d"), e, sprintf_P(cmd, PSTR("M303 U1 E%d S%d"), e,
#if ENABLED(PIDTEMP) && ENABLED(PIDTEMPBED) #if HAS_PID_FOR_BOTH
e < 0 ? autotune_temp_bed : autotune_temp[e] e < 0 ? autotune_temp_bed : autotune_temp[e]
#elif ENABLED(PIDTEMPBED) #elif ENABLED(PIDTEMPBED)
autotune_temp_bed autotune_temp_bed