Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2013
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. /*
  2. ServoTimer2.h - Interrupt driven Servo library for Arduino using
  3. /*
  4. This library uses Timer2 to drive up to 8 servos using interrupts so no refresh activity is required from within the sketch.
  5. The usage and method naming is similar to the Arduino software servo library http://www.arduino.cc/playground/ComponentLib/Servo
  6. except that pulse widths are in microseconds.
  7.  
  8.  
  9. A servo is activated by creating an instance of the Servo class passing the desired pin to the attach() method.
  10. The servo is pulsed in the background to the value most recently written using the write() method
  11.  
  12. Note that analogWrite of PWM on pins 3 and 11 is disabled when the first servo is attached
  13.  
  14. The methods are:
  15.  
  16. ServoTimer2 - Class for manipulating servo motors connected to Arduino pins.
  17.  
  18. attach(pin ) - Attaches a servo motor to an i/o pin.
  19. attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds
  20. default min is 544, max is 2400
  21.  
  22. write() - Sets the servo pulse width in microseconds.
  23.  
  24. read() - Gets the last written servo pulse width in microseconds.
  25.  
  26. attached() - Returns true if there is a servo attached.
  27.  
  28. detach() - Stops an attached servos from pulsing its i/o pin.
  29.  
  30.  
  31. The library takes about 824 bytes of program memory and 32+(1*servos) bytes of SRAM.
  32. The pulse width timing is accurate to within 1%
  33.  
  34. */
  35.  
  36. // ensure this library description is only included once
  37. #ifndef ServoTimer2_h
  38. #define ServoTimer2_h
  39.  
  40. #include <inttypes.h>
  41. typedef uint8_t boolean;
  42. typedef uint8_t byte;
  43.  
  44. #define MIN_PULSE_WIDTH 750 // the shortest pulse sent to a servo
  45. #define MAX_PULSE_WIDTH 2250 // the longest pulse sent to a servo
  46. #define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached
  47. #define FRAME_SYNC_PERIOD 20000 // total frame duration in microseconds
  48. #define NBR_CHANNELS 8 // the maximum number of channels, don't change this
  49.  
  50. typedef struct {
  51. uint8_t nbr :5 ; // a pin number from 0 to 31
  52. uint8_t isActive :1 ; // false if this channel not enabled, pin only pulsed if true
  53. } ServoPin_t ;
  54.  
  55. typedef struct {
  56. ServoPin_t Pin;
  57. byte counter;
  58. byte remainder;
  59. } servo_t;
  60.  
  61. class ServoTimer2
  62. {
  63. public:
  64. // constructor:
  65. ServoTimer2();
  66.  
  67. uint8_t attach(int); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure
  68. // the attached servo is pulsed with the current pulse width value, (see the write method)
  69. uint8_t attach(int, int, int); // as above but also sets min and max values for writes.
  70. void detach();
  71. void write(int); // store the pulse width in microseconds (between MIN_PULSE_WIDTH and MAX_PULSE_WIDTH)for this channel
  72. int read(); // returns current pulse width in microseconds for this servo
  73. boolean attached(); // return true if this servo is attached
  74. private:
  75. uint8_t chanIndex; // index into the channel data for this servo
  76.  
  77. };
  78.  
  79.  
  80. // the following ServoArrayT2 class is not implemented in the first version of this library
  81. class ServoArrayT2
  82. {
  83. public:
  84. // constructor:
  85. ServoArrayT2();
  86.  
  87. uint8_t attach(int); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure
  88. // channels are assigned consecutively starting from 1
  89. // the attached servo is pulsed with the current pulse width value, (see the write method)
  90. void detach(int); // detach the servo on the given channel
  91. void write(int,int); // store the pulse width in microseconds (between MIN_PULSE_WIDTH and MAX_PULSE_WIDTH)for the given channel
  92. int read(int); // returns current pulse width in microseconds for the given channel
  93. boolean attached(int); // return true if the servo on the given channel is attached
  94. private:
  95. uint8_t chanIndex; // index into the channel data for this servo
  96.  
  97. };
  98.  
  99. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement