88#include < SafeSerial.h>
99#include < AdvancedStepper.h>
1010#include < XBeeApi.h>
11+ #include < Timer.h>
1112#include " RainSensor.h"
1213#include " NexDome.h"
1314#include " PersistentSettings.h"
1415#include " HomeSensor.h"
1516#include " CommandProcessor.h"
1617#include " XBeeStartupState.h"
1718
19+ constexpr Duration SerialInactivityTimeout = Timer::Minutes(10 );
20+
1821// Forward declarations
1922void onXbeeFrameReceived (FrameType type, std::vector<byte> &payload);
2023void onMotorStopped ();
@@ -32,6 +35,7 @@ auto machine = XBeeStateMachine(xbeeSerial, xbeeApi);
3235auto commandProcessor = CommandProcessor(stepper, settings, machine);
3336auto home = HomeSensor(&stepper, &settings.home, HOME_INDEX_PIN, commandProcessor);
3437Timer periodicTasks;
38+ Timer serialInactivityTimer;
3539auto rain = RainSensor(RAIN_SENSOR_PIN);
3640
3741// cin and cout for ArduinoSTL
@@ -58,6 +62,8 @@ void HandleSerialCommunications()
5862 const auto rx = host.read ();
5963 if (rx < 0 )
6064 return ; // No data available.
65+
66+ serialInactivityTimer.SetDuration (SerialInactivityTimeout);
6167 const char rxChar = char (rx);
6268 switch (rxChar)
6369 {
@@ -161,6 +167,9 @@ void loop()
161167 std::cout << " P" << std::dec << commandProcessor.getPositionInWholeSteps () << std::endl;
162168 ProcessManualControls ();
163169 rain.loop ();
170+ // Release stepper holding torque if there has been no serial communication for "a long time".
171+ if (serialInactivityTimer.Expired ())
172+ stepper.releaseMotor ();
164173 }
165174}
166175
0 commit comments