浏览代码

added external limits management

allow user to change limit state from outisde of the machine and still
trigger event sequence
compiles, needs tests
titi 5 年之前
父节点
当前提交
2bdfeaf1ad
共有 2 个文件被更改,包括 34 次插入20 次删除
  1. 32 19
      Atm_AccelStepper.cpp
  2. 2 1
      Atm_AccelStepper.h

+ 32 - 19
Atm_AccelStepper.cpp

@@ -34,6 +34,7 @@ Atm_AccelStepper& Atm_AccelStepper::begin(int step_pin, int dir_pin) {
 
 int Atm_AccelStepper::event( int id ) {
   //updateLimitSwitch();
+  bool changed ;
   switch ( id ) {
     case EVT_DISABLE:
       return 0;
@@ -48,49 +49,61 @@ int Atm_AccelStepper::event( int id ) {
     case EVT_EMERGENCY_STOP:
       return 0;
     case EVT_ON_LIMIT_LOW:
-      previous_state = limitLow_State;
+      // previous_state = limitLow_State;
       switch(_limitLow_Mode) {
         case 0:
-          return 0;
+          changed = limitLow_State != limitLow_State_prev ? 1 : 0 ;
+          break;
         case 1: //digital INPUT
           // Serial.println("digital");
           limitLow_State = digitalRead(_limitLow_Pin);
           limitLow_State = _limitLow_Reversed ? !limitLow_State : limitLow_State;
-          return limitLow_State != previous_state;
+          changed = limitLow_State != limitLow_State_prev ? 1 : 0 ;
+          break;
         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;
           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;
+	          delay(3);
+	          analogTemp = analogRead(_limitLow_Pin);
+        	  limitLow_State = (_limitLow_Thresholds[0] < analogTemp) && (analogTemp < _limitLow_Thresholds[1]);
+        	  limitLow_State = _limitLow_Reversed ? !limitLow_State : limitLow_State;
+		        }
+          changed = limitLow_State != limitLow_State_prev ? 1 : 0 ;
+          break;
       }
+      limitLow_State_prev = limitLow_State ;
+      return changed ;
+
     case EVT_ON_LIMIT_HIGH:
-      previous_state = limitHigh_State;
+      // previous_state = limitHigh_State;
       switch(_limitHigh_Mode) {
         case 0:
-          return 0;
+          changed = limitHigh_State != limitHigh_State_prev ? 1 : 0;
+          break;
         case 1: //digital INPUT
           limitHigh_State = digitalRead(_limitHigh_Pin);
           limitHigh_State = _limitHigh_Reversed ? !limitHigh_State : limitHigh_State;
-          return limitHigh_State != previous_state;
+          changed = limitHigh_State != limitHigh_State_prev ? 1 : 0;
+          break;
         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;
-		  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;
+		      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;
+		          }
+          changed = limitHigh_State != limitHigh_State_prev ? 1 : 0;
+          break;
       }
+      limitHigh_State_prev = limitHigh_State_prev;
+      return changed;
+
     case EVT_ON_TARGET:
       return runMode ? 0 : _currentStep == _targetStep;
     case EVT_HOMING_LOW:

+ 2 - 1
Atm_AccelStepper.h

@@ -76,7 +76,8 @@ class Atm_AccelStepper: public Machine {
   Atm_AccelStepper& limitHigh_setThresholds (int threshold_low=510, int threshold_high = 1024);
   bool limitLow_State;
   bool limitHigh_State;
-  bool previous_state = limitLow_State;
+  bool limitLow_State_prev;
+  bool limitHigh_State_prev;
 
 
  private: