Added support for parking the scope

Added support for reading the scope Azimuth
This commit is contained in:
2019-05-02 16:03:24 +01:00
parent 76c88420ca
commit c0c0bedbba
4 changed files with 71 additions and 30 deletions
@@ -7,7 +7,6 @@ using ASCOM.DeviceInterface;
using ASCOM.MeadeAutostar497.Controller; using ASCOM.MeadeAutostar497.Controller;
using Moq; using Moq;
using NUnit.Framework; using NUnit.Framework;
using InvalidOperationException = ASCOM.InvalidOperationException;
namespace MeadeAutostar497.UnitTests namespace MeadeAutostar497.UnitTests
{ {
@@ -31,19 +30,20 @@ namespace MeadeAutostar497.UnitTests
serialMock = new Mock<ISerialProcessor>(); serialMock = new Mock<ISerialProcessor>();
serialMock.SetupAllProperties(); serialMock.SetupAllProperties();
serialMock.Setup(x => x.GetPortNames()).Returns(() => _availableComPorts.ToArray()); serialMock.Setup(x => x.GetPortNames()).Returns(() => _availableComPorts.ToArray());
serialMock.Setup(x => x.CommandTerminated(It.IsAny<string>(), It.IsAny<string>())).Returns(() => _stringToRecieve); serialMock.Setup(x => x.CommandTerminated(It.IsAny<string>(), It.IsAny<string>()))
.Returns(() => _stringToRecieve);
serialMock.Setup(x => x.IsOpen).Returns(() => _isConnected); serialMock.Setup(x => x.IsOpen).Returns(() => _isConnected);
//Todo inject the serialMock instead of using a singleton to increase code stability. //Todo inject the serialMock instead of using a singleton to increase code stability.
_telescopeController = TelescopeController.Instance; _telescopeController = TelescopeController.Instance;
_telescopeController.Connected = false;
_telescopeController.SerialPort = serialMock.Object; _telescopeController.SerialPort = serialMock.Object;
} }
[TearDown] [TearDown]
public void TearDown() public void TearDown()
{ {
_isConnected = false;
_telescopeController.Connected = false;
_telescopeController.Port = "COM1"; _telescopeController.Port = "COM1";
} }
@@ -97,7 +97,7 @@ namespace MeadeAutostar497.UnitTests
public void WhenOpensComPortToNonAutostarThrowException() public void WhenOpensComPortToNonAutostarThrowException()
{ {
Assert.That(serialMock.Object.IsOpen, Is.False); Assert.That(serialMock.Object.IsOpen, Is.False);
var exception = Assert.Throws<InvalidOperationException>(() => { _telescopeController.Connected = true; }); var exception = Assert.Throws<ASCOM.InvalidOperationException>(() => { _telescopeController.Connected = true; });
Assert.That(exception.Message, Is.EqualTo("Failed to communicate with telescope.")); Assert.That(exception.Message, Is.EqualTo("Failed to communicate with telescope."));
@@ -114,7 +114,7 @@ namespace MeadeAutostar497.UnitTests
Mock<ISerialProcessor> newSerialMock = new Mock<ISerialProcessor>(); Mock<ISerialProcessor> newSerialMock = new Mock<ISerialProcessor>();
var exception = Assert.Throws<InvalidOperationException>( () => { _telescopeController.SerialPort = newSerialMock.Object; }); var exception = Assert.Throws<ASCOM.InvalidOperationException>( () => { _telescopeController.SerialPort = newSerialMock.Object; });
Assert.That(exception, Is.Not.Null); Assert.That(exception, Is.Not.Null);
Assert.That(exception.Message, Is.EqualTo("Please disconnect before changing the serial engine.")); Assert.That(exception.Message, Is.EqualTo("Please disconnect before changing the serial engine."));
@@ -141,7 +141,7 @@ namespace MeadeAutostar497.UnitTests
_isConnected = true; _isConnected = true;
_telescopeController.Connected = true; _telescopeController.Connected = true;
var exception = Assert.Throws<InvalidOperationException>( () => _telescopeController.Port = "COM2"); var exception = Assert.Throws<ASCOM.InvalidOperationException>( () => _telescopeController.Port = "COM2");
Assert.That(exception.Message, Is.EqualTo("Please disconnect from the scope before changing port.")); Assert.That(exception.Message, Is.EqualTo("Please disconnect from the scope before changing port."));
@@ -151,7 +151,7 @@ namespace MeadeAutostar497.UnitTests
[Test] [Test]
public void SettingPortToInvalidPortFails() public void SettingPortToInvalidPortFails()
{ {
var exception = Assert.Throws<InvalidOperationException>(() => _telescopeController.Port = "COM5"); var exception = Assert.Throws<ASCOM.InvalidOperationException>(() => _telescopeController.Port = "COM5");
Assert.That(exception.Message, Is.EqualTo("Unable to select port COM5 as it does not exist.")); Assert.That(exception.Message, Is.EqualTo("Unable to select port COM5 as it does not exist."));
@@ -318,7 +318,7 @@ namespace MeadeAutostar497.UnitTests
_telescopeController.Connected = true; _telescopeController.Connected = true;
var exception = Assert.Throws<InvalidOperationException>(() => { _telescopeController.SiteLatitude = 10; }); var exception = Assert.Throws<ASCOM.InvalidOperationException>(() => { _telescopeController.SiteLatitude = 10; });
Assert.That(exception.Message, Is.EqualTo("Failed to set site latitude.")); Assert.That(exception.Message, Is.EqualTo("Failed to set site latitude."));
} }
@@ -383,7 +383,7 @@ namespace MeadeAutostar497.UnitTests
_telescopeController.Connected = true; _telescopeController.Connected = true;
var exception = Assert.Throws<InvalidOperationException>(() => { _telescopeController.SiteLongitude = 10; }); var exception = Assert.Throws<ASCOM.InvalidOperationException>(() => { _telescopeController.SiteLongitude = 10; });
Assert.That(exception.Message, Is.EqualTo("Failed to set site longitude.")); Assert.That(exception.Message, Is.EqualTo("Failed to set site longitude."));
} }
@@ -482,7 +482,6 @@ namespace MeadeAutostar497.UnitTests
public void AtParkIsFalseByDefault() public void AtParkIsFalseByDefault()
{ {
_isConnected = true; _isConnected = true;
_telescopeController.Connected = true; _telescopeController.Connected = true;
Assert.That( _telescopeController.AtPark, Is.False ); Assert.That( _telescopeController.AtPark, Is.False );
@@ -522,5 +521,20 @@ namespace MeadeAutostar497.UnitTests
serialMock.Verify(x => x.Command(":hP#"), Times.Once); serialMock.Verify(x => x.Command(":hP#"), Times.Once);
} }
[TestCase("356*13",356.21666666666664)]
[TestCase("356*13'21", 356.22249999999997)]
public void Azimuth_CanGetValue( string response, double expectedResult )
{
serialMock.Setup(x => x.CommandTerminated(":GZ#", "#")).Returns(response);
_isConnected = true;
_telescopeController.Connected = true;
var az = _telescopeController.Azimuth;
Assert.That( az, Is.EqualTo(expectedResult));
}
} }
} }
+3 -2
View File
@@ -358,8 +358,9 @@ namespace ASCOM.MeadeAutostar497
{ {
get get
{ {
tl.LogMessage("Azimuth Get", "Not implemented"); var az = _telescopeController.Azimuth;
throw new ASCOM.PropertyNotImplementedException("Azimuth", false); tl.LogMessage("Azimuth Get", $"{az}");
return az;
} }
} }
@@ -14,6 +14,7 @@ namespace ASCOM.MeadeAutostar497.Controller
double SiteLongitude { get; set; } double SiteLongitude { get; set; }
AlignmentModes AlignmentMode { get; set; } AlignmentModes AlignmentMode { get; set; }
bool AtPark { get; } bool AtPark { get; }
double Azimuth { get; }
void AbortSlew(); void AbortSlew();
void PulseGuide(GuideDirections direction, int duration); void PulseGuide(GuideDirections direction, int duration);
void Park(); void Park();
@@ -13,6 +13,7 @@ namespace ASCOM.MeadeAutostar497.Controller
public static TelescopeController Instance => lazy.Value; public static TelescopeController Instance => lazy.Value;
//todo remove this as it can cause problems in production
private ISerialProcessor _serialPort; private ISerialProcessor _serialPort;
public ISerialProcessor SerialPort public ISerialProcessor SerialPort
{ {
@@ -68,6 +69,7 @@ namespace ASCOM.MeadeAutostar497.Controller
//Connecting //Connecting
try try
{ {
_parked = false;
SerialPort.DtrEnable = false; SerialPort.DtrEnable = false;
SerialPort.RtsEnable = false; SerialPort.RtsEnable = false;
SerialPort.BaudRate = 9600; SerialPort.BaudRate = 9600;
@@ -90,6 +92,7 @@ namespace ASCOM.MeadeAutostar497.Controller
{ {
//Disconnecting //Disconnecting
SerialPort.Close(); SerialPort.Close();
_parked = false;
} }
} }
} }
@@ -215,16 +218,22 @@ namespace ASCOM.MeadeAutostar497.Controller
} }
} }
private double DMSToDouble(string DMS)
{
double l = int.Parse(DMS.Substring(0, 3));
l = l + double.Parse(DMS.Substring(4, 2)) / 60;
if (DMS.Length == 9)
l = l + double.Parse(DMS.Substring(7, 2)) / 60 / 60;
return l;
}
public double SiteLongitude public double SiteLongitude
{ {
get get
{ {
var longitude = SerialPort.CommandTerminated(":Gg#", "#"); var longitude = SerialPort.CommandTerminated(":Gg#", "#");
double l = DMSToDouble(longitude);
double l = int.Parse(longitude.Substring(0, 3));
l = l + double.Parse(longitude.Substring(4, 2)) / 60;
if (longitude.Length == 9)
l = l + double.Parse(longitude.Substring(7, 2)) / 60 / 60;
if (l > 180) if (l > 180)
l = l - 360; l = l - 360;
@@ -254,6 +263,12 @@ namespace ASCOM.MeadeAutostar497.Controller
get get
{ {
var alignmentString = SerialPort.CommandTerminated(":GW#", "#"); var alignmentString = SerialPort.CommandTerminated(":GW#", "#");
//:GW# Get Scope Alignment Status
//Returns: <mount><tracking><alignment>#
// 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.
switch (alignmentString[0]) switch (alignmentString[0])
{ {
@@ -263,22 +278,21 @@ namespace ASCOM.MeadeAutostar497.Controller
default: default:
throw new ASCOM.InvalidValueException($"unknown alignment returned from telescope: {alignmentString[0]}"); throw new ASCOM.InvalidValueException($"unknown alignment returned from telescope: {alignmentString[0]}");
} }
//:GW# Get Scope Alignment Status
//Returns: <mount><tracking><alignment>#
// 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.
} }
set set
{ {
switch (value) switch (value)
{ {
case AlignmentModes.algAltAz: SerialPort.Command(":AA#"); case AlignmentModes.algAltAz:
SerialPort.Command(":AA#");
//:AA# Sets telescope the AltAz alignment mode
//Returns: nothing
break; break;
case AlignmentModes.algPolar: case AlignmentModes.algPolar:
case AlignmentModes.algGermanPolar: case AlignmentModes.algGermanPolar:
SerialPort.Command(":AP#"); SerialPort.Command(":AP#");
//:AP# Sets telescope to Polar alignment mode
//Returns: nothing
break; break;
default: default:
throw new ArgumentOutOfRangeException(nameof(value), value, null); throw new ArgumentOutOfRangeException(nameof(value), value, null);
@@ -286,10 +300,6 @@ namespace ASCOM.MeadeAutostar497.Controller
//:AL# Sets telescope to Land alignment mode //:AL# Sets telescope to Land alignment mode
//Returns: nothing //Returns: nothing
//:AP# Sets telescope to Polar alignment mode
//Returns: nothing
//:AA# Sets telescope the AltAz alignment mode
//Returns: nothing
} }
} }
@@ -297,6 +307,21 @@ namespace ASCOM.MeadeAutostar497.Controller
public bool AtPark => _parked; public bool AtPark => _parked;
public double Azimuth
{
get
{
var result = SerialPort.CommandTerminated(":GZ#", "#");
//:GZ# Get telescope azimuth
//Returns: DDD*MM#T or DDD*MMSS#
//The current telescope Azimuth depending on the selected precision.
double az = DMSToDouble(result);
return az;
}
}
public void AbortSlew() public void AbortSlew()
{ {
SerialPort.Command("#:Q#"); SerialPort.Command("#:Q#");