BrickUp API Service for Docker version.

FR_DCBServo.h 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef FR_DCBServo_h
  2. #define FR_DCBServo_h
  3. #include <inttypes.h>
  4. // Say which 16 bit timers can be used and in what order
  5. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  6. #define _useTimer5
  7. #define _useTimer1
  8. #define _useTimer3
  9. #define _useTimer4
  10. typedef enum { _timer5, _timer1, _timer3, _timer4, _Nbr_16timers } timer16_Sequence_t ;
  11. #elif defined(__AVR_ATmega32U4__)
  12. #define _useTimer3
  13. #define _useTimer1
  14. typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t ;
  15. #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
  16. #define _useTimer3
  17. #define _useTimer1
  18. typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t ;
  19. #elif defined(__AVR_ATmega128__) ||defined(__AVR_ATmega1281__)||defined(__AVR_ATmega2561__)
  20. #define _useTimer3
  21. #define _useTimer1
  22. typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t ;
  23. #else // everything else
  24. #define _useTimer1
  25. typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t ;
  26. #endif
  27. #define FR_DCBServo_VERSION 2 // software version of this library
  28. #define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo
  29. #define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo
  30. #define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached
  31. #define REFRESH_INTERVAL 20000 // minumim time to refresh servos in microseconds
  32. #define SERVOS_PER_TIMER 12 // the maximum number of servos controlled by one timer
  33. #define MAX_SERVOS (_Nbr_16timers * SERVOS_PER_TIMER)
  34. #define INVALID_SERVO 255 // flag indicating an invalid servo index
  35. #define CURRENT_SEQUENCE_STOP 255 // used to indicate the current sequence is not used and sequence should stop
  36. typedef struct {
  37. uint8_t nbr :6 ; // a pin number from 0 to 63
  38. uint8_t isActive :1 ; // true if this channel is enabled, pin not pulsed if false
  39. } ServoPin_t ;
  40. typedef struct {
  41. ServoPin_t Pin;
  42. unsigned int ticks;
  43. unsigned int target; // Extension for slowmove
  44. uint8_t speed; // Extension for slowmove
  45. } servo_t;
  46. typedef struct {
  47. uint8_t position;
  48. uint8_t speed;
  49. } servoSequencePoint;
  50. class FR_DCBServo
  51. {
  52. public:
  53. FR_DCBServo();
  54. uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure
  55. uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes.
  56. void detach();
  57. void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds
  58. void write(int value, uint8_t speed); // Move to given position at reduced speed.
  59. // speed=0 is identical to write, speed=1 slowest and speed=255 fastest.
  60. // On the RC-Servos tested, speeds differences above 127 can't be noticed,
  61. // because of the mechanical limits of the servo.
  62. void write(int value, uint8_t speed, bool wait); // wait parameter causes call to block until move completes
  63. void writeMicroseconds(int value); // Write pulse width in microseconds
  64. void slowmove(int value, uint8_t speed);
  65. void stop(); // stop the servo where it is
  66. int read(); // returns current pulse width as an angle between 0 and 180 degrees
  67. int readMicroseconds(); // returns current pulse width in microseconds for this servo (was read_us() in first release)
  68. bool attached(); // return true if this servo is attached, otherwise false
  69. uint8_t sequencePlay(servoSequencePoint sequenceIn[], uint8_t numPositions, bool loop, uint8_t startPos);
  70. uint8_t sequencePlay(servoSequencePoint sequenceIn[], uint8_t numPositions); // play a looping sequence starting at position 0
  71. void sequenceStop(); // stop movement
  72. private:
  73. uint8_t servoIndex; // index into the channel data for this servo
  74. int8_t min; // minimum is this value times 4 added to MIN_PULSE_WIDTH
  75. int8_t max; // maximum is this value times 4 added to MAX_PULSE_WIDTH
  76. servoSequencePoint * curSequence; // for sequences
  77. uint8_t curSeqPosition; // for sequences
  78. };
  79. #endif