stepper.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. 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. ///////////////////// SETUP ///////////////////////////
  90. void setup_steppers(){
  91. for (int i; i++; i<NUM_OF_STEPPERS) {
  92. pinMode(homing_Pins[i], INPUT_PULLUP);
  93. }
  94. // stepper.moveTo(rand() % 200000);
  95. for(int i ; i < NUM_OF_STEPPERS ; i++ ) {
  96. steppers[i].setMaxSpeed( 20000);
  97. steppers[i].setSpeed(10000);
  98. }
  99. // while (!stepper.distanceToGo()==0)
  100. // stepper.run();
  101. // stepper.moveTo(0);
  102. // while (!stepper.distanceToGo()==0)
  103. // stepper.run();
  104. // stepper.setMaxSpeed(speedLimit[1]);
  105. // //stepper.setSpeed(10000);
  106. // stepper.setAcceleration(accLimit[1]);
  107. }
  108. ///////////////////// LOOP ///////////////////////////
  109. bool update_steppers() {
  110. for(int i ; i < NUM_OF_STEPPERS ; i++ ) {
  111. switch (driveMode) {
  112. case 1:
  113. steppers[i].runSpeedToPosition();
  114. break ;
  115. case 2:
  116. steppers[i].runSpeed();
  117. break ;
  118. default :
  119. steppers[i].run();
  120. }
  121. }
  122. // bool updated = 1 ;
  123. // if (!digitalRead(switchLow)) {
  124. // stepper.setCurrentPosition(0);
  125. // updated=0;
  126. // }
  127. // if (!digitalRead(switchHigh)) {
  128. // stepper.setCurrentPosition(maxPos * stepToCm);
  129. // updated=0;
  130. // }
  131. // return updated ;
  132. }