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.

LPC1768_Servo.cpp 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. * Based on servo.cpp - Interrupt driven Servo library for Arduino using 16 bit
  24. * timers- Version 2 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. /**
  52. * The only time that this library wants physical movement is when a WRITE
  53. * command is issued. Before that all the attach & detach activity is solely
  54. * within the data base.
  55. *
  56. * The PWM output is inactive until the first WRITE. After that it stays active
  57. * unless DEACTIVATE_SERVOS_AFTER_MOVE is enabled and a MOVE command was issued.
  58. */
  59. #include "../../inc/MarlinConfig.h"
  60. #if HAS_SERVOS && defined(TARGET_LPC1768)
  61. #include "LPC1768_PWM.h"
  62. #include "LPC1768_Servo.h"
  63. #include "servo_private.h"
  64. ServoInfo_t servo_info[MAX_SERVOS]; // static array of servo info structures
  65. uint8_t ServoCount = 0; // the total number of attached servos
  66. #define US_TO_PULSE_WIDTH(p) p
  67. #define PULSE_WIDTH_TO_US(p) p
  68. #define TRIM_DURATION 0
  69. #define SERVO_MIN() MIN_PULSE_WIDTH // minimum value in uS for this servo
  70. #define SERVO_MAX() MAX_PULSE_WIDTH // maximum value in uS for this servo
  71. Servo::Servo() {
  72. if (ServoCount < MAX_SERVOS) {
  73. this->servoIndex = ServoCount++; // assign a servo index to this instance
  74. servo_info[this->servoIndex].pulse_width = US_TO_PULSE_WIDTH(DEFAULT_PULSE_WIDTH); // store default values - 12 Aug 2009
  75. }
  76. else
  77. this->servoIndex = INVALID_SERVO; // too many servos
  78. }
  79. int8_t Servo::attach(const int pin) {
  80. return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
  81. }
  82. int8_t Servo::attach(const int pin, const int min, const int max) {
  83. if (this->servoIndex >= MAX_SERVOS) return -1;
  84. if (pin > 0) servo_info[this->servoIndex].Pin.nbr = pin; // only assign a pin value if the pin info is
  85. // greater than zero. This way the init routine can
  86. // assign the pin and the MOVE command only needs the value.
  87. this->min = MIN_PULSE_WIDTH; //resolution of min/max is 1 uS
  88. this->max = MAX_PULSE_WIDTH;
  89. servo_info[this->servoIndex].Pin.isActive = true;
  90. return this->servoIndex;
  91. }
  92. void Servo::detach() {
  93. servo_info[this->servoIndex].Pin.isActive = false;
  94. }
  95. void Servo::write(int value) {
  96. if (value < MIN_PULSE_WIDTH) { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
  97. value = map(constrain(value, 0, 180), 0, 180, SERVO_MIN(), SERVO_MAX());
  98. // odd - this sets zero degrees to 544 and 180 degrees to 2400 microseconds but the literature says
  99. // zero degrees should be 500 microseconds and 180 should be 2500
  100. }
  101. this->writeMicroseconds(value);
  102. }
  103. void Servo::writeMicroseconds(int value) {
  104. // calculate and store the values for the given channel
  105. byte channel = this->servoIndex;
  106. if (channel < MAX_SERVOS) { // ensure channel is valid
  107. // ensure pulse width is valid
  108. value = constrain(value, SERVO_MIN(), SERVO_MAX()) - (TRIM_DURATION);
  109. value = US_TO_PULSE_WIDTH(value); // convert to pulse_width after compensating for interrupt overhead - 12 Aug 2009
  110. servo_info[channel].pulse_width = value;
  111. LPC1768_PWM_attach_pin(servo_info[this->servoIndex].Pin.nbr, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH, this->servoIndex);
  112. LPC1768_PWM_write(servo_info[this->servoIndex].Pin.nbr, value);
  113. }
  114. }
  115. // return the value as degrees
  116. int Servo::read() { return map(this->readMicroseconds() + 1, SERVO_MIN(), SERVO_MAX(), 0, 180); }
  117. int Servo::readMicroseconds() {
  118. return (this->servoIndex == INVALID_SERVO) ? 0 : PULSE_WIDTH_TO_US(servo_info[this->servoIndex].pulse_width) + TRIM_DURATION;
  119. }
  120. bool Servo::attached() { return servo_info[this->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 (this->attach(0) >= 0) { // notice the pin number is zero here
  125. this->write(value);
  126. safe_delay(servo_delay[this->servoIndex]);
  127. #if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
  128. this->detach();
  129. LPC1768_PWM_detach_pin(servo_info[this->servoIndex].Pin.nbr); // shut down the PWM signal
  130. LPC1768_PWM_attach_pin(servo_info[this->servoIndex].Pin.nbr, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH, this->servoIndex); // make sure no one else steals the slot
  131. #endif
  132. }
  133. }
  134. #endif // HAS_SERVOS && TARGET_LPC1768