stepper.h 5.1 KB

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