Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

273 lines
7.3KB

  1. // Simple test of USB Host Mouse/Keyboard
  2. //
  3. // This example is in the public domain
  4. #include "USBHost_t36.h"
  5. #define USBBAUD 115200
  6. USBHost myusb;
  7. USBHub hub1(myusb);
  8. USBHub hub2(myusb);
  9. USBHIDParser hid1(myusb);
  10. USBHIDParser hid2(myusb);
  11. USBHIDParser hid3(myusb);
  12. USBSerial userial(myusb);
  13. USBDriver *drivers[] = {&hub1, &hub2, &hid1, &hid2, &hid3, &userial};
  14. #define CNT_DEVICES (sizeof(drivers)/sizeof(drivers[0]))
  15. const char * driver_names[CNT_DEVICES] = {"Hub1", "Hub2", "HID1", "HID2", "HID3", "USERIAL1" };
  16. bool driver_active[CNT_DEVICES] = {false, false, false, false};
  17. void setup()
  18. {
  19. pinMode(13, OUTPUT);
  20. pinMode(2, OUTPUT);
  21. pinMode(3, OUTPUT);
  22. for (int i = 0; i < 5; i++) {
  23. digitalWrite(2, HIGH);
  24. delayMicroseconds(50);
  25. digitalWrite(2, LOW);
  26. delayMicroseconds(50);
  27. }
  28. while (!Serial && (millis() < 5000)) ; // wait for Arduino Serial Monitor
  29. Serial.println("\n\nUSB Host Testing - Serial");
  30. myusb.begin();
  31. Serial1.begin(115200); // We will echo stuff Through Serial1...
  32. }
  33. void loop()
  34. {
  35. digitalWrite(13, !digitalRead(13));
  36. myusb.Task();
  37. // Print out information about different devices.
  38. for (uint8_t i = 0; i < CNT_DEVICES; i++) {
  39. if (*drivers[i] != driver_active[i]) {
  40. if (driver_active[i]) {
  41. Serial.printf("*** Device %s - disconnected ***\n", driver_names[i]);
  42. driver_active[i] = false;
  43. } else {
  44. Serial.printf("*** Device %s %x:%x - connected ***\n", driver_names[i], drivers[i]->idVendor(), drivers[i]->idProduct());
  45. driver_active[i] = true;
  46. const uint8_t *psz = drivers[i]->manufacturer();
  47. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  48. psz = drivers[i]->product();
  49. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  50. psz = drivers[i]->serialNumber();
  51. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  52. // If this is a new Serial device.
  53. if (drivers[i] == &userial) {
  54. // Lets try first outputting something to our USerial to see if it will go out...
  55. userial.begin(USBBAUD);
  56. // delay(5);
  57. // userial.println("ver");
  58. #if 0
  59. userial.println("abcdefghijklmnopqrstuvwxyz");
  60. userial.println("ABCDEFGHIJKLMNOPQURSTUVWYZ");
  61. userial.flush(); // force it out now.
  62. userial.println("0123456789");
  63. userial.flush();
  64. delay(2);
  65. userial.println("abcdefghijklmnopqrstuvwxyz");
  66. userial.println("ABCDEFGHIJKLMNOPQURSTUVWYZ");
  67. delay(2);
  68. userial.println("!@#$%^&*()");
  69. userial.flush();
  70. #endif
  71. }
  72. }
  73. }
  74. }
  75. if (Serial.available()) {
  76. Serial.println("Serial Available");
  77. while (Serial.available()) {
  78. int ch = Serial.read();
  79. if (ch == '$') {
  80. BioloidTest();
  81. while (Serial.read() != -1);
  82. }
  83. else userial.write(ch);
  84. }
  85. }
  86. while (Serial1.available()) {
  87. // Serial.println("Serial1 Available");
  88. Serial1.write(Serial1.read());
  89. }
  90. while (userial.available()) {
  91. // Serial.println("USerial Available");
  92. Serial.write(userial.read());
  93. }
  94. }
  95. //#define ID_MASTER 200
  96. #define ID_MASTER 0xfd
  97. // Extract stuff from Bioloid library..
  98. #define AX12_BUFFER_SIZE 128
  99. #define COUNTER_TIMEOUT 12000
  100. /** Instruction Set **/
  101. #define AX_PING 1
  102. #define AX_READ_DATA 2
  103. #define AX_WRITE_DATA 3
  104. #define AX_REG_WRITE 4
  105. #define AX_ACTION 5
  106. #define AX_RESET 6
  107. #define AX_SYNC_WRITE 131
  108. #define AX_TORQUE_ENABLE 24
  109. #define AX_LED 25
  110. #define AX_CW_COMPLIANCE_MARGIN 26
  111. #define AX_CCW_COMPLIANCE_MARGIN 27
  112. #define AX_CW_COMPLIANCE_SLOPE 28
  113. #define AX_CCW_COMPLIANCE_SLOPE 29
  114. #define AX_GOAL_POSITION_L 30
  115. #define AX_GOAL_POSITION_H 31
  116. #define AX_GOAL_SPEED_L 32
  117. #define AX_GOAL_SPEED_H 33
  118. #define AX_TORQUE_LIMIT_L 34
  119. #define AX_TORQUE_LIMIT_H 35
  120. #define AX_PRESENT_POSITION_L 36
  121. #define AX_PRESENT_POSITION_H 37
  122. void BioloidTest() {
  123. uint8_t master_id = 200;
  124. Serial.println("\n*** Bioloid Test ***");
  125. if (ax12GetRegister(master_id, 0, 1) != -1) {
  126. Serial.println("Controller found at 200");
  127. } else {
  128. Serial.println("Controller not at 200 try 0xfd");
  129. master_id = 0xfd;
  130. if (ax12GetRegister(master_id, 0, 1) != -1) {
  131. Serial.println("Controller found at 0xfd");
  132. } else {
  133. Serial.println("Controller not found");
  134. }
  135. }
  136. for (uint8_t reg = 0; reg < 10; reg++) {
  137. myusb.Task();
  138. Serial.print(ax12GetRegister(master_id, reg, 1), HEX);
  139. Serial.print(" ");
  140. }
  141. Serial.println();
  142. // Now assuming we found controller...
  143. // May need to turn on power on controller
  144. ax12SetRegister(master_id, AX_TORQUE_ENABLE, 1);
  145. delay(2);
  146. // Lets see if we can get the current position for any servo
  147. for (int i = 0; i < 254; i++) {
  148. int servo_pos = ax12GetRegister(i, AX_PRESENT_POSITION_L, 2);
  149. if (servo_pos != -1) {
  150. Serial.printf("Servo: %d Pos: %d\n", i, servo_pos);
  151. }
  152. }
  153. }
  154. unsigned char ax_rx_buffer[AX12_BUFFER_SIZE];
  155. int ax12GetRegister(int id, int regstart, int length) {
  156. // 0xFF 0xFF ID LENGTH INSTRUCTION PARAM... CHECKSUM
  157. int return_value;
  158. digitalWriteFast(2, HIGH);
  159. int checksum = ~((id + 6 + regstart + length) % 256);
  160. userial.write(0xFF);
  161. userial.write(0xFF);
  162. userial.write(id);
  163. userial.write(4); // length
  164. userial.write(AX_READ_DATA);
  165. userial.write(regstart);
  166. userial.write(length);
  167. userial.write(checksum);
  168. userial.flush(); // make sure the data goes out.
  169. if (ax12ReadPacket(length + 6) > 0) {
  170. // ax12Error = ax_rx_buffer[4];
  171. if (length == 1)
  172. return_value = ax_rx_buffer[5];
  173. else
  174. return_value = ax_rx_buffer[5] + (ax_rx_buffer[6] << 8);
  175. } else {
  176. digitalWriteFast(3, !digitalReadFast(3));
  177. return_value = -1;
  178. }
  179. digitalWriteFast(2, LOW);
  180. return return_value;
  181. }
  182. void ax12SetRegister(int id, int regstart, int data){
  183. int checksum = ~((id + 4 + AX_WRITE_DATA + regstart + (data&0xff)) % 256);
  184. userial.write(0xFF);
  185. userial.write(0xFF);
  186. userial.write(id);
  187. userial.write(4); // length
  188. userial.write(AX_WRITE_DATA);
  189. userial.write(regstart);
  190. userial.write(data&0xff);
  191. // checksum =
  192. userial.write(checksum);
  193. userial.flush();
  194. //ax12ReadPacket();
  195. }
  196. int ax12ReadPacket(int length) {
  197. unsigned long ulCounter;
  198. unsigned char offset, checksum;
  199. unsigned char *psz;
  200. unsigned char *pszEnd;
  201. int ch;
  202. offset = 0;
  203. psz = ax_rx_buffer;
  204. pszEnd = &ax_rx_buffer[length];
  205. while (userial.read() != -1) ;
  206. uint32_t ulStart = millis();
  207. // Need to wait for a character or a timeout...
  208. do {
  209. ulCounter = COUNTER_TIMEOUT;
  210. while ((ch = userial.read()) == -1) {
  211. if ((millis() - ulStart) > 10) {
  212. //if (!--ulCounter) {
  213. // Serial.println("Timeout");
  214. return 0; // Timeout
  215. }
  216. }
  217. } while (ch != 0xff) ;
  218. *psz++ = 0xff;
  219. while (psz != pszEnd) {
  220. ulCounter = COUNTER_TIMEOUT;
  221. while ((ch = userial.read()) == -1) {
  222. //Serial.printf("Read ch: %x\n", ch);
  223. if (!--ulCounter) {
  224. return 0; // Timeout
  225. }
  226. }
  227. *psz++ = (unsigned char)ch;
  228. }
  229. checksum = 0;
  230. for (offset = 2; offset < length; offset++)
  231. checksum += ax_rx_buffer[offset];
  232. if (checksum != 255) {
  233. return 0;
  234. } else {
  235. return 1;
  236. }
  237. }