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.

getting_started.ino 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**************************************************************************/
  2. /*!
  3. @file getting_started.ino
  4. @author Moritz Walter
  5. @license GPLv3 (see license.txt)
  6. Demo of SPI configuration tool for Trinamic TMC2130 Motor Drivers
  7. @section HISTORY
  8. v0.1 - it works
  9. */
  10. /**************************************************************************/
  11. ////////////////////
  12. //
  13. // HOW TO USE
  14. //
  15. ////////////////////
  16. // This tool uses hardware SPI
  17. // Just wire the drivers to your SPI pins and define the CS pin below.
  18. // Once flashed, the sketch sends a basic configuration to the driver and makes the motor spin.
  19. #include <SPI.h>
  20. #include <Trinamic_TMC2130.h>
  21. // pin configuration (this is the X-axis on a standard RAMPS 1.4)
  22. #define CS_PIN 7
  23. #define EN_PIN 8 //enable (CFG6)
  24. #define DIR_PIN 9 //direction
  25. #define STEP_PIN 10 //step
  26. Trinamic_TMC2130 myStepper(CS_PIN);
  27. #define PRINT_REG(NAME, regval) \
  28. Serial.print(#NAME ": "); \
  29. myStepper.read_REG(NAME, &regval); \
  30. Serial.println(regval, BIN); \
  31. void print_regdump()
  32. {
  33. uint32_t regval;
  34. PRINT_REG(TMC_REG_GCONF, regval)
  35. PRINT_REG(TMC_REG_GSTAT, regval)
  36. PRINT_REG(TMC_REG_IOIN, regval)
  37. PRINT_REG(TMC_REG_TSTEP, regval)
  38. PRINT_REG(TMC_REG_XDIRECT, regval)
  39. PRINT_REG(TMC_REG_MSCNT, regval)
  40. PRINT_REG(TMC_REG_MSCURACT, regval)
  41. PRINT_REG(TMC_REG_CHOPCONF, regval)
  42. PRINT_REG(TMC_REG_DRV_STATUS, regval)
  43. PRINT_REG(TMC_REG_PWM_SCALE, regval)
  44. PRINT_REG(TMC_REG_LOST_STEPS, regval)
  45. }
  46. void setup(){
  47. Serial.begin(115200);
  48. Serial.println("Waiting for serial...");
  49. while (!Serial);
  50. Serial.println("Hello");
  51. // pins
  52. pinMode(EN_PIN, OUTPUT);
  53. pinMode(DIR_PIN, OUTPUT);
  54. pinMode(STEP_PIN, OUTPUT);
  55. digitalWrite(EN_PIN, HIGH); // disable driver
  56. digitalWrite(DIR_PIN, LOW); // chose direction
  57. digitalWrite(STEP_PIN, LOW); // no step yet
  58. digitalWrite(EN_PIN, LOW); // enable driver
  59. // stepper
  60. myStepper.init();
  61. uint32_t ioin;
  62. myStepper.read_REG(TMC_REG_IOIN, &ioin);
  63. if (ioin >> 24 != 0x11) {
  64. Serial.println("SPI Communication failed or incompatible firmware");
  65. while (true);
  66. }
  67. // verify connection
  68. uint32_t chopconf;
  69. myStepper.read_REG(TMC_REG_CHOPCONF, &chopconf);
  70. chopconf = chopconf ^ 0x05;
  71. myStepper.write_REG(TMC_REG_CHOPCONF, chopconf);
  72. uint32_t chopconf_r;
  73. myStepper.read_REG(TMC_REG_CHOPCONF, &chopconf_r);
  74. if (chopconf != chopconf_r) {
  75. Serial.println("Could not set CHOPCONF!!!");
  76. Serial.print("Expected ");
  77. Serial.print(chopconf);
  78. Serial.print(" Got ");
  79. Serial.print(chopconf_r);
  80. while (true);
  81. }
  82. chopconf = chopconf ^ 0x05;
  83. myStepper.write_REG(TMC_REG_CHOPCONF, chopconf);
  84. //myStepper.set_mres(64); // ({1,2,4,8,16,32,64,128,256}) number of microsteps
  85. //myStepper.set_IHOLD_IRUN(31,31,5); // ([0-31],[0-31],[0-5]) sets all currents to maximum
  86. //myStepper.set_I_scale_analog(1); // ({0,1}) 0: I_REF internal, 1: sets I_REF to AIN
  87. //myStepper.set_tbl(1); // ([0-3]) set comparator blank time to 16, 24, 36 or 54 clocks, 1 or 2 is recommended
  88. myStepper.set_mres(128);
  89. // myStepper.set_chm(2);
  90. // myStepper.set_toff(8); // ([0-15]) 0: driver disable, 1: use only with TBL>2, 2-15: off time setting during slow decay phase
  91. myStepper.set_hstrt(0);
  92. myStepper.set_hend(13);
  93. // configure motor current
  94. myStepper.set_IHOLD_IRUN(1,31,31);
  95. myStepper.set_I_scale_analog(1);
  96. myStepper.set_tbl(4);
  97. // configure dcStep mode
  98. myStepper.set_VDCMIN(1);
  99. myStepper.set_DCCTRL(50, 10);
  100. myStepper.set_vhighchm(1);
  101. myStepper.set_vhighfs(1);
  102. myStepper.set_sgt(127);
  103. myStepper.set_TCOOLTHRS(1);
  104. myStepper.set_TPWMTHRS(1);
  105. myStepper.set_toff(8); // ([0-15]) 0: driver disable, 1: use only with TBL>2, 2-15: off time setting during slow decay phase
  106. Serial.println("Finished Configuration: ");
  107. print_regdump();
  108. // get ready
  109. digitalWrite(EN_PIN, LOW); // enable driver
  110. }
  111. void loop(){
  112. // make a step
  113. digitalWrite(STEP_PIN, HIGH);
  114. delayMicroseconds(42);
  115. digitalWrite(STEP_PIN, LOW);
  116. delayMicroseconds(42);
  117. static int sometimes = 0;
  118. if (sometimes++ > 100) {
  119. sometimes = 0;
  120. while (!Serial);
  121. uint32_t drv_status = 0;
  122. myStepper.read_REG(TMC_REG_DRV_STATUS, &drv_status);
  123. uint32_t stallguard = drv_status & 0x1ff;
  124. uint32_t current = (drv_status >> 16) & 0x1f;
  125. Serial.print("Stallguard value: ");
  126. Serial.println(stallguard);
  127. Serial.print("Current: ");
  128. Serial.println(current);
  129. int32_t lost_steps;
  130. myStepper.read_REG(TMC_REG_LOST_STEPS, &lost_steps);
  131. static int32_t last_lost_steps = 0;
  132. Serial.print("Lost steps: ");
  133. Serial.println(lost_steps);
  134. const int STEP_THRESHOLD = 5;
  135. const int DIRCHANGE_HOLDOFF = 2;
  136. /*
  137. static int dirchange_timeout = DIRCHANGE_HOLDOFF;
  138. if ((lost_steps >= last_lost_steps + STEP_THRESHOLD) || (lost_steps <= last_lost_steps - STEP_THRESHOLD) ) {
  139. if (--dirchange_timeout < 0) {
  140. Serial.print("Lost Steps: ");
  141. Serial.println(lost_steps);
  142. Serial.print("Last Lost Steps: ");
  143. Serial.println(lost_steps);
  144. Serial.print("dirchange_timeout: ");
  145. Serial.println(dirchange_timeout);
  146. digitalWrite(DIR_PIN, !digitalRead(DIR_PIN));
  147. last_lost_steps = lost_steps;
  148. dirchange_timeout = DIRCHANGE_HOLDOFF;
  149. }
  150. }
  151. */
  152. }
  153. }