Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Cleanup on stepper axis modification

  • Loading branch information...
commit f0d246f76d1ab3939e49fcc1635b295cb172b435 1 parent 31cfc89
Adam Mayer authored October 03, 2011
107  v2/src/shared/StepperAxis.cc
@@ -54,43 +54,52 @@ void StepperAxis::reset() {
54 54
 #endif //SINGLE_SWITCH_ENDSTOPS
55 55
 }
56 56
 
  57
+bool StepperAxis::checkEndstop(const bool isHoming) {
  58
+#if defined(SINGLE_SWITCH_ENDSTOPS) && (SINGLE_SWITCH_ENDSTOPS == 1)
  59
+  bool hit_endstop = direction ? interface->isAtMaximum() : interface->isAtMinimum();
  60
+  // We must move at least ENDSTOP_DEBOUNCE from where we hit the endstop before we declare traveling
  61
+  if (hit_endstop || ((endstop_play < ENDSTOP_DEFAULT_PLAY - ENDSTOP_DEBOUNCE) && endstop_status != ESS_TRAVELING)) {
  62
+    // Did we *just* hit the endstop?
  63
+    if (endstop_status == ESS_TRAVELING || (isHoming && endstop_status == ESS_UNKNOWN)) {
  64
+      endstop_play   = ENDSTOP_DEFAULT_PLAY;
  65
+      if (isHoming?direction:prev_direction)
  66
+	endstop_status = ESS_AT_MAXIMUM;
  67
+      else
  68
+	endstop_status = ESS_AT_MINIMUM;
  69
+      
  70
+      // OR, are we traveling away from the endstop we just hit and still have play...
  71
+    } else if ((direction && endstop_status != ESS_AT_MAXIMUM) || (!direction && endstop_status != ESS_AT_MINIMUM)) {
  72
+      if (endstop_play > 0) {
  73
+	--endstop_play;
  74
+	hit_endstop = false; // pretend this never happened...
  75
+      } else {
  76
+	// we ran out of play, so we must be ramming into the side, switch directions
  77
+	// endstop_status = !direction ? ESS_AT_MAXIMUM : ESS_AT_MINIMUM;
  78
+	// endstop_play   = ENDSTOP_DEFAULT_PLAY;
  79
+      }
  80
+    }
  81
+    // otherwise we hit the endstop
  82
+    
  83
+    // but if we didn't hit an endstop, clear the status
  84
+  } else {
  85
+    endstop_status = ESS_TRAVELING;
  86
+    if (!isHoming) {
  87
+      endstop_play   = ENDSTOP_DEFAULT_PLAY;
  88
+    }
  89
+  }
  90
+  prev_direction = direction;
  91
+  return hit_endstop;
  92
+#else
  93
+  return direction ? interface->isAtMaximum() : interface->isAtMinimum();
  94
+#endif
  95
+}
  96
