stepper.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. long int speedLimit[2] = { 50 , 10000000 } ;// sets an absolute minimum/maximum speed limit
  27. long int accLimit[2] = { 100, 200000} ;
  28. //////////// UTILITIES ///////////////
  29. long int camRot2steps (float camRot ) {
  30. return (long int) STEPS_PER_TURN * MICROSTEPPING * REDUCTION_RATIO *camRot ;
  31. }
  32. //////////// ACCELSTEPPER /////////////
  33. void stop_Steppers (void) {
  34. for(int i ; i < NUM_OF_STEPPERS ; i++ ) {
  35. steppers[i].stop();
  36. }
  37. }
  38. long int moveTo_Stepper(int stepperIndex, long int stepto ) {
  39. steppers[stepperIndex].moveTo(stepto);
  40. // while (!steppers[stepperIndex].distanceToGo()==0)
  41. // steppers[stepperIndex].run();
  42. // steppers[stepperIndex].moveTo( constrain( stepto , originPos * stepToCm, maxPos * stepToCm ) );
  43. return steppers[stepperIndex].distanceToGo();
  44. }
  45. //float moveToCm_Stepper(float cmto ) {
  46. // stepper.moveTo(constrain( cmto * stepToCm , originPos, maxPos ));
  47. // return stepper.distanceToGo()/stepToCm;
  48. //}
  49. //
  50. long int move_Stepper(int stepperIndex, long int stepRelative ) {
  51. steppers[stepperIndex].move(stepRelative);
  52. return steppers[stepperIndex].distanceToGo();
  53. }
  54. //float moveCm_Stepper(float cmRelative ) {
  55. // stepper.move(cmRelative * stepToCm );
  56. // return stepper.distanceToGo()/stepToCm;
  57. //}
  58. //
  59. long int maxSpeed_Stepper(int stepperIndex, long int maxspeed){
  60. steppers[stepperIndex].setMaxSpeed( constrain (maxspeed, speedLimit[0], speedLimit[1] ) );
  61. return steppers[stepperIndex].maxSpeed();
  62. }
  63. long int setSpeed_Stepper(int stepperIndex, long int maxspeed){
  64. steppers[stepperIndex].setSpeed( constrain (maxspeed, speedLimit[0], speedLimit[1] ) );
  65. return steppers[stepperIndex].maxSpeed();
  66. }
  67. long int acceleration_Stepper(int stepperIndex, long int acc ){
  68. steppers[stepperIndex].setAcceleration(acc + 1); // prevent 0 step/s²
  69. return acc;
  70. }
  71. //////////////// CALIBRATION ////////////////////////
  72. float init_steppers(){
  73. // //run at constant speed until hitting switchLow
  74. // stepper.setSpeed(-1*speedLimit[1]);
  75. // while(digitalRead(switchLow)) {
  76. // stepper.runSpeed();
  77. // }
  78. // stepper.stop();
  79. // //this is the new 0 step
  80. // stepper.setCurrentPosition(0);
  81. //
  82. //
  83. //
  84. // delay(1000); //take a breath
  85. //
  86. // //run at constant speed until hitting switchHigh
  87. // stepper.setSpeed(speedLimit[1]);
  88. // while(digitalRead(switchHigh)) {
  89. // stepper.runSpeed();
  90. // }
  91. // stepper.stop();
  92. // maxPos = stepper.currentPosition() / stepToCm ;
  93. //
  94. // delay(1000);
  95. //
  96. // stepper.runToNewPosition(maxPos * stepToCm / 2) ;
  97. // return maxPos ;
  98. }
  99. ///////////////////// SETUP ///////////////////////////
  100. void setup_steppers(){
  101. pinMode (switchLow, INPUT_PULLUP);
  102. // pinMode (switchHigh, INPUT_PULLUP);
  103. // stepper.moveTo(rand() % 200000);
  104. for(int i ; i < NUM_OF_STEPPERS ; i++ ) {
  105. steppers[i].setMaxSpeed( 20000);
  106. steppers[i].setSpeed(10000);
  107. }
  108. // while (!stepper.distanceToGo()==0)
  109. // stepper.run();
  110. // stepper.moveTo(0);
  111. // while (!stepper.distanceToGo()==0)
  112. // stepper.run();
  113. // stepper.setMaxSpeed(speedLimit[1]);
  114. // //stepper.setSpeed(10000);
  115. // stepper.setAcceleration(accLimit[1]);
  116. }
  117. ///////////////////// LOOP ///////////////////////////
  118. bool update_steppers() {
  119. for(int i ; i < NUM_OF_STEPPERS ; i++ ) {
  120. steppers[i].run();
  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. }