stepper.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. int speedLimit[2] = { 50 , 1000 } ;// sets an absolute minimum/maximum speed limit
  22. int accLimit[2] = { 100, 2000} ;
  23. long int moveTo_Stepper(long int stepto ) {
  24. stepper.moveTo( constrain( stepto , originPos * stepToCm, maxPos * stepToCm ) );
  25. return stepper.distanceToGo();
  26. }
  27. float moveToCm_Stepper(float cmto ) {
  28. stepper.moveTo(constrain( cmto * stepToCm , originPos, maxPos ));
  29. return stepper.distanceToGo()/stepToCm;
  30. }
  31. long int move_Stepper(long int stepRelative ) {
  32. stepper.move(stepRelative);
  33. return stepper.distanceToGo();
  34. }
  35. float moveCm_Stepper(float cmRelative ) {
  36. stepper.move(cmRelative * stepToCm );
  37. return stepper.distanceToGo()/stepToCm;
  38. }
  39. long int maxSpeed_Stepper(long int maxspeed){
  40. stepper.setMaxSpeed( constrain (maxspeed, speedLimit[0], speedLimit[1] ) );
  41. return stepper.maxSpeed();
  42. }
  43. long int acceleration_Stepper( long int acc ){
  44. stepper.setAcceleration(acc + 1); // prevent 0 step/s²
  45. return acc;
  46. }
  47. //////////////// CALIBRATION ////////////////////////
  48. float init_Stepper(){
  49. //run at constant speed until hitting switchLow
  50. stepper.setSpeed(-1*speedLimit[1]);
  51. while(digitalRead(switchLow)) {
  52. stepper.runSpeed();
  53. }
  54. stepper.stop();
  55. //this is the new 0 step
  56. stepper.setCurrentPosition(0);
  57. delay(1000); //take a breath
  58. //run at constant speed until hitting switchHigh
  59. stepper.setSpeed(speedLimit[1]);
  60. while(digitalRead(switchHigh)) {
  61. stepper.runSpeed();
  62. }
  63. stepper.stop();
  64. maxPos = stepper.currentPosition() / stepToCm ;
  65. delay(1000);
  66. stepper.runToNewPosition(maxPos * stepToCm / 2) ;
  67. return maxPos ;
  68. }
  69. ///////////////////// SETUP ///////////////////////////
  70. void setup_Stepper(){
  71. pinMode (switchLow, INPUT_PULLUP);
  72. pinMode (switchHigh, INPUT_PULLUP);
  73. stepper.setMaxSpeed(speedLimit[1]);
  74. //stepper.setSpeed(10000);
  75. stepper.setAcceleration(accLimit[1]);
  76. }
  77. ///////////////////// LOOP ///////////////////////////
  78. bool update_Stepper() {
  79. bool updated = 1 ;
  80. stepper.run();
  81. if (!digitalRead(switchLow)) {
  82. stepper.setCurrentPosition(0);
  83. updated=0;
  84. }
  85. if (!digitalRead(switchHigh)) {
  86. stepper.setCurrentPosition(maxPos * stepToCm);
  87. updated=0;
  88. }
  89. return updated ;
  90. }