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.

controllerfan.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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. #include "../inc/MarlinConfig.h"
  23. #if ENABLED(USE_CONTROLLER_FAN)
  24. #include "controllerfan.h"
  25. #include "../module/stepper.h"
  26. #include "../module/temperature.h"
  27. ControllerFan controllerFan;
  28. uint8_t ControllerFan::speed;
  29. #if ENABLED(CONTROLLER_FAN_EDITABLE)
  30. controllerFan_settings_t ControllerFan::settings; // {0}
  31. #else
  32. const controllerFan_settings_t &ControllerFan::settings = controllerFan_defaults;
  33. #endif
  34. void ControllerFan::setup() {
  35. SET_OUTPUT(CONTROLLER_FAN_PIN);
  36. init();
  37. }
  38. void ControllerFan::set_fan_speed(const uint8_t s) {
  39. speed = s < (CONTROLLERFAN_SPEED_MIN) ? 0 : s; // Fan OFF below minimum
  40. }
  41. void ControllerFan::update() {
  42. static millis_t lastMotorOn = 0, // Last time a motor was turned on
  43. nextMotorCheck = 0; // Last time the state was checked
  44. const millis_t ms = millis();
  45. if (ELAPSED(ms, nextMotorCheck)) {
  46. nextMotorCheck = ms + 2500UL; // Not a time critical function, so only check every 2.5s
  47. // If any triggers for the controller fan are true...
  48. // - At least one stepper driver is enabled
  49. // - The heated bed is enabled
  50. // - TEMP_SENSOR_BOARD is reporting >= CONTROLLER_FAN_MIN_BOARD_TEMP
  51. const ena_mask_t axis_mask = TERN(CONTROLLER_FAN_USE_Z_ONLY, _BV(Z_AXIS), (ena_mask_t)~TERN0(CONTROLLER_FAN_IGNORE_Z, _BV(Z_AXIS)));
  52. if ( (stepper.axis_enabled.bits & axis_mask)
  53. || TERN0(HAS_HEATED_BED, thermalManager.temp_bed.soft_pwm_amount > 0)
  54. || TERN0(HAS_CONTROLLER_FAN_MIN_BOARD_TEMP, thermalManager.wholeDegBoard() >= CONTROLLER_FAN_MIN_BOARD_TEMP)
  55. ) lastMotorOn = ms; //... set time to NOW so the fan will turn on
  56. // Fan Settings. Set fan > 0:
  57. // - If AutoMode is on and steppers have been enabled for CONTROLLERFAN_IDLE_TIME seconds.
  58. // - If System is on idle and idle fan speed settings is activated.
  59. set_fan_speed(
  60. settings.auto_mode && lastMotorOn && PENDING(ms, lastMotorOn + SEC_TO_MS(settings.duration))
  61. ? settings.active_speed : settings.idle_speed
  62. );
  63. speed = CALC_FAN_SPEED(speed);
  64. #if FAN_KICKSTART_TIME
  65. static millis_t fan_kick_end = 0;
  66. if (speed > FAN_OFF_PWM) {
  67. if (!fan_kick_end) {
  68. fan_kick_end = ms + FAN_KICKSTART_TIME; // May be longer based on slow update interval for controller fn check. Sets minimum
  69. speed = FAN_KICKSTART_POWER;
  70. }
  71. else if (PENDING(ms, fan_kick_end))
  72. speed = FAN_KICKSTART_POWER;
  73. }
  74. else
  75. fan_kick_end = 0;
  76. #endif
  77. #if ENABLED(FAN_SOFT_PWM)
  78. thermalManager.soft_pwm_controller_speed = speed;
  79. #else
  80. if (PWM_PIN(CONTROLLER_FAN_PIN))
  81. hal.set_pwm_duty(pin_t(CONTROLLER_FAN_PIN), speed);
  82. else
  83. WRITE(CONTROLLER_FAN_PIN, speed > 0);
  84. #endif
  85. }
  86. }
  87. #endif // USE_CONTROLLER_FAN