@@ -43,13 +43,39 @@ HomingState HallSensorHoming::getHomingState() const
4343 return _homingData.state ;
4444}
4545
46+ // ///////////////////////////////
47+ //
48+ // getLastResult
49+ //
50+ // ///////////////////////////////
51+ String HallSensorHoming::getLastResult () const
52+ {
53+ MappedDict<int , String>::DictEntry_t lookupTable[] = {
54+ {HOMING_RESULT_SUCCEEDED, F (" SUCCEEDED" )},
55+ {HOMING_RESULT_HOMING_NEVER_RUN, F (" NEVER RUN" )},
56+ {HOMING_RESULT_HOMING_IN_PROGRESS, F (" IN PROGRESS" )},
57+ {HOMING_RESULT_CANT_MOVE_OFF_SENSOR, F (" CANT MOVE OFF SENSOR" )},
58+ {HOMING_RESULT_CANT_FIND_SENSOR_ON_REVERSE, F (" CANT FIND SENSOR BEGIN" )},
59+ {HOMING_RESULT_CANT_FIND_SENSOR_END, F (" CANT FIND SENSOR END" )},
60+ };
61+
62+ auto strLookup = MappedDict<int , String>(lookupTable, ARRAY_SIZE (lookupTable));
63+ String rtnStr;
64+ if (strLookup.tryGet (_lastResult, &rtnStr))
65+ {
66+ return rtnStr;
67+ }
68+ return F (" WTF_RESULT" );
69+ }
70+
4671// ///////////////////////////////
4772//
4873// findHomeByHallSensor
4974//
5075// ///////////////////////////////
5176bool HallSensorHoming::findHomeByHallSensor (int initialDirection, int searchDistance)
5277{
78+ _lastResult = HOMING_RESULT_HOMING_IN_PROGRESS;
5379 _homingData.startPos = _pMount->getCurrentStepperPosition (_axis);
5480 _homingData.savedRate = _pMount->getSlewRate ();
5581 _homingData.initialDir = initialDirection;
@@ -149,6 +175,7 @@ void HallSensorHoming::processHomingProgress()
149175 " [HOMING]: Stepper was unable to move off sensor... homing failed! Advance to %s" ,
150176 getHomingState (HomingState::HOMING_FAILED).c_str ());
151177 _homingData.state = HomingState::HOMING_FAILED;
178+ _lastResult = HOMING_RESULT_CANT_MOVE_OFF_SENSOR;
152179 }
153180 }
154181 break ;
@@ -179,14 +206,41 @@ void HallSensorHoming::processHomingProgress()
179206 int homingPinState = digitalRead (_sensorPin);
180207 if (_homingData.lastPinState != homingPinState)
181208 {
182- LOG (DEBUG_STEPPERS,
183- " [HOMING]: Found start of sensor at %l, continuing until end is found. Advance to %s" ,
184- _pMount->getCurrentStepperPosition (_axis),
185- getHomingState (HomingState::HOMING_FINDING_END).c_str ());
186- // Found the start of the sensor, keep going until we find the end
187- _homingData.position [HOMING_START_PIN_POSITION] = _pMount->getCurrentStepperPosition (_axis);
188- _homingData.lastPinState = homingPinState;
189- _homingData.state = HomingState::HOMING_FINDING_END;
209+ if (_homingData.pinChangeCount == 0 )
210+ {
211+ // First time the pin has triggered, record that position
212+ _homingData.position [HOMING_START_PIN_POSITION] = _pMount->getCurrentStepperPosition (_axis);
213+ LOG (DEBUG_STEPPERS,
214+ " [HOMING]: Potentially found start of sensor at %l" ,
215+ _homingData.position [HOMING_START_PIN_POSITION]);
216+ }
217+
218+ // Keep track of how many times the pin has been triggered
219+ _homingData.pinChangeCount ++;
220+
221+ if (_homingData.pinChangeCount > 4 )
222+ {
223+ // If we have seen this pin stay triggered for 5 cycles, assume we've found the start of the sensor, now
224+ // change state to keep going until we find the end
225+ LOG (DEBUG_STEPPERS,
226+ " [HOMING]: Found start of sensor at %l, continuing until end is found. Advance to %s" ,
227+ _homingData.position [HOMING_START_PIN_POSITION],
228+ getHomingState (HomingState::HOMING_FINDING_END).c_str ());
229+
230+ // Make sure we continue moving far enough to reach end
231+ long distance = _homingData.initialDir * _stepsPerDegree * _homingData.searchDistance ;
232+ _pMount->moveStepperBy (_axis, distance);
233+
234+ _homingData.lastPinState = homingPinState;
235+ _homingData.pinChangeCount = 0 ;
236+ _homingData.state = HomingState::HOMING_FINDING_END;
237+ }
238+ }
239+ else
240+ {
241+ // The pin is not triggered, so clear the recorded position and change count
242+ _homingData.position [HOMING_START_PIN_POSITION] = 0 ;
243+ _homingData.pinChangeCount = 0 ;
190244 }
191245 }
192246 else
@@ -211,13 +265,41 @@ void HallSensorHoming::processHomingProgress()
211265 int homingPinState = digitalRead (_sensorPin);
212266 if (_homingData.lastPinState != homingPinState)
213267 {
214- LOG (DEBUG_STEPPERS,
215- " [HOMING]: Found start of sensor in reverse at %l, continuing until end is found. Advance to %s" ,
216- _pMount->getCurrentStepperPosition (_axis),
217- getHomingState (HomingState::HOMING_FINDING_END).c_str ());
218- _homingData.position [HOMING_START_PIN_POSITION] = _pMount->getCurrentStepperPosition (_axis);
219- _homingData.lastPinState = homingPinState;
220- _homingData.state = HomingState::HOMING_FINDING_END;
268+ if (_homingData.pinChangeCount == 0 )
269+ {
270+ // First time the pin has triggered, record that position
271+ _homingData.position [HOMING_START_PIN_POSITION] = _pMount->getCurrentStepperPosition (_axis);
272+ LOG (DEBUG_STEPPERS,
273+ " [HOMING]: Potentially found start of sensor in reverse pass at %l" ,
274+ _homingData.position [HOMING_START_PIN_POSITION]);
275+ }
276+
277+ // Keep track of how many times the pin has been triggered
278+ _homingData.pinChangeCount ++;
279+
280+ if (_homingData.pinChangeCount > 4 )
281+ {
282+ // If we have seen this pin stay triggered for 5 cycles, assume we've found the start of the sensor, now
283+ // change state to keep going until we find the end
284+ LOG (DEBUG_STEPPERS,
285+ " [HOMING]: Found start of sensor in reverse at %l, continuing until end is found. Advance to %s" ,
286+ _homingData.position [HOMING_START_PIN_POSITION],
287+ getHomingState (HomingState::HOMING_FINDING_END).c_str ());
288+
289+ // Make sure we continue moving far enough to reach end
290+ long distance = -_homingData.initialDir * _stepsPerDegree * _homingData.searchDistance ;
291+ _pMount->moveStepperBy (_axis, distance);
292+
293+ _homingData.lastPinState = homingPinState;
294+ _homingData.pinChangeCount = 0 ;
295+ _homingData.state = HomingState::HOMING_FINDING_END;
296+ }
297+ }
298+ else
299+ {
300+ // The pin is not triggered, so clear the recorded position and change count
301+ _homingData.position [HOMING_START_PIN_POSITION] = 0 ;
302+ _homingData.pinChangeCount = 0 ;
221303 }
222304 }
223305 else
@@ -227,6 +309,7 @@ void HallSensorHoming::processHomingProgress()
227309 " [HOMING]: Sensor not found on reverse pass either. Homing Failed. Advance to %s" ,
228310 getHomingState (HomingState::HOMING_FAILED).c_str ());
229311 _homingData.state = HomingState::HOMING_FAILED;
312+ _lastResult = HOMING_RESULT_CANT_FIND_SENSOR_ON_REVERSE;
230313 }
231314 }
232315 break ;
@@ -238,15 +321,36 @@ void HallSensorHoming::processHomingProgress()
238321 int homingPinState = digitalRead (_sensorPin);
239322 if (_homingData.lastPinState != homingPinState)
240323 {
241- LOG (DEBUG_STEPPERS,
242- " [HOMING]: Found end of sensor at %l, stopping... Advance to %s" ,
243- _pMount->getCurrentStepperPosition (_axis),
244- getHomingState (HomingState::HOMING_WAIT_FOR_STOP).c_str ());
245- _homingData.position [HOMING_END_PIN_POSITION] = _pMount->getCurrentStepperPosition (_axis);
246- _homingData.lastPinState = homingPinState;
247- _homingData.state = HomingState::HOMING_WAIT_FOR_STOP;
248- _homingData.nextState = HomingState::HOMING_RANGE_FOUND;
249- _pMount->stopSlewing (_axis);
324+ if (_homingData.pinChangeCount == 0 )
325+ {
326+ _homingData.position [HOMING_END_PIN_POSITION] = _pMount->getCurrentStepperPosition (_axis);
327+ LOG (DEBUG_STEPPERS,
328+ " [HOMING]: Potentially found end of sensor at %l" ,
329+ _homingData.position [HOMING_END_PIN_POSITION]);
330+ }
331+
332+ // Keep track of how many times the pin has been triggered
333+ _homingData.pinChangeCount ++;
334+
335+ if (_homingData.pinChangeCount > 4 )
336+ {
337+ // If we have seen this pin stay triggered for 5 cycles, assume we've found the start of the sensor, now
338+ // change state to keep going until we find the end
339+ LOG (DEBUG_STEPPERS,
340+ " [HOMING]: Found end of sensor at %l, stopping... Advance to %s" ,
341+ _homingData.position [HOMING_END_PIN_POSITION],
342+ getHomingState (HomingState::HOMING_WAIT_FOR_STOP).c_str ());
343+ _homingData.lastPinState = homingPinState;
344+ _homingData.pinChangeCount = 0 ;
345+ _homingData.state = HomingState::HOMING_WAIT_FOR_STOP;
346+ _homingData.nextState = HomingState::HOMING_RANGE_FOUND;
347+ _pMount->stopSlewing (_axis);
348+ }
349+ }
350+ else
351+ {
352+ _homingData.position [HOMING_END_PIN_POSITION] = 0 ;
353+ _homingData.pinChangeCount = 0 ;
250354 }
251355 }
252356 else
@@ -255,19 +359,21 @@ void HallSensorHoming::processHomingProgress()
255359 " [HOMING]: End of sensor not found! Advance to %s" ,
256360 getHomingState (HomingState::HOMING_FAILED).c_str ());
257361 _homingData.state = HomingState::HOMING_FAILED;
362+ _lastResult = HOMING_RESULT_CANT_FIND_SENSOR_END;
258363 }
259364 }
260365 break ;
261366
262367 case HomingState::HOMING_RANGE_FOUND:
263368 {
369+ long midPos = (_homingData.position [HOMING_START_PIN_POSITION] + _homingData.position [HOMING_END_PIN_POSITION]) / 2 ;
370+
264371 LOG (DEBUG_STEPPERS,
265- " [HOMING]: Stepper stopped, Hall sensor found! Range: [%l to %l] size: %l" ,
372+ " [HOMING]: Stepper stopped, Hall sensor found! Range: [%l to %l] size: %l, MidPos: %l " ,
266373 _homingData.position [HOMING_START_PIN_POSITION],
267374 _homingData.position [HOMING_END_PIN_POSITION],
268- _homingData.position [HOMING_START_PIN_POSITION] - _homingData.position [HOMING_END_PIN_POSITION]);
269-
270- long midPos = (_homingData.position [HOMING_START_PIN_POSITION] + _homingData.position [HOMING_END_PIN_POSITION]) / 2 ;
375+ _homingData.position [HOMING_START_PIN_POSITION] - _homingData.position [HOMING_END_PIN_POSITION],
376+ midPos);
271377
272378 LOG (DEBUG_STEPPERS,
273379 " [HOMING]: Moving home by %l - (%l) - (%l), so %l steps. Advance to %s" ,
@@ -289,10 +395,15 @@ void HallSensorHoming::processHomingProgress()
289395 LOG (DEBUG_STEPPERS,
290396 " [HOMING]: Successfully homed! Setting home and restoring Rate setting. Advance to %s" ,
291397 getHomingState (HomingState::HOMING_NOT_ACTIVE).c_str ());
398+ _lastResult = HOMING_RESULT_SUCCEEDED;
292399 _homingData.state = HomingState::HOMING_NOT_ACTIVE;
293400 _pMount->setHome (false );
294401 _pMount->setSlewRate (_homingData.savedRate );
295402 _pMount->clearStatusFlag (STATUS_FINDING_HOME);
403+ if (_wasTracking)
404+ {
405+ _pMount->startSlewing (TRACKING);
406+ }
296407 }
297408 break ;
298409
0 commit comments