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.

HAL_Servo_ESP32.cpp 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifdef ARDUINO_ARCH_ESP32
  23. #include "../../inc/MarlinConfig.h"
  24. #if HAS_SERVOS
  25. #include "HAL_Servo_ESP32.h"
  26. // Adjacent channels (0/1, 2/3 etc.) share the same timer and therefore the same frequency and resolution settings on ESP32,
  27. // so we only allocate servo channels up high to avoid side effects with regards to analogWrite (fans, leds, laser pwm etc.)
  28. int Servo::channel_next_free = 12;
  29. Servo::Servo() {
  30. this->channel = channel_next_free++;
  31. }
  32. int8_t Servo::attach(const int pin) {
  33. if (this->channel >= CHANNEL_MAX_NUM) return -1;
  34. if (pin > 0) this->pin = pin;
  35. ledcSetup(this->channel, 50, 16); // channel X, 50 Hz, 16-bit depth
  36. ledcAttachPin(this->pin, this->channel);
  37. return true;
  38. }
  39. void Servo::detach() { ledcDetachPin(this->pin); }
  40. int Servo::read() { return this->degrees; }
  41. void Servo::write(int degrees) {
  42. this->degrees = constrain(degrees, MIN_ANGLE, MAX_ANGLE);
  43. int us = map(this->degrees, MIN_ANGLE, MAX_ANGLE, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
  44. int duty = map(us, 0, TAU_USEC, 0, MAX_COMPARE);
  45. ledcWrite(channel, duty);
  46. }
  47. void Servo::move(const int value) {
  48. constexpr uint16_t servo_delay[] = SERVO_DELAY;
  49. static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
  50. if (this->attach(0) >= 0) {
  51. this->write(value);
  52. safe_delay(servo_delay[this->channel]);
  53. #if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
  54. this->detach();
  55. #endif
  56. }
  57. }
  58. #endif // HAS_SERVOS
  59. #endif // ARDUINO_ARCH_ESP32