@@ -429,31 +429,70 @@ private void PollDomeTask( CancellationToken token )
429429 Stopwatch watch = new Stopwatch ( ) ;
430430 double overhead = 0.0 ;
431431
432+ TimeSpan fastPollExtension = new TimeSpan ( 0 , 0 , 3 ) ; //Wait 3 seconds after movement stops to return to normal polling.
433+ bool previousMoveStatus = false ;
434+ DateTime returnToNormalPollingTime = DateTime . MinValue ;
435+ int previousPollingPeriod ;
436+
432437 while ( ! taskCancelled )
433438 {
434439 DateTime wakeupTime = DateTime . Now ;
435440 //Debug.WriteLine( $"Awakened @ {wakeupTime:hh:mm:ss.fff}." );
441+ previousPollingPeriod = PollingPeriod ;
442+ PollingPeriod = POLLING_INTERVAL_NORMAL ;
443+ int fastPollingMs = Convert . ToInt32 ( FastPollingPeriod * 1000.0 ) ;
436444
437445 if ( Service . DeviceAvailable )
438446 {
439447 UpdateDomeStatusTask ( ) ;
440448
441449 if ( ! Status . Slewing )
442450 {
443- SlewTheSlavedDome ( ref nextSlaveAdjustmentTime ) ;
444- UpdateDomeStatusTask ( ) ;
451+ if ( SlewTheSlavedDome ( ref nextSlaveAdjustmentTime ) )
452+ {
453+ UpdateDomeStatusTask ( ) ;
454+ }
445455 }
446456
447- bool shutterMoving = Status . ShutterStatus == ShutterState . shutterOpening
448- || Status . ShutterStatus == ShutterState . shutterClosing ;
457+ bool isMoving = Status . Slewing
458+ || Status . ShutterStatus == ShutterState . shutterOpening
459+ || Status . ShutterStatus == ShutterState . shutterClosing ;
449460
450- if ( ! ( Status . Slewing || shutterMoving ) && PollingPeriod != POLLING_INTERVAL_NORMAL )
461+ if ( isMoving )
451462 {
452- LogActivityLine ( ActivityMessageTypes . Commands , $ "Returning to normal polling every { POLLING_INTERVAL_NORMAL } ms." ) ;
463+ // We are moving, so use the fast polling rate.
464+
465+ PollingPeriod = fastPollingMs ;
453466 }
467+ else if ( previousMoveStatus )
468+ {
469+ // We stopped moving, so start the timer to return to normal polling.
454470
455- PollingPeriod = ( Status . Slewing || shutterMoving ) ? Convert . ToInt32 ( FastPollingPeriod * 1000.0 ) : POLLING_INTERVAL_NORMAL ;
456- }
471+ returnToNormalPollingTime = DateTime . Now + fastPollExtension ;
472+ PollingPeriod = fastPollingMs ;
473+ }
474+ else if ( DateTime . Now < returnToNormalPollingTime )
475+ {
476+ // Continue fast polling.
477+
478+ PollingPeriod = fastPollingMs ;
479+ }
480+ else
481+ {
482+ // Return to normal polling.
483+
484+ returnToNormalPollingTime = DateTime . MinValue ;
485+ }
486+
487+ // Remember our state for the next time through this loop.
488+
489+ previousMoveStatus = isMoving ;
490+
491+ if ( PollingPeriod == POLLING_INTERVAL_NORMAL && previousPollingPeriod != POLLING_INTERVAL_NORMAL )
492+ {
493+ LogActivityLine ( ActivityMessageTypes . Commands , $ "Returning to normal polling every { PollingPeriod } ms." ) ;
494+ }
495+ }
457496
458497 TimeSpan waitInterval = wakeupTime . AddMilliseconds ( ( double ) PollingPeriod ) - DateTime . Now ;
459498 waitInterval -= TimeSpan . FromMilliseconds ( overhead ) ;
@@ -500,8 +539,9 @@ private void PollDomeTask( CancellationToken token )
500539 IsPolling = false ;
501540 }
502541
503- private void SlewTheSlavedDome ( ref DateTime nextAdjustmentTime )
542+ private bool SlewTheSlavedDome ( ref DateTime nextAdjustmentTime )
504543 {
544+ bool retval = false ;
505545 ActivityMessageTypes msgType = ActivityMessageTypes . Commands ;
506546 DateTime returnTime = nextAdjustmentTime ;
507547
@@ -549,6 +589,7 @@ private void SlewTheSlavedDome( ref DateTime nextAdjustmentTime )
549589 double localHourAngle = TelescopeStatus . CalculateHourAngle ( SlavedSlewState . RightAscension ) ;
550590
551591 SlaveDomePointing ( scopeTargetPosition , localHourAngle , SlavedSlewState . SideOfPier ) ;
592+ retval = true ;
552593 }
553594 catch ( TransformUninitialisedException xcp )
554595 {
@@ -588,6 +629,7 @@ private void SlewTheSlavedDome( ref DateTime nextAdjustmentTime )
588629 try
589630 {
590631 SlaveDomePointing ( scopePosition , TelescopeStatus . LocalHourAngle , TelescopeStatus . SideOfPier ) ;
632+ retval = true ;
591633 }
592634 catch ( Exception xcp )
593635 {
@@ -610,6 +652,8 @@ private void SlewTheSlavedDome( ref DateTime nextAdjustmentTime )
610652 }
611653
612654 nextAdjustmentTime = returnTime ;
655+
656+ return retval ;
613657 }
614658
615659 private void SlaveDomePointing ( Point scopePosition , double localHourAngle , PierSide sideOfPier )
0 commit comments