Browse Source

Clean up set_heaters_for_bltouch

Scott Lahteine 8 years ago
parent
commit
1a111180de
1 changed files with 40 additions and 38 deletions
  1. 40
    38
      Marlin/Marlin_main.cpp

+ 40
- 38
Marlin/Marlin_main.cpp View File

2060
   #endif
2060
   #endif
2061
 
2061
 
2062
   #if ENABLED(BLTOUCH)
2062
   #if ENABLED(BLTOUCH)
2063
+
2063
     void bltouch_command(int angle) {
2064
     void bltouch_command(int angle) {
2064
       servo[Z_ENDSTOP_SERVO_NR].move(angle);  // Give the BL-Touch the command and wait
2065
       servo[Z_ENDSTOP_SERVO_NR].move(angle);  // Give the BL-Touch the command and wait
2065
       safe_delay(BLTOUCH_DELAY);
2066
       safe_delay(BLTOUCH_DELAY);
2066
     }
2067
     }
2067
 
2068
 
2068
-    //
2069
-    // The BL-Touch probes have a HAL effect sensor.  The high currents switching
2070
-    // on and off cause big magnetic fields that can affect the repeatability of the
2071
-    // sensor.  So, for BL-Touch probes, we turn off the heaters during the actual probe.
2072
-    // And then we quickly turn them back on after we have sampled the point
2073
-    //
2074
-  #if ENABLED(BLTOUCH_HEATERS_OFF)
2075
-    void turn_heaters_on_or_off_for_bltouch(const bool deploy) {
2076
-      static int8_t bltouch_recursion_cnt=0;
2077
-      static millis_t last_emi_protection=0;
2078
-      static float temps_at_entry[HOTENDS];
2079
-      #if HAS_TEMP_BED
2080
-        static float bed_temp_at_entry;
2081
-      #endif
2082
-
2083
-      if (deploy && bltouch_recursion_cnt>0)         // if already in the correct state, we don't need to do anything
2084
-        return;                                      // with the heaters.
2085
-      if (!deploy && bltouch_recursion_cnt<1)        // if already in the correct state, we don't need to do anything
2086
-        return;                                      // with the heaters.
2069
+    /**
2070
+     * BLTouch probes have a Hall effect sensor. The high currents switching
2071
+     * on and off cause a magnetic field that can affect the repeatability of the
2072
+     * sensor. So for BLTouch probes, heaters are turned off during the probe,
2073
+     * then quickly turned back on after the point is sampled.
2074
+     */
2075
+    #if ENABLED(BLTOUCH_HEATERS_OFF)
2087
 
2076
 
2088
-      if (deploy) {
2089
-        bltouch_recursion_cnt++;
2090
-        last_emi_protection = millis();
2091
-        HOTEND_LOOP() temps_at_entry[e] = thermalManager.degTargetHotend(e);        // save the current target temperatures
2092
-        HOTEND_LOOP() thermalManager.setTargetHotend(0, e);                         // so we know what to restore them to.
2077
+      bool set_heaters_for_bltouch(const bool deploy) {
2078
+        static bool heaters_were_disabled = false;
2079
+        static millis_t next_emi_protection;
2080
+        static float temps_at_entry[HOTENDS];
2093
 
2081
 
2094
         #if HAS_TEMP_BED
2082
         #if HAS_TEMP_BED
2095
-          bed_temp_at_entry = thermalManager.degTargetBed();
2096
-          thermalManager.setTargetBed(0.0);
2083
+          static float bed_temp_at_entry;
2097
         #endif
2084
         #endif
2098
-      }
2099
-      else {
2100
-        bltouch_recursion_cnt--;                                                    // the heaters are only turned back on
2101
-	if (bltouch_recursion_cnt==0 && ((last_emi_protection+20000L)>millis())) {  // if everything is perfect.  It is expected
2102
-          HOTEND_LOOP() thermalManager.setTargetHotend(temps_at_entry[e], e);       // that the bltouch_recursion_cnt is zero and
2103
-          #if HAS_TEMP_BED                                                          // that the heaters were shut off less than
2104
-            thermalManager.setTargetBed(bed_temp_at_entry);                         // 20 seconds ago
2085
+
2086
+        // If called out of order or far apart something is seriously wrong
2087
+        if (deploy == heaters_were_disabled
2088
+            || (next_emi_protection && ELAPSED(millis(), next_emi_protection)))
2089
+          kill(PSTR(MSG_KILLED));
2090
+
2091
+        if (deploy) {
2092
+          next_emi_protection = millis() + 20 * 1000UL;
2093
+          HOTEND_LOOP() {
2094
+            temps_at_entry[e] = thermalManager.degTargetHotend(e);
2095
+            thermalManager.setTargetHotend(0, e);
2096
+          }
2097
+          #if HAS_TEMP_BED
2098
+            bed_temp_at_entry = thermalManager.degTargetBed();
2099
+            thermalManager.setTargetBed(0);
2100
+          #endif
2101
+        }
2102
+        else {
2103
+          HOTEND_LOOP() thermalManager.setTargetHotend(temps_at_entry[e], e);
2104
+          #if HAS_TEMP_BED
2105
+            thermalManager.setTargetBed(bed_temp_at_entry);
2105
           #endif
2106
           #endif
2106
         }
2107
         }
2107
       }
2108
       }
2108
-    }
2109
-    #endif
2109
+
2110
+    #endif // BLTOUCH_HEATERS_OFF
2110
 
2111
 
2111
     void set_bltouch_deployed(const bool deploy) {
2112
     void set_bltouch_deployed(const bool deploy) {
2112
       #if ENABLED(BLTOUCH_HEATERS_OFF)
2113
       #if ENABLED(BLTOUCH_HEATERS_OFF)
2113
-        turn_heaters_on_or_off_for_bltouch(deploy);
2114
+        set_heaters_for_bltouch(deploy);
2114
       #endif
2115
       #endif
2115
       if (deploy && TEST_BLTOUCH()) {      // If BL-Touch says it's triggered
2116
       if (deploy && TEST_BLTOUCH()) {      // If BL-Touch says it's triggered
2116
         bltouch_command(BLTOUCH_RESET);    //  try to reset it.
2117
         bltouch_command(BLTOUCH_RESET);    //  try to reset it.
2134
         }
2135
         }
2135
       #endif
2136
       #endif
2136
     }
2137
     }
2137
-  #endif
2138
+
2139
+  #endif // BLTOUCH
2138
 
2140
 
2139
   // returns false for ok and true for failure
2141
   // returns false for ok and true for failure
2140
   bool set_probe_deployed(bool deploy) {
2142
   bool set_probe_deployed(bool deploy) {
2147
     #endif
2149
     #endif
2148
 
2150
 
2149
     #if ENABLED(BLTOUCH) && ENABLED(BLTOUCH_HEATERS_OFF)
2151
     #if ENABLED(BLTOUCH) && ENABLED(BLTOUCH_HEATERS_OFF)
2150
-      turn_heaters_on_or_off_for_bltouch(deploy);
2152
+      set_heaters_for_bltouch(deploy);
2151
     #endif
2153
     #endif
2152
 
2154
 
2153
     if (endstops.z_probe_enabled == deploy) return false;
2155
     if (endstops.z_probe_enabled == deploy) return false;

Loading…
Cancel
Save