Implement PID_ADD_EXTRUSION_RATE

master
Scott Lahteine 9 years ago committed by Richard Wackerbarth
parent b5987de08a
commit bc50db9cf6

@ -41,7 +41,8 @@
// if Kc is chosen well, the additional required power due to increased melting should be compensated. // if Kc is chosen well, the additional required power due to increased melting should be compensated.
#define PID_ADD_EXTRUSION_RATE #define PID_ADD_EXTRUSION_RATE
#if ENABLED(PID_ADD_EXTRUSION_RATE) #if ENABLED(PID_ADD_EXTRUSION_RATE)
#define DEFAULT_Kc (1) //heating power=Kc*(e_speed) #define DEFAULT_Kc (100) //heating power=Kc*(e_speed)
#define LPQ_MAX_LEN 50
#endif #endif
#endif #endif

@ -329,6 +329,10 @@ extern int fanSpeed;
extern int meas_delay_cm; //delay distance extern int meas_delay_cm; //delay distance
#endif #endif
#if ENABLED(PID_ADD_EXTRUSION_RATE)
extern int lpq_len;
#endif
#if ENABLED(FWRETRACT) #if ENABLED(FWRETRACT)
extern bool autoretract_enabled; extern bool autoretract_enabled;
extern bool retracted[EXTRUDERS]; // extruder[n].retracted extern bool retracted[EXTRUDERS]; // extruder[n].retracted

