浏览代码

limits working

needs more "real world" testing, couldn't test dual limit modes, dual
mixed mode is not working but might be related to wiring of test card.
titi 5 年之前
父节点
当前提交
0727afaba4
共有 2 个文件被更改,包括 64 次插入6 次删除
  1. 62 4
      Atm_AccelStepper.cpp
  2. 2 2
      Atm_AccelStepper.h

+ 62 - 4
Atm_AccelStepper.cpp

@@ -11,7 +11,7 @@ Atm_AccelStepper& Atm_AccelStepper::begin(int step_pin, int dir_pin) {
     /*    DISABLED */    ENT_DISABLED,         -1,              -1,          -1,    ENABLED,                 -1,   RUNNING,        -1,                -1,               -1,                -1,            -1,   -1,
     /*     ENABLED */     ENT_ENABLED,         -1,              -1,    DISABLE,         -1,             DISABLE,   RUNNING,      STOP,              STOP,               -1,                -1,            -1,   -1,
     /*     RUNNING */     ENT_RUNNING, LP_RUNNING,              -1,    DISABLE,         -1,                  -1,   RUNNING,      STOP,              STOP,        LIMIT_LOW,        LIMIT_HIGH,       ENABLED,   -1,
-    /*        STOP */        ENT_STOP,         -1,              -1,    DISABLE,         -1,                  -1,   RUNNING,        -1,              STOP,               -1,                -1,            -1,   -1,
+    /*        STOP */        ENT_STOP,    LP_STOP,              -1,    DISABLE,         -1,                  -1,   RUNNING,        -1,                -1,               -1,                -1,       ENABLED,   -1,
     /*  HOMING_LOW */  ENT_HOMING_LOW,         -1,  EXT_HOMING_LOW,    DISABLE,         -1,                  -1,        -1,      STOP,              STOP,             STOP,                -1,            -1,   -1,
     /* HOMING_HIGH */ ENT_HOMING_HIGH,         -1, EXT_HOMING_HIGH,    DISABLE,         -1,                  -1,        -1,      STOP,              STOP,               -1,              STOP,            -1,   -1,
     /*   LIMIT_LOW */   ENT_LIMIT_LOW,         -1,              -1,          -1,         -1,                  -1,  RUNNING,      STOP,              STOP,        LIMIT_LOW,                -1,            -1,   -1,
@@ -48,15 +48,23 @@ int Atm_AccelStepper::event( int id ) {
     case EVT_ON_LIMIT_LOW:
       switch(_limitLow_Mode) {
         case 0:
+          //
+          Serial.println("no limit");
           return 0;
         case 1: //digital INPUT
+          // Serial.println("digital");
           limitLow_State = digitalRead(_limitLow_Pin);
           limitLow_State = _limitLow_Reversed ? !limitLow_State : limitLow_State;
           return limitLow_State;
         case 2:
+
           int analogTemp = analogRead(_limitLow_Pin);
           limitLow_State = _limitLow_Thresholds[0] < analogTemp && analogTemp < _limitLow_Thresholds[1];
           limitLow_State = _limitLow_Reversed ? !limitLow_State : limitLow_State;
+          // Serial.print("analog ");
+          // Serial.println(analogTemp);
+          // Serial.print("state ");
+          // Serial.println(limitLow_State);
           return limitLow_State;
       }
     case EVT_ON_LIMIT_HIGH:
@@ -68,6 +76,7 @@ int Atm_AccelStepper::event( int id ) {
           limitHigh_State = _limitHigh_Reversed ? !limitHigh_State : limitHigh_State;
           return limitHigh_State;
         case 2:
+        //Serial.println("analog");
           int analogTemp = analogRead(_limitHigh_Pin);
           limitHigh_State = _limitHigh_Thresholds[0] < analogTemp && analogTemp < _limitHigh_Thresholds[1];
           limitHigh_State = _limitHigh_Reversed ? !limitHigh_State : limitHigh_State;
@@ -122,23 +131,29 @@ void Atm_AccelStepper::action( int id ) {
       tempStep = stepper->currentPosition();
       if (tempStep != _currentStep){
         _currentStep =  tempStep;
-        push(connectors, ON_CHANGEPOSITION, 0,  tempStep, 0);
+        push(connectors, ON_CHANGEPOSITION, 0,  _currentStep, 0);
       }
 
       return;
     case ENT_STOP:
     push(connectors, ON_CHANGESTATE, 0,  state(), 0);
-    stepper->stop();
-    push( connectors, ON_STOP, 0, 0, 0 );
       if (last_trigger == EVT_STOP) {
         stepper->stop();
+        _targetStep = stepper->targetPosition();
         push( connectors, ON_STOP, 0, 0, 0 );
       }
       if (last_trigger == EVT_EMERGENCY_STOP) {
+        _currentStep = stepper->currentPosition();
+        _targetStep = _currentStep ;
+        stepper->moveTo(_targetStep);
         stepper->setSpeed(0);
         push( connectors, ON_STOP, 0, 1, 0 );
       }
       return;
+    case LP_STOP:
+      stepper->run();
+      _currentStep = stepper->currentPosition();
+      return;
     case ENT_HOMING_LOW:
       return;
     case EXT_HOMING_LOW:
@@ -148,8 +163,13 @@ void Atm_AccelStepper::action( int id ) {
     case EXT_HOMING_HIGH:
       return;
     case ENT_LIMIT_LOW:
+      push( connectors, ON_ONLIMITLOW, 0, 0, 0 );
+      //stop motor if going down, allow going up
+      stepper->speed() < 0 ? trigger(EVT_EMERGENCY_STOP) :  trigger(EVT_MOVE);
       return;
     case ENT_LIMIT_HIGH:
+      push( connectors, ON_ONLIMITHIGH, 0, 1, 0 );
+      stepper->speed() > 0 ? trigger(EVT_EMERGENCY_STOP) : trigger(EVT_MOVE);
       return;
   }
 }
@@ -200,6 +220,44 @@ Atm_AccelStepper& Atm_AccelStepper::rotate( long int speed) {
   return *this;
 }
 
+Atm_AccelStepper& Atm_AccelStepper::setEnablePin( int enablePin ){
+  _enablePin = enablePin ;
+  pinMode(_enablePin, OUTPUT);
+  return *this;
+}
+
+Atm_AccelStepper& Atm_AccelStepper::enableReversed( bool reverse ){
+  _enableReversed = reverse ;
+  return *this;
+}
+
+Atm_AccelStepper& Atm_AccelStepper::limitLow_set(int mode,  int pin,  int reversed){
+  _limitLow_Mode = mode ;
+  _limitLow_Pin = pin ;
+  _limitLow_Reversed = reversed ;
+  if (_limitLow_Mode) {pinMode(_limitLow_Pin, INPUT);}
+  return *this;
+}
+
+Atm_AccelStepper& Atm_AccelStepper::limitLow_setThresholds (int threshold_low, int threshold_high){
+  _limitLow_Thresholds[0] = threshold_low ;
+  _limitLow_Thresholds[1] = threshold_high ;
+  return *this;
+}
+
+Atm_AccelStepper& Atm_AccelStepper::limitHigh_set(int mode,  int pin,  int reversed){
+  _limitHigh_Mode = mode ;
+  _limitHigh_Pin = pin ;
+  _limitHigh_Reversed = reversed ;
+  if (_limitHigh_Mode) {pinMode(_limitHigh_Pin, INPUT);}
+  return *this;
+}
+
+Atm_AccelStepper& Atm_AccelStepper::limitHigh_setThresholds (int threshold_low, int threshold_high){
+  _limitHigh_Thresholds[0] = threshold_low ;
+  _limitHigh_Thresholds[1] = threshold_high ;
+  return *this;
+}
 
 /* Public event methods
  *

+ 2 - 2
Atm_AccelStepper.h

@@ -42,7 +42,7 @@ class Atm_AccelStepper: public Machine {
   Atm_AccelStepper& homing( bool direction );
   int runMode = 0; // 0 uses run() for positioning, 1 uses runSpeed() for constant speed
 
-  Atm_AccelStepper& setEnablePin( int enablePin );
+    Atm_AccelStepper& setEnablePin( int enablePin );
   Atm_AccelStepper& enableReversed( bool reverse );
   bool enabled ;
 
@@ -56,7 +56,7 @@ class Atm_AccelStepper: public Machine {
 
 
  private:
-  enum { ENT_DISABLED, ENT_ENABLED,ENT_RUNNING, LP_RUNNING, ENT_STOP, ENT_HOMING_LOW, EXT_HOMING_LOW, ENT_HOMING_HIGH, EXT_HOMING_HIGH, ENT_LIMIT_LOW, ENT_LIMIT_HIGH }; // ACTIONS
+  enum { ENT_DISABLED, ENT_ENABLED,ENT_RUNNING, LP_RUNNING, ENT_STOP, LP_STOP, ENT_HOMING_LOW, EXT_HOMING_LOW, ENT_HOMING_HIGH, EXT_HOMING_HIGH, ENT_LIMIT_LOW, ENT_LIMIT_HIGH }; // ACTIONS
   enum { ON_CHANGEPOSITION, ON_CHANGESTATE, ON_ONLIMITHIGH, ON_ONLIMITLOW, ON_ONTARGET, ON_STOP, CONN_MAX }; // CONNECTORS
   atm_connector connectors[CONN_MAX];
   int event( int id );