Serial.ino 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. TMC26XMotorTest.ino - - TMC26X Stepper library tester for Wiring/Arduino
  3. Copyright (c) 2011, Interactive Matter, Marcus Nowotny
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #define INPUT_BUFFER_LENGTH 32
  21. #define SERIAL_SPEED 115200
  22. #define STATUS_COUNTER 100
  23. char inputBuffer[INPUT_BUFFER_LENGTH+1]; //ad on character to keep the trainling 0
  24. unsigned char inputBufferPosition;
  25. void startSerial() {
  26. Serial.begin(SERIAL_SPEED);
  27. Serial.println(F("================================="));
  28. Serial.println(F("TMC26X Stepper Driver Motor Tester"));
  29. Serial.println(F("================================="));
  30. //empty the input buffer
  31. for (unsigned char i=0; i< INPUT_BUFFER_LENGTH+1; i++) {
  32. inputBuffer[i]=0;
  33. }
  34. inputBufferPosition=0;
  35. }
  36. void loopSerial() {
  37. if (Serial.available()>0 && inputBufferPosition<INPUT_BUFFER_LENGTH) {
  38. char c = Serial.read();
  39. //Read the char
  40. inputBuffer[inputBufferPosition]=c;
  41. inputBufferPosition++;
  42. //always terminate the string
  43. inputBuffer[inputBufferPosition]=0;
  44. //and if the line ended we execute the command
  45. if (c=='\n') {
  46. executeSerialCommand();
  47. }
  48. }
  49. if (motor_moved) {
  50. Serial.print("#sg");
  51. Serial.print(tmc26XStepper.getCurrentStallGuardReading(),DEC);
  52. Serial.print(",p");
  53. Serial.print(tmc26XStepper.getMotorPosition(),DEC);
  54. Serial.print(",k");
  55. Serial.print(tmc26XStepper.getCurrentCurrent(),DEC);
  56. Serial.println(',');
  57. motor_moved=0;
  58. }
  59. if (motor_counter>STATUS_COUNTER) {
  60. motor_counter=0;
  61. char moving = tmc26XStepper.isMoving();
  62. Serial.print('#');
  63. if (moving) {
  64. Serial.print('r');
  65. }
  66. else {
  67. Serial.print('s');
  68. }
  69. Serial.print(',');
  70. Serial.print('d');
  71. Serial.print(direction);
  72. Serial.print(',');
  73. Serial.print("c");
  74. Serial.print(tmc26XStepper.getCurrent(),DEC);
  75. Serial.print(',');
  76. Serial.print('S');
  77. Serial.print(tmc26XStepper.getSpeed(),DEC);
  78. Serial.print(',');
  79. Serial.print('m');
  80. Serial.print(tmc26XStepper.getMicrosteps(),DEC);
  81. Serial.print(',');
  82. Serial.print("t");
  83. Serial.print(tmc26XStepper.getStallGuardThreshold(),DEC);
  84. Serial.print(',');
  85. Serial.print('f');
  86. Serial.print(tmc26XStepper.getStallGuardFilter(),DEC);
  87. Serial.print(',');
  88. //print out the general cool step config
  89. if (tmc26XStepper.isCoolStepEnabled()) {
  90. Serial.print("Ke+,");
  91. }
  92. else {
  93. Serial.print("Ke-,");
  94. }
  95. Serial.print("Kl");
  96. Serial.print(tmc26XStepper.getCoolStepLowerSgThreshold(),DEC);
  97. Serial.print(",Ku");
  98. Serial.print(tmc26XStepper.getCoolStepUpperSgThreshold(),DEC);
  99. Serial.print(",Kn");
  100. Serial.print(tmc26XStepper.getCoolStepNumberOfSGReadings(),DEC);
  101. Serial.print(",Ki");
  102. Serial.print(tmc26XStepper.getCoolStepCurrentIncrementSize(),DEC);
  103. Serial.print(",Km");
  104. Serial.print(tmc26XStepper.getCoolStepLowerCurrentLimit(),DEC);
  105. Serial.print(',');
  106. //detect the winding status
  107. if (tmc26XStepper.isOpenLoadA()) {
  108. Serial.print("ao,");
  109. }
  110. else if(tmc26XStepper.isShortToGroundA()) {
  111. Serial.print("ag,");
  112. }
  113. else {
  114. Serial.print("a-,");
  115. }
  116. //detect the winding status
  117. if (tmc26XStepper.isOpenLoadB()) {
  118. Serial.print("bo,");
  119. }
  120. else if(tmc26XStepper.isShortToGroundB()) {
  121. Serial.print("bg,");
  122. }
  123. else {
  124. Serial.print("b-,");
  125. }
  126. char temperature = tmc26XStepper.getOverTemperature();
  127. if (temperature==0) {
  128. Serial.print("x-,");
  129. }
  130. else if (temperature==TMC26X_OVERTEMPERATURE_PREWARING) {
  131. Serial.print("xw,");
  132. }
  133. else {
  134. Serial.print("xe,");
  135. }
  136. if (tmc26XStepper.isEnabled()) {
  137. Serial.print("e1,");
  138. }
  139. else {
  140. Serial.print("e0,");
  141. }
  142. //write out the current chopper config
  143. Serial.print("Cm");
  144. Serial.print(chopperMode,DEC);
  145. Serial.print(",Co");
  146. Serial.print(t_off,DEC);
  147. Serial.print(",Cb");
  148. Serial.print(t_blank,DEC);
  149. if (chopperMode==0) {
  150. Serial.print(",Cs");
  151. Serial.print(h_start,DEC);
  152. Serial.print(",Ce");
  153. Serial.print(h_end,DEC);
  154. Serial.print(",Cd");
  155. Serial.print(h_decrement,DEC);
  156. }
  157. Serial.print(',');
  158. Serial.println();
  159. }
  160. }
  161. void executeSerialCommand() {
  162. Serial.print("Executing ");
  163. Serial.println(inputBuffer);
  164. //stimple runn & stop commands
  165. switch(inputBuffer[0]) {
  166. case 's':
  167. running = 0;
  168. break;
  169. case 'r':
  170. running = -1;
  171. break;
  172. case 'S':
  173. {
  174. int targetSpeed = decode(1);
  175. setSpeed(targetSpeed);
  176. }
  177. break;
  178. case 'm':
  179. {
  180. int microstepping = decode(1);
  181. setMicrostepping(microstepping);
  182. }
  183. break;
  184. case 't':
  185. {
  186. int threshold = decode(1);
  187. setStallGuardThreshold(threshold);
  188. }
  189. break;
  190. case 'f':
  191. {
  192. int filter = decode(1);
  193. setStallGuardFilter(filter);
  194. }
  195. break;
  196. case 'd':
  197. {
  198. int value = decode(1);
  199. tmc26XStepper.stop();
  200. if (value<0) {
  201. direction=-1;
  202. }
  203. else {
  204. direction=1;
  205. }
  206. }
  207. break;
  208. case 'c':
  209. {
  210. int current = decode(1);
  211. setCurrent(current);
  212. }
  213. break;
  214. case 'e':
  215. {
  216. int enabled = decode(1);
  217. if (enabled) {
  218. tmc26XStepper.setEnabled(true);
  219. }
  220. else {
  221. tmc26XStepper.setEnabled(false);
  222. }
  223. }
  224. break;
  225. case 'C':
  226. switch(inputBuffer[1]) {
  227. case 'o':
  228. {
  229. int value = decode(2);
  230. if (value>0 && value<16) {
  231. t_off=value;
  232. updateChopper();
  233. }
  234. }
  235. break;
  236. case 'b':
  237. {
  238. int value = decode(2);
  239. if (value>=0 && value<=3) {
  240. t_blank=value;
  241. updateChopper();
  242. }
  243. }
  244. break;
  245. case 's':
  246. {
  247. int value = decode(2);
  248. if (value>=0 && value<=8) {
  249. h_start=value;
  250. updateChopper();
  251. }
  252. }
  253. break;
  254. case 'e':
  255. {
  256. int value = decode(2);
  257. if (value>=-3 && value<=12) {
  258. h_end=value;
  259. updateChopper();
  260. }
  261. }
  262. break;
  263. case 'd':
  264. {
  265. int value = decode(2);
  266. if (value>=0 && value<=3) {
  267. h_decrement=value;
  268. updateChopper();
  269. }
  270. }
  271. break;
  272. }
  273. break;
  274. case 'K':
  275. switch(inputBuffer[1]) {
  276. case '+':
  277. tmc26XStepper.setCoolStepEnabled(true);
  278. break;
  279. case '-':
  280. tmc26XStepper.setCoolStepEnabled(false);
  281. break;
  282. case 'l':
  283. {
  284. int value = decode(2);
  285. if (value>=0 && value<480) {
  286. lower_SG_threshold=value;
  287. updateCoolStep();
  288. }
  289. }
  290. break;
  291. case 'u':
  292. {
  293. int value = decode(2);
  294. if (value>=0 && value<480) {
  295. upper_SG_threshold=value;
  296. updateCoolStep();
  297. }
  298. }
  299. break;
  300. case 'n':
  301. {
  302. int value = decode(2);
  303. if (value>=0 && value<4) {
  304. number_of_SG_readings=value;
  305. updateCoolStep();
  306. }
  307. }
  308. break;
  309. case 'i':
  310. {
  311. int value = decode(2);
  312. if (value>=0 && value<4) {
  313. current_increment_step_size=value;
  314. updateCoolStep();
  315. }
  316. }
  317. break;
  318. case 'm':
  319. {
  320. int value = decode(2);
  321. if (value>=0 && value<2) {
  322. lower_current_limit=value;
  323. updateCoolStep();
  324. }
  325. }
  326. break;
  327. }
  328. break;
  329. }
  330. //at the end delete buffer
  331. inputBufferPosition=0;
  332. inputBuffer[0]=0;
  333. }
  334. int decode(unsigned char startPosition) {
  335. int result=0;
  336. boolean negative = false;
  337. if (inputBuffer[startPosition]=='-') {
  338. negative=true;
  339. startPosition++;
  340. }
  341. for (unsigned char i=startPosition; i< (INPUT_BUFFER_LENGTH+1) && inputBuffer[i]!=0; i++) {
  342. char number = inputBuffer[i];
  343. //this very dumb approac can lead to errors, but we expect only numbers after the command anyway
  344. if (number <= '9' && number >='0') {
  345. result *= 10;
  346. result += number - '0';
  347. }
  348. }
  349. if (negative) {
  350. return -result;
  351. }
  352. else {
  353. return result;
  354. }
  355. }