+
57 97
 void StepperAxis::doInterrupt(const int32_t intervals) {
58 98
         counter += delta;
59 99
         if (counter >= 0) {
60 100
                 interface->setDirection(direction);
61 101
                 counter -= intervals;
62  
-                bool hit_endstop = direction ? interface->isAtMaximum() : interface->isAtMinimum();
63  
-#if defined(SINGLE_SWITCH_ENDSTOPS) && (SINGLE_SWITCH_ENDSTOPS == 1)
64  
-                // We must move at least ENDSTOP_DEBOUNCE from where we hit the endstop before we declare traveling
65  
-                if (hit_endstop || ((endstop_play < ENDSTOP_DEFAULT_PLAY - ENDSTOP_DEBOUNCE) && endstop_status != ESS_TRAVELING)) {
66  
-                // Did we *just* hit the endstop?
67  
-                        if (endstop_status == ESS_TRAVELING) {
68  
-                                endstop_play   = ENDSTOP_DEFAULT_PLAY;
69  
-                                if (prev_direction)
70  
-                                        endstop_status = ESS_AT_MAXIMUM;
71  
-                                else
72  
-                                        endstop_status = ESS_AT_MINIMUM;
73  
-
74  
-                // OR, are we traveling away from the endstop we just hit and still have play...
75  
-                        } else if ((direction && endstop_status != ESS_AT_MAXIMUM) || (!direction && endstop_status != ESS_AT_MINIMUM)) {
76  
-                                if (endstop_play > 0) {
77  
-                                        --endstop_play;
78  
-                                        hit_endstop = false; // pretend this never happened...
79  
-                                } else {
80  
-                                        // we ran out of play, so we must be ramming into the side, switch directions
81  
-                                        // endstop_status = !direction ? ESS_AT_MAXIMUM : ESS_AT_MINIMUM;
82  
-                                        // endstop_play   = ENDSTOP_DEFAULT_PLAY;
83  
-                                }
84  
-                        }
85  
-                // otherwise we hit the endstop
86  
-
87  
-                // but if we didn't hit an endstop, clear the status
88  
-                } else {
89  
-                        endstop_status = ESS_TRAVELING;
90  
-                        endstop_play   = ENDSTOP_DEFAULT_PLAY;
91  
-                }
92  
-                prev_direction = direction;
93  
-#endif //SINGLE_SWITCH_ENDSTOPS
  102
+                bool hit_endstop = checkEndstop(false);
94 103
                 if (direction) {
95 104
                         if (!hit_endstop) interface->step(true);
96 105
                         position++;
@@ -109,39 +118,7 @@ bool StepperAxis::doHoming(const int32_t intervals) {
109 118
         if (counter >= 0) {
110 119
                 interface->setDirection(direction);
111 120
                 counter -= intervals;
112  
-                bool hit_endstop           = direction ? interface->isAtMaximum() : interface->isAtMinimum();
113  
-#if defined(SINGLE_SWITCH_ENDSTOPS) && (SINGLE_SWITCH_ENDSTOPS == 1)
114  
-                // For homing we act a little different .. we assume we are there if we start out hitting the endstop
115  
-                if (hit_endstop|| ((endstop_play < ENDSTOP_DEFAULT_PLAY - ENDSTOP_DEBOUNCE) && endstop_status != ESS_TRAVELING)) {
116  
-                        // Did we *just* hit the endstop?
117  
-                        if (endstop_status == ESS_TRAVELING || endstop_status == ESS_UNKNOWN) {
118  
-                        // we "hit" the endstop, so we'll record this direction as the one we hit
119  
-                                endstop_play   = ENDSTOP_DEFAULT_PLAY;
120  
-                                if (direction)
121  
-                                        endstop_status = ESS_AT_MAXIMUM;
122  
-                                else
123  
-                                        endstop_status = ESS_AT_MINIMUM;
124  
-
125  
-                        // OR, are we traveling away from the endstop we just hit
126  
-                        } else if ((direction && endstop_status != ESS_AT_MAXIMUM) || (!direction && endstop_status != ESS_AT_MINIMUM)) {
127  
-                                if (endstop_play > 0) {
128  
-                                        --endstop_play;
129  
-                                        hit_endstop = false; // pretend this never happened... :-)
130  
-                                } else {
131  
-                                        // we ran out of play, so we must be ramming into the side, switch directions
132  
-                                        // endstop_status = direction ? ESS_AT_MINIMUM : ESS_AT_MAXIMUM;
133  
-                                        // endstop_play   = ENDSTOP_DEFAULT_PLAY;
134  
-                                }
135  
-                        }
136  
-                        // otherwise we hit the endstop
137  
-
138  
-                // but if we didn't hit an endstop, clear the status
139  
-                } else {
140  
-                        endstop_status = ESS_TRAVELING;
141  
-                }
142  
-                prev_direction = direction;
143  
-#endif //SINGLE_SWITCH_ENDSTOPS
144  
-
  121
+                bool hit_endstop = checkEndstop(true);
145 122
                 if (direction) {
146 123
                         if (!hit_endstop) {
147 124
                                 interface->step(true);
16  v2/src/shared/StepperAxis.hh
@@ -33,7 +33,15 @@ public:
33 33
         };
34 34
         
35 35
         volatile endstop_status_t endstop_status;
  36
+
  37
+        // If we started with an endstop triggered, then we don't know where 
  38
+        // we are. We can go this many steps either way until we find out.
  39
+        const static uint16_t ENDSTOP_DEFAULT_PLAY =10000;
  40
+        const static uint16_t ENDSTOP_DEBOUNCE =20;
  41
+
36 42
 #endif //SINGLE_SWITCH_ENDSTOPS
  43
+        // Return true if the endstop for the current direction is triggered.
  44
+        inline bool checkEndstop(const bool isHoming);
37 45
 
38 46
 public:
39 47
         /// Construct a stepper axis with a null interface
@@ -75,12 +83,4 @@ public:
75 83
         bool doHoming(const int32_t intervals);
76 84
 };
77 85
 
78  
-
79  
-#if defined(SINGLE_SWITCH_ENDSTOPS) && (SINGLE_SWITCH_ENDSTOPS == 1)
80  
-// If we started with an endstop triggered, then we don't know where we are
81  
-// So we can go this many steps either way until we find out...
82  
-#define ENDSTOP_DEFAULT_PLAY 10000
83  
-#define ENDSTOP_DEBOUNCE 20
84  
-#endif
85  
-
86 86
 #endif // STEPPERAXIS_HH

0 notes on commit f0d246f

Please sign in to comment.
Something went wrong with that request. Please try again.