Browse Source

Qcarnaille delivery

titi 5 years ago
parent
commit
f0333e6312
2 changed files with 47 additions and 21 deletions
  1. 46 20
      Atm_AccelStepper.cpp
  2. 1 1
      Atm_AccelStepper.h

+ 46 - 20
Atm_AccelStepper.cpp

@@ -9,7 +9,7 @@ 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,               -1,                -1,            -1,    HOMING_LOW,     HOMING_HIGH,   -1,
-    /*     ENABLED */     ENT_ENABLED,              -1,              -1,    DISABLE,         -1,             DISABLE,     RUNNING,      STOP,              STOP,        LIMIT_LOW,        LIMIT_HIGH,            -1,    HOMING_LOW,     HOMING_HIGH,   -1,
+    /*     ENABLED */     ENT_ENABLED,              -1,              -1,    DISABLE,         -1,             DISABLE,     RUNNING,      STOP,              STOP,               -1,                -1,            -1,    HOMING_LOW,     HOMING_HIGH,   -1,
     /*     RUNNING */     ENT_RUNNING,      LP_RUNNING,              -1,    DISABLE,         -1,                  -1,     RUNNING,      STOP,              STOP,        LIMIT_LOW,        LIMIT_HIGH,       ENABLED,            -1,              -1,   -1,
     /*        STOP */        ENT_STOP,         LP_STOP,              -1,    DISABLE,         -1,                  -1,     RUNNING,        -1,                -1,               -1,                -1,       ENABLED,            -1,              -1,   -1,
     /*  HOMING_LOW */  ENT_HOMING_LOW,   LP_HOMING_LOW,  EXT_HOMING_LOW,    DISABLE,         -1,                  -1,          -1,      STOP,              STOP,        LIMIT_LOW,        LIMIT_HIGH,            -1,            -1,              -1,   -1,
@@ -61,9 +61,12 @@ int Atm_AccelStepper::event( int id ) {
           int analogTemp = analogRead(_limitLow_Pin);
           limitLow_State = (_limitLow_Thresholds[0] < analogTemp) && (analogTemp < _limitLow_Thresholds[1]);
           limitLow_State = _limitLow_Reversed ? !limitLow_State : limitLow_State;
-          if(limitLow_State){analogTemp = analogRead(_limitLow_Pin);
-          limitLow_State = (_limitLow_Thresholds[0] < analogTemp) && (analogTemp < _limitLow_Thresholds[1]);
-          limitLow_State = _limitLow_Reversed ? !limitLow_State : limitLow_State;}
+          if(limitLow_State){
+			delay(3);
+			analogTemp = analogRead(_limitLow_Pin);
+          	limitLow_State = (_limitLow_Thresholds[0] < analogTemp) && (analogTemp < _limitLow_Thresholds[1]);
+          	limitLow_State = _limitLow_Reversed ? !limitLow_State : limitLow_State;
+		  }
           return limitLow_State != previous_state;
       }
     case EVT_ON_LIMIT_HIGH:
@@ -80,6 +83,12 @@ int Atm_AccelStepper::event( int id ) {
           int analogTemp = analogRead(_limitHigh_Pin);
           limitHigh_State = (_limitHigh_Thresholds[0] < analogTemp) && (analogTemp < _limitHigh_Thresholds[1]);
           limitHigh_State = _limitHigh_Reversed ? !limitHigh_State : limitHigh_State;
+		  if(limitHigh_State){
+			delay(3);
+			analogTemp = analogRead(_limitHigh_Pin);
+          	limitHigh_State = (_limitHigh_Thresholds[0] < analogTemp) && (analogTemp < _limitHigh_Thresholds[1]);
+          	limitHigh_State = _limitHigh_Reversed ? !limitHigh_State : limitHigh_State;
+		  }
           return limitHigh_State != previous_state;
       }
     case EVT_ON_TARGET:
@@ -115,17 +124,19 @@ void Atm_AccelStepper::action( int id ) {
       _isHoming = 0 ;
       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());
       enabled = _enableReversed ? LOW : HIGH ;
+      //reset limit state so that they trigger again if we're stopped on it
+      limitLow_State = 0;
+      limitHigh_State = 0;
       digitalWrite(_enablePin, enabled);
 
 
       return;
     case ENT_RUNNING:
       push(connectors, ON_CHANGESTATE, 0,  state(), 0);
