My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fancheck.cpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * fancheck.cpp - fan tachometer check
  24. */
  25. #include "../inc/MarlinConfig.h"
  26. #if HAS_FANCHECK
  27. #include "fancheck.h"
  28. #include "../module/temperature.h"
  29. #if HAS_AUTO_FAN && EXTRUDER_AUTO_FAN_SPEED != 255 && DISABLED(FOURWIRES_FANS)
  30. bool FanCheck::measuring = false;
  31. #endif
  32. Flags<TACHO_COUNT> FanCheck::tacho_state;
  33. uint16_t FanCheck::edge_counter[TACHO_COUNT];
  34. uint8_t FanCheck::rps[TACHO_COUNT];
  35. FanCheck::TachoError FanCheck::error = FanCheck::TachoError::NONE;
  36. bool FanCheck::enabled;
  37. void FanCheck::init() {
  38. #define _TACHINIT(N) TERN(E##N##_FAN_TACHO_PULLUP, SET_INPUT_PULLUP, TERN(E##N##_FAN_TACHO_PULLDOWN, SET_INPUT_PULLDOWN, SET_INPUT))(E##N##_FAN_TACHO_PIN)
  39. #if HAS_E0_FAN_TACHO
  40. _TACHINIT(0);
  41. #endif
  42. #if HAS_E1_FAN_TACHO
  43. _TACHINIT(1);
  44. #endif
  45. #if HAS_E2_FAN_TACHO
  46. _TACHINIT(2);
  47. #endif
  48. #if HAS_E3_FAN_TACHO
  49. _TACHINIT(3);
  50. #endif
  51. #if HAS_E4_FAN_TACHO
  52. _TACHINIT(4);
  53. #endif
  54. #if HAS_E5_FAN_TACHO
  55. _TACHINIT(5);
  56. #endif
  57. #if HAS_E6_FAN_TACHO
  58. _TACHINIT(6);
  59. #endif
  60. #if HAS_E7_FAN_TACHO
  61. _TACHINIT(7);
  62. #endif
  63. }
  64. void FanCheck::update_tachometers() {
  65. bool status;
  66. #define _TACHO_CASE(N) case N: status = READ(E##N##_FAN_TACHO_PIN); break;
  67. LOOP_L_N(f, TACHO_COUNT) {
  68. switch (f) {
  69. #if HAS_E0_FAN_TACHO
  70. _TACHO_CASE(0)
  71. #endif
  72. #if HAS_E1_FAN_TACHO
  73. _TACHO_CASE(1)
  74. #endif
  75. #if HAS_E2_FAN_TACHO
  76. _TACHO_CASE(2)
  77. #endif
  78. #if HAS_E3_FAN_TACHO
  79. _TACHO_CASE(3)
  80. #endif
  81. #if HAS_E4_FAN_TACHO
  82. _TACHO_CASE(4)
  83. #endif
  84. #if HAS_E5_FAN_TACHO
  85. _TACHO_CASE(5)
  86. #endif
  87. #if HAS_E6_FAN_TACHO
  88. _TACHO_CASE(6)
  89. #endif
  90. #if HAS_E7_FAN_TACHO
  91. _TACHO_CASE(7)
  92. #endif
  93. default: continue;
  94. }
  95. if (status != tacho_state[f]) {
  96. if (measuring) ++edge_counter[f];
  97. tacho_state.set(f, status);
  98. }
  99. }
  100. }
  101. void FanCheck::compute_speed(uint16_t elapsedTime) {
  102. static uint8_t errors_count[TACHO_COUNT];
  103. static uint8_t fan_reported_errors_msk = 0;
  104. uint8_t fan_error_msk = 0;
  105. LOOP_L_N(f, TACHO_COUNT) {
  106. switch (f) {
  107. TERN_(HAS_E0_FAN_TACHO, case 0:)
  108. TERN_(HAS_E1_FAN_TACHO, case 1:)
  109. TERN_(HAS_E2_FAN_TACHO, case 2:)
  110. TERN_(HAS_E3_FAN_TACHO, case 3:)
  111. TERN_(HAS_E4_FAN_TACHO, case 4:)
  112. TERN_(HAS_E5_FAN_TACHO, case 5:)
  113. TERN_(HAS_E6_FAN_TACHO, case 6:)
  114. TERN_(HAS_E7_FAN_TACHO, case 7:)
  115. // Compute fan speed
  116. rps[f] = edge_counter[f] * float(250) / elapsedTime;
  117. edge_counter[f] = 0;
  118. // Check fan speed
  119. constexpr int8_t max_extruder_fan_errors = TERN(HAS_PWMFANCHECK, 10000, 5000) / Temperature::fan_update_interval_ms;
  120. if (rps[f] >= 20 || TERN0(HAS_AUTO_FAN, thermalManager.autofan_speed[f] == 0))
  121. errors_count[f] = 0;
  122. else if (errors_count[f] < max_extruder_fan_errors)
  123. ++errors_count[f];
  124. else if (enabled)
  125. SBI(fan_error_msk, f);
  126. break;
  127. }
  128. }
  129. // Drop the error when all fans are ok
  130. if (!fan_error_msk && error == TachoError::REPORTED) error = TachoError::FIXED;
  131. if (error == TachoError::FIXED && !printJobOngoing() && !printingIsPaused()) {
  132. error = TachoError::NONE; // if the issue has been fixed while the printer is idle, reenable immediately
  133. ui.reset_alert_level();
  134. }
  135. if (fan_error_msk & ~fan_reported_errors_msk) {
  136. // Handle new faults only
  137. LOOP_L_N(f, TACHO_COUNT) if (TEST(fan_error_msk, f)) report_speed_error(f);
  138. }
  139. fan_reported_errors_msk = fan_error_msk;
  140. }
  141. void FanCheck::report_speed_error(uint8_t fan) {
  142. if (printJobOngoing()) {
  143. if (error == TachoError::NONE) {
  144. if (thermalManager.degTargetHotend(fan) != 0) {
  145. kill(GET_TEXT_F(MSG_FAN_SPEED_FAULT));
  146. error = TachoError::REPORTED;
  147. }
  148. else
  149. error = TachoError::DETECTED; // Plans error for next processed command
  150. }
  151. }
  152. else if (!printingIsPaused()) {
  153. thermalManager.setTargetHotend(0, fan); // Always disable heating
  154. if (error == TachoError::NONE) error = TachoError::REPORTED;
  155. }
  156. SERIAL_ERROR_MSG(STR_ERR_FANSPEED, fan);
  157. LCD_ALERTMESSAGE(MSG_FAN_SPEED_FAULT);
  158. }
  159. void FanCheck::print_fan_states() {
  160. LOOP_L_N(s, 2) {
  161. LOOP_L_N(f, TACHO_COUNT) {
  162. switch (f) {
  163. TERN_(HAS_E0_FAN_TACHO, case 0:)
  164. TERN_(HAS_E1_FAN_TACHO, case 1:)
  165. TERN_(HAS_E2_FAN_TACHO, case 2:)
  166. TERN_(HAS_E3_FAN_TACHO, case 3:)
  167. TERN_(HAS_E4_FAN_TACHO, case 4:)
  168. TERN_(HAS_E5_FAN_TACHO, case 5:)
  169. TERN_(HAS_E6_FAN_TACHO, case 6:)
  170. TERN_(HAS_E7_FAN_TACHO, case 7:)
  171. SERIAL_ECHOPGM("E", f);
  172. if (s == 0)
  173. SERIAL_ECHOPGM(":", 60 * rps[f], " RPM ");
  174. else
  175. SERIAL_ECHOPGM("@:", TERN(HAS_AUTO_FAN, thermalManager.autofan_speed[f], 255), " ");
  176. break;
  177. }
  178. }
  179. }
  180. SERIAL_EOL();
  181. }
  182. #if ENABLED(AUTO_REPORT_FANS)
  183. AutoReporter<FanCheck::AutoReportFan> FanCheck::auto_reporter;
  184. void FanCheck::AutoReportFan::report() { print_fan_states(); }
  185. #endif
  186. #endif // HAS_FANCHECK