123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #include <AccelStepper.h>
- /*
- * stepper control through big easy driver
- * positions are expressed in centimeters
- * originPos is near the motor, where switchLow gets LOW
- * maxPos is the maximum position, where switchHigh gets LOW
- * idlePos is the idle position
- * lowOffset
- * highOffset are offsets from originPos/maxPos respectively to define the running zone
- *
- * Stepper driven through big easy driver
- * configured for 1/4 microstepping (mover micro-steps gives low speed)
- * 800 steps is one rotation
- * one rotation is 1.6cm translation
- * cm to steps ratio is 500
- */
- // Define a stepper and the pins it will use
- AccelStepper stepper(1, 8, 7);
- // microswitch pins
- int switchLow = A5;
- int speedLimit[2] = { 50 , 1000 } ;// sets an absolute minimum/maximum speed limit
- int accLimit[2] = { 100, 2000} ;
- long int moveTo_Stepper(long int stepto ) {
- stepper.moveTo( constrain( stepto , originPos * stepToCm, maxPos * stepToCm ) );
- return stepper.distanceToGo();
- }
- float moveToCm_Stepper(float cmto ) {
- stepper.moveTo(constrain( cmto * stepToCm , originPos, maxPos ));
- return stepper.distanceToGo()/stepToCm;
- }
- long int move_Stepper(long int stepRelative ) {
- stepper.move(stepRelative);
- return stepper.distanceToGo();
- }
- float moveCm_Stepper(float cmRelative ) {
- stepper.move(cmRelative * stepToCm );
- return stepper.distanceToGo()/stepToCm;
- }
- long int maxSpeed_Stepper(long int maxspeed){
- stepper.setMaxSpeed( constrain (maxspeed, speedLimit[0], speedLimit[1] ) );
- return stepper.maxSpeed();
- }
- long int acceleration_Stepper( long int acc ){
- stepper.setAcceleration(acc + 1); // prevent 0 step/s²
- return acc;
- }
- //////////////// CALIBRATION ////////////////////////
- float init_Stepper(){
- //run at constant speed until hitting switchLow
- stepper.setSpeed(-1*speedLimit[1]);
- while(digitalRead(switchLow)) {
- stepper.runSpeed();
- }
- stepper.stop();
- //this is the new 0 step
- stepper.setCurrentPosition(0);
- delay(1000); //take a breath
- //run at constant speed until hitting switchHigh
- stepper.setSpeed(speedLimit[1]);
- while(digitalRead(switchHigh)) {
- stepper.runSpeed();
- }
- stepper.stop();
- maxPos = stepper.currentPosition() / stepToCm ;
- delay(1000);
- stepper.runToNewPosition(maxPos * stepToCm / 2) ;
- return maxPos ;
- }
- ///////////////////// SETUP ///////////////////////////
- void setup_Stepper(){
- pinMode (switchLow, INPUT_PULLUP);
- pinMode (switchHigh, INPUT_PULLUP);
- stepper.setMaxSpeed(speedLimit[1]);
- //stepper.setSpeed(10000);
- stepper.setAcceleration(accLimit[1]);
- }
- ///////////////////// LOOP ///////////////////////////
- bool update_Stepper() {
-
- bool updated = 1 ;
- stepper.run();
-
- if (!digitalRead(switchLow)) {
- stepper.setCurrentPosition(0);
- updated=0;
- }
- if (!digitalRead(switchHigh)) {
- stepper.setCurrentPosition(maxPos * stepToCm);
- updated=0;
- }
- return updated ;
- }
|