From a3408a86c37425a9cbd56517b24fcb0267a72d7e Mon Sep 17 00:00:00 2001 From: Sebastian Godelet Date: Sun, 27 Jun 2021 18:57:17 +1000 Subject: [PATCH] Update unit tests to support GW command SideOfPier also supports German EQ --- .../TelescopeUnitTests.cs | 14 ++- Meade.net.Telescope/Alignment.cs | 4 +- Meade.net.Telescope/Telescope.cs | 91 ++++++++++--------- Meade.net/SharedResources.cs | 15 +-- Meade.net/Wrapper/SharedResourcesWrapper.cs | 6 ++ 5 files changed, 72 insertions(+), 58 deletions(-) diff --git a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs index a917901..a5343bd 100644 --- a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs +++ b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs @@ -121,8 +121,9 @@ namespace Meade.net.Telescope.UnitTests _sharedResourcesWrapperMock.Object, _astroMathsMock.Object, _clockMock.Object, _novasMock.Object); } - private void ConnectTelescope(string productName = TelescopeList.Autostar497, string firmwareVersion = TelescopeList.Autostar497_31Ee) + private void ConnectTelescope(string productName = TelescopeList.Autostar497, string firmwareVersion = TelescopeList.Autostar497_31Ee, string alignmentStatus = "GT0") { + _sharedResourcesWrapperMock.Setup(x => x.SendChars("GW", false, 3)).Returns(alignmentStatus); _sharedResourcesWrapperMock.Setup(x => x.SendString("Gt", false)).Returns( () => _testProperties.SiteLatitudeString); _utilMock.Setup(x => x.DMSToDegrees(_testProperties.SiteLatitudeString)).Returns( () => _testProperties.SiteLatitudeValue); @@ -778,9 +779,8 @@ namespace Meade.net.Telescope.UnitTests { const char ack = (char)6; _sharedResourcesWrapperMock.Setup(x => x.SendChar(ack.ToString(), false)).Returns(telescopeMode); - _sharedResourcesWrapperMock.Setup(x => x.SendString("GW", false)).Returns($"{telescopeMode}N0"); - ConnectTelescope(productName, firmware); + ConnectTelescope(productName, firmware, $"{telescopeMode}N0"); var actualResult = _telescope.AlignmentMode; @@ -1942,7 +1942,7 @@ namespace Meade.net.Telescope.UnitTests _astroUtilsMock.Setup(x => x.ConditionHA(It.IsAny())).Returns(pHA => pHA < -12 ? pHA + 12 : pHA > 12 ? pHA - 12 : pHA); _astroUtilsMock.Setup(x => x.ConditionRA(It.IsAny())).Returns(pRA => pRA < 0 ? pRA + 24 : pRA >= 24 ? pRA - 24 : pRA); - ConnectTelescope(); + ConnectTelescope(firmwareVersion: TelescopeList.Autostar497_43Eg); Assert.That(_connectionInfo.SameDevice, Is.EqualTo(1)); _telescope.SlewToCoordinates(ra, dec); @@ -2056,7 +2056,8 @@ namespace Meade.net.Telescope.UnitTests // Setup DestinationSideOfPier _astroUtilsMock.Setup(x => x.ConditionHA(It.IsAny())).Returns(pHA => pHA < -12 ? pHA + 12 : pHA > 12 ? pHA - 12 : pHA); - ConnectTelescope(); + // Use firmware that supports GW + ConnectTelescope(firmwareVersion: TelescopeList.Autostar497_43Eg); // when _telescope.SlewToCoordinatesAsync(ra, dec); @@ -3203,7 +3204,6 @@ namespace Meade.net.Telescope.UnitTests _telescope.SlewToTarget(); _utilMock.Verify(x => x.WaitForMilliseconds(It.IsAny()), Times.Exactly(iterations)); - _astroUtilsMock.Verify(x => x.ConditionHA(It.IsAny()), Times.Once); } [Test] @@ -3298,7 +3298,6 @@ namespace Meade.net.Telescope.UnitTests _sharedResourcesWrapperMock.Verify(x => x.SendChar("MS", false), Times.Once); _utilMock.Verify(x => x.WaitForMilliseconds(It.IsAny()), Times.Exactly(iterations)); - _astroUtilsMock.Verify(x => x.ConditionHA(It.IsAny()), Times.Once); } [Test] @@ -3419,7 +3418,6 @@ namespace Meade.net.Telescope.UnitTests Assert.That(_telescope.TargetDeclination, Is.EqualTo(_testProperties.declination)); _sharedResourcesWrapperMock.Verify(x => x.SendChar("MS", false), Times.Once); _utilMock.Verify(x => x.WaitForMilliseconds(It.IsAny()), Times.Exactly(iterations)); - _astroUtilsMock.Verify(x => x.ConditionHA(It.IsAny()), Times.Once); } [Test] diff --git a/Meade.net.Telescope/Alignment.cs b/Meade.net.Telescope/Alignment.cs index 7450c67..78ef928 100644 --- a/Meade.net.Telescope/Alignment.cs +++ b/Meade.net.Telescope/Alignment.cs @@ -5,6 +5,8 @@ namespace ASCOM.Meade.net NeedsAlignment, OneStarAligned, TwoStarAligned, - ThreeStarAligned + ThreeStarAligned, + AlignedOnHome, + ScopeWasParked, } } \ No newline at end of file diff --git a/Meade.net.Telescope/Telescope.cs b/Meade.net.Telescope/Telescope.cs index e10c5ea..402375e 100644 --- a/Meade.net.Telescope/Telescope.cs +++ b/Meade.net.Telescope/Telescope.cs @@ -360,9 +360,6 @@ namespace ASCOM.Meade.net var result = SharedResourcesWrapper.SendBool(command, raw); LogMessage("CommandBool", "Completed: {0}", result); return result; - // or - //throw new MethodNotImplementedException("CommandBool"); - // DO NOT have both these sections! One or the other } public string CommandString(string command, bool raw) @@ -372,10 +369,19 @@ namespace ASCOM.Meade.net // it's a good idea to put all the low level communication with the device here, // then all communication calls this function // you need something to ensure that only one command is in progress at a time - var result = SharedResourcesWrapper.SendString(command, raw); - LogMessage("CommandBool", "Completed: {0}", result); + string result; + // :GW# is not terminated with a # for some reason, see reported comment + // https://bitbucket.org/cjdskunkworks/meadeautostar497/issues/24/get-set-tracking#comment-60586901 + if (command == (raw ? ":GW#" : "GW")) + { + result = SharedResourcesWrapper.SendChars(command, raw, count: 3); + } + else + { + result = SharedResourcesWrapper.SendString(command, raw); + } + LogMessage("CommandString", "Completed: {0}", result); return result; - //throw new ASCOM.MethodNotImplementedException("CommandString"); } public void Dispose() @@ -416,7 +422,6 @@ namespace ASCOM.Meade.net $"Connected to port {ComPort}. Product: {SharedResourcesWrapper.ProductName} Version:{SharedResourcesWrapper.FirmwareVersion}"); _userNewerPulseGuiding = IsNewPulseGuidingSupported(); - _tracking = true; LogMessage("Connected Set", $"New Pulse Guiding Supported: {_userNewerPulseGuiding}"); IsConnected = true; @@ -620,17 +625,13 @@ namespace ASCOM.Meade.net return false; } - // true iff the mount will perform a meridian flip when required - // TODO: Needs checking what mounts actually support this - private bool IsMeridianFlipOnSlewSupported() - { - if (SharedResourcesWrapper.ProductName == TelescopeList.LX200CLASSIC) - { - return false; - } + private bool IsGWCommandSupported() => FirmwareIsGreaterThan(TelescopeList.Autostar497_43Eg); - return true; - } + // true iff the mount will perform a meridian flip when required + // According to "A User's Guide to the Meade LXD55 and LXD75 Telescopes" Autostar supports meridian flip so + // we assume that for any telescope that supports the GW command and is not in Alt-Az mode then + // meridian flip on slew is supported + private bool IsMeridianFlipOnSlewSupported() => IsGWCommandSupported() && AlignmentMode != AlignmentModes.algAltAz; private bool FirmwareIsGreaterThan(string minVersion) { @@ -957,7 +958,7 @@ namespace ASCOM.Meade.net CheckConnected("AlignmentMode Get"); - if (FirmwareIsGreaterThan(TelescopeList.Autostar497_43Eg)) + if (IsGWCommandSupported()) { var alignmentStatus = GetScopeAlignmentStatus(); return alignmentStatus.AlignmentMode; @@ -998,7 +999,7 @@ namespace ASCOM.Meade.net CheckConnected("AlignmentMode Set"); //todo tidy this up into a better solution that means can :GW#, :AL#, :AA#, & :AP# and checked for Autostar properly - if (!FirmwareIsGreaterThan(TelescopeList.Autostar497_43Eg)) + if (!IsGWCommandSupported()) throw new PropertyNotImplementedException("AlignmentMode", true); switch (value) @@ -1025,13 +1026,14 @@ namespace ASCOM.Meade.net private AlignmentStatus GetScopeAlignmentStatus() { - var alignmentString = SharedResourcesWrapper.SendString("GW"); + var alignmentString = CommandString("GW", false); //:GW# Get Scope Alignment Status //Returns: # // where: //mount: A - AzEl mounted, P - Equatorially mounted, G - german mounted equatorial - //tracking: T - tracking, N - not tracking - //alignment: 0 - needs alignment, 1 - one star aligned, 2 - two star aligned, 3 - three star aligned. + //tracking: T - tracking, N - not tracking, S - sleeping + //alignment: 0 - needs alignment, 1 - one star aligned, 2 - two star aligned, 3 - three star aligned., H - Aligned on Home, P - Scope was parked + // https://www.cloudynights.com/topic/72166-lx-200-gps-serial-commands/ var alignmentStatus = new AlignmentStatus(); switch (alignmentString[0]) @@ -1046,8 +1048,8 @@ namespace ASCOM.Meade.net alignmentStatus.AlignmentMode = AlignmentModes.algGermanPolar; break; } - alignmentStatus.Tracking = alignmentString[0] == 'T'; - switch (alignmentString[1]) + alignmentStatus.Tracking = alignmentString[1] == 'T'; + switch (alignmentString[2]) { case '0': alignmentStatus.Status = Alignment.NeedsAlignment; @@ -1061,9 +1063,14 @@ namespace ASCOM.Meade.net case '3': alignmentStatus.Status = Alignment.ThreeStarAligned; break; + case 'H': + alignmentStatus.Status = Alignment.AlignedOnHome; + break; + case 'P': + alignmentStatus.Status = Alignment.ScopeWasParked; + break; } - return alignmentStatus; } @@ -1228,7 +1235,7 @@ namespace ASCOM.Meade.net switch (axis) { case TelescopeAxes.axisPrimary: return true; //RA or AZ - case TelescopeAxes.axisSecondary: return true; //Dev or Alt + case TelescopeAxes.axisSecondary: return true; //DEC or Alt case TelescopeAxes.axisTertiary: return false; //rotator / derotator default: throw new InvalidValueException("CanMoveAxis", axis.ToString(), "0 to 2"); } @@ -1890,24 +1897,25 @@ namespace ASCOM.Meade.net { get { - if (!FirmwareIsGreaterThan(TelescopeList.Autostar497_43Eg)) + if (!IsMeridianFlipOnSlewSupported()) { LogMessage("SideOfPier Get", "Not implemented"); throw new PropertyNotImplementedException("SideOfPier", false); } + + PierSide pierSide; + if (Slewing) + { + // because we update SideOfPier after initiating the slew command we return unknown while still slewing + pierSide = PierSide.pierUnknown; + } else { - var alignmentStatus = GetScopeAlignmentStatus(); - if (alignmentStatus.AlignmentMode != AlignmentModes.algPolar) - throw new PropertyNotImplementedException("SideOfPier", false); - - // while mount is slewing return unknown, this is required since - // DoSlewAsync updates _pierSide before slew is finished - var pierSide = Slewing ? PierSide.pierUnknown : SharedResourcesWrapper.SideOfPier; - - LogMessage("SideOfPier", "Get - " + pierSide); - return pierSide; + pierSide = SharedResourcesWrapper.SideOfPier; } + + LogMessage("SideOfPier", "Get - " + pierSide); + return pierSide; } // ReSharper disable once ValueParameterNotUsed set @@ -2594,19 +2602,18 @@ namespace ASCOM.Meade.net } } - private bool _tracking = true; public bool Tracking { get { - LogMessage("Tracking", $"Get - {_tracking}"); - if (FirmwareIsGreaterThan(TelescopeList.Autostar497_43Eg)) + LogMessage("Tracking", "Get"); + if (IsGWCommandSupported()) { var alignmentStatus = GetScopeAlignmentStatus(); - _tracking = alignmentStatus.Tracking; + return alignmentStatus.Tracking; } - return _tracking; + return true; } set { diff --git a/Meade.net/SharedResources.cs b/Meade.net/SharedResources.cs index 6567a07..440b051 100644 --- a/Meade.net/SharedResources.cs +++ b/Meade.net/SharedResources.cs @@ -124,13 +124,17 @@ namespace ASCOM.Meade.net public static bool SendBool(string command, bool raw = false) { - var result = SendChar(command, raw); return result == "1"; } public static string SendChar(string command, bool raw = false) + { + return SendChars(command, raw, count: 1); + } + + public static string SendChars(string command, bool raw = false, int count = 1) { lock (LockObject) { @@ -141,14 +145,11 @@ namespace ASCOM.Meade.net try { - return SharedSerial.ReceiveCounted(1); + return SharedSerial.ReceiveCounted(count); } - catch (COMException ex) + catch (COMException ex) when (ex.Message.Contains("Timed out waiting for received data")) { - if (ex.Message.Contains("Timed out waiting for received data")) - throw new TimeoutException(ex.Message, ex); - - throw; + throw new TimeoutException(ex.Message, ex); } } } diff --git a/Meade.net/Wrapper/SharedResourcesWrapper.cs b/Meade.net/Wrapper/SharedResourcesWrapper.cs index 8e91bb7..84ecf4a 100644 --- a/Meade.net/Wrapper/SharedResourcesWrapper.cs +++ b/Meade.net/Wrapper/SharedResourcesWrapper.cs @@ -20,6 +20,7 @@ namespace ASCOM.Meade.net.Wrapper void SendBlind(string message, bool raw = false); bool SendBool(string command, bool raw = false); string SendChar(string message, bool raw = false); + string SendChars(string message, bool raw = false, int count = 1); string ReadTerminated(); @@ -94,6 +95,11 @@ namespace ASCOM.Meade.net.Wrapper return SharedResources.SendChar(message, raw); } + public string SendChars(string message, bool raw = false, int count = 1) + { + return SharedResources.SendChars(message, raw, count); + } + public string ReadTerminated() { return SharedResources.ReadTerminated();