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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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 HAS_SERVOS && !(IS_32BIT_TEENSY || defined(TARGET_LPC1768) || defined(STM32F4))
  53. //#include <Arduino.h>
  54. #include "servo.h"
  55. #include "servo_private.h"
  56. ServoInfo_t servo_info[MAX_SERVOS]; // static array of servo info structures
  57. uint8_t ServoCount = 0; // the total number of attached servos
  58. #define SERVO_MIN() (MIN_PULSE_WIDTH - this->min * 4) // minimum value in uS for this servo
  59. #define SERVO_MAX() (MAX_PULSE_WIDTH - this->max * 4) // maximum value in uS for this servo
  60. /************ static functions common to all instances ***********************/
  61. static boolean isTimerActive(timer16_Sequence_t timer) {
  62. // returns true if any servo is active on this timer
  63. for (uint8_t channel = 0; channel < SERVOS_PER_TIMER; channel++) {
  64. if (SERVO(timer, channel).Pin.isActive)
  65. return true;
  66. }
  67. return false;
  68. }
  69. /****************** end of static functions ******************************/
  70. Servo::Servo() {
  71. if (ServoCount < MAX_SERVOS) {
  72. this->servoIndex = ServoCount++; // assign a servo index to this instance
  73. servo_info[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH); // store default values - 12 Aug 2009
  74. }
  75. else
  76. this->servoIndex = INVALID_SERVO; // too many servos
  77. }
  78. int8_t Servo::attach(const int pin) {
  79. return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
  80. }
  81. int8_t Servo::attach(const int pin, const int min, const int max) {
  82. if (this->servoIndex >= MAX_SERVOS) return -1;
  83. if (pin > 0) servo_info[this->servoIndex].Pin.nbr = pin;
  84. pinMode(servo_info[this->servoIndex].Pin.nbr, OUTPUT); // set servo pin to output
  85. // todo min/max check: ABS(min - MIN_PULSE_WIDTH) /4 < 128
  86. this->min = (MIN_PULSE_WIDTH - min) / 4; //resolution of min/max is 4 uS
  87. this->max = (MAX_PULSE_WIDTH - max) / 4;
  88. // initialize the timer if it has not already been initialized
  89. timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
  90. if (!isTimerActive(timer)) initISR(timer);
  91. servo_info[this->servoIndex].Pin.isActive = true; // this must be set after the check for isTimerActive
  92. return this->servoIndex;
  93. }
  94. void Servo::detach() {
  95. servo_info[this->servoIndex].Pin.isActive = false;
  96. timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
  97. if (!isTimerActive(timer)) finISR(timer);
  98. }
  99. void Servo::write(int value) {
  100. if (value < MIN_PULSE_WIDTH) { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
  101. value = map(constrain(value, 0, 180), 0, 180, SERVO_MIN(), SERVO_MAX());
  102. }
  103. this->writeMicroseconds(value);
  104. }
  105. void Servo::writeMicroseconds(int value) {
  106. // calculate and store the values for the given channel
  107. byte channel = this->servoIndex;
  108. if (channel < MAX_SERVOS) { // ensure channel is valid
  109. // ensure pulse width is valid
  110. value = constrain(value, SERVO_MIN(), SERVO_MAX()) - (TRIM_DURATION);
  111. value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009
  112. CRITICAL_SECTION_START;
  113. servo_info[channel].ticks = value;
  114. CRITICAL_SECTION_END;
  115. }
  116. }
  117. // return the value as degrees
  118. int Servo::read() { return map(this->readMicroseconds() + 1, SERVO_MIN(), SERVO_MAX(), 0, 180); }
  119. int Servo::readMicroseconds() {
  120. return (this->servoIndex == INVALID_SERVO) ? 0 : ticksToUs(servo_info[this->servoIndex].ticks) + TRIM_DURATION;
  121. }
  122. bool Servo::attached() { return servo_info[this->servoIndex].Pin.isActive; }
  123. void Servo::move(const int value) {
  124. constexpr uint16_t servo_delay[] = SERVO_DELAY;
  125. static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
  126. if (this->attach(0) >= 0) {
  127. this->write(value);
  128. safe_delay(servo_delay[this->servoIndex]);
  129. #if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
  130. this->detach();
  131. #endif
  132. }
  133. }
  134. #endif // HAS_SERVOS