chiark / gitweb /
this is not working. Do you maybe know why?
authorBernhard Kubicek <kubicek@gmx.at>
Sat, 5 Nov 2011 15:43:44 +0000 (16:43 +0100)
committerBernhard Kubicek <kubicek@gmx.at>
Sat, 5 Nov 2011 15:43:44 +0000 (16:43 +0100)
Marlin/EEPROMwrite.h
Marlin/Marlin.h
Marlin/Marlin.pde
Marlin/planner.cpp
Marlin/stepper.cpp
Marlin/temperature.cpp
Marlin/temperature.h
Marlin/ultralcd.pde

index 2247e2f52d486cf9d401d0c61e96da47f4bc526d..4e5364d80b43b22c5f560395bd95fd51d4315253 100644 (file)
@@ -48,9 +48,9 @@ void StoreSettings() {
   EEPROM_writeAnything(i,max_xy_jerk);\r
   EEPROM_writeAnything(i,max_z_jerk);\r
   #ifdef PIDTEMP\r
-  EEPROM_writeAnything(i,Kp);\r
-  EEPROM_writeAnything(i,Ki);\r
-  EEPROM_writeAnything(i,Kd);\r
+  EEPROM_writeAnything(i,Heater::Kp);\r
+  EEPROM_writeAnything(i,Heater::Ki);\r
+  EEPROM_writeAnything(i,Heater::Kd);\r
 #else\r
   EEPROM_writeAnything(i,3000);\r
   EEPROM_writeAnything(i,0);\r
@@ -81,11 +81,11 @@ void RetrieveSettings(bool def=false){  // if def=true, the default values will
       EEPROM_readAnything(i,max_xy_jerk);\r
       EEPROM_readAnything(i,max_z_jerk);\r
 #ifndef PIDTEMP\r
-      float Kp,Ki,Kd;\r
+      float Kp,Ki,Kd; //read and ignore..\r
 #endif\r
-      EEPROM_readAnything(i,Kp);\r
-      EEPROM_readAnything(i,Ki);\r
-      EEPROM_readAnything(i,Kd);\r
+      EEPROM_readAnything(i,Heater::Kp);\r
+      EEPROM_readAnything(i,Heater::Ki);\r
+      EEPROM_readAnything(i,Heater::Kd);\r
 \r
       ECHOLN("Stored settings retreived:");\r
   }\r
@@ -119,7 +119,7 @@ void RetrieveSettings(bool def=false){  // if def=true, the default values will
   ECHOLN("   M205 S"  <<_FLOAT(minimumfeedrate/60,2) << " T" << _FLOAT(mintravelfeedrate/60,2) << " B" << _FLOAT(minsegmenttime,2) << " X" << _FLOAT(max_xy_jerk/60,2) << " Z" << _FLOAT(max_z_jerk/60,2));\r
 #ifdef PIDTEMP\r
   ECHOLN("PID settings:");\r
-  ECHOLN("   M301 P"  << _FLOAT(Kp,3) << " I" << _FLOAT(Ki,3) << " D" << _FLOAT(Kd,3));  \r
+  ECHOLN("   M301 P"  << _FLOAT(Heater::Kp,3) << " I" << _FLOAT(Heater::Ki,3) << " D" << _FLOAT(Heater::Kd,3));  \r
 #endif\r
   \r
 }  \r
index f85dad35761b825b2bbd89cc6d94ff18cb60be50..4cd4c8d39beffd85509e402d5ad45c2bb08604c8 100644 (file)
@@ -10,6 +10,7 @@
 #define ECHO(x) Serial << "echo: " << x;\r
 #define ECHOLN(x) Serial << "echo: "<<x<<endl;\r
 \r
+\r
 void get_command();\r
 void process_commands();\r
 \r
index 3cf8d3bdf033ff7a96ccc7b14fb8d65313438e4b..b126848a626e593d201f31aaf64740fbd41807e4 100644 (file)
@@ -40,6 +40,7 @@
   #include "Simplelcd.h"\r
 #endif\r
 \r
+Heater htr;\r
 char version_string[] = "1.0.0 Alpha 1";\r
 \r
 #ifdef SDSUPPORT\r
@@ -263,7 +264,7 @@ void setup()
 #endif //SDSUPPORT\r
   plan_init();  // Initialize planner;\r
   st_init();    // Initialize stepper;\r
-  tp_init();    // Initialize temperature loop\r
+  //tp_init();    // Initialize temperature loop is now done by the constructor of the Heater class\r
        //checkautostart();\r
 }\r
 \r
@@ -367,7 +368,7 @@ void loop()
     bufindr = (bufindr + 1)%BUFSIZE;\r
   }\r
   //check heater every n milliseconds\r
-  manage_heater();\r
+  Heater::manage_heater();\r
   manage_inactivity(1);\r
   LCD_STATUS;\r
 }\r
@@ -547,7 +548,7 @@ inline void process_commands()
       if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait\r
       codenum += millis();  // keep track of when we started waiting\r
       while(millis()  < codenum ){\r
-        manage_heater();\r
+        Heater::manage_heater();\r
       }\r
       break;\r
     case 28: //G28 Home all Axis one at a time\r
@@ -801,12 +802,9 @@ inline void process_commands()
         }\r
         break;\r
       case 104: // M104\r
-                if (code_seen('S')) target_raw[TEMPSENSOR_HOTEND] = temp2analog(code_value());\r
-#ifdef PIDTEMP\r
-                pid_setpoint = code_value();\r
-#endif //PIDTEM\r
+        if (code_seen('S')) Heater::setCelsius(TEMPSENSOR_HOTEND,code_value());\r
         #ifdef WATCHPERIOD\r
