Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

318 lines
8.9KB

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