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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. int8_t Servo::attach(const int inPin) {
  31. if (inPin > 0) pin = inPin;
  32. channel = get_pwm_channel(pin, 50u, 16u);
  33. return channel; // -1 if no PWM avail.
  34. }
  35. // leave channel connected to servo - set duty to zero
  36. void Servo::detach() {
  37. if (channel >= 0) ledcWrite(channel, 0);
  38. }
  39. int Servo::read() { return degrees; }
  40. void Servo::write(int inDegrees) {
  41. degrees = constrain(inDegrees, MIN_ANGLE, MAX_ANGLE);
  42. int us = map(degrees, MIN_ANGLE, MAX_ANGLE, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
  43. int duty = map(us, 0, TAU_USEC, 0, MAX_COMPARE);
  44. if (channel >= 0) ledcWrite(channel, duty); // don't save duty for servos!
  45. }
  46. void Servo::move(const int value) {
  47. constexpr uint16_t servo_delay[] = SERVO_DELAY;
  48. static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
  49. if (attach(0) >= 0) {
  50. write(value);
  51. safe_delay(servo_delay[channel]);
  52. TERN_(DEACTIVATE_SERVOS_AFTER_MOVE, detach());
  53. }
  54. }
  55. #endif // HAS_SERVOS
  56. #endif // ARDUINO_ARCH_ESP32