My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Servo.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. Servo.cpp - Interrupt driven Servo library for Arduino using 16 bit timers- Version 2
  3. Copyright (c) 2009 Michael Margolis. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. /*
  17. A servo is activated by creating an instance of the Servo class passing the desired pin to the attach() method.
  18. The servos are pulsed in the background using the value most recently written using the write() method
  19. Note that analogWrite of PWM on pins associated with the timer are disabled when the first servo is attached.
  20. Timers are seized as needed in groups of 12 servos - 24 servos use two timers, 48 servos will use four.
  21. The methods are:
  22. Servo - Class for manipulating servo motors connected to Arduino pins.
  23. attach(pin ) - Attaches a servo motor to an i/o pin.
  24. attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds
  25. default min is 544, max is 2400
  26. write() - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds)
  27. writeMicroseconds() - Sets the servo pulse width in microseconds
  28. read() - Gets the last written servo pulse width as an angle between 0 and 180.
  29. readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release)
  30. attached() - Returns true if there is a servo attached.
  31. detach() - Stops an attached servos from pulsing its i/o pin.
  32. */
  33. #include <avr/interrupt.h>
  34. #include <Arduino.h>
  35. #include "Servo.h"
  36. #define usToTicks(_us) (( clockCyclesPerMicrosecond()* _us) / 8) // converts microseconds to tick (assumes prescale of 8) // 12 Aug 2009
  37. #define ticksToUs(_ticks) (( (unsigned)_ticks * 8)/ clockCyclesPerMicrosecond() ) // converts from ticks back to microseconds
  38. #define TRIM_DURATION 2 // compensation ticks to trim adjust for digitalWrite delays // 12 August 2009
  39. //#define NBR_TIMERS (MAX_SERVOS / SERVOS_PER_TIMER)
  40. static servo_t servos[MAX_SERVOS]; // static array of servo structures
  41. static volatile int8_t Channel[_Nbr_16timers ]; // counter for the servo being pulsed for each timer (or -1 if refresh interval)
  42. uint8_t ServoCount = 0; // the total number of attached servos
  43. // convenience macros
  44. #define SERVO_INDEX_TO_TIMER(_servo_nbr) ((timer16_Sequence_t)(_servo_nbr / SERVOS_PER_TIMER)) // returns the timer controlling this servo
  45. #define SERVO_INDEX_TO_CHANNEL(_servo_nbr) (_servo_nbr % SERVOS_PER_TIMER) // returns the index of the servo on this timer
  46. #define SERVO_INDEX(_timer,_channel) ((_timer*SERVOS_PER_TIMER) + _channel) // macro to access servo index by timer and channel
  47. #define SERVO(_timer,_channel) (servos[SERVO_INDEX(_timer,_channel)]) // macro to access servo class by timer and channel
  48. #define SERVO_MIN() (MIN_PULSE_WIDTH - this->min * 4) // minimum value in uS for this servo
  49. #define SERVO_MAX() (MAX_PULSE_WIDTH - this->max * 4) // maximum value in uS for this servo
  50. /************ static functions common to all instances ***********************/
  51. static inline void handle_interrupts(timer16_Sequence_t timer, volatile uint16_t *TCNTn, volatile uint16_t* OCRnA)
  52. {
  53. if( Channel[timer] < 0 )
  54. *TCNTn = 0; // channel set to -1 indicated that refresh interval completed so reset the timer
  55. else{
  56. if( SERVO_INDEX(timer,Channel[timer]) < ServoCount && SERVO(timer,Channel[timer]).Pin.isActive == true )
  57. digitalWrite( SERVO(timer,Channel[timer]).Pin.nbr,LOW); // pulse this channel low if activated
  58. }
  59. Channel[timer]++; // increment to the next channel
  60. if( SERVO_INDEX(timer,Channel[timer]) < ServoCount && Channel[timer] < SERVOS_PER_TIMER) {
  61. *OCRnA = *TCNTn + SERVO(timer,Channel[timer]).ticks;
  62. if(SERVO(timer,Channel[timer]).Pin.isActive == true) // check if activated
  63. digitalWrite( SERVO(timer,Channel[timer]).Pin.nbr,HIGH); // its an active channel so pulse it high
  64. }
  65. else {
  66. // finished all channels so wait for the refresh period to expire before starting over
  67. if( ((unsigned)*TCNTn) + 4 < usToTicks(REFRESH_INTERVAL) ) // allow a few ticks to ensure the next OCR1A not missed
  68. *OCRnA = (unsigned int)usToTicks(REFRESH_INTERVAL);
  69. else
  70. *OCRnA = *TCNTn + 4; // at least REFRESH_INTERVAL has elapsed
  71. Channel[timer] = -1; // this will get incremented at the end of the refresh period to start again at the first channel
  72. }
  73. }
  74. #ifndef WIRING // Wiring pre-defines signal handlers so don't define any if compiling for the Wiring platform
  75. // Interrupt handlers for Arduino
  76. #if defined(_useTimer1)
  77. SIGNAL (TIMER1_COMPA_vect)
  78. {
  79. handle_interrupts(_timer1, &TCNT1, &OCR1A);
  80. }
  81. #endif
  82. #if defined(_useTimer3)
  83. SIGNAL (TIMER3_COMPA_vect)
  84. {
  85. handle_interrupts(_timer3, &TCNT3, &OCR3A);
  86. }
  87. #endif
  88. #if defined(_useTimer4)
  89. SIGNAL (TIMER4_COMPA_vect)
  90. {
  91. handle_interrupts(_timer4, &TCNT4, &OCR4A);
  92. }
  93. #endif
  94. #if defined(_useTimer5)
  95. SIGNAL (TIMER5_COMPA_vect)
  96. {
  97. handle_interrupts(_timer5, &TCNT5, &OCR5A);
  98. }
  99. #endif
  100. #elif defined WIRING
  101. // Interrupt handlers for Wiring
  102. #if defined(_useTimer1)
  103. void Timer1Service()
  104. {
  105. handle_interrupts(_timer1, &TCNT1, &OCR1A);
  106. }
  107. #endif
  108. #if defined(_useTimer3)
  109. void Timer3Service()
  110. {
  111. handle_interrupts(_timer3, &TCNT3, &OCR3A);
  112. }
  113. #endif
  114. #endif
  115. static void initISR(timer16_Sequence_t timer)
  116. {
  117. #if defined (_useTimer1)
  118. if(timer == _timer1) {
  119. TCCR1A = 0; // normal counting mode
  120. TCCR1B = _BV(CS11); // set prescaler of 8
  121. TCNT1 = 0; // clear the timer count
  122. #if defined(__AVR_ATmega8__)|| defined(__AVR_ATmega128__)
  123. TIFR |= _BV(OCF1A); // clear any pending interrupts;
  124. TIMSK |= _BV(OCIE1A) ; // enable the output compare interrupt
  125. #else
  126. // here if not ATmega8 or ATmega128
  127. TIFR1 |= _BV(OCF1A); // clear any pending interrupts;
  128. TIMSK1 |= _BV(OCIE1A) ; // enable the output compare interrupt
  129. #endif
  130. #if defined(WIRING)
  131. timerAttach(TIMER1OUTCOMPAREA_INT, Timer1Service);
  132. #endif
  133. }
  134. #endif
  135. #if defined (_useTimer3)
  136. if(timer == _timer3) {
  137. TCCR3A = 0; // normal counting mode
  138. TCCR3B = _BV(CS31); // set prescaler of 8
  139. TCNT3 = 0; // clear the timer count
  140. #if defined(__AVR_ATmega128__)
  141. TIFR |= _BV(OCF3A); // clear any pending interrupts;
  142. ETIMSK |= _BV(OCIE3A); // enable the output compare interrupt
  143. #else
  144. TIFR3 = _BV(OCF3A); // clear any pending interrupts;
  145. TIMSK3 = _BV(OCIE3A) ; // enable the output compare interrupt
  146. #endif
  147. #if defined(WIRING)
  148. timerAttach(TIMER3OUTCOMPAREA_INT, Timer3Service); // for Wiring platform only
  149. #endif
  150. }
  151. #endif
  152. #if defined (_useTimer4)
  153. if(timer == _timer4) {
  154. TCCR4A = 0; // normal counting mode
  155. TCCR4B = _BV(CS41); // set prescaler of 8
  156. TCNT4 = 0; // clear the timer count
  157. TIFR4 = _BV(OCF4A); // clear any pending interrupts;
  158. TIMSK4 = _BV(OCIE4A) ; // enable the output compare interrupt
  159. }
  160. #endif
  161. #if defined (_useTimer5)
  162. if(timer == _timer5) {
  163. TCCR5A = 0; // normal counting mode
  164. TCCR5B = _BV(CS51); // set prescaler of 8
  165. TCNT5 = 0; // clear the timer count
  166. TIFR5 = _BV(OCF5A); // clear any pending interrupts;
  167. TIMSK5 = _BV(OCIE5A) ; // enable the output compare interrupt
  168. }
  169. #endif
  170. }
  171. static void finISR(timer16_Sequence_t timer)
  172. {
  173. //disable use of the given timer
  174. #if defined WIRING // Wiring
  175. if(timer == _timer1) {
  176. #if defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__)
  177. TIMSK1 &= ~_BV(OCIE1A) ; // disable timer 1 output compare interrupt
  178. #else
  179. TIMSK &= ~_BV(OCIE1A) ; // disable timer 1 output compare interrupt
  180. #endif
  181. timerDetach(TIMER1OUTCOMPAREA_INT);
  182. }
  183. else if(timer == _timer3) {
  184. #if defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__)
  185. TIMSK3 &= ~_BV(OCIE3A); // disable the timer3 output compare A interrupt
  186. #else
  187. ETIMSK &= ~_BV(OCIE3A); // disable the timer3 output compare A interrupt
  188. #endif
  189. timerDetach(TIMER3OUTCOMPAREA_INT);
  190. }
  191. #else
  192. //For arduino - in future: call here to a currently undefined function to reset the timer
  193. #endif
  194. }
  195. static boolean isTimerActive(timer16_Sequence_t timer)
  196. {
  197. // returns true if any servo is active on this timer
  198. for(uint8_t channel=0; channel < SERVOS_PER_TIMER; channel++) {
  199. if(SERVO(timer,channel).Pin.isActive == true)
  200. return true;
  201. }
  202. return false;
  203. }
  204. /****************** end of static functions ******************************/
  205. Servo::Servo()
  206. {
  207. if( ServoCount < MAX_SERVOS) {
  208. this->servoIndex = ServoCount++; // assign a servo index to this instance
  209. servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH); // store default values - 12 Aug 2009
  210. }
  211. else
  212. this->servoIndex = INVALID_SERVO ; // too many servos
  213. }
  214. uint8_t Servo::attach(int pin)
  215. {
  216. return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
  217. }
  218. uint8_t Servo::attach(int pin, int min, int max)
  219. {
  220. if(this->servoIndex < MAX_SERVOS ) {
  221. pinMode( pin, OUTPUT) ; // set servo pin to output
  222. servos[this->servoIndex].Pin.nbr = pin;
  223. // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128
  224. this->min = (MIN_PULSE_WIDTH - min)/4; //resolution of min/max is 4 uS
  225. this->max = (MAX_PULSE_WIDTH - max)/4;
  226. // initialize the timer if it has not already been initialized
  227. timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
  228. if(isTimerActive(timer) == false)
  229. initISR(timer);
  230. servos[this->servoIndex].Pin.isActive = true; // this must be set after the check for isTimerActive
  231. }
  232. return this->servoIndex ;
  233. }
  234. void Servo::detach()
  235. {
  236. servos[this->servoIndex].Pin.isActive = false;
  237. timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
  238. if(isTimerActive(timer) == false) {
  239. finISR(timer);
  240. }
  241. }
  242. void Servo::write(int value)
  243. {
  244. if(value < MIN_PULSE_WIDTH)
  245. { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
  246. if(value < 0) value = 0;
  247. if(value > 180) value = 180;
  248. value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX());
  249. }
  250. this->writeMicroseconds(value);
  251. }
  252. void Servo::writeMicroseconds(int value)
  253. {
  254. // calculate and store the values for the given channel
  255. byte channel = this->servoIndex;
  256. if( (channel < MAX_SERVOS) ) // ensure channel is valid
  257. {
  258. if( value < SERVO_MIN() ) // ensure pulse width is valid
  259. value = SERVO_MIN();
  260. else if( value > SERVO_MAX() )
  261. value = SERVO_MAX();
  262. value = value - TRIM_DURATION;
  263. value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009
  264. uint8_t oldSREG = SREG;
  265. cli();
  266. servos[channel].ticks = value;
  267. SREG = oldSREG;
  268. }
  269. }
  270. int Servo::read() // return the value as degrees
  271. {
  272. return map( this->readMicroseconds()+1, SERVO_MIN(), SERVO_MAX(), 0, 180);
  273. }
  274. int Servo::readMicroseconds()
  275. {
  276. unsigned int pulsewidth;
  277. if( this->servoIndex != INVALID_SERVO )
  278. pulsewidth = ticksToUs(servos[this->servoIndex].ticks) + TRIM_DURATION ; // 12 aug 2009
  279. else
  280. pulsewidth = 0;
  281. return pulsewidth;
  282. }
  283. bool Servo::attached()
  284. {
  285. return servos[this->servoIndex].Pin.isActive ;
  286. }