11/*
22 * Provides interrupt processing for the home sensor.
3- *
3+ *
44 * The home sensor synchronizes the dome rotation step position when it is triggered.
55 * When rotating in the positive direction, we synchronise to the falling edge.
66 * When rotating in the negative direction, we synchronise to the rising edge.
77 * When not rotating, activity is ignored.
8- *
8+ *
99 * Note: some fields have to be static because they are used during interrupts
1010 */
1111
1414
1515#pragma region static fields used within interrupt service routines
1616MicrosteppingMotor* HomeSensor::motor;
17- Home* HomeSensor::settings ;
17+ Home* HomeSensor::homeSettings ;
1818uint8_t HomeSensor::sensorPin;
19- volatile bool HomeSensor::state;
20- volatile bool HomeSensor::homingInProgress;
21- volatile bool HomeSensor::performingPostHomeSlew;
19+ volatile HomingPhase HomeSensor::phase;
2220#pragma endregion
2321
2422/*
@@ -31,7 +29,7 @@ HomeSensor::HomeSensor(MicrosteppingMotor* stepper, Home* settings, const uint8_
3129 : commandProcessor(processor)
3230 {
3331 motor = stepper;
34- HomeSensor::settings = settings;
32+ HomeSensor::homeSettings = settings;
3533 HomeSensor::sensorPin = sensorPin;
3634 }
3735
@@ -42,19 +40,9 @@ HomeSensor::HomeSensor(MicrosteppingMotor* stepper, Home* settings, const uint8_
4240 */
4341void HomeSensor::onHomeSensorChanged ()
4442 {
45- state = digitalRead (sensorPin);
46- if (homingInProgress )
43+ const auto state = digitalRead (sensorPin);
44+ if (state == 0 && phase==Detecting )
4745 foundHome ();
48- // if (!motor->isMoving()) // Ignore state change if rotator not moving
49- // return;
50- // const auto direction = motor->getCurrentDirection();
51- // if ((state && direction < 0) || (!state && direction > 0))
52- // {
53- // // sync position on either the rising or falling edge, depending on rotation direction.
54- // motor->SetCurrentPosition(settings->position);
55- // if (homingInProgress)
56- // foundHome();
57- // }
5846 }
5947
6048/*
@@ -63,67 +51,87 @@ void HomeSensor::onHomeSensorChanged()
6351void HomeSensor::init ()
6452 {
6553 pinMode (sensorPin, INPUT_PULLUP);
66- state = digitalRead (sensorPin);
67- homingInProgress = false ;
68- performingPostHomeSlew = false ;
54+ setPhase (Idle);
6955 attachInterrupt (digitalPinToInterrupt (sensorPin), onHomeSensorChanged, CHANGE);
7056 }
7157
7258
59+ // / <summary>
60+ // / Indicates whether the dome is currently in the home position
61+ // / (only valid after a successful homing operation and before and slews occur)
62+ // / </summary>
7363bool HomeSensor::atHome ()
7464 {
75- return !state && !homingInProgress && !performingPostHomeSlew;
65+ return phase == AtHome;
66+ }
67+
68+ void HomeSensor::setPhase (HomingPhase newPhase)
69+ {
70+ phase = newPhase;
71+ Serial.print (" Phase " );
72+ Serial.println (phase);
7673 }
7774
7875/*
7976 * Rotates up to 2 full rotations clockwise while attempting to detect the home sensor.
77+ * Ignored if a homing operation is already in progress.
8078 */
8179void HomeSensor::findHome (int direction)
8280 {
83- homingInProgress = true ;
84- performingPostHomeSlew = false ;
85- const auto distance = 2 * settings->microstepsPerRotation ; // Allow 2 full rotations only
86- motor->moveToPosition (distance);
81+ if (phase == Idle || phase == AtHome)
82+ {
83+ const auto distance = 2 * homeSettings->microstepsPerRotation ; // Allow 2 full rotations only
84+ setPhase (Detecting);
85+ motor->moveToPosition (distance);
86+ }
8787 }
8888
8989/*
9090 * Stops a homing operation in progress.
9191 */
9292void HomeSensor::cancelHoming ()
9393 {
94- performingPostHomeSlew = false ;
95- homingInProgress = false ;
94+ setPhase (Idle);
9695 if (motor->isMoving ())
9796 motor->SoftStop ();
9897 }
9998
10099/*
101100 * Once the home sensor has been detected, we instruct the motor to soft-stop.
102- * We also set the flaf performingPostHomeSlew .
101+ * We also set the flag performPostHomeSlew .
103102 * At some point in the future, the onMotorStopped method will be called, which will
104103 * then initiate the final slew to return exactly to the home sensor position.
105104 */
106105void HomeSensor::foundHome ()
107106 {
108- std::cout << " foundHome" << std::endl;
109- motor->SetCurrentPosition (settings->position );
110- homingInProgress = false ;
111- performingPostHomeSlew = true ;
107+ setPhase (Stopping);
108+ motor->SetCurrentPosition (homeSettings->position );
112109 motor->SoftStop ();
113110 }
114111
115- void HomeSensor::onMotorStopped ()
112+ /*
113+ * Handles the onMotorStopped event. Action depends on the homing phase.
114+ */
115+ void HomeSensor::onMotorStopped () const
116116 {
117- std::cout << " onMotorStopped" << std::endl;
118- homingInProgress = false ;
119- if (performingPostHomeSlew)
117+ std::cout << " Hstop " << phase << std::endl;
118+ switch (phase)
120119 {
121- performingPostHomeSlew = false ;
122- const auto target = commandProcessor.targetStepPosition (settings->position );
123- std::cout << " Post slew from "
124- << motor->getCurrentPosition ()
125- << " to " << target
126- << std::endl;
127- motor->moveToPosition (target);
120+ case Reversing:
121+ setPhase (AtHome);
122+ break ;
123+ case Stopping:
124+ setPhase (Reversing);
125+ const auto target = commandProcessor.targetStepPosition (homeSettings->position );
126+ motor->moveToPosition (target);
127+ break ;
128+ default :
129+ setPhase (Idle);
130+ return ;
128131 }
129132 }
133+
134+ bool HomeSensor::homingInProgress ()
135+ {
136+ return !(phase == Idle || phase == AtHome);
137+ }
0 commit comments