-            if(target_raw[TEMPSENSOR_HOTEND] > current_raw[TEMPSENSOR_HOTEND]){\r
+            if(Heater::isHeating(TEMPSENSOR_HOTEND)){\r
                 watchmillis = max(1,millis());\r
                 watch_raw[TEMPSENSOR_HOTEND] = current_raw[TEMPSENSOR_HOTEND];\r
             }else{\r
@@ -815,14 +813,14 @@ inline void process_commands()
         #endif\r
         break;\r
       case 140: // M140 set bed temp\r
-                if (code_seen('S')) target_raw[TEMPSENSOR_BED] = temp2analogBed(code_value());\r
+                if (code_seen('S')) Heater::setCelsius(TEMPSENSOR_BED,code_value());\r
         break;\r
       case 105: // M105\r
         #if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595)\r
-                tt = analog2temp(current_raw[TEMPSENSOR_HOTEND]);\r
+                tt = Heater::celsius(TEMPSENSOR_HOTEND);\r
         #endif\r
         #if TEMP_1_PIN > -1\r
-                bt = analog2tempBed(current_raw[TEMPSENSOR_BED]);\r
+                bt = Heater::celsius(TEMPSENSOR_BED);\r
         #endif\r
         #if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595)\r
             Serial.print("ok T:");\r
@@ -833,14 +831,14 @@ inline void process_commands()
 #ifdef PIDTEMP\r
             Serial.print(" B:");\r
             #if TEMP_1_PIN > -1\r
-            Serial.println(bt); \r
+             Serial.println(bt); \r
             #else\r
-            Serial.println(HeaterPower); \r
+             Serial.println(Heater::HeaterPower); \r
             #endif\r
 #else\r
             Serial.println();\r
 #endif\r
-          #else\r
+          #else<\r
             Serial.println();\r
           #endif\r
         #else\r
@@ -850,14 +848,12 @@ inline void process_commands()
         //break;\r
       case 109: {// M109 - Wait for extruder heater to reach target.\r
             LCD_MESSAGE("Heating...");\r
-               if (code_seen('S')) target_raw[TEMPSENSOR_HOTEND] = temp2analog(code_value());\r
-            #ifdef PIDTEMP\r
-            pid_setpoint = code_value();\r
-            #endif //PIDTEM\r
+               if (code_seen('S')) Heater::setCelsius(TEMPSENSOR_HOTEND,code_value());\r
+            \r
             #ifdef WATCHPERIOD\r
-          if(target_raw[TEMPSENSOR_HOTEND]>current_raw[TEMPSENSOR_HOTEND]){\r
+          if(Heater::isHeating(TEMPSENSOR_HOTEND)){\r
               watchmillis = max(1,millis());\r
-              watch_raw[TEMPSENSOR_HOTEND] = current_raw[TEMPSENSOR_HOTEND];\r
+              watch_raw[TEMPSENSOR_HOTEND] = Heater::current_raw[TEMPSENSOR_HOTEND];\r
             } else {\r
               watchmillis = 0;\r
             }\r
@@ -865,31 +861,31 @@ inline void process_commands()
             codenum = millis(); \r
      \r
                /* See if we are heating up or cooling down */\r
-              bool target_direction = (current_raw[0] < target_raw[0]); // true if heating, false if cooling\r
+              bool target_direction = Heater::isHeating(TEMPSENSOR_HOTEND); // true if heating, false if cooling\r
 \r
             #ifdef TEMP_RESIDENCY_TIME\r
             long residencyStart;\r
             residencyStart = -1;\r
             /* continue to loop until we have reached the target temp   \r
               _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */\r
-            while((target_direction ? (current_raw[0] < target_raw[0]) : (current_raw[0] > target_raw[0])) ||\r
+            while((target_direction ? Heater::isHeating(TEMPSENSOR_HOTEND) : Heater::isCooling(TEMPSENSOR_HOTEND)) ||\r
                     (residencyStart > -1 && (millis() - residencyStart) < TEMP_RESIDENCY_TIME*1000) ) {\r
             #else\r
-            while ( target_direction ? (current_raw[0] < target_raw[0]) : (current_raw[0] > target_raw[0]) ) {\r
+            while ( target_direction ? Heater::isHeating(TEMPSENSOR_HOTEND) : Heater::isCooling(TEMPSENSOR_HOTEND) ) {\r
             #endif //TEMP_RESIDENCY_TIME\r
               if( (millis() - codenum) > 1000 ) { //Print Temp Reading every 1 second while heating up/cooling down\r
                 Serial.print("T:");\r
-              Serial.println( analog2temp(current_raw[TEMPSENSOR_HOTEND]) ); \r
+              Serial.println( Heater::celsius(TEMPSENSOR_HOTEND) ); \r
                 codenum = millis();\r
               }\r
-              manage_heater();\r
+              Heater::manage_heater();\r
               LCD_STATUS;\r
               #ifdef TEMP_RESIDENCY_TIME\r
                /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time\r
                   or when current temp falls outside the hysteresis after target temp was reached */\r
-              if ((residencyStart == -1 &&  target_direction && current_raw[0] >= target_raw[0]) ||\r
-                  (residencyStart == -1 && !target_direction && current_raw[0] <= target_raw[0]) ||\r
-                  (residencyStart > -1 && labs(analog2temp(current_raw[0]) - analog2temp(target_raw[0])) > TEMP_HYSTERESIS) ) {\r
+              if ((residencyStart == -1 &&  target_direction && !Heater::isHeating(TEMPSENSOR_HOTEND)) ||\r
+                  (residencyStart == -1 && !target_direction && !Heater::isCooling(TEMPSENSOR_HOTEND)) ||\r
+                  (residencyStart > -1 && labs(Heater::celsius(TEMPSENSOR_HOTEND) - Heater::celsiusTarget(TEMPSENSOR_HOTEND)) > TEMP_HYSTERESIS) ) {\r
                 residencyStart = millis();\r
               }\r
               #endif //TEMP_RESIDENCY_TIME\r
@@ -899,22 +895,22 @@ inline void process_commands()
           break;\r
       case 190: // M190 - Wait bed for heater to reach target.\r
       #if TEMP_1_PIN > -1\r
-          if (code_seen('S')) target_raw[TEMPSENSOR_BED] = temp2analog(code_value());\r
+          if (code_seen('S')) Heater::setCelsius(TEMPSENSOR_BED,code_value());\r
         codenum = millis(); \r
-          while(current_raw[TEMPSENSOR_BED] < target_raw[TEMPSENSOR_BED]\r
-                                {\r
+          while(Heater::isHeating(TEMPSENSOR_BED)\r
+          {\r
           if( (millis()-codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.\r
           {\r
-            float tt=analog2temp(current_raw[TEMPSENSOR_HOTEND]);\r
+            float tt=Heater::celsius(TEMPSENSOR_HOTEND);\r
             Serial.print("T:");\r
             Serial.println( tt );\r
             Serial.print("ok T:");\r
             Serial.print( tt ); \r
             Serial.print(" B:");\r
-            Serial.println( analog2temp(current_raw[TEMPSENSOR_BED]) ); \r
+            Serial.println( Heater::celsius(TEMPSENSOR_BED) ); \r
             codenum = millis(); \r
           }\r
-            manage_heater();\r
+            Heater::manage_heater();\r
         }\r
       #endif\r
       break;\r
@@ -1066,9 +1062,13 @@ inline void process_commands()
       break;\r
 #ifdef PIDTEMP\r
     case 301: // M301\r
-      if(code_seen('P')) Kp = code_value();\r
-      if(code_seen('I')) Ki = code_value()*PID_dT;\r
-      if(code_seen('D')) Kd = code_value()/PID_dT;\r
+      if(code_seen('P')) Heater::Kp = code_value();\r
+      if(code_seen('I')) Heater::Ki = code_value()*PID_dT;\r
+      if(code_seen('D')) Heater::Kd = code_value()/PID_dT;\r
+      #ifdef PID_ADD_EXTRUSION_RATE\r
+        if(code_seen('C')) Heater::Kc = code_value();\r
+      #endif\r
+\r
 //      ECHOLN("Kp "<<_FLOAT(Kp,2));\r
 //      ECHOLN("Ki "<<_FLOAT(Ki/PID_dT,2));\r
 //      ECHOLN("Kd "<<_FLOAT(Kd*PID_dT,2));\r
@@ -1194,19 +1194,19 @@ void wd_reset() {
 inline void kill()\r
 {\r
   #if TEMP_0_PIN > -1\r
-  target_raw[0]=0;\r
+  Heater::setCelsius(TEMPSENSOR_HOTEND,0);\r
    #if HEATER_0_PIN > -1  \r
      WRITE(HEATER_0_PIN,LOW);\r
    #endif\r
   #endif\r
   #if TEMP_1_PIN > -1\r
-  target_raw[1]=0;\r
+  Heater::setCelsius(TEMPSENSOR_BED,0);\r
   #if HEATER_1_PIN > -1 \r
     WRITE(HEATER_1_PIN,LOW);\r
   #endif\r
   #endif\r
   #if TEMP_2_PIN > -1\r
-  target_raw[2]=0;\r
+  Heater::setCelsius(TEMPSENSOR_AUX,0);\r
   #if HEATER_2_PIN > -1  \r
     WRITE(HEATER_2_PIN,LOW);\r
   #endif\r
index 14f0bb1043fc533fa6d72c8e1c47571364c41f32..cfeb6b2234fa96d1fd147f6ba888437afe3e7b0d 100644 (file)
@@ -388,7 +388,7 @@ void plan_buffer_line(float x, float y, float z, float e, float feed_rate) {
   // If the buffer is full: good! That means we are well ahead of the robot. \r
   // Rest here until there is room in the buffer.\r
   while(block_buffer_tail == next_buffer_head) { \r
-    manage_heater(); \r
+    htr.manage_heater(); \r
     manage_inactivity(1); \r
     LCD_STATUS;\r
   }\r
index f4bd1289e001e51946ae5499c6e8188d0b62d559..01ff430f35506e7a2c15ec1988a47b0a9f4233f6 100644 (file)
@@ -585,7 +585,7 @@ void st_init()
 void st_synchronize()\r
 {\r
   while(plan_get_current_block()) {\r
-    manage_heater();\r
+    htr.manage_heater();\r
     manage_inactivity(1);\r
     LCD_STATUS;\r
   }   \r
index ebc772e1d883f211f0fcbd6066057029471ae24f..9be4bdc2f783540f0a147acd786d9fab7d2ebb7b 100644 (file)
 #include "streaming.h"\r
 #include "temperature.h"\r
 \r
-int target_bed_raw = 0;\r
-int current_bed_raw = 0;\r
 \r
-int target_raw[3] = {0, 0, 0};\r
-int current_raw[3] = {0, 0, 0};\r
-unsigned char temp_meas_ready = false;\r
 \r
-unsigned long previous_millis_heater, previous_millis_bed_heater;\r
 \r
-#ifdef PIDTEMP\r
-  double temp_iState = 0;\r
-  double temp_dState = 0;\r
-  double pTerm;\r
-  double iTerm;\r
-  double dTerm;\r
-      //int output;\r
-  double pid_error;\r
-  double temp_iState_min;\r
-  double temp_iState_max;\r
-  double pid_setpoint = 0.0;\r
-  double pid_input;\r
-  double pid_output;\r
-  bool pid_reset;\r
-  float HeaterPower;\r
-  \r
-  float Kp=DEFAULT_Kp;\r
-  float Ki=DEFAULT_Ki;\r
-  float Kd=DEFAULT_Kd;\r
-  float Kc=DEFAULT_Kc;\r
-#endif //PIDTEMP\r
-\r
-#ifdef MINTEMP\r
-int minttemp = temp2analog(MINTEMP);\r
-#endif //MINTEMP\r
-#ifdef MAXTEMP\r
-int maxttemp = temp2analog(MAXTEMP);\r
-#endif //MAXTEMP\r
-\r
-#ifdef BED_MINTEMP\r
-int bed_minttemp = temp2analog(BED_MINTEMP);\r
-#endif //BED_MINTEMP\r
-#ifdef BED_MAXTEMP\r
-int bed_maxttemp = temp2analog(BED_MAXTEMP);\r
-#endif //BED_MAXTEMP\r
 \r
-void manage_heater()\r
+void static Heater::manage_heater()\r
 {\r
 #ifdef USE_WATCHDOG\r
   wd_reset();\r
@@ -90,11 +49,11 @@ void manage_heater()
   \r
   float pid_input;\r
   float pid_output;\r
-  if(temp_meas_ready != true)   //better readability\r
+  if(htr.temp_meas_ready != true)   //better readability\r
     return; \r
 \r
 CRITICAL_SECTION_START;\r
-    temp_meas_ready = false;\r
+    htr.temp_meas_ready = false;\r
 CRITICAL_SECTION_END;\r
 \r
 #ifdef PIDTEMP\r
@@ -176,7 +135,8 @@ CRITICAL_SECTION_END;
 // For a thermistor, it uses the RepRap thermistor temp table.\r
 // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.\r
 // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.\r
-float temp2analog(int celsius) {\r
+float const static temp2analog(const int celsius)\r
+{\r
   #ifdef HEATER_USES_THERMISTOR_1\r
     int raw = 0;\r
     byte i;\r
@@ -207,7 +167,8 @@ float temp2analog(int celsius) {
 // For a thermistor, it uses the RepRap thermistor temp table.\r
 // This is needed because PID in hydra firmware hovers around a given analog value, not a temp value.\r
 // This function is derived from inversing the logic from a portion of getTemperature() in FiveD RepRap firmware.\r
-float temp2analogBed(int celsius) {\r
+float const static temp2analogBed(const int celsius)\r
+{\r
   #ifdef BED_USES_THERMISTOR\r
 \r
     int raw = 0;\r
@@ -237,7 +198,7 @@ float temp2analogBed(int celsius) {
 \r
 // Derived from RepRap FiveD extruder::getTemperature()\r
 // For hot end temperature measurement.\r
-float analog2temp(int raw) {\r
+float const static Heater::analog2temp(const int raw) {\r
   #ifdef HEATER_1_USES_THERMISTOR\r
     int celsius = 0;\r
     byte i;  \r
@@ -266,7 +227,7 @@ float analog2temp(int raw) {
 \r
 // Derived from RepRap FiveD extruder::getTemperature()\r
 // For bed temperature measurement.\r
-float analog2tempBed(int raw) {\r
+float const static Heater::analog2tempBed(const int raw) {\r
   #ifdef BED_USES_THERMISTOR\r
     int celsius = 0;\r
     byte i;\r
@@ -296,8 +257,28 @@ float analog2tempBed(int raw) {
   #endif\r
 }\r
 \r
-void tp_init()\r
+Heater::Heater()\r
 {\r
+  for(short i=0;i<3;i++)\r
+  {\r
+    target_raw[i]=0;\r
+    current_raw[i] =0;\r
+  }\r
+  htr.temp_meas_ready = false;\r
+  #ifdef MINTEMP\r
+  minttemp = temp2analog(MINTEMP);\r
+  #endif //MINTEMP\r
+  #ifdef MAXTEMP\r
+  maxttemp = temp2analog(MAXTEMP);\r
+  #endif //MAXTEMP\r
+\r
+  #ifdef BED_MINTEMP\r
+  bed_minttemp = temp2analog(BED_MINTEMP);\r
+  #endif //BED_MINTEMP\r
+  #ifdef BED_MAXTEMP\r
+  bed_maxttemp = temp2analog(BED_MAXTEMP);\r
+  #endif //BED_MAXTEMP\r
+  \r
 #if (HEATER_0_PIN > -1) \r
   SET_OUTPUT(HEATER_0_PIN);\r
 #endif  \r
@@ -311,6 +292,14 @@ void tp_init()
 #ifdef PIDTEMP\r
   temp_iState_min = 0.0;\r
   temp_iState_max = PID_INTEGRAL_DRIVE_MAX / Ki;\r
+  temp_iState = 0;\r
+  temp_dState = 0;\r
+  Kp=DEFAULT_Kp;\r
+  Ki=DEFAULT_Ki;\r
+  Kd=DEFAULT_Kd;\r
+  Kc=DEFAULT_Kc;\r
+  pid_setpoint = 0.0;\r
+\r
 #endif //PIDTEMP\r
 \r
 // Set analog inputs\r
@@ -407,39 +396,39 @@ ISR(TIMER0_COMPB_vect)
   if(temp_count >= 16) // 6 ms * 16 = 96ms.\r
   {\r
     #ifdef HEATER_1_USES_AD595\r
-      current_raw[0] = raw_temp_0_value;\r
+      htr.current_raw[0] = raw_temp_0_value;\r
     #else\r
-      current_raw[0] = 16383 - raw_temp_0_value;\r
+      htr.current_raw[0] = 16383 - raw_temp_0_value;\r
     #endif\r
     \r
     #ifdef HEATER_2_USES_AD595\r
-      current_raw[2] = raw_temp_2_value;\r
+      htr.current_raw[2] = raw_temp_2_value;\r
     #else\r
-      current_raw[2] = 16383 - raw_temp_2_value;\r
+      htr.current_raw[2] = 16383 - raw_temp_2_value;\r
     #endif\r
     \r
     #ifdef BED_USES_AD595\r
-      current_raw[1] = raw_temp_1_value;\r
+      htr.current_raw[1] = raw_temp_1_value;\r
     #else\r
-      current_raw[1] = 16383 - raw_temp_1_value;\r
+      htr.current_raw[1] = 16383 - raw_temp_1_value;\r
     #endif\r
     \r
-    temp_meas_ready = true;\r
+   htr.temp_meas_ready = true;\r
     temp_count = 0;\r
     raw_temp_0_value = 0;\r
     raw_temp_1_value = 0;\r
     raw_temp_2_value = 0;\r
 #ifdef MAXTEMP\r
   #if (HEATER_0_PIN > -1)\r
-    if(current_raw[TEMPSENSOR_HOTEND] >= maxttemp) {\r
-      target_raw[TEMPSENSOR_HOTEND] = 0;\r
+    if(htr.current_raw[TEMPSENSOR_HOTEND] >= htr.maxttemp) {\r
+      htr.target_raw[TEMPSENSOR_HOTEND] = 0;\r
       analogWrite(HEATER_0_PIN, 0);\r
       Serial.println("!! Temperature extruder 0 switched off. MAXTEMP triggered !!");\r
     }\r
   #endif\r
   #if (HEATER_2_PIN > -1)\r
-    if(current_raw[TEMPSENSOR_AUX] >= maxttemp) {\r
-      target_raw[TEMPSENSOR_AUX] = 0;\r
+    if(htr.current_raw[TEMPSENSOR_AUX] >= htr.maxttemp) {\r
+      htr.target_raw[TEMPSENSOR_AUX] = 0;\r
       analogWrite(HEATER_2_PIN, 0);\r
       Serial.println("!! Temperature extruder 1 switched off. MAXTEMP triggered !!");\r
     }\r
@@ -447,15 +436,15 @@ ISR(TIMER0_COMPB_vect)
 #endif //MAXTEMP\r
 #ifdef MINTEMP\r
   #if (HEATER_0_PIN > -1)\r
-    if(current_raw[TEMPSENSOR_HOTEND] <= minttemp) {\r
-      target_raw[TEMPSENSOR_HOTEND] = 0;\r
+    if(htr.current_raw[TEMPSENSOR_HOTEND] <= htr.minttemp) {\r
+      htr.target_raw[TEMPSENSOR_HOTEND] = 0;\r
       analogWrite(HEATER_0_PIN, 0);\r
       Serial.println("!! Temperature extruder 0 switched off. MINTEMP triggered !!");\r
     }\r
   #endif\r
   #if (HEATER_2_PIN > -1)\r
-    if(current_raw[TEMPSENSOR_AUX] <= minttemp) {\r
-      target_raw[TEMPSENSOR_AUX] = 0;\r
+    if(htr.current_raw[TEMPSENSOR_AUX] <= htr.minttemp) {\r
+      htr.target_raw[TEMPSENSOR_AUX] = 0;\r
       analogWrite(HEATER_2_PIN, 0);\r
       Serial.println("!! Temperature extruder 1 switched off. MINTEMP triggered !!");\r
     }\r
@@ -463,8 +452,8 @@ ISR(TIMER0_COMPB_vect)
 #endif //MAXTEMP\r
 #ifdef BED_MINTEMP\r
   #if (HEATER_1_PIN > -1)\r
-    if(current_raw[1] <= bed_minttemp) {\r
-      target_raw[1] = 0;\r
+    if(htr.current_raw[1] <= htr.bed_minttemp) {\r
+      htr.target_raw[1] = 0;\r
       WRITE(HEATER_1_PIN, 0);\r
       Serial.println("!! Temperatur heated bed switched off. MINTEMP triggered !!");\r
     }\r
@@ -472,8 +461,8 @@ ISR(TIMER0_COMPB_vect)
 #endif\r
 #ifdef BED_MAXTEMP\r
   #if (HEATER_1_PIN > -1)\r
-    if(current_raw[1] >= bed_maxttemp) {\r
-      target_raw[1] = 0;\r
+    if(htr.current_raw[1] >= htr.bed_maxttemp) {\r
+      htr.target_raw[1] = 0;\r
       WRITE(HEATER_1_PIN, 0);\r
       Serial.println("!! Temperature heated bed switched off. MAXTEMP triggered !!");\r
     }\r
@@ -481,3 +470,6 @@ ISR(TIMER0_COMPB_vect)
 #endif\r
   }\r
 }\r
+\r
+//Heater htr;\r
+\r
index e2e6c459c606b3df0b119ac309b85b3172524460..a93d46ba4bb9c84d8726e7e17f72e58a9ae3c254 100644 (file)
 #ifdef PID_ADD_EXTRUSION_RATE\r
   #include "stepper.h"\r
 #endif\r
-void tp_init();\r
-void manage_heater();\r
-//int temp2analogu(int celsius, const short table[][2], int numtemps);\r
-//float analog2tempu(int raw, const short table[][2], int numtemps);\r
-float temp2analog(int celsius);\r
-float temp2analogBed(int celsius);\r
-float analog2temp(int raw);\r
-float analog2tempBed(int raw);\r
+\r
+enum TempSensor {TEMPSENSOR_HOTEND=0,TEMPSENSOR_BED=1, TEMPSENSOR_AUX=2};\r
+\r
+// ther must be only one instance of this class, and it is created in temperature.cpp by itself and is called "htr".\r
+// all the variables are static, so that of the compiler optimization is more easy.\r
+// I honestly hope that this increases readability and structure.\r
+// none of the variables or routines should be called from an secondary process/interrupt with the exceptino of current_raw[].\r
+\r
+class Heater\r
+{\r
+public:\r
+  Heater(); //treplaces tp_init();\r
+  ~Heater(); \r
+  \r
+  void static manage_heater(); /// it is critical that this is called continously. \r
+  \r
+  // conversion routines, const since they don't change any class variables.\r
+  float const static temp2analog(const int celsius);\r
+  float const static temp2analogBed(const int celsius);\r
+  float const static analog2temp(const int raw);\r
+  float const static analog2tempBed(const int raw);\r
+  \r
+  inline float const static celsius(const TempSensor s) \r
+    {\r
+      if(s==TEMPSENSOR_BED) \r
+       return analog2tempBed(Heater::current_raw[s]); \r
+      else \r
+       return analog2temp(Heater::current_raw[s]);\r
+    };\r
+  inline float const static celsiusTarget(const TempSensor s) \r
+    {\r
+      if(s==TEMPSENSOR_BED) \r
+       return analog2tempBed(Heater::target_raw[s]); \r
+      else \r
+       return analog2temp(Heater::target_raw[s]);\r
+    };\r
+  inline float static setCelsius(const TempSensor s, const int celsius) \r
+    {\r
+      #ifdef PIDTEMP\r
+       if(s==TEMPSENSOR_HOTEND)\r
+            Heater::pid_setpoint = celsius;\r
+      #endif //PIDTEM\r
+      if(s==TEMPSENSOR_BED) \r
+       Heater::target_raw[s] = temp2analog(celsius); \r
+      else \r
+       Heater::target_raw[s] = temp2analogBed(celsius); \r
+    };\r
+\r
+   inline bool const static isHeating(TempSensor s)\r
+   { return (Heater::target_raw[s]>Heater::current_raw[s]);};\r
+   inline bool const static isCooling(TempSensor s)\r
+   { return (Heater::target_raw[s]<Heater::current_raw[s]);};\r
+\r
+public:\r
+  #ifdef PIDTEMP\r
+    static float Kp;\r
+    static float Ki;\r
+    static float Kd;\r
+    static float Kc;\r
+  #endif\r
+  \r
+  static int target_raw[3];\r
+  static float pid_setpoint;\r
+  \r
+  volatile static int current_raw[3]; //this are written by an ISR, so volatile.\r
+  volatile static bool temp_meas_ready ; //also this is set by the ISR\r
+  \r
+  \r
+private:\r
\r
+  \r
+\r
+  static unsigned long previous_millis_heater, previous_millis_bed_heater;\r
+\r
+  #ifdef PIDTEMP\r
+    static float temp_iState;\r
+    static float temp_dState;\r
+    static float pTerm;\r
+    static float iTerm;\r
+    static float dTerm;\r
+       //int output;\r
+    static float pid_error;\r
+    static float temp_iState_min;\r
+    static float temp_iState_max;\r
+    static float pid_input;\r
+    static float pid_output;\r
+    \r
+    static bool pid_reset;\r
+    static float HeaterPower;\r
+    \r
+  #endif //PIDTEMP\r
+\r
+public: //but only accesed from the ISR hence not volatile\r
+   #ifdef MINTEMP\r
+  static int minttemp;\r
+  #endif //MINTEMP\r
+  #ifdef MAXTEMP\r
+  static int maxttemp;\r
+  #endif //MAXTEMP\r
+\r
+  #ifdef BED_MINTEMP\r
+  static int bed_minttemp ;\r
+  #endif //BED_MINTEMP\r
+  #ifdef BED_MAXTEMP\r
+  static int bed_maxttemp;\r
+  #endif //BED_MAXTEMP\r
+  \r
+};\r
+\r
+extern Heater htr; //this creates the single, global instance \r
 \r
 #ifdef HEATER_USES_THERMISTOR\r
     #define HEATERSOURCE 1\r
@@ -41,18 +143,5 @@ float analog2tempBed(int raw);
     #define BEDSOURCE 1\r
 #endif\r
 \r
-//#define temp2analogh( c ) temp2analogu((c),temptable,NUMTEMPS)\r
-//#define analog2temp( c ) analog2tempu((c),temptable,NUMTEMPS\r
-\r
-\r
-extern float Kp;\r
-extern float Ki;\r
-extern float Kd;\r
-extern float Kc;\r
-\r
-enum {TEMPSENSOR_HOTEND=0,TEMPSENSOR_BED=1, TEMPSENSOR_AUX=2};\r
-extern int target_raw[3];\r
-extern int current_raw[3];\r
-extern double pid_setpoint;\r
 \r
 #endif\r
index a6c75e0fc281903963cf2d9607bd4d6793e4a9a8..57c558236709f57a883a45b230c7e77a08d7959d 100644 (file)
@@ -1,5 +1,5 @@
 #include "ultralcd.h"\r
-\r
+#include "temperature.h"\r
 \r
 #ifdef ULTRA_LCD\r
 extern volatile int feedmultiply;\r
@@ -238,8 +238,8 @@ extern volatile bool feedmultiplychanged;
 void MainMenu::showStatus()\r
 { \r
 #if LCD_HEIGHT==4\r
-  static int oldcurrentraw=-1;\r
-  static int oldtargetraw=-1;\r
+  static int oldcurrent=-1;\r
+  static int oldtarget=-1;\r
   //force_lcd_update=true;\r
   if(force_lcd_update||feedmultiplychanged)  //initial display of content\r
   {\r
@@ -252,33 +252,36 @@ void MainMenu::showStatus()
 #endif\r
   }\r
     \r
-\r
-  if((abs(current_raw[TEMPSENSOR_HOTEND]-oldcurrentraw)>3)||force_lcd_update)\r
+  int tt=Heater::celsius(TEMPSENSOR_HOTEND);\r
+  if((abs(tt-oldcurrent)>1)||force_lcd_update)\r
   {\r
     lcd.setCursor(1,0);\r
-    lcd.print(ftostr3(analog2temp(current_raw[TEMPSENSOR_HOTEND])));\r
-    oldcurrentraw=current_raw[TEMPSENSOR_HOTEND];\r
+    lcd.print(ftostr3(tt));\r
+    oldcurrent=tt;\r
   }\r
-  if((target_raw[TEMPSENSOR_HOTEND]!=oldtargetraw)||force_lcd_update)\r
+  int ttg=Heater::celsiusTarget(TEMPSENSOR_HOTEND);\r
+  if((ttg!=oldtarget)||force_lcd_update)\r
   {\r
     lcd.setCursor(5,0);\r
-    lcd.print(ftostr3(analog2temp(target_raw[TEMPSENSOR_HOTEND])));\r
-    oldtargetraw=target_raw[TEMPSENSOR_HOTEND];\r
+    lcd.print(ftostr3(ttg));\r
+    oldtarget=ttg;\r
   }\r
   #if defined BED_USES_THERMISTOR || defined BED_USES_AD595 \r
-  static int oldcurrentbedraw=-1;\r
-  static int oldtargetbedraw=-1; \r
-  if((current_bed_raw!=oldcurrentbedraw)||force_lcd_update)\r
+  static int oldcurrentbed=-1;\r
+  static int oldtargetbed=-1;\r
+  int tb=Heater::celsius(TEMPSENSOR_BED);\r
+  if((tb!=oldcurrentbed)||force_lcd_update)\r
   {\r
     lcd.setCursor(1,0);\r
-    lcd.print(ftostr3(analog2temp(current_bed_raw)));\r
-    oldcurrentraw=current_raw[TEMPSENSOR_BED];\r
+    lcd.print(ftostr3(tb));\r
+    oldcurrentbed=tb;\r
   }\r
-  if((target_bed_raw!=oldtargebedtraw)||force_lcd_update)\r
+  int tg=Heater::celsiusTarget(TEMPSENSOR_BED);\r
+  if((tg!=oldtargebed)||force_lcd_update)\r
   {\r
     lcd.setCursor(5,0);\r
-    lcd.print(ftostr3(analog2temp(target_bed_raw)));\r
-    oldtargetraw=target_bed_raw;\r
+    lcd.print(Heater::celsiusTarget(TEMPSENSOR_BED));\r
+    oldtargebed=tg;\r
   }\r
   #endif\r
   //starttime=2;\r
@@ -327,8 +330,8 @@ void MainMenu::showStatus()
     messagetext[0]='\0';\r
   }\r
 #else //smaller LCDS----------------------------------\r
-  static int oldcurrentraw=-1;\r
-  static int oldtargetraw=-1;\r
+  static int oldcurrent=-1;\r
+  static int oldtarget=-1;\r
   if(force_lcd_update)  //initial display of content\r
   {\r
     encoderpos=feedmultiply;\r
@@ -338,18 +341,19 @@ void MainMenu::showStatus()
     #endif\r
   }\r
     \r
-\r
-  if((abs(current_raw[TEMPSENSOR_HOTEND]-oldcurrentraw)>3)||force_lcd_update)\r
+  int tt=Heater::celsius(TEMPSENSOR_HOTEND);\r
+  if((abs(tt-oldcurrent)>1)||force_lcd_update)\r
   {\r
     lcd.setCursor(1,0);\r
-    lcd.print(ftostr3(analog2temp(current_raw[TEMPSENSOR_HOTEND])));\r
-    oldcurrentraw=current_raw[TEMPSENSOR_HOTEND];\r
+    lcd.print(ftostr3(tt));\r
+    oldcurrent=tt;\r
   }\r
-  if((target_raw[TEMPSENSOR_HOTEND]!=oldtargetraw)||force_lcd_update)\r
+  int ttg=Heater::celsiusTarget(TEMPSENSOR_HOTEND);\r
+  if((ttg!=oldtarget)||force_lcd_update)\r
   {\r
     lcd.setCursor(5,0);\r
-    lcd.print(ftostr3(analog2temp(target_raw[TEMPSENSOR_HOTEND])));\r
-    oldtargetraw=target_raw[TEMPSENSOR_HOTEND];\r
+    lcd.print(ftostr3(ttg));\r
+    oldtarge=ttg;\r
   }\r
 \r
   if(messagetext[0]!='\0')\r
@@ -426,7 +430,7 @@ void MainMenu::showPrepare()
         if((activeline==line) && CLICKED)\r
         {\r
           BLOCK\r
-          target_raw[TEMPSENSOR_HOTEND] = temp2analog(170);\r
+          Heater::setCelsius(TEMPSENSOR_HOTEND, 170);\r
           beepshort();\r
         }\r
       }break;\r
@@ -531,7 +535,7 @@ void MainMenu::showControl()
         if(force_lcd_update)\r
         {\r
           lcd.setCursor(0,line);lcd.print(" \002Nozzle:");\r
-          lcd.setCursor(13,line);lcd.print(ftostr3(analog2temp(target_raw[TEMPSENSOR_HOTEND])));\r
+          lcd.setCursor(13,line);lcd.print(ftostr3(Heater::celsiusTarget(TEMPSENSOR_HOTEND)));\r
         }\r
         \r
         if((activeline==line) )\r
@@ -541,11 +545,11 @@ void MainMenu::showControl()
             linechanging=!linechanging;\r
             if(linechanging)\r
             {\r
-               encoderpos=(int)analog2temp(target_raw[TEMPSENSOR_HOTEND]);\r
+               encoderpos=(int)Heater::celsiusTarget(TEMPSENSOR_HOTEND);\r
             }\r
             else\r
             {\r
-              target_raw[TEMPSENSOR_HOTEND] = temp2analog(encoderpos);\r
+              Heater::setCelsius(TEMPSENSOR_HOTEND,encoderpos);\r
               encoderpos=activeline*lcdslow;\r
               beepshort();\r
             }\r
@@ -669,7 +673,7 @@ void MainMenu::showControl()
       if(force_lcd_update)\r
         {\r
           lcd.setCursor(0,line);lcd.print(" PID-P: ");\r
-          lcd.setCursor(13,line);lcd.print(itostr4(Kp));\r
+          lcd.setCursor(13,line);lcd.print(itostr4(Heater::Kp));\r
         }\r
         \r
         if((activeline==line) )\r
@@ -679,11 +683,11 @@ void MainMenu::showControl()
             linechanging=!linechanging;\r
             if(linechanging)\r
             {\r
-               encoderpos=(int)Kp/5;\r
+               encoderpos=(int)Heater::Kp/5;\r
             }\r
             else\r
             {\r
-              Kp= encoderpos*5;\r
+              Heater::Kp= encoderpos*5;\r
               encoderpos=activeline*lcdslow;\r
                 \r
             }\r
@@ -703,7 +707,7 @@ void MainMenu::showControl()
       if(force_lcd_update)\r
         {\r
           lcd.setCursor(0,line);lcd.print(" PID-I: ");\r
-          lcd.setCursor(13,line);lcd.print(ftostr51(Ki));\r
+          lcd.setCursor(13,line);lcd.print(ftostr51(Heater::Ki));\r
         }\r
         \r
         if((activeline==line) )\r
@@ -713,11 +717,11 @@ void MainMenu::showControl()
             linechanging=!linechanging;\r
             if(linechanging)\r
             {\r
-               encoderpos=(int)(Ki*10);\r
+               encoderpos=(int)(Heater::Ki*10);\r
             }\r
             else\r
             {\r
-              Ki= encoderpos/10.;\r
+              Heater::Ki= encoderpos/10.;\r
               encoderpos=activeline*lcdslow;\r
                 \r
             }\r
@@ -737,7 +741,7 @@ void MainMenu::showControl()
       if(force_lcd_update)\r
         {\r
           lcd.setCursor(0,line);lcd.print(" PID-D: ");\r
-          lcd.setCursor(13,line);lcd.print(itostr4(Kd));\r
+          lcd.setCursor(13,line);lcd.print(itostr4(Heater::Kd));\r
         }\r
         \r
         if((activeline==line) )\r
@@ -747,11 +751,11 @@ void MainMenu::showControl()
             linechanging=!linechanging;\r
             if(linechanging)\r
             {\r
-               encoderpos=(int)Kd/5;\r
+               encoderpos=(int)(Heater::Kd/5.);\r
             }\r
             else\r
             {\r
-              Kd= encoderpos*5;\r
+              Heater::Kd= encoderpos*5;\r
               encoderpos=activeline*lcdslow;\r
                 \r
             }\r
@@ -774,7 +778,7 @@ void MainMenu::showControl()
       if(force_lcd_update)\r
         {\r
           lcd.setCursor(0,line);lcd.print(" PID-C: ");\r
-          lcd.setCursor(13,line);lcd.print(itostr3(Kc));\r
+          lcd.setCursor(13,line);lcd.print(itostr3(Heater::Kc));\r
         }\r
         \r
         if((activeline==line) )\r
@@ -784,11 +788,11 @@ void MainMenu::showControl()
             linechanging=!linechanging;\r
             if(linechanging)\r
             {\r
-               encoderpos=(int)Kc;\r
+               encoderpos=(int)Heater::Kc;\r
             }\r
             else\r
             {\r
-              Kc= encoderpos;\r
+              Heater::Kc= encoderpos;\r
               encoderpos=activeline*lcdslow;\r
                 \r
             }\r