Parcourir la source

added maxstep feature

can lock move over _maxstep value
titi il y a 5 ans
Parent
commit
38c7f1ea4d
2 fichiers modifiés avec 38 ajouts et 31 suppressions
  1. 35 30
      Atm_AccelStepper.cpp
  2. 3 1
      Atm_AccelStepper.h

+ 35 - 30
Atm_AccelStepper.cpp

@@ -154,7 +154,7 @@ void Atm_AccelStepper::action( int id ) {
       stepper_update();
       if(last_trigger == EVT_ON_TARGET){push( connectors, ON_ONTARGET, 0, _currentStep, 0 );};
       push(connectors, ON_CHANGESTATE, 0,  state(), 0);
-      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
+      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, _currentSpeed);
       enabled = _enableReversed ? LOW : HIGH ;
       //reset limit state so that they trigger again if we're stopped on it
       limitLow_State = 0;
@@ -169,23 +169,30 @@ void Atm_AccelStepper::action( int id ) {
       stepper->moveTo(_targetStep);
       position_timer.setFromNow(this, POSITION_SEND_TIMER);
       limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
-      stepper_update();
+      // stepper_update();
       return;
 
     case LP_RUNNING:
       // if on limits and limits are hard, stop moving in one direction
-      if(limitLow_State && _limitLow_Hard && (stepper->speed()<0.)) {
+      if(limitLow_State && _limitLow_Hard && (_currentSpeed<0.)) {
           trigger(EVT_EMERGENCY_STOP);
 
         }
-      if(limitHigh_State && _limitHigh_Hard && ((stepper->speed()>0.))) {
+      if( limitHigh_State && _limitHigh_Hard && (_currentSpeed>0.)) {
           trigger(EVT_EMERGENCY_STOP);
         }
+      // if hard limits and homing is done, check current position is within limits
+      if (_limitLow_Hard &&  homingLow_done) {
+        if( (_currentStep<0) && (_currentSpeed<0.)) {trigger(EVT_EMERGENCY_STOP);}
+      }
+      if (_limitHigh_Hard &&  _maxStep) { //maxstep is 0 if not defined
+        if( (_currentStep>_maxStep) && (_currentSpeed>0.)) {trigger(EVT_EMERGENCY_STOP);}
+      }
 
       stepper_update();
 
-      if(runMode && stepper->speed() == 0.) {trigger(EVT_EMERGENCY_STOP);}//trigger(EVT_ON_TARGET);
-      if(!runMode && stepper->distanceToGo() == 0){trigger(EVT_EMERGENCY_STOP);}
+      if(runMode && _currentSpeed == 0.) {trigger(EVT_EMERGENCY_STOP);}//trigger(EVT_ON_TARGET);
+      if(!runMode && (_currentStep==_targetStep)){trigger(EVT_EMERGENCY_STOP);}
       return;
 
     case ENT_STOP:
@@ -214,19 +221,20 @@ void Atm_AccelStepper::action( int id ) {
 
     case LP_STOP:
       stepper_update();
-      if(stepper->speed() == 0.) {trigger(EVT_ENABLE);}
+      if(_currentSpeed == 0.) {trigger(EVT_ENABLE);}
       return;
 
     case ENT_HOMING_LOW:
       homingLow_done = 0;
       push(connectors, ON_CHANGESTATE, 0,  state(), 0);
-      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
+      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, _currentSpeed);
       runMode = 1;
       //_isHoming = 1 ;
       stepper->setSpeed(-1*homing_speed);
       position_timer.setFromNow(this, POSITION_SEND_TIMER);
       limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
       return;
+
     case LP_HOMING_LOW:
       stepper_update();
       // if (changed && (limitHigh_State||limitLow_State)){
@@ -244,15 +252,8 @@ void Atm_AccelStepper::action( int id ) {
           trigger(EVT_EMERGENCY_STOP);
           push(connectors, ON_ONHOMINGLOW, 0,  _currentStep, homingLow_done);
         }//Serial.println("homing low failed");}
-
-        // push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
-
-
-
-
-      //}
-      // push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
       return;
+
     case EXT_HOMING_LOW:
       // runMode = 0;
       // //_isHoming = 0;
@@ -266,15 +267,17 @@ void Atm_AccelStepper::action( int id ) {
       // push(connectors, ON_ONHOMINGLOW, 0,  _currentStep, homingLow_done);
       // trigger(EVT_EMERGENCY_STOP);
       return;
+
     case ENT_HOMING_HIGH:
       push(connectors, ON_CHANGESTATE, 0,  state(), 0);
-      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
+      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, _currentSpeed);
       runMode = 1;
       //_isHoming = 2 ;
       stepper->setSpeed(homing_speed);
       position_timer.setFromNow(this, POSITION_SEND_TIMER);
       limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
       return;
+
     case LP_HOMING_HIGH:
       stepper_update();
       //if (changed && (limitHigh_State||limitLow_State)){
@@ -294,14 +297,8 @@ void Atm_AccelStepper::action( int id ) {
           trigger(EVT_EMERGENCY_STOP);
           push(connectors, ON_ONHOMINGHIGH, 0,  _currentStep, homingHigh_done);
         }//Serial.println("homing high failed");}
-        // push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
-
-
-        //stepper_update();
-
-      //}
-      // push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
       return;
+
     case EXT_HOMING_HIGH:
       //
       // runMode = 0;
@@ -319,6 +316,7 @@ void Atm_AccelStepper::action( int id ) {
       // push(connectors, ON_ONHOMINGHIGH, 0,  _currentStep, homingHigh_done);
       // trigger(EVT_EMERGENCY_STOP);
       // return;
+
     case ENT_LIMIT_LOW:
       /*triggered by a change in limit state
        if state is 0, we may leave this state for running
@@ -326,7 +324,7 @@ void Atm_AccelStepper::action( int id ) {
             the free direction, until a trigger comes with state 0
       */
       push( connectors, ON_ONLIMITLOW, 0, limitLow_State, 0 );
-      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
+      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, _currentSpeed);
       if (!limitLow_State){trigger(EVT_MOVE);}
       return;
 
@@ -344,15 +342,17 @@ void Atm_AccelStepper::action( int id ) {
         stepper_update();
       //else{} // _isHoming ? trigger(EVT_STOP):
       return;
+
     case ENT_LIMIT_HIGH:
       push( connectors, ON_ONLIMITHIGH, 0, limitHigh_State, 0 );
-      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
+      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, _currentSpeed);
       if (!limitHigh_State){trigger(EVT_MOVE);};
       return;
+
     case LP_LIMIT_HIGH:
       //stop motor if going down, allow going up
 
-      if(_limitHigh_Hard && ((stepper->speed()>0.))) {
+      if(_limitHigh_Hard && (_currentSpeed>0.)) {
           // Serial.println("youpi");
           _currentStep = stepper->currentPosition();
           stepper->moveTo(_currentStep);
@@ -400,19 +400,23 @@ void Atm_AccelStepper::stepper_update(void) {
       stepper->runSpeed();
       break;
   }
-  // Serial.print("update ");
-  // Serial.println(stepper->speed());
+  _currentSpeed = stepper->speed();
   long int tempStep = stepper->currentPosition();
   if (tempStep != _currentStep){
     _currentStep =  tempStep;
     //Serial.println(stepper->currentPosition());
     if (position_timer.expired(this)){
-      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
+      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, _currentSpeed);
       position_timer.setFromNow(this, POSITION_SEND_TIMER);
     }
   }
 }
 
+Atm_AccelStepper& Atm_AccelStepper::setMaxStep( long int maxStep ) {
+  _maxStep = maxStep;
+  return *this ;
+}
+
 Atm_AccelStepper& Atm_AccelStepper::setMaxSpeed( long int maxSpeed){
   max_speed = maxSpeed ;
   stepper->setMaxSpeed(max_speed);
@@ -483,6 +487,7 @@ Atm_AccelStepper& Atm_AccelStepper::rotate( long  int speed) {
   runMode = 1;
   // _isHoming = 0 ;
   stepper->setSpeed( speed);
+  stepper->runSpeed();
   enable();
   trigger( EVT_MOVE );
   return *this;

+ 3 - 1
Atm_AccelStepper.h

@@ -47,6 +47,7 @@ class Atm_AccelStepper: public Machine {
   bool homingHigh_done = 0 ;
   long int max_speed = 1000;
   long int acceleration = 100;
+  Atm_AccelStepper& setMaxStep( long int maxStep );
   Atm_AccelStepper& setMaxSpeed( long int maxSpeed );
   Atm_AccelStepper& setHomingSpeed( long int homingSpeed);
   Atm_AccelStepper& setAcceleration( long int acc);
@@ -111,7 +112,8 @@ class Atm_AccelStepper: public Machine {
   //positionning private variables
   long int _currentStep = 0;
   long int _targetStep = 0;
-  long int _maxStep = -1;
+  long int _maxStep = 0; //0 is undefined, can be set using setMaxstep or homingHigh
+  long int _currentSpeed;
   atm_timer_millis position_timer ;
   int POSITION_SEND_TIMER = 50 ;
   void stepper_update(void);