Motor.ino 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. unsigned int motor_counter = 0;
  21. unsigned char motor_moved = 0;
  22. int sgThreshold = 4;
  23. int sgFilter = 0;
  24. int direction = 1;
  25. unsigned int lower_SG_threshold = 0;
  26. unsigned int upper_SG_threshold = 0;
  27. unsigned char number_of_SG_readings=0;
  28. unsigned char current_increment_step_size=0;
  29. unsigned char lower_current_limit=0;
  30. char chopperMode = 0; //0 for spread, 1 for constant off
  31. char t_off = 2;
  32. char t_blank = 24;
  33. char h_start = 8;
  34. char h_end = 6;
  35. char h_decrement = 0;
  36. void startMotor() {
  37. Serial.println(F("Configuring stepper driver"));
  38. //char constant_off_time, char blank_time, char hysteresis_start, char hysteresis_end, char hysteresis_decrement
  39. tmc26XStepper.setSpreadCycleChopper(t_off,t_blank,h_start,h_end,h_decrement);
  40. tmc26XStepper.setRandomOffTime(0);
  41. tmc26XStepper.setMicrosteps(32);
  42. tmc26XStepper.setStallGuardThreshold(sgThreshold,sgFilter);
  43. // Serial.println("config finished, starting");
  44. digitalWrite(ENABLE_PIN,LOW);
  45. tmc26XStepper.start();
  46. tmc26XStepper.setSpeed(10);
  47. TCNT2=setupTimer2(10000);
  48. sei();
  49. }
  50. void runMotor() {
  51. if (running && !tmc26XStepper.isMoving()) {
  52. tmc26XStepper.step(direction*10000);
  53. Serial.println("run");
  54. }
  55. if (!running & tmc26XStepper.isMoving()) {
  56. tmc26XStepper.stop();
  57. Serial.println("stop");
  58. }
  59. }
  60. void setSpeed(unsigned int targetSpeed) {
  61. if (targetSpeed>0 && targetSpeed<MAX_SPEED) {
  62. Serial.print(F("Setting speed: "));
  63. Serial.println(targetSpeed);
  64. tmc26XStepper.setSpeed(targetSpeed);
  65. }
  66. else {
  67. Serial.print(F("improper speed "));
  68. Serial.println(targetSpeed);
  69. }
  70. }
  71. void setMicrostepping(int microstepping) {
  72. if (microstepping<1 || microstepping>256) {
  73. Serial.print(F("Improperd microstepping setting [1...256]: "));
  74. Serial.print(microstepping);
  75. }
  76. else {
  77. tmc26XStepper.setMicrosteps(microstepping);
  78. }
  79. }
  80. void setStallGuardThreshold(int threshold) {
  81. if (threshold<-64 || threshold > 63) {
  82. Serial.print(F("Improper Stall Guard Threshold [-64...63]: "));
  83. Serial.println(threshold);
  84. }
  85. else {
  86. sgThreshold = threshold;
  87. tmc26XStepper.setStallGuardThreshold(threshold,sgFilter);
  88. }
  89. }
  90. void setStallGuardFilter(int filter) {
  91. if (filter) {
  92. sgFilter=1;
  93. }
  94. else {
  95. sgFilter=0;
  96. }
  97. tmc26XStepper.setStallGuardThreshold(sgThreshold,sgFilter);
  98. }
  99. void setCurrent(int current) {
  100. if (current>0 && current <1700) {
  101. tmc26XStepper.setCurrent(current);
  102. }
  103. else {
  104. Serial.print(F("Improper current {0 ... 1200}: "));
  105. Serial.print(current);
  106. }
  107. }
  108. void updateChopper() {
  109. //we can do only spread now
  110. if (chopperMode==0) {
  111. tmc26XStepper.setSpreadCycleChopper(t_off,t_blank,h_start,h_end,h_decrement);
  112. }
  113. }
  114. void updateCoolStep() {
  115. tmc26XStepper.setCoolStepConfiguration(
  116. lower_SG_threshold, upper_SG_threshold, number_of_SG_readings,
  117. current_increment_step_size, lower_current_limit);
  118. }
  119. //from http://www.uchobby.com/index.php/2007/11/24/arduino-interrupts/
  120. //Setup Timer2.s
  121. //Configures the ATMega168 8-Bit Timer2 to generate an interrupt
  122. //at the specified frequency.
  123. //Returns the timer load value which must be loaded into TCNT2
  124. //inside your ISR routine.
  125. //See the example usage below.
  126. unsigned char setupTimer2(float timeoutFrequency){
  127. unsigned char result; //The timer load value.
  128. //Calculate the timer load value
  129. result=(int)((257.0-(TIMER_CLOCK_FREQ/timeoutFrequency))+0.5);
  130. //The 257 really should be 256 but I get better results with 257.
  131. //Timer2 Settings: Timer Prescaler /8, mode 0
  132. //Timer clock = 16MHz/8 = 2Mhz or 0.5us
  133. //The /8 prescale gives us a good range to work with
  134. //so we just hard code this for now.
  135. TCCR2A = 0;
  136. TCCR2B = 0<<CS22 | 1<<CS21 | 0<<CS20;
  137. //Timer2 Overflow Interrupt Enable
  138. TIMSK2 = 1<<TOIE2;
  139. //load the timer for its first cycle
  140. TCNT2=result;
  141. return(result);
  142. }
  143. ISR(TIMER2_OVF_vect) {
  144. motor_moved = tmc26XStepper.move();
  145. motor_counter++;
  146. }