Browse Source

added limit averaging

to prevent false detection
titi 5 years ago
parent
commit
87cba924cf
2 changed files with 62 additions and 25 deletions
  1. 51 24
      Atm_AccelStepper.cpp
  2. 11 1
      Atm_AccelStepper.h

+ 51 - 24
Atm_AccelStepper.cpp

@@ -33,7 +33,7 @@ Atm_AccelStepper& Atm_AccelStepper::begin(int step_pin, int dir_pin) {
  */
 
 int Atm_AccelStepper::event( int id ) {
-  //updateLimitSwitch();
+  bool tempState ;
   bool changed = 0 ;
   switch ( id ) {
     case EVT_DISABLE:
@@ -49,56 +49,55 @@ int Atm_AccelStepper::event( int id ) {
     case EVT_EMERGENCY_STOP:
       return 0;
     case EVT_ON_LIMIT_LOW:
-      // previous_state = limitLow_State;
-
       switch(_limitLow_Mode) {
 
         case 0:
           break;
         case 1: //digital INPUT
           // Serial.println("digital");
-          limitLow_State = digitalRead(_limitLow_Pin);
-          limitLow_State = _limitLow_Reversed ? !limitLow_State : limitLow_State;
+          limitLow_State_raw = digitalRead(_limitLow_Pin);
+          limitLow_State_raw = _limitLow_Reversed ? !limitLow_State_raw : limitLow_State_raw;
           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;
-		        }
+          limitLow_State_raw = (_limitLow_Thresholds[0] < analogTemp) && (analogTemp < _limitLow_Thresholds[1]);
+          limitLow_State_raw = _limitLow_Reversed ? !limitLow_State_raw : limitLow_State_raw;
+          // 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;
+		      //   }
           break;
         }
+        limitLow_State = limitLow_avg(limitLow_State_raw);
         changed = limitLow_State != limitLow_State_prev ? 1 : 0 ;
         limitLow_State_prev = limitLow_State ;
         return changed ;
 
 
     case EVT_ON_LIMIT_HIGH:
-      // previous_state = limitHigh_State;
       switch(_limitHigh_Mode) {
         case 0:
           break;
         case 1: //digital INPUT
-          limitHigh_State = digitalRead(_limitHigh_Pin);
-          limitHigh_State = _limitHigh_Reversed ? !limitHigh_State : limitHigh_State;
+          limitHigh_State_raw = digitalRead(_limitHigh_Pin);
+          limitHigh_State_raw = _limitHigh_Reversed ? !limitHigh_State_raw : limitHigh_State_raw;
           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;
-		          }
+          limitHigh_State_raw = (_limitHigh_Thresholds[0] < analogTemp) && (analogTemp < _limitHigh_Thresholds[1]);
+          limitHigh_State_raw = _limitHigh_Reversed ? !limitHigh_State_raw : limitHigh_State_raw;
+		      // 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;
+		      //     }
           break;
         }
+        limitHigh_State = limitHigh_avg(limitHigh_State_raw);
         changed = limitHigh_State != limitHigh_State_prev ? 1 : 0;
         limitHigh_State_prev = limitHigh_State;
         return changed;
@@ -415,6 +414,34 @@ Atm_AccelStepper& Atm_AccelStepper::homing( bool direction ){
   return *this;
 }
 
+int Atm_AccelStepper::limitLow_avg(bool limistate){
+  limitLow_state_total = limitLow_state_total + limistate - limitLow_state_buf[limitLow_buf_head];
+  limitLow_state_buf[limitLow_buf_head] = limistate;
+  if ( limitLow_buf_head + 1 >= limit_buf_size ) {
+    limitLow_buf_head = 0;
+  } else {
+    limitLow_buf_head++;
+  }
+  for (int i =0; i<limit_buf_size; i++){
+    Serial.print(limitLow_state_buf[i]);
+    Serial.print(" ");
+  }
+  Serial.println();
+  return limitLow_state_total > limit_buf_size / 2; //all values should agree
+}
+
+int Atm_AccelStepper::limitHigh_avg(bool limistate){
+  limitHigh_state_total = limitHigh_state_total + limistate - limitHigh_state_buf[limitHigh_buf_head];
+  limitHigh_state_buf[limitHigh_buf_head] = limistate;
+  if ( limitHigh_buf_head + 1 >= limit_buf_size ) {
+    limitHigh_buf_head = 0;
+  } else {
+    limitHigh_buf_head++;
+  }
+
+  return limitHigh_state_total < limit_buf_size / 2; //all values should agree
+}
+
 // Atm_AccelStepper& Atm_AccelStepper::rotationReversed(bool reversed){
 //   _rotationReversed = reversed ? -1 : 1 ;
 // }

+ 11 - 1
Atm_AccelStepper.h

@@ -76,11 +76,21 @@ class Atm_AccelStepper: public Machine {
   Atm_AccelStepper& limitHigh_set(int mode = 0, int pin = -1, int reversed=0);
   Atm_AccelStepper& limitHigh_isHard(bool hardlimit = 1);
   Atm_AccelStepper& limitHigh_setThresholds (int threshold_low=510, int threshold_high = 1024);
+  bool limitLow_State_raw;
   bool limitLow_State;
   bool limitHigh_State;
+  bool limitHigh_State_raw;
   bool limitLow_State_prev;
   bool limitHigh_State_prev;
-
+  const static int limit_buf_size = 20 ;
+  bool limitLow_state_buf [limit_buf_size] ;
+  int limitLow_state_total ;
+  int limitLow_buf_head ;
+  int limitLow_avg(bool limitState);
+  bool limitHigh_state_buf [limit_buf_size] ;
+  int limitHigh_state_total ;
+  int limitHigh_buf_head ;
+  int limitHigh_avg(bool limitState);
 
  private:
   // ACTIONS