stepper.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <AccelStepper.h>
  2. /*
  3. * stepper control through big easy driver
  4. * positions are expressed in centimeters
  5. * originPos is near the motor, where switchLow gets LOW
  6. * maxPos is the maximum position, where switchHigh gets LOW
  7. * idlePos is the idle position
  8. * lowOffset
  9. * highOffset are offsets from originPos/maxPos respectively to define the running zone
  10. *
  11. * Stepper driven through big easy driver
  12. * configured for 1/4 microstepping (mover micro-steps gives low speed)
  13. * 800 steps is one rotation
  14. * one rotation is 1.6cm translation
  15. * cm to steps ratio is 500
  16. */
  17. // Define a stepper and the pins it will use
  18. AccelStepper stepper(1, 8, 7);
  19. // microswitch pins
  20. int switchLow = A5;
  21. long int speedLimit[2] = { 50 , 10000000 } ;// sets an absolute minimum/maximum speed limit
  22. long int accLimit[2] = { 100, 200000} ;
  23. long int moveTo_Stepper(long int stepto ) {
  24. stepper.moveTo(stepto);
  25. // while (!stepper.distanceToGo()==0)
  26. // stepper.run();
  27. // stepper.moveTo( constrain( stepto , originPos * stepToCm, maxPos * stepToCm ) );
  28. return stepper.distanceToGo();
  29. }
  30. //float moveToCm_Stepper(float cmto ) {
  31. // stepper.moveTo(constrain( cmto * stepToCm , originPos, maxPos ));
  32. // return stepper.distanceToGo()/stepToCm;
  33. //}
  34. //
  35. long int move_Stepper(long int stepRelative ) {
  36. stepper.move(stepRelative);
  37. return stepper.distanceToGo();
  38. }
  39. //float moveCm_Stepper(float cmRelative ) {
  40. // stepper.move(cmRelative * stepToCm );
  41. // return stepper.distanceToGo()/stepToCm;
  42. //}
  43. //
  44. long int maxSpeed_Stepper(long int maxspeed){
  45. stepper.setMaxSpeed( constrain (maxspeed, speedLimit[0], speedLimit[1] ) );
  46. return stepper.maxSpeed();
  47. }
  48. long int setSpeed_Stepper(long int maxspeed){
  49. stepper.setSpeed( constrain (maxspeed, speedLimit[0], speedLimit[1] ) );
  50. return stepper.maxSpeed();
  51. }
  52. long int acceleration_Stepper( long int acc ){
  53. stepper.setAcceleration(acc + 1); // prevent 0 step/s²
  54. return acc;
  55. }
  56. //////////////// CALIBRATION ////////////////////////
  57. float init_Stepper(){
  58. // //run at constant speed until hitting switchLow
  59. // stepper.setSpeed(-1*speedLimit[1]);
  60. // while(digitalRead(switchLow)) {
  61. // stepper.runSpeed();
  62. // }
  63. // stepper.stop();
  64. // //this is the new 0 step
  65. // stepper.setCurrentPosition(0);
  66. //
  67. //
  68. //
  69. // delay(1000); //take a breath
  70. //
  71. // //run at constant speed until hitting switchHigh
  72. // stepper.setSpeed(speedLimit[1]);
  73. // while(digitalRead(switchHigh)) {
  74. // stepper.runSpeed();
  75. // }
  76. // stepper.stop();
  77. // maxPos = stepper.currentPosition() / stepToCm ;
  78. //
  79. // delay(1000);
  80. //
  81. // stepper.runToNewPosition(maxPos * stepToCm / 2) ;
  82. // return maxPos ;
  83. }
  84. ///////////////////// SETUP ///////////////////////////
  85. void setup_Stepper(){
  86. pinMode (switchLow, INPUT_PULLUP);
  87. // pinMode (switchHigh, INPUT_PULLUP);
  88. // stepper.moveTo(rand() % 200000);
  89. stepper.setMaxSpeed( 20000);
  90. stepper.setSpeed(10000);
  91. // while (!stepper.distanceToGo()==0)
  92. // stepper.run();
  93. // stepper.moveTo(0);
  94. // while (!stepper.distanceToGo()==0)
  95. // stepper.run();
  96. // stepper.setMaxSpeed(speedLimit[1]);
  97. // //stepper.setSpeed(10000);
  98. // stepper.setAcceleration(accLimit[1]);
  99. }
  100. ///////////////////// LOOP ///////////////////////////
  101. bool update_Stepper() {
  102. // bool updated = 1 ;
  103. stepper.run();
  104. // if (!digitalRead(switchLow)) {
  105. // stepper.setCurrentPosition(0);
  106. // updated=0;
  107. // }
  108. // if (!digitalRead(switchHigh)) {
  109. // stepper.setCurrentPosition(maxPos * stepToCm);
  110. // updated=0;
  111. // }
  112. // return updated ;
  113. }