My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

servo.cpp 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * servo.cpp - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2
  24. * Copyright (c) 2009 Michael Margolis. All right reserved.
  25. */
  26. /**
  27. * A servo is activated by creating an instance of the Servo class passing the desired pin to the attach() method.
  28. * The servos are pulsed in the background using the value most recently written using the write() method
  29. *
  30. * Note that analogWrite of PWM on pins associated with the timer are disabled when the first servo is attached.
  31. * Timers are seized as needed in groups of 12 servos - 24 servos use two timers, 48 servos will use four.
  32. *
  33. * The methods are:
  34. *
  35. * Servo - Class for manipulating servo motors connected to Arduino pins.
  36. *
  37. * attach(pin) - Attach a servo motor to an i/o pin.
  38. * attach(pin, min, max) - Attach to a pin, setting min and max values in microseconds
  39. * Default min is 544, max is 2400
  40. *
  41. * write() - Set the servo angle in degrees. (Invalid angles —over MIN_PULSE_WIDTH— are treated as µs.)
  42. * writeMicroseconds() - Set the servo pulse width in microseconds.
  43. * move(pin, angle) - Sequence of attach(pin), write(angle), safe_delay(servo_delay[servoIndex]).
  44. * With DEACTIVATE_SERVOS_AFTER_MOVE it detaches after servo_delay[servoIndex].
  45. * read() - Get the last-written servo pulse width as an angle between 0 and 180.
  46. * readMicroseconds() - Get the last-written servo pulse width in microseconds.
  47. * attached() - Return true if a servo is attached.
  48. * detach() - Stop an attached servo from pulsing its i/o pin.
  49. *
  50. */
  51. #include "../../inc/MarlinConfig.h"
  52. #if SHARED_SERVOS
  53. #include "servo.h"
  54. #include "servo_private.h"
  55. ServoInfo_t servo_info[MAX_SERVOS]; // static array of servo info structures
  56. uint8_t ServoCount = 0; // the total number of attached servos
  57. #define SERVO_MIN(v) (MIN_PULSE_WIDTH - (v) * 4) // minimum value in uS for this servo
  58. #define SERVO_MAX(v) (MAX_PULSE_WIDTH - (v) * 4) // maximum value in uS for this servo
  59. /************ static functions common to all instances ***********************/
  60. static boolean isTimerActive(timer16_Sequence_t timer) {
  61. // returns true if any servo is active on this timer
  62. LOOP_L_N(channel, SERVOS_PER_TIMER) {
  63. if (SERVO(timer, channel).Pin.isActive)
  64. return true;
  65. }
  66. return false;
  67. }
  68. /****************** end of static functions ******************************/
  69. Servo::Servo() {
  70. if (ServoCount < MAX_SERVOS) {
  71. servoIndex = ServoCount++; // assign a servo index to this instance
  72. servo_info[servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH); // store default values - 12 Aug 2009
  73. }
  74. else
  75. servoIndex = INVALID_SERVO; // too many servos
  76. }
  77. int8_t Servo::attach(const int inPin) {
  78. return attach(inPin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
  79. }
  80. int8_t Servo::attach(const int inPin, const int inMin, const int inMax) {
  81. if (servoIndex >= MAX_SERVOS) return -1;
  82. if (inPin > 0) servo_info[servoIndex].Pin.nbr = inPin;
  83. pinMode(servo_info[servoIndex].Pin.nbr, OUTPUT); // set servo pin to output
  84. // TODO: min/max check: ABS(min - MIN_PULSE_WIDTH) / 4 < 128
  85. min = (MIN_PULSE_WIDTH - inMin) / 4; //resolution of min/max is 4 uS
  86. max = (MAX_PULSE_WIDTH - inMax) / 4;
  87. // initialize the timer if it has not already been initialized
  88. timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
  89. if (!isTimerActive(timer)) initISR(timer);
  90. servo_info[servoIndex].Pin.isActive = true; // this must be set after the check for isTimerActive
  91. return servoIndex;
  92. }
  93. void Servo::detach() {
  94. servo_info[servoIndex].Pin.isActive = false;
  95. timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
  96. if (!isTimerActive(timer)) finISR(timer);
  97. }
  98. void Servo::write(int value) {
  99. if (value < MIN_PULSE_WIDTH) // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
  100. value = map(constrain(value, 0, 180), 0, 180, SERVO_MIN(min), SERVO_MAX(max));
  101. writeMicroseconds(value);
  102. }
  103. void Servo::writeMicroseconds(int value) {
  104. // calculate and store the values for the given channel
  105. byte channel = servoIndex;
  106. if (channel < MAX_SERVOS) { // ensure channel is valid
  107. // ensure pulse width is valid
  108. value = constrain(value, SERVO_MIN(min), SERVO_MAX(max)) - (TRIM_DURATION);
  109. value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009
  110. CRITICAL_SECTION_START();
  111. servo_info[channel].ticks = value;
  112. CRITICAL_SECTION_END();
  113. }
  114. }
  115. // return the value as degrees
  116. int Servo::read() { return map(readMicroseconds() + 1, SERVO_MIN(min), SERVO_MAX(max), 0, 180); }
  117. int Servo::readMicroseconds() {
  118. return (servoIndex == INVALID_SERVO) ? 0 : ticksToUs(servo_info[servoIndex].ticks) + (TRIM_DURATION);
  119. }
  120. bool Servo::attached() { return servo_info[servoIndex].Pin.isActive; }
  121. void Servo::move(const int value) {
  122. constexpr uint16_t servo_delay[] = SERVO_DELAY;
  123. static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
  124. if (attach(0) >= 0) {
  125. write(value);
  126. safe_delay(servo_delay[servoIndex]);
  127. #if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
  128. detach();
  129. #endif
  130. }
  131. }
  132. #endif // SHARED_SERVOS