stepper.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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[NUM_OF_STEPPERS] = { 2, 3 } ;
  26. volatile 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. // switch is attached to an interrupt so checking pins is done elsewhere
  77. while( homing[0] + homing[1] != 2 ){
  78. for (int i; i++; i<NUM_OF_STEPPERS) {
  79. if (!homing[i]) {
  80. steppers[i].runSpeed() ;
  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. // interrupt routine
  90. void home_switch () {
  91. for (int i; i++; i<NUM_OF_STEPPERS) {
  92. homing[i] = digitalRead(homing_Pins[i]) ;
  93. }
  94. }
  95. ///////////////////// SETUP ///////////////////////////
  96. void setup_steppers(){
  97. //setup homing pins
  98. for (int i; i++; i<NUM_OF_STEPPERS) {
  99. pinMode(homing_Pins[i], INPUT_PULLUP);
  100. attachInterrupt(digitalPinToInterrupt(homing_Pins[i]), home_switch, CHANGE);
  101. }
  102. // stepper.moveTo(rand() % 200000);
  103. for(int i ; i < NUM_OF_STEPPERS ; i++ ) {
  104. steppers[i].setMaxSpeed( 20000);
  105. steppers[i].setSpeed(10000);
  106. }
  107. // while (!stepper.distanceToGo()==0)
  108. // stepper.run();
  109. // stepper.moveTo(0);
  110. // while (!stepper.distanceToGo()==0)
  111. // stepper.run();
  112. // stepper.setMaxSpeed(speedLimit[1]);
  113. // //stepper.setSpeed(10000);
  114. // stepper.setAcceleration(accLimit[1]);
  115. }
  116. ///////////////////// LOOP ///////////////////////////
  117. bool update_steppers() {
  118. for(int i ; i < NUM_OF_STEPPERS ; i++ ) {
  119. switch (driveMode) {
  120. case 1:
  121. steppers[i].runSpeedToPosition();
  122. break ;
  123. case 2:
  124. steppers[i].runSpeed();
  125. break ;
  126. default :
  127. steppers[i].run();
  128. }
  129. }
  130. // bool updated = 1 ;
  131. // if (!digitalRead(switchLow)) {
  132. // stepper.setCurrentPosition(0);
  133. // updated=0;
  134. // }
  135. // if (!digitalRead(switchHigh)) {
  136. // stepper.setCurrentPosition(maxPos * stepToCm);
  137. // updated=0;
  138. // }
  139. // return updated ;
  140. }