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.

Servo.cpp 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #ifdef ARDUINO_ARCH_ESP32
  23. #include "../../inc/MarlinConfig.h"
  24. #if HAS_SERVOS
  25. #include "Servo.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. channel = channel_next_free++;
  31. }
  32. int8_t Servo::attach(const int inPin) {
  33. if (channel >= CHANNEL_MAX_NUM) return -1;
  34. if (inPin > 0) pin = inPin;
  35. ledcSetup(channel, 50, 16); // channel X, 50 Hz, 16-bit depth
  36. ledcAttachPin(pin, channel);
  37. return true;
  38. }
  39. void Servo::detach() { ledcDetachPin(pin); }
  40. int Servo::read() { return degrees; }
  41. void Servo::write(int inDegrees) {
  42. degrees = constrain(inDegrees, MIN_ANGLE, MAX_ANGLE);
  43. int us = map(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 (attach(0) >= 0) {
  51. write(value);
  52. safe_delay(servo_delay[channel]);
  53. TERN_(DEACTIVATE_SERVOS_AFTER_MOVE, detach());
  54. }
  55. }
  56. #endif // HAS_SERVOS
  57. #endif // ARDUINO_ARCH_ESP32