-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathHallSensorHoming.cpp
More file actions
441 lines (399 loc) · 19.4 KB
/
HallSensorHoming.cpp
File metadata and controls
441 lines (399 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include "../Configuration.hpp"
#include "Utility.hpp"
#include "EPROMStore.hpp"
#include "HallSensorHoming.hpp"
#include "libs/MappedDict/MappedDict.hpp"
#define HOMING_START_PIN_POSITION 0
#define HOMING_END_PIN_POSITION 1
/////////////////////////////////
//
// getHomingState
//
/////////////////////////////////
String HallSensorHoming::getHomingState(HomingState state) const
{
MappedDict<HomingState, String>::DictEntry_t lookupTable[] = {
{HOMING_MOVE_OFF, F("MOVE_OFF")},
{HOMING_MOVING_OFF, F("MOVING_OFF")},
{HOMING_STOP_AT_TIME, F("STOP_AT_TIME")},
{HOMING_WAIT_FOR_STOP, F("WAIT_FOR_STOP")},
{HOMING_START_FIND_START, F("START_FIND_START")},
{HOMING_FINDING_START, F("FINDING_START")},
{HOMING_FINDING_START_REVERSE, F("FINDING_START_REVERSE")},
{HOMING_FINDING_END, F("FINDING_END")},
{HOMING_RANGE_FOUND, F("RANGE_FOUND")},
{HOMING_FAILED, F("FAILED")},
{HOMING_SUCCESSFUL, F("SUCCESSFUL")},
{HOMING_NOT_ACTIVE, F("NOT_ACTIVE")},
};
auto strLookup = MappedDict<HomingState, String>(lookupTable, ARRAY_SIZE(lookupTable));
String rtnStr;
if (strLookup.tryGet(state, &rtnStr))
{
return rtnStr;
}
return F("WTF_STATE");
}
HomingState HallSensorHoming::getHomingState() const
{
return _homingData.state;
}
/////////////////////////////////
//
// getLastResult
//
/////////////////////////////////
String HallSensorHoming::getLastResult() const
{
MappedDict<int, String>::DictEntry_t lookupTable[] = {
{HOMING_RESULT_SUCCEEDED, F("SUCCEEDED")},
{HOMING_RESULT_HOMING_NEVER_RUN, F("NEVER RUN")},
{HOMING_RESULT_HOMING_IN_PROGRESS, F("IN PROGRESS")},
{HOMING_RESULT_CANT_MOVE_OFF_SENSOR, F("CANT MOVE OFF SENSOR")},
{HOMING_RESULT_CANT_FIND_SENSOR_ON_REVERSE, F("CANT FIND SENSOR BEGIN")},
{HOMING_RESULT_CANT_FIND_SENSOR_END, F("CANT FIND SENSOR END")},
};
auto strLookup = MappedDict<int, String>(lookupTable, ARRAY_SIZE(lookupTable));
String rtnStr;
if (strLookup.tryGet(_lastResult, &rtnStr))
{
return rtnStr;
}
return F("WTF_RESULT");
}
/////////////////////////////////
//
// findHomeByHallSensor
//
/////////////////////////////////
bool HallSensorHoming::findHomeByHallSensor(int initialDirection, int searchDistance)
{
_lastResult = HOMING_RESULT_HOMING_IN_PROGRESS;
_homingData.startPos = _pMount->getCurrentStepperPosition(_axis);
_homingData.savedRate = _pMount->getSlewRate();
_homingData.initialDir = initialDirection;
_homingData.searchDistance = searchDistance;
_pMount->setSlewRate(4);
_pMount->setStatusFlag(STATUS_FINDING_HOME);
LOG(DEBUG_STEPPERS,
"[HOMING]: Start homing procedure. Axis %d, StepsPerDegree: %l, SearchDist: %d",
(int) _axis,
_stepsPerDegree,
searchDistance);
// Check where we are over the sensor already
if (digitalRead(_sensorPin) == _activeState)
{
_homingData.state = HomingState::HOMING_MOVE_OFF;
LOG(DEBUG_STEPPERS, "[HOMING]: Sensor is signalled, move off sensor started");
}
else
{
_homingData.state = HomingState::HOMING_START_FIND_START;
LOG(DEBUG_STEPPERS, "[HOMING]: Sensor is not signalled, find start of range");
}
return true;
}
/////////////////////////////////
//
// processHomingProgress
//
/////////////////////////////////
void HallSensorHoming::processHomingProgress()
{
switch (_homingData.state)
{
case HomingState::HOMING_NOT_ACTIVE:
break;
case HomingState::HOMING_STOP_AT_TIME:
{
if (millis() > _homingData.stopAt)
{
LOG(DEBUG_STEPPERS,
"[HOMING]: Initiating stop at requested time. Advance to state %s",
getHomingState(HomingState::HOMING_WAIT_FOR_STOP).c_str());
_pMount->stopSlewing(_axis);
_homingData.state = HomingState::HOMING_WAIT_FOR_STOP;
}
}
break;
case HomingState::HOMING_WAIT_FOR_STOP:
{
if (!_pMount->isAxisRunning(_axis))
{
LOG(DEBUG_STEPPERS,
"[HOMING]: Stepper has stopped as expected, advancing to next state %s",
getHomingState(_homingData.nextState).c_str());
_homingData.state = _homingData.nextState;
_homingData.nextState = HomingState::HOMING_NOT_ACTIVE;
}
}
break;
case HomingState::HOMING_MOVE_OFF:
{
LOG(DEBUG_STEPPERS,
"[HOMING]: Currently over Sensor, so moving off of it by reverse 15deg. (%l steps). Advance to %s",
(-_homingData.initialDir * _stepsPerDegree * 15),
getHomingState(HomingState::HOMING_MOVING_OFF).c_str());
_pMount->moveStepperBy(_axis, -_homingData.initialDir * _stepsPerDegree * 15);
_homingData.state = HomingState::HOMING_MOVING_OFF;
}
break;
case HomingState::HOMING_MOVING_OFF:
{
if (_pMount->isAxisRunning(_axis))
{
int homingPinState = digitalRead(_sensorPin);
if (homingPinState != _activeState)
{
LOG(DEBUG_STEPPERS,
"[HOMING]: Stepper has moved off sensor... stopping in 2s. Advance to %s",
getHomingState(HomingState::HOMING_STOP_AT_TIME).c_str());
_homingData.stopAt = millis() + 2000;
_homingData.state = HomingState::HOMING_STOP_AT_TIME;
_homingData.nextState = HomingState::HOMING_START_FIND_START;
}
}
else
{
LOG(DEBUG_STEPPERS,
"[HOMING]: Stepper was unable to move off sensor... homing failed! Advance to %s",
getHomingState(HomingState::HOMING_FAILED).c_str());
_homingData.state = HomingState::HOMING_FAILED;
_lastResult = HOMING_RESULT_CANT_MOVE_OFF_SENSOR;
}
}
break;
case HomingState::HOMING_START_FIND_START:
{
long distance = _homingData.initialDir * _stepsPerDegree * _homingData.searchDistance;
LOG(DEBUG_STEPPERS,
"[HOMING]: Finding start on forward pass by moving by %l deg (%l steps). Advance to %s",
_homingData.searchDistance,
distance,
getHomingState(HomingState::HOMING_FINDING_START).c_str());
_homingData.pinState = _homingData.lastPinState = digitalRead(_sensorPin);
_homingData.position[HOMING_START_PIN_POSITION] = 0;
_homingData.position[HOMING_END_PIN_POSITION] = 0;
// Move in initial direction
_pMount->moveStepperBy(_axis, distance);
_homingData.state = HomingState::HOMING_FINDING_START;
}
break;
case HomingState::HOMING_FINDING_START:
{
if (_pMount->isAxisRunning(_axis))
{
int homingPinState = digitalRead(_sensorPin);
if (_homingData.lastPinState != homingPinState)
{
if (_homingData.pinChangeCount == 0)
{
// First time the pin has triggered, record that position
_homingData.position[HOMING_START_PIN_POSITION] = _pMount->getCurrentStepperPosition(_axis);
LOG(DEBUG_STEPPERS,
"[HOMING]: Potentially found start of sensor at %l",
_homingData.position[HOMING_START_PIN_POSITION]);
}
// Keep track of how many times the pin has been triggered
_homingData.pinChangeCount++;
if (_homingData.pinChangeCount > 4)
{
// If we have seen this pin stay triggered for 5 cycles, assume we've found the start of the sensor, now
// change state to keep going until we find the end
LOG(DEBUG_STEPPERS,
"[HOMING]: Found start of sensor at %l, continuing until end is found. Advance to %s",
_homingData.position[HOMING_START_PIN_POSITION],
getHomingState(HomingState::HOMING_FINDING_END).c_str());
// Make sure we continue moving far enough to reach end
long distance = _homingData.initialDir * _stepsPerDegree * _homingData.searchDistance;
_pMount->moveStepperBy(_axis, distance);
LOG(DEBUG_STEPPERS,
"[HOMING]: Making sure stepper keeps going by another %l steps. New target is %l",
distance,
_pMount->getCurrentStepperPosition(_axis) + distance);
_homingData.lastPinState = homingPinState;
_homingData.pinChangeCount = 0;
_homingData.state = HomingState::HOMING_FINDING_END;
}
}
else
{
// The pin is not triggered, so clear the recorded position and change count
_homingData.position[HOMING_START_PIN_POSITION] = 0;
_homingData.pinChangeCount = 0;
}
}
else
{
// Did not find start. Go reverse direction for twice the distance
long distance = -_homingData.initialDir * _stepsPerDegree * _homingData.searchDistance * 2L;
LOG(DEBUG_STEPPERS,
"[HOMING]: Hall not found on forward pass. Moving reverse by %l deg (%l steps). Advance to %s",
2 * _homingData.searchDistance,
distance,
getHomingState(HomingState::HOMING_FINDING_START_REVERSE).c_str());
_pMount->moveStepperBy(_axis, distance);
_homingData.state = HomingState::HOMING_FINDING_START_REVERSE;
}
}
break;
case HomingState::HOMING_FINDING_START_REVERSE:
{
if (_pMount->isAxisRunning(_axis))
{
int homingPinState = digitalRead(_sensorPin);
if (_homingData.lastPinState != homingPinState)
{
if (_homingData.pinChangeCount == 0)
{
// First time the pin has triggered, record that position
_homingData.position[HOMING_START_PIN_POSITION] = _pMount->getCurrentStepperPosition(_axis);
LOG(DEBUG_STEPPERS,
"[HOMING]: Potentially found start of sensor in reverse pass at %l",
_homingData.position[HOMING_START_PIN_POSITION]);
}
// Keep track of how many times the pin has been triggered
_homingData.pinChangeCount++;
if (_homingData.pinChangeCount > 4)
{
// If we have seen this pin stay triggered for 5 cycles, assume we've found the start of the sensor, now
// change state to keep going until we find the end
LOG(DEBUG_STEPPERS,
"[HOMING]: Found start of sensor in reverse at %l, continuing until end is found. Advance to %s",
_homingData.position[HOMING_START_PIN_POSITION],
getHomingState(HomingState::HOMING_FINDING_END).c_str());
// Make sure we continue moving far enough to reach end
long distance = -_homingData.initialDir * _stepsPerDegree * _homingData.searchDistance;
_pMount->moveStepperBy(_axis, distance);
LOG(DEBUG_STEPPERS,
"[HOMING]: Making sure stepper keeps going by another %l steps. New target is %l",
distance,
_pMount->getCurrentStepperPosition(_axis) + distance);
_homingData.lastPinState = homingPinState;
_homingData.pinChangeCount = 0;
_homingData.state = HomingState::HOMING_FINDING_END;
}
}
else
{
// The pin is not triggered, so clear the recorded position and change count
_homingData.position[HOMING_START_PIN_POSITION] = 0;
_homingData.pinChangeCount = 0;
}
}
else
{
// Did not find start in either direction, abort.
LOG(DEBUG_STEPPERS,
"[HOMING]: Sensor not found on reverse pass either. Homing Failed. Advance to %s",
getHomingState(HomingState::HOMING_FAILED).c_str());
_homingData.state = HomingState::HOMING_FAILED;
_lastResult = HOMING_RESULT_CANT_FIND_SENSOR_ON_REVERSE;
}
}
break;
case HomingState::HOMING_FINDING_END:
{
if (_pMount->isAxisRunning(_axis))
{
int homingPinState = digitalRead(_sensorPin);
if (_homingData.lastPinState != homingPinState)
{
if (_homingData.pinChangeCount == 0)
{
_homingData.position[HOMING_END_PIN_POSITION] = _pMount->getCurrentStepperPosition(_axis);
LOG(DEBUG_STEPPERS,
"[HOMING]: Potentially found end of sensor at %l",
_homingData.position[HOMING_END_PIN_POSITION]);
}
// Keep track of how many times the pin has been triggered
_homingData.pinChangeCount++;
if (_homingData.pinChangeCount > 4)
{
// If we have seen this pin stay triggered for 5 cycles, assume we've found the start of the sensor, now
// change state to keep going until we find the end
LOG(DEBUG_STEPPERS,
"[HOMING]: Found end of sensor at %l, stopping... Advance to %s",
_homingData.position[HOMING_END_PIN_POSITION],
getHomingState(HomingState::HOMING_WAIT_FOR_STOP).c_str());
_homingData.lastPinState = homingPinState;
_homingData.pinChangeCount = 0;
_homingData.state = HomingState::HOMING_WAIT_FOR_STOP;
_homingData.nextState = HomingState::HOMING_RANGE_FOUND;
_pMount->stopSlewing(_axis);
}
}
else
{
_homingData.position[HOMING_END_PIN_POSITION] = 0;
_homingData.pinChangeCount = 0;
}
}
else
{
LOG(DEBUG_STEPPERS,
"[HOMING]: End of sensor not found! Advance to %s",
getHomingState(HomingState::HOMING_FAILED).c_str());
_homingData.state = HomingState::HOMING_FAILED;
_lastResult = HOMING_RESULT_CANT_FIND_SENSOR_END;
}
}
break;
case HomingState::HOMING_RANGE_FOUND:
{
long midPos = (_homingData.position[HOMING_START_PIN_POSITION] + _homingData.position[HOMING_END_PIN_POSITION]) / 2;
LOG(DEBUG_STEPPERS,
"[HOMING]: Stepper stopped, Hall sensor found! Range: [%l to %l] size: %l, MidPos: %l",
_homingData.position[HOMING_START_PIN_POSITION],
_homingData.position[HOMING_END_PIN_POSITION],
_homingData.position[HOMING_START_PIN_POSITION] - _homingData.position[HOMING_END_PIN_POSITION],
midPos);
LOG(DEBUG_STEPPERS,
"[HOMING]: Moving home by %l - (%l) - (%l), so %l steps. Advance to %s",
midPos,
_pMount->getCurrentStepperPosition(_axis),
_homingData.offset,
midPos - _pMount->getCurrentStepperPosition(_axis) - _homingData.offset,
getHomingState(HomingState::HOMING_WAIT_FOR_STOP).c_str());
_pMount->moveStepperBy(_axis, midPos - _pMount->getCurrentStepperPosition(_axis) - _homingData.offset);
_homingData.state = HomingState::HOMING_WAIT_FOR_STOP;
_homingData.nextState = HomingState::HOMING_SUCCESSFUL;
}
break;
case HomingState::HOMING_SUCCESSFUL:
{
LOG(DEBUG_STEPPERS,
"[HOMING]: Successfully homed! Setting home and restoring Rate setting. Advance to %s",
getHomingState(HomingState::HOMING_NOT_ACTIVE).c_str());
_lastResult = HOMING_RESULT_SUCCEEDED;
_homingData.state = HomingState::HOMING_NOT_ACTIVE;
_pMount->setHome(false);
_pMount->setSlewRate(_homingData.savedRate);
_pMount->clearStatusFlag(STATUS_FINDING_HOME);
if (_wasTracking)
{
_pMount->startSlewing(TRACKING);
}
}
break;
case HomingState::HOMING_FAILED:
{
LOG(DEBUG_STEPPERS,
"[HOMING]: Failed to home! Restoring Rate setting and slewing to start position. Advance to %s",
getHomingState(HomingState::HOMING_NOT_ACTIVE).c_str());
_pMount->setSlewRate(_homingData.savedRate);
_homingData.state = HomingState::HOMING_NOT_ACTIVE;
_pMount->clearStatusFlag(STATUS_FINDING_HOME);
_pMount->setStatusFlag(STATUS_SLEWING | STATUS_SLEWING_TO_TARGET);
_pMount->moveStepperTo(_axis, _homingData.startPos);
}
break;
default:
LOG(DEBUG_STEPPERS, "[HOMING]: Unhandled state (%d)! ", _homingData.state);
break;
}
}
bool HallSensorHoming::isIdleOrComplete() const
{
return _homingData.state == HomingState::HOMING_NOT_ACTIVE;
}