Permalink
Browse files

Cleanup on stepper axis modification

  • Loading branch information...
1 parent 31cfc89 commit f0d246f76d1ab3939e49fcc1635b295cb172b435 @phooky phooky committed Oct 3, 2011
Showing with 50 additions and 73 deletions.
  1. +42 −65 v2/src/shared/StepperAxis.cc
  2. +8 −8 v2/src/shared/StepperAxis.hh
View
107 v2/src/shared/StepperAxis.cc
@@ -54,43 +54,52 @@ void StepperAxis::reset() {
#endif //SINGLE_SWITCH_ENDSTOPS
}
+bool StepperAxis::checkEndstop(const bool isHoming) {
+#if defined(SINGLE_SWITCH_ENDSTOPS) && (SINGLE_SWITCH_ENDSTOPS == 1)
+ bool hit_endstop = direction ? interface->isAtMaximum() : interface->isAtMinimum();
+ // We must move at least ENDSTOP_DEBOUNCE from where we hit the endstop before we declare traveling
+ if (hit_endstop || ((endstop_play < ENDSTOP_DEFAULT_PLAY - ENDSTOP_DEBOUNCE) && endstop_status != ESS_TRAVELING)) {
+ // Did we *just* hit the endstop?
+ if (endstop_status == ESS_TRAVELING || (isHoming && endstop_status == ESS_UNKNOWN)) {
+ endstop_play = ENDSTOP_DEFAULT_PLAY;
+ if (isHoming?direction:prev_direction)
+ endstop_status = ESS_AT_MAXIMUM;
+ else
+ endstop_status = ESS_AT_MINIMUM;
+
+ // OR, are we traveling away from the endstop we just hit and still have play...
+ } else if ((direction && endstop_status != ESS_AT_MAXIMUM) || (!direction && endstop_status != ESS_AT_MINIMUM)) {
+ if (endstop_play > 0) {
+ --endstop_play;
+ hit_endstop = false; // pretend this never happened...
+ } else {
+ // we ran out of play, so we must be ramming into the side, switch directions
+ // endstop_status = !direction ? ESS_AT_MAXIMUM : ESS_AT_MINIMUM;
+ // endstop_play = ENDSTOP_DEFAULT_PLAY;
+ }
+ }
+ // otherwise we hit the endstop
+
+ // but if we didn't hit an endstop, clear the status
+ } else {
+ endstop_status = ESS_TRAVELING;
+ if (!isHoming) {
+ endstop_play = ENDSTOP_DEFAULT_PLAY;
+ }
+ }
+ prev_direction = direction;
+ return hit_endstop;
+#else
+ return direction ? interface->isAtMaximum() : interface->isAtMinimum();
+#endif
+}
+
void StepperAxis::doInterrupt(const int32_t intervals) {
counter += delta;
if (counter >= 0) {
interface->setDirection(direction);
counter -= intervals;
- bool hit_endstop = direction ? interface->isAtMaximum() : interface->isAtMinimum();
-#if defined(SINGLE_SWITCH_ENDSTOPS) && (SINGLE_SWITCH_ENDSTOPS == 1)
- // We must move at least ENDSTOP_DEBOUNCE from where we hit the endstop before we declare traveling
- if (hit_endstop || ((endstop_play < ENDSTOP_DEFAULT_PLAY - ENDSTOP_DEBOUNCE) && endstop_status != ESS_TRAVELING)) {
- // Did we *just* hit the endstop?
- if (endstop_status == ESS_TRAVELING) {
- endstop_play = ENDSTOP_DEFAULT_PLAY;
- if (prev_direction)
- endstop_status = ESS_AT_MAXIMUM;
- else
- endstop_status = ESS_AT_MINIMUM;
-
- // OR, are we traveling away from the endstop we just hit and still have play...
- } else if ((direction && endstop_status != ESS_AT_MAXIMUM) || (!direction && endstop_status != ESS_AT_MINIMUM)) {
- if (endstop_play > 0) {
- --endstop_play;
- hit_endstop = false; // pretend this never happened...
- } else {
- // we ran out of play, so we must be ramming into the side, switch directions
- // endstop_status = !direction ? ESS_AT_MAXIMUM : ESS_AT_MINIMUM;
- // endstop_play = ENDSTOP_DEFAULT_PLAY;
- }
- }
- // otherwise we hit the endstop
-
- // but if we didn't hit an endstop, clear the status
- } else {
- endstop_status = ESS_TRAVELING;
- endstop_play = ENDSTOP_DEFAULT_PLAY;
- }
- prev_direction = direction;
-#endif //SINGLE_SWITCH_ENDSTOPS
+ bool hit_endstop = checkEndstop(false);
if (direction) {
if (!hit_endstop) interface->step(true);
position++;
@@ -109,39 +118,7 @@ bool StepperAxis::doHoming(const int32_t intervals) {
if (counter >= 0) {
interface->setDirection(direction);
counter -= intervals;
- bool hit_endstop = direction ? interface->isAtMaximum() : interface->isAtMinimum();
-#if defined(SINGLE_SWITCH_ENDSTOPS) && (SINGLE_SWITCH_ENDSTOPS == 1)
- // For homing we act a little different .. we assume we are there if we start out hitting the endstop
- if (hit_endstop|| ((endstop_play < ENDSTOP_DEFAULT_PLAY - ENDSTOP_DEBOUNCE) && endstop_status != ESS_TRAVELING)) {
- // Did we *just* hit the endstop?
- if (endstop_status == ESS_TRAVELING || endstop_status == ESS_UNKNOWN) {
- // we "hit" the endstop, so we'll record this direction as the one we hit
- endstop_play = ENDSTOP_DEFAULT_PLAY;
- if (direction)
- endstop_status = ESS_AT_MAXIMUM;
- else
- endstop_status = ESS_AT_MINIMUM;
-
- // OR, are we traveling away from the endstop we just hit
- } else if ((direction && endstop_status != ESS_AT_MAXIMUM) || (!direction && endstop_status != ESS_AT_MINIMUM)) {
- if (endstop_play > 0) {
- --endstop_play;
- hit_endstop = false; // pretend this never happened... :-)
- } else {
- // we ran out of play, so we must be ramming into the side, switch directions
- // endstop_status = direction ? ESS_AT_MINIMUM : ESS_AT_MAXIMUM;
- // endstop_play = ENDSTOP_DEFAULT_PLAY;
- }
- }
- // otherwise we hit the endstop
-
- // but if we didn't hit an endstop, clear the status
- } else {
- endstop_status = ESS_TRAVELING;
- }
- prev_direction = direction;
-#endif //SINGLE_SWITCH_ENDSTOPS
-
+ bool hit_endstop = checkEndstop(true);
if (direction) {
if (!hit_endstop) {
interface->step(true);
View
16 v2/src/shared/StepperAxis.hh
@@ -33,7 +33,15 @@ public:
};
volatile endstop_status_t endstop_status;
+
+ // If we started with an endstop triggered, then we don't know where
+ // we are. We can go this many steps either way until we find out.
+ const static uint16_t ENDSTOP_DEFAULT_PLAY =10000;
+ const static uint16_t ENDSTOP_DEBOUNCE =20;
+
#endif //SINGLE_SWITCH_ENDSTOPS
+ // Return true if the endstop for the current direction is triggered.
+ inline bool checkEndstop(const bool isHoming);
public:
/// Construct a stepper axis with a null interface
@@ -75,12 +83,4 @@ public:
bool doHoming(const int32_t intervals);
};
-
-#if defined(SINGLE_SWITCH_ENDSTOPS) && (SINGLE_SWITCH_ENDSTOPS == 1)
-// If we started with an endstop triggered, then we don't know where we are
-// So we can go this many steps either way until we find out...
-#define ENDSTOP_DEFAULT_PLAY 10000
-#define ENDSTOP_DEBOUNCE 20
-#endif
-
#endif // STEPPERAXIS_HH

0 comments on commit f0d246f

Please sign in to comment.