Browse Source

cleanup

better state sending, go trough STOP state more often, speed and
position better updated
titi 5 years ago
parent
commit
5d16b3f288
1 changed files with 42 additions and 44 deletions
  1. 42 44
      Atm_AccelStepper.cpp

+ 42 - 44
Atm_AccelStepper.cpp

@@ -10,11 +10,11 @@ Atm_AccelStepper& Atm_AccelStepper::begin(int step_pin, int dir_pin) {
   const static state_t state_table[] PROGMEM = {
     /*                       ON_ENTER           ON_LOOP          ON_EXIT  EVT_DISABLE  EVT_ENABLE  EVT_ENABLED_TIMEOUT    EVT_MOVE  EVT_STOP  EVT_EMERGENCY_STOP  EVT_ON_LIMIT_LOW  EVT_ON_LIMIT_HIGH  EVT_ON_TARGET  EVT_HOMING_LOW  EVT_HOMING_HIGH  ELSE */
     /*    DISABLE */     ENT_DISABLED,              -1,              -1,         -1,    ENABLED,                  -1,     RUNNING,        -1,                -1,          DISABLE,           DISABLE,            -1,    HOMING_LOW,     HOMING_HIGH,   -1,
-    /*     ENABLED */     ENT_ENABLED,              -1,              -1,    DISABLE,         -1,             DISABLE,     RUNNING,      STOP,              STOP,          ENABLED,           ENABLED,            -1,    HOMING_LOW,     HOMING_HIGH,   -1,
-    /*     RUNNING */     ENT_RUNNING,      LP_RUNNING,              -1,    DISABLE,         -1,                  -1,     RUNNING,      STOP,              STOP,          RUNNING,           RUNNING,       ENABLED,            -1,              -1,   -1,
-    /*        STOP */        ENT_STOP,         LP_STOP,              -1,    DISABLE,         -1,                  -1,     RUNNING,      STOP,              STOP,             STOP,              STOP,       ENABLED,            -1,              -1,   -1,
-    /*  HOMING_LOW */  ENT_HOMING_LOW,   LP_HOMING_LOW,  EXT_HOMING_LOW,    DISABLE,         -1,                  -1,          -1,      STOP,              STOP,          HOMING_LOW,           HOMING_LOW,            -1,            -1,              -1,   -1,
-    /* HOMING_HIGH */ ENT_HOMING_HIGH,  LP_HOMING_HIGH, EXT_HOMING_HIGH,    DISABLE,         -1,                  -1,          -1,      STOP,              STOP,          HOMING_HIGH,           HOMING_HIGH,            -1,            -1,              -1,   -1,
+    /*     ENABLED */     ENT_ENABLED,              -1,              -1,    DISABLE,         -1,             DISABLE,     RUNNING,        -1,               -1,          ENABLED,           ENABLED,             -1,    HOMING_LOW,     HOMING_HIGH,   -1,
+    /*     RUNNING */     ENT_RUNNING,      LP_RUNNING,              -1,    DISABLE,         -1,                  -1,     RUNNING,      STOP,              STOP,          RUNNING,           RUNNING,          -1,            -1,              -1,   -1,
+    /*        STOP */        ENT_STOP,         LP_STOP,              -1,    DISABLE,    ENABLED,                  -1,     RUNNING,      STOP,              STOP,             STOP,              STOP,          -1,            -1,              -1,   -1,
+    /*  HOMING_LOW */  ENT_HOMING_LOW,   LP_HOMING_LOW,  EXT_HOMING_LOW,    DISABLE,         -1,                  -1,          -1,      STOP,              STOP,       HOMING_LOW,        HOMING_LOW,            -1,            -1,              -1,   -1,
+    /* HOMING_HIGH */ ENT_HOMING_HIGH,  LP_HOMING_HIGH, EXT_HOMING_HIGH,    DISABLE,         -1,                  -1,          -1,      STOP,              STOP,      HOMING_HIGH,       HOMING_HIGH,            -1,            -1,              -1,   -1,
     /*   LIMIT_LOW */   ENT_LIMIT_LOW,    LP_LIMIT_LOW,              -1,         -1,         -1,                  -1,     RUNNING,      STOP,              STOP,               -1,                -1,            -1,            -1,              -1,   -1,
     /*  LIMIT_HIGH */  ENT_LIMIT_HIGH,   LP_LIMIT_HIGH,              -1,         -1,         -1,                  -1,     RUNNING,      STOP,              STOP,               -1,                -1,            -1,            -1,              -1,   -1
   };
@@ -119,7 +119,7 @@ int Atm_AccelStepper::event( int id ) {
         else{return 0;}
 
     case EVT_ON_TARGET:
-      return runMode ? 0 : _currentStep == _targetStep;
+      return runMode ? 0 : _currentStep == _targetStep; //only send when in positionning mode
     case EVT_HOMING_LOW:
       return 0;
     case EVT_HOMING_HIGH:
@@ -148,6 +148,7 @@ void Atm_AccelStepper::action( int id ) {
       digitalWrite(_enablePin, enabled);
       limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
       return;
+
     case ENT_ENABLED:
       // _isHoming = 0 ;
       stepper_update();
@@ -160,65 +161,62 @@ void Atm_AccelStepper::action( int id ) {
       limitHigh_State = 0;
       digitalWrite(_enablePin, enabled);
       limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
-
       return;
+
     case ENT_RUNNING:
       push(connectors, ON_CHANGESTATE, 0,  state(), 0);
-      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
-      // _isHoming = 0;
-      // Serial.print("target ");
-      // Serial.println(_targetStep);
+      // push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
       stepper->moveTo(_targetStep);
-      // stepper->computeNewSpeed();
-      //stepper_update();
-      //push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
       position_timer.setFromNow(this, POSITION_SEND_TIMER);
       limits_timer.setFromNow(this, LIMIT_UPDATE_RATE);
       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.)) {
-          // Serial.println("youpi");
-          _currentStep = stepper->currentPosition();
-          stepper->moveTo(_currentStep);
-          _targetStep = _currentStep;
-          stepper->setSpeed(0);
+          trigger(EVT_EMERGENCY_STOP);
+
         }
       if(limitHigh_State && _limitHigh_Hard && ((stepper->speed()>0.))) {
-          // Serial.println("youpi");
-          _currentStep = stepper->currentPosition();
-          stepper->moveTo(_currentStep);
-          _targetStep = _currentStep;
-          stepper->setSpeed(0);
+          trigger(EVT_EMERGENCY_STOP);
         }
 
       stepper_update();
-      if(stepper->speed() == 0.) {trigger(EVT_ON_TARGET);}
+
+      if(runMode && stepper->speed() == 0.) {trigger(EVT_EMERGENCY_STOP);}//trigger(EVT_ON_TARGET);
+      if(!runMode && stepper->distanceToGo() == 0){trigger(EVT_EMERGENCY_STOP);}
       return;
+
     case ENT_STOP:
-    push(connectors, ON_CHANGESTATE, 0,  state(), 0);
-    push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
-      if (last_trigger == EVT_STOP) {
-        runMode = 0 ;
-        stepper->stop();
-        _targetStep = stepper->targetPosition();
-        push( connectors, ON_STOP, 0, 0, 0 );
-      }
-      if (last_trigger == EVT_EMERGENCY_STOP) {
-        stepper->setSpeed(0);
-        _currentStep = stepper->currentPosition();
-        _targetStep = _currentStep ;
-        stepper->moveTo(_targetStep);
-        push( connectors, ON_STOP, 0, 1, 0 );
-      }
-      return;
+      push(connectors, ON_CHANGESTATE, 0,  state(), 0);
+        if (last_trigger == EVT_STOP) {
+          runMode = 0 ;
+          // stepper->setSpeed(stepper->speed());
+          // stepper_update();
+          stepper->stop();
+          _targetStep = stepper->targetPosition();
+          // push( connectors, ON_STOP, 0, 0, 0 );
+        }
+        if (last_trigger == EVT_EMERGENCY_STOP) {
+          runMode=0 ;
+          stepper->setSpeed(0);
+          _currentStep = stepper->currentPosition();
+          _targetStep = _currentStep ;
+          stepper->setCurrentPosition(_currentStep);
+          // stepper->moveTo(_targetStep);
+          // push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
+          // push( connectors, ON_STOP, 0, 1, 0 );
+        }
+        stepper_update();
+        // push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
+        return;
+
     case LP_STOP:
       stepper_update();
-      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
-      if(stepper->speed() == 0.) {trigger(EVT_ON_TARGET);}
-      // _currentStep = stepper->currentPosition();
+      if(stepper->speed() == 0.) {trigger(EVT_ENABLE);}
       return;
+
     case ENT_HOMING_LOW:
       homingLow_done = 0;
       push(connectors, ON_CHANGESTATE, 0,  state(), 0);