stepper.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. const int NUM_OF_STEPPERS = 2 ;
  19. AccelStepper steppers[NUM_OF_STEPPERS] = {AccelStepper(1, 8, 7), AccelStepper(1, 10, 9)}; ;
  20. // Mechanical values
  21. long int STEPS_PER_TURN = 200 ; // stepper motor
  22. int MICROSTEPPING = 8 ; //stepper driver
  23. int REDUCTION_RATIO = 70 ; // mechanical reduction ration to real camera rotation
  24. // microswitch pins
  25. int switchLow = A5;
  26. int driveMode ; // global stepper driving mode : default position with acceleration, 1 position with constant speed, 2 constant speed
  27. long int speedLimit[2] = { 50 , 10000000 } ;// sets an absolute minimum/maximum speed limit
  28. long int accLimit[2] = { 100, 200000} ;
  29. //////////// UTILITIES ///////////////
  30. long int camRot2steps (float camRot ) {
  31. return (long int) STEPS_PER_TURN * MICROSTEPPING * REDUCTION_RATIO *camRot ;
  32. }
  33. //////////// ACCELSTEPPER /////////////
  34. void stop_Steppers (void) {
  35. for(int i ; i < NUM_OF_STEPPERS ; i++ ) {
  36. steppers[i].stop();
  37. }
  38. }
  39. long int moveTo_Stepper(int stepperIndex, long int stepto ) {
  40. steppers[stepperIndex].moveTo(stepto);
  41. // while (!steppers[stepperIndex].distanceToGo()==0)
  42. // steppers[stepperIndex].run();
  43. // steppers[stepperIndex].moveTo( constrain( stepto , originPos * stepToCm, maxPos * stepToCm ) );
  44. return steppers[stepperIndex].distanceToGo();
  45. }
  46. //float moveToCm_Stepper(float cmto ) {
  47. // stepper.moveTo(constrain( cmto * stepToCm , originPos, maxPos ));
  48. // return stepper.distanceToGo()/stepToCm;
  49. //}
  50. //
  51. long int move_Stepper(int stepperIndex, long int stepRelative ) {
  52. steppers[stepperIndex].move(stepRelative);
  53. return steppers[stepperIndex].distanceToGo();
  54. }
  55. //float moveCm_Stepper(float cmRelative ) {
  56. // stepper.move(cmRelative * stepToCm );
  57. // return stepper.distanceToGo()/stepToCm;
  58. //}
  59. //
  60. long int maxSpeed_Stepper(int stepperIndex, long int maxspeed){
  61. steppers[stepperIndex].setMaxSpeed( constrain (maxspeed, speedLimit[0], speedLimit[1] ) );
  62. return steppers[stepperIndex].maxSpeed();
  63. }
  64. long int setSpeed_Stepper(int stepperIndex, long int maxspeed){
  65. steppers[stepperIndex].setSpeed( constrain (maxspeed, speedLimit[0], speedLimit[1] ) );
  66. return steppers[stepperIndex].maxSpeed();
  67. }
  68. long int acceleration_Stepper(int stepperIndex, long int acc ){
  69. steppers[stepperIndex].setAcceleration(acc + 1); // prevent 0 step/s²
  70. return acc;
  71. }
  72. //////////////// CALIBRATION ////////////////////////
  73. float init_steppers(){
  74. // //run at constant speed until hitting switchLow
  75. // stepper.setSpeed(-1*speedLimit[1]);
  76. // while(digitalRead(switchLow)) {
  77. // stepper.runSpeed();
  78. // }
  79. // stepper.stop();
  80. // //this is the new 0 step
  81. // stepper.setCurrentPosition(0);
  82. //
  83. //
  84. //
  85. // delay(1000); //take a breath
  86. //
  87. // //run at constant speed until hitting switchHigh
  88. // stepper.setSpeed(speedLimit[1]);
  89. // while(digitalRead(switchHigh)) {
  90. // stepper.runSpeed();
  91. // }
  92. // stepper.stop();
  93. // maxPos = stepper.currentPosition() / stepToCm ;
  94. //
  95. // delay(1000);
  96. //
  97. // stepper.runToNewPosition(maxPos * stepToCm / 2) ;
  98. // return maxPos ;
  99. }
  100. ///////////////////// SETUP ///////////////////////////
  101. void setup_steppers(){
  102. pinMode (switchLow, INPUT_PULLUP);
  103. // pinMode (switchHigh, INPUT_PULLUP);
  104. // stepper.moveTo(rand() % 200000);
  105. for(int i ; i < NUM_OF_STEPPERS ; i++ ) {
  106. steppers[i].setMaxSpeed( 20000);
  107. steppers[i].setSpeed(10000);
  108. }
  109. // while (!stepper.distanceToGo()==0)
  110. // stepper.run();
  111. // stepper.moveTo(0);
  112. // while (!stepper.distanceToGo()==0)
  113. // stepper.run();
  114. // stepper.setMaxSpeed(speedLimit[1]);
  115. // //stepper.setSpeed(10000);
  116. // stepper.setAcceleration(accLimit[1]);
  117. }
  118. ///////////////////// LOOP ///////////////////////////
  119. bool update_steppers() {
  120. for(int i ; i < NUM_OF_STEPPERS ; i++ ) {
  121. switch (driveMode) {
  122. case 1:
  123. steppers[i].runSpeedToPosition();
  124. break ;
  125. case 2:
  126. steppers[i].runSpeed();
  127. break ;
  128. default :
  129. steppers[i].run();
  130. }
  131. }
  132. // bool updated = 1 ;
  133. // if (!digitalRead(switchLow)) {
  134. // stepper.setCurrentPosition(0);
  135. // updated=0;
  136. // }
  137. // if (!digitalRead(switchHigh)) {
  138. // stepper.setCurrentPosition(maxPos * stepToCm);
  139. // updated=0;
  140. // }
  141. // return updated ;
  142. }