@ -420,6 +420,10 @@ bool target_direction;
boolean chdkActive = false; boolean chdkActive = false;
#endif #endif
#if ENABLED(PID_ADD_EXTRUSION_RATE)
int lpq_len = 20;
#endif
//=========================================================================== //===========================================================================
//================================ Functions ================================ //================================ Functions ================================
//=========================================================================== //===========================================================================
@ -4770,7 +4774,16 @@ inline void gcode_M226() {
#if ENABLED(PIDTEMP) #if ENABLED(PIDTEMP)
/** /**
* M301: Set PID parameters P I D (and optionally C) * M301: Set PID parameters P I D (and optionally C, L)
*
* P[float] Kp term
* I[float] Ki term (unscaled)
* D[float] Kd term (unscaled)
*
* With PID_ADD_EXTRUSION_RATE:
*
* C[float] Kc term
* L[float] LPQ length
*/ */
inline void gcode_M301() { inline void gcode_M301() {
@ -4784,6 +4797,8 @@ inline void gcode_M226() {
if (code_seen('D')) PID_PARAM(Kd, e) = scalePID_d(code_value()); if (code_seen('D')) PID_PARAM(Kd, e) = scalePID_d(code_value());
#if ENABLED(PID_ADD_EXTRUSION_RATE) #if ENABLED(PID_ADD_EXTRUSION_RATE)
if (code_seen('C')) PID_PARAM(Kc, e) = code_value(); if (code_seen('C')) PID_PARAM(Kc, e) = code_value();
if (code_seen('L')) lpq_len = code_value();
NOMORE(lpq_len, LPQ_MAX_LEN);
#endif #endif
updatePID(); updatePID();

@ -14,7 +14,7 @@
* *
*/ */
#define EEPROM_VERSION "V20" #define EEPROM_VERSION "V21"
/** /**
* V19 EEPROM Layout: * V19 EEPROM Layout:
@ -60,6 +60,7 @@
* M301 E1 PIDC Kp[1], Ki[1], Kd[1], Kc[1] * M301 E1 PIDC Kp[1], Ki[1], Kd[1], Kc[1]
* M301 E2 PIDC Kp[2], Ki[2], Kd[2], Kc[2] * M301 E2 PIDC Kp[2], Ki[2], Kd[2], Kc[2]
* M301 E3 PIDC Kp[3], Ki[3], Kd[3], Kc[3] * M301 E3 PIDC Kp[3], Ki[3], Kd[3], Kc[3]
* M301 L lpq_len
* *
* PIDTEMPBED: * PIDTEMPBED:
* M304 PID bedKp, bedKi, bedKd * M304 PID bedKp, bedKi, bedKd
@ -227,6 +228,11 @@ void Config_StoreSettings() {
} // Extruders Loop } // Extruders Loop
#if DISABLED(PID_ADD_EXTRUSION_RATE)
int lpq_len = 20;
#endif
EEPROM_WRITE_VAR(i, lpq_len);
#if DISABLED(PIDTEMPBED) #if DISABLED(PIDTEMPBED)
float bedKp = DUMMY_PID_VALUE, bedKi = DUMMY_PID_VALUE, bedKd = DUMMY_PID_VALUE; float bedKp = DUMMY_PID_VALUE, bedKi = DUMMY_PID_VALUE, bedKd = DUMMY_PID_VALUE;
#endif #endif
@ -393,6 +399,11 @@ void Config_RetrieveSettings() {
for (int q=16; q--;) EEPROM_READ_VAR(i, dummy); // 4x Kp, Ki, Kd, Kc for (int q=16; q--;) EEPROM_READ_VAR(i, dummy); // 4x Kp, Ki, Kd, Kc
#endif // !PIDTEMP #endif // !PIDTEMP
#if DISABLED(PID_ADD_EXTRUSION_RATE)
int lpq_len;
#endif
EEPROM_READ_VAR(i, lpq_len);
#if DISABLED(PIDTEMPBED) #if DISABLED(PIDTEMPBED)
float bedKp, bedKi, bedKd; float bedKp, bedKi, bedKd;
#endif #endif
@ -539,6 +550,9 @@ void Config_ResetDefault() {
PID_PARAM(Kc, e) = DEFAULT_Kc; PID_PARAM(Kc, e) = DEFAULT_Kc;
#endif #endif
} }
#if ENABLED(PID_ADD_EXTRUSION_RATE)
lpq_len = 20; // default last-position-queue size
#endif
// call updatePID (similar to when we have processed M301) // call updatePID (similar to when we have processed M301)
updatePID(); updatePID();
#endif // PIDTEMP #endif // PIDTEMP
@ -744,6 +758,7 @@ void Config_PrintSettings(bool forReplay) {
SERIAL_ECHOPAIR(" D", unscalePID_d(PID_PARAM(Kd, i))); SERIAL_ECHOPAIR(" D", unscalePID_d(PID_PARAM(Kd, i)));
#if ENABLED(PID_ADD_EXTRUSION_RATE) #if ENABLED(PID_ADD_EXTRUSION_RATE)
SERIAL_ECHOPAIR(" C", PID_PARAM(Kc, i)); SERIAL_ECHOPAIR(" C", PID_PARAM(Kc, i));
if (i == 0) SERIAL_ECHOPAIR(" L", lpq_len);
#endif #endif
SERIAL_EOL; SERIAL_EOL;
} }
@ -758,6 +773,7 @@ void Config_PrintSettings(bool forReplay) {
SERIAL_ECHOPAIR(" D", unscalePID_d(PID_PARAM(Kd, 0))); SERIAL_ECHOPAIR(" D", unscalePID_d(PID_PARAM(Kd, 0)));
#if ENABLED(PID_ADD_EXTRUSION_RATE) #if ENABLED(PID_ADD_EXTRUSION_RATE)
SERIAL_ECHOPAIR(" C", PID_PARAM(Kc, 0)); SERIAL_ECHOPAIR(" C", PID_PARAM(Kc, 0));
SERIAL_ECHOPAIR(" L", lpq_len);
#endif #endif
SERIAL_EOL; SERIAL_EOL;
} }

@ -200,6 +200,7 @@
#define MSG_PID_DEBUG_PTERM " pTerm " #define MSG_PID_DEBUG_PTERM " pTerm "
#define MSG_PID_DEBUG_ITERM " iTerm " #define MSG_PID_DEBUG_ITERM " iTerm "
#define MSG_PID_DEBUG_DTERM " dTerm " #define MSG_PID_DEBUG_DTERM " dTerm "
#define MSG_PID_DEBUG_CTERM " cTerm "
#define MSG_INVALID_EXTRUDER_NUM " - Invalid extruder number !" #define MSG_INVALID_EXTRUDER_NUM " - Invalid extruder number !"
#define MSG_HEATER_BED "bed" #define MSG_HEATER_BED "bed"

@ -99,6 +99,12 @@ static volatile bool temp_meas_ready = false;
static float pTerm[EXTRUDERS]; static float pTerm[EXTRUDERS];
static float iTerm[EXTRUDERS]; static float iTerm[EXTRUDERS];
static float dTerm[EXTRUDERS]; static float dTerm[EXTRUDERS];
#if ENABLED(PID_ADD_EXTRUSION_RATE)
static float cTerm[EXTRUDERS];
static long last_position[EXTRUDERS];
static long lpq[LPQ_MAX_LEN];
static int lpq_ptr = 0;
#endif
//int output; //int output;
static float pid_error[EXTRUDERS]; static float pid_error[EXTRUDERS];
static float temp_iState_min[EXTRUDERS]; static float temp_iState_min[EXTRUDERS];
@ -357,6 +363,9 @@ void updatePID() {
#if ENABLED(PIDTEMP) #if ENABLED(PIDTEMP)
for (int e = 0; e < EXTRUDERS; e++) { for (int e = 0; e < EXTRUDERS; e++) {
temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e); temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e);
#if ENABLED(PID_ADD_EXTRUSION_RATE)
last_position[e] = 0;
#endif
} }
#endif #endif
#if ENABLED(PIDTEMPBED) #if ENABLED(PIDTEMPBED)
@ -497,6 +506,23 @@ float get_pid_output(int e) {
iTerm[e] = PID_PARAM(Ki,e) * temp_iState[e]; iTerm[e] = PID_PARAM(Ki,e) * temp_iState[e];
pid_output = pTerm[e] + iTerm[e] - dTerm[e]; pid_output = pTerm[e] + iTerm[e] - dTerm[e];
#if ENABLED(PID_ADD_EXTRUSION_RATE)
cTerm[e] = 0;
if (e == active_extruder) {
long e_position = st_get_position(E_AXIS);
if (e_position > last_position[e]) {
lpq[lpq_ptr++] = e_position - last_position[e];
last_position[e] = e_position;
} else {
lpq[lpq_ptr++] = 0;
}
if (lpq_ptr >= lpq_len) lpq_ptr = 0;
cTerm[e] = (lpq[lpq_ptr] / axis_steps_per_unit[E_AXIS]) * Kc;
pid_output += cTerm[e];
}
#endif //PID_ADD_EXTRUSION_RATE
if (pid_output > PID_MAX) { if (pid_output > PID_MAX) {
if (pid_error[e] > 0) temp_iState[e] -= pid_error[e]; // conditional un-integration if (pid_error[e] > 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
pid_output = PID_MAX; pid_output = PID_MAX;
@ -512,18 +538,16 @@ float get_pid_output(int e) {
#if ENABLED(PID_DEBUG) #if ENABLED(PID_DEBUG)
SERIAL_ECHO_START; SERIAL_ECHO_START;
SERIAL_ECHO(MSG_PID_DEBUG); SERIAL_ECHOPAIR(MSG_PID_DEBUG, e);
SERIAL_ECHO(e); SERIAL_ECHOPAIR(MSG_PID_DEBUG_INPUT, current_temperature[e]);
SERIAL_ECHO(MSG_PID_DEBUG_INPUT); SERIAL_ECHOPAIR(MSG_PID_DEBUG_OUTPUT, pid_output);
SERIAL_ECHO(current_temperature[e]); SERIAL_ECHOPAIR(MSG_PID_DEBUG_PTERM, pTerm[e]);
SERIAL_ECHO(MSG_PID_DEBUG_OUTPUT); SERIAL_ECHOPAIR(MSG_PID_DEBUG_ITERM, iTerm[e]);
SERIAL_ECHO(pid_output); SERIAL_ECHOPAIR(MSG_PID_DEBUG_DTERM, dTerm[e]);
SERIAL_ECHO(MSG_PID_DEBUG_PTERM); #if ENABLED(PID_ADD_EXTRUSION_RATE)
SERIAL_ECHO(pTerm[e]); SERIAL_ECHOPAIR(MSG_PID_DEBUG_CTERM, cTerm[e]);
SERIAL_ECHO(MSG_PID_DEBUG_ITERM); #endif
SERIAL_ECHO(iTerm[e]); SERIAL_EOL;
SERIAL_ECHO(MSG_PID_DEBUG_DTERM);
SERIAL_ECHOLN(dTerm[e]);
#endif //PID_DEBUG #endif //PID_DEBUG
#else /* PID off */ #else /* PID off */
@ -837,6 +861,9 @@ void tp_init() {
#if ENABLED(PIDTEMP) #if ENABLED(PIDTEMP)
temp_iState_min[e] = 0.0; temp_iState_min[e] = 0.0;
temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e); temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e);
#if ENABLED(PID_ADD_EXTRUSION_RATE)
last_position[e] = 0;
#endif
#endif //PIDTEMP #endif //PIDTEMP
#if ENABLED(PIDTEMPBED) #if ENABLED(PIDTEMPBED)
temp_iState_min_bed = 0.0; temp_iState_min_bed = 0.0;

Loading…
Cancel
Save