Permalink
Browse files

enable watchdog with manual reset (still untested, but doing at least…

… no harm) and adding some reasonable pid settings. Tested without actually printing.
  • Loading branch information...
1 parent 328a5dd commit 6d559672fcf7ee20f9229490af8b5ebb2c6533a1 @bkubicek committed Oct 26, 2011
Showing with 55 additions and 6 deletions.
  1. +12 −5 Marlin/Configuration.h
  2. +43 −1 Marlin/Marlin.pde
@@ -122,8 +122,15 @@ bool axis_relative_modes[] = {false, false, false, false};
#define DEFAULT_XYJERK 30.0*60
#define DEFAULT_ZJERK 10.0*60
-
// The watchdog waits for the watchperiod in milliseconds whenever an M104 or M109 increases the target temperature
+//this enables the watchdog interrupt.
+#define USE_WATCHDOG
+//you cannot reboot on a mega2560 due to a bug in he bootloader. Hence, you have to reset manually, and this is done hereby:
+#define RESET_MANUAL
+
+#define WATCHDOG_TIMEOUT 4
+
+
// If the temperature has not increased at the end of that period, the target temperature is set to zero. It can be reset with another M104/M109
//#define WATCHPERIOD 5000 //5 seconds
@@ -146,15 +153,15 @@ bool axis_relative_modes[] = {false, false, false, false};
#define SMOOTHFACTOR 5.0
float current_raw_average=0;
-//#define PIDTEMP
+#define PIDTEMP
#ifdef PIDTEMP
//#define PID_DEBUG 1 // Sends debug data to the serial port.
//#define PID_OPENLOOP 1 // Puts PID in open loop. M104 sets the output power in %
#define PID_MAX 255 // limits current to nozzle
#define PID_INTEGRAL_DRIVE_MAX 255
-#define PID_dT 0.16
-double Kp = 3000; //20.0;
-double Ki = 0; //1.5*PID_dT;
+#define PID_dT 0.05
+double Kp = 750; //20.0;
+double Ki =10*PID_dT; //1.5*PID_dT;
double Kd = 0; //80/PID_dT;
#endif // PIDTEMP
View
@@ -1356,7 +1356,48 @@ inline void kill()
}
*/
+#ifdef USE_WATCHDOG
+
+#include <avr/wdt.h>
+#include <avr/interrupt.h>
+
+volatile uint8_t timeout_seconds=0;
+
+void(* ctrlaltdelete) (void) = 0;
+
+ISR(WDT_vect) { //Watchdog timer interrupt, called if main program blocks >1sec
+ if(timeout_seconds++ >= WATCHDOG_TIMEOUT)
+ {
+ kill();
+#ifdef RESET_MANUAL
+ LCD_MESSAGE("Please Reset!");
+ ECHOLN("echo_: Something is wrong, please turn off the printer.");
+#else
+ LCD_MESSAGE("Timeout, resetting!");
+#endif
+ //disable watchdog, it will survife reboot.
+ WDTCSR |= (1<<WDCE) | (1<<WDE);
+ WDTCSR = 0;
+#ifdef RESET_MANUAL
+ while(1); //wait for user or serial reset
+#else
+ ctrlaltdelete();
+#endif
+ }
+}
+/// intialise watch dog with a 1 sec interrupt time
+void wd_init() {
+ WDTCSR = (1<<WDCE )|(1<<WDE ); //allow changes
+ WDTCSR = (1<<WDIF)|(1<<WDIE)| (1<<WDCE )|(1<<WDE )| (1<<WDP2 )|(1<<WDP1)|(0<<WDP0);
+}
+
+/// reset watchdog. MUST be called every 1s after init or avr will reset.
+void wd_reset() {
+ wdt_reset();
+ timeout_seconds=0; //reset counter for resets
+}
+#endif /* USE_WATCHDOG */
//####################################################################################################################
//####################################################################################################################
@@ -1415,7 +1456,8 @@ void manage_heater()
float error = target_raw - current_raw_average;
pTerm = (Kp * error) / 100.0;
temp_iState += error;
- temp_iState = constrain(temp_iState, temp_iState_min, temp_iState_max);
+ //temp_iState = constrain(temp_iState, temp_iState_min, temp_iState_max);
+ temp_iState = constrain(temp_iState, -1000, 1000);
iTerm = (Ki * temp_iState)/100.0;
dTerm = (Kd * (current_raw_average - temp_dState)) / 100.0;
temp_dState = current_raw_average;

0 comments on commit 6d55967

Please sign in to comment.