+      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
       _isHoming = 0;
-      //reset limit state so that they trigger again if we're stopped on it
-      // limitLow_State = 0;
-      // limitHigh_State = 0;
       Serial.print("target ");
       Serial.println(_targetStep);
       stepper->moveTo(_targetStep);
@@ -136,59 +147,69 @@ void Atm_AccelStepper::action( int id ) {
       return;
     case LP_RUNNING:
       stepper_update();
+      if(stepper->speed() == 0.) {trigger(EVT_ON_TARGET);}
       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 ;
+        // 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);
-        stepper->setSpeed(0);
         push( connectors, ON_STOP, 0, 1, 0 );
       }
       return;
     case LP_STOP:
       stepper_update();
-      _currentStep = stepper->currentPosition();
+      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
+      if(stepper->speed() == 0.) {trigger(EVT_ON_TARGET);}
+      // _currentStep = stepper->currentPosition();
       return;
     case ENT_HOMING_LOW:
       push(connectors, ON_CHANGESTATE, 0,  state(), 0);
+      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
       runMode = 1;
       _isHoming = 1 ;
       stepper->setSpeed(-1*homing_speed);
       return;
     case LP_HOMING_LOW:
       stepper_update();
+      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
       return;
     case EXT_HOMING_LOW:
       runMode = 0;
       _isHoming = 0;
-      trigger(EVT_EMERGENCY_STOP);
+      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
       if(last_trigger == EVT_ON_LIMIT_LOW) {
         stepper->setCurrentPosition(0);
         _currentStep = 0;
+        push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
         Serial.println("homing low done");
       }
       else{Serial.println("homing low failed");}
-      _targetStep = _currentStep;
+      trigger(EVT_EMERGENCY_STOP);
 
       return;
     case ENT_HOMING_HIGH:
       push(connectors, ON_CHANGESTATE, 0,  state(), 0);
+      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
       runMode = 1;
       _isHoming = 2 ;
       stepper->setSpeed(homing_speed);
       return;
     case LP_HOMING_HIGH:
       stepper_update();
+      push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
       return;
     case EXT_HOMING_HIGH:
+    push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, stepper->speed());
       runMode = 0;
       _isHoming = 0;
       trigger(EVT_EMERGENCY_STOP);
@@ -207,35 +228,40 @@ 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());
       if (!limitLow_State){trigger(EVT_MOVE);}
       return;
 
     case LP_LIMIT_LOW:
       //stop motor if going down, allow going up
-      stepper_update();
-      if(_limitLow_Hard && (_targetStep < _currentStep)) {
+
+      // if(_limitLow_Hard && (_targetStep < _currentStep)) {
+      if(_limitLow_Hard && (stepper->speed()<0.)) {
           // Serial.println("youpi");
           _currentStep = stepper->currentPosition();
-          //stepper->moveTo(_currentStep);
-          //_targetStep = _currentStep;
+          stepper->moveTo(_currentStep);
+          _targetStep = _currentStep;
           stepper->setSpeed(0);
         }
+        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());
       if (!limitHigh_State){trigger(EVT_MOVE);};
       return;
     case LP_LIMIT_HIGH:
       //stop motor if going down, allow going up
-      stepper_update();
-      if(_limitHigh_Hard && (_targetStep > _currentStep)) {
+
+      if(_limitHigh_Hard && ((stepper->speed()>0.))) {
           // Serial.println("youpi");
           _currentStep = stepper->currentPosition();
-          //stepper->moveTo(_currentStep);
-          //_targetStep = _currentStep;
+          stepper->moveTo(_currentStep);
+          _targetStep = _currentStep;
           stepper->setSpeed(0);
         }
+        stepper_update();
       // else{}
       return;
   }

+ 1 - 1
Atm_AccelStepper.h

@@ -42,7 +42,7 @@ class Atm_AccelStepper: public Machine {
 
   AccelStepper *stepper;
 
-  long int homing_speed = 100;
+  long int homing_speed = 1000;
   long int max_speed = 10000;
   long int acceleration = 1000;
   Atm_AccelStepper& setMaxSpeed( long int maxSpeed );