Keyboard firmwares for Atmel AVR and Cortex-M
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.

AX12.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* mbed AX-12+ Servo Library
  2. *
  3. * Copyright (c) 2010, cstyles (http://mbed.org)
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #ifndef MBED_AX12_H
  24. #define MBED_AX12_H
  25. #include "mbed.h"
  26. #define AX12_WRITE_DEBUG 1
  27. #define AX12_READ_DEBUG 1
  28. #define AX12_TRIGGER_DEBUG 1
  29. #define AX12_DEBUG 1
  30. #define AX12_REG_ID 0x3
  31. #define AX12_REG_BAUD 0x4
  32. #define AX12_REG_CW_LIMIT 0x06
  33. #define AX12_REG_CCW_LIMIT 0x08
  34. #define AX12_REG_GOAL_POSITION 0x1E
  35. #define AX12_REG_MOVING_SPEED 0x20
  36. #define AX12_REG_VOLTS 0x2A
  37. #define AX12_REG_TEMP 0x2B
  38. #define AX12_REG_MOVING 0x2E
  39. #define AX12_REG_POSITION 0x24
  40. #define AX12_MODE_POSITION 0
  41. #define AX12_MODE_ROTATION 1
  42. #define AX12_CW 1
  43. #define AX12_CCW 0
  44. /** Servo control class, based on a PwmOut
  45. *
  46. * Example:
  47. * @code
  48. * #include "mbed.h"
  49. * #include "AX12.h"
  50. *
  51. * int main() {
  52. *
  53. * AX12 myax12 (p9, p10, 1);
  54. *
  55. * while (1) {
  56. * myax12.SetGoal(0); // go to 0 degrees
  57. * wait (2.0);
  58. * myax12.SetGoal(300); // go to 300 degrees
  59. * wait (2.0);
  60. * }
  61. * }
  62. * @endcode
  63. */
  64. class AX12 {
  65. public:
  66. /** Create an AX12 servo object connected to the specified serial port, with the specified ID
  67. *
  68. * @param pin tx pin
  69. * @param pin rx pin
  70. * @param int ID, the Bus ID of the servo 1-255
  71. */
  72. AX12(PinName tx, PinName rx, int ID, int baud=1000000);
  73. /** Set the mode of the servo
  74. * @param mode
  75. * 0 = Positional, default
  76. * 1 = Continuous rotation
  77. */
  78. int SetMode(int mode);
  79. /** Set baud rate of all attached servos
  80. * @param mode
  81. * 0x01 = 1,000,000 bps
  82. * 0x03 = 500,000 bps
  83. * 0x04 = 400,000 bps
  84. * 0x07 = 250,000 bps
  85. * 0x09 = 200,000 bps
  86. * 0x10 = 115,200 bps
  87. * 0x22 = 57,600 bps
  88. * 0x67 = 19,200 bps
  89. * 0xCF = 9,600 bp
  90. */
  91. int SetBaud(int baud);
  92. /** Set goal angle in integer degrees, in positional mode
  93. *
  94. * @param degrees 0-300
  95. * @param flags, defaults to 0
  96. * flags[0] = blocking, return when goal position reached
  97. * flags[1] = register, activate with a broadcast trigger
  98. *
  99. */
  100. int SetGoal(int degrees, int flags = 0);
  101. /** Set the speed of the servo in continuous rotation mode
  102. *
  103. * @param speed, -1.0 to 1.0
  104. * -1.0 = full speed counter clock wise
  105. * 1.0 = full speed clock wise
  106. */
  107. int SetCRSpeed(float speed);
  108. /** Set the clockwise limit of the servo
  109. *
  110. * @param degrees, 0-300
  111. */
  112. int SetCWLimit(int degrees);
  113. /** Set the counter-clockwise limit of the servo
  114. *
  115. * @param degrees, 0-300
  116. */
  117. int SetCCWLimit(int degrees);
  118. // Change the ID
  119. /** Change the ID of a servo
  120. *
  121. * @param CurentID 1-255
  122. * @param NewID 1-255
  123. *
  124. * If a servo ID is not know, the broadcast address of 0 can be used for CurrentID.
  125. * In this situation, only one servo should be connected to the bus
  126. */
  127. int SetID(int CurrentID, int NewID);
  128. /** Poll to see if the servo is moving
  129. *
  130. * @returns true is the servo is moving
  131. */
  132. int isMoving(void);
  133. /** Send the broadcast "trigger" command, to activate any outstanding registered commands
  134. */
  135. void trigger(void);
  136. /** Read the current angle of the servo
  137. *
  138. * @returns float in the range 0.0-300.0
  139. */
  140. float GetPosition();
  141. /** Read the temperature of the servo
  142. *
  143. * @returns float temperature
  144. */
  145. float GetTemp(void);
  146. /** Read the supply voltage of the servo
  147. *
  148. * @returns float voltage
  149. */
  150. float GetVolts(void);
  151. int read(int ID, int start, int length, char* data);
  152. int write(int ID, int start, int length, char* data, int flag=0);
  153. private :
  154. SerialHalfDuplex _ax12;
  155. int _ID;
  156. int _baud;
  157. };
  158. #endif