Skip to content

Commit 8f9ac8a

Browse files
OATControl - WIP update
- Converted over to Wifi communication - Made lots of things use async/await - Lots of things still missing (no IP config, Polar Alignment not implemented)
1 parent 9a4c5fe commit 8f9ac8a

File tree

5 files changed

+223
-166
lines changed

5 files changed

+223
-166
lines changed

Software/OATMobile/OATCommunications/CommunicationHandlers/TcpCommunicationHandler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ private async Task<CommandResponse> SendCommand(string command, bool needsRespon
6868
try {
6969
var response = new byte[256];
7070
var respCount = await stream.ReadAsync(response, 0, response.Length);
71-
respString = Encoding.ASCII.GetString(response, 0, respCount);
72-
71+
respString = Encoding.ASCII.GetString(response, 0, respCount).TrimEnd("#".ToCharArray());
7372
Debug.WriteLine($"Received {respString}");
7473
}
7574
catch (Exception e) {

Software/OATMobile/OATCommunications/ICommunicationHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace OATCommunications
88
{
99
public interface ICommunicationHandler {
10+
Task<CommandResponse> SendBlind(string command);
1011
Task<CommandResponse> SendCommand(string command);
1112
bool Connected { get; }
1213
}

Software/OATMobile/OATCommunications/TelescopeCommandHandlers/OatmealTelescopeCommandHandlers.cs

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Linq;
3+
using System.Runtime.CompilerServices;
34
using System.Threading.Tasks;
5+
using Microsoft.Win32.SafeHandles;
46
using OATCommunications.CommunicationHandlers;
57
using OATCommunications.Model;
68
using OATCommunications.TelescopeCommandHandlers;
@@ -22,9 +24,9 @@ public OatmealTelescopeCommandHandlers(ICommunicationHandler commHandler) {
2224
public bool Connected { get { return _commHandler.Connected; } }
2325

2426
public async Task<bool> RefreshMountState() {
25-
var _slewingStates = new []{"SlewToTarget", "FreeSlew", "ManualSlew"};
27+
var _slewingStates = new[] { "SlewToTarget", "FreeSlew", "ManualSlew" };
2628

27-
var status = await SendCommand(":GX#");
29+
var status = await SendCommand(":GX#,#");
2830
if (!status.Success) {
2931
return false;
3032
}
@@ -56,11 +58,11 @@ private double GetCompactRA(string part) {
5658
}
5759

5860
public async Task<TelescopePosition> GetPosition() {
59-
var ra = await SendCommand(":GR#");
60-
var dec = await SendCommand(":GD#");
61+
var ra = await SendCommand(":GR#,#");
62+
var dec = await SendCommand(":GD#,#");
6163

62-
if(ra.Success && dec.Success &&
63-
TryParseRA(ra.Data, out double dRa) &&
64+
if (ra.Success && dec.Success &&
65+
TryParseRA(ra.Data, out double dRa) &&
6466
TryParseDec(dec.Data, out double dDec)) {
6567

6668
MountState.RightAscension = dRa;
@@ -71,7 +73,15 @@ public async Task<TelescopePosition> GetPosition() {
7173
MountState.RightAscension = 0;
7274
MountState.Declination = 0;
7375
return TelescopePosition.Invalid;
74-
76+
}
77+
78+
private void FloatToHMS(double val, out int h, out int m, out int s)
79+
{
80+
h = (int)Math.Floor(val);
81+
val = (val - h) * 60;
82+
m = (int)Math.Floor(val);
83+
val = (val - m) * 60;
84+
s = (int)Math.Round(val);
7585
}
7686

7787
private bool TryParseRA(string ra, out double dRa) {
@@ -104,8 +114,18 @@ public async Task<bool> StopMoving(string dir) {
104114
return status.Success;
105115
}
106116

107-
public Task<bool> Slew(TelescopePosition position) {
108-
throw new NotImplementedException();
117+
public async Task<bool> Slew(TelescopePosition position) {
118+
int deg, hour, min, sec;
119+
120+
FloatToHMS(Math.Abs(position.Declination), out deg, out min, out sec);
121+
string sign = position.Declination < 0 ? "-" : "+";
122+
var result = await SendCommand(string.Format(":Sd{0}{1:00}*{2:00}:{3:00}#,#", sign, deg, min, sec));
123+
if (!result.Success || result.Data != "1") return false;
124+
FloatToHMS(Math.Abs(position.RightAscension), out hour, out min, out sec);
125+
await SendCommand(string.Format(":Sr{0:00}:{1:00}:{2:00}#,#", hour, min, sec));
126+
if (!result.Success || result.Data != "1") return false;
127+
await SendCommand($":MS#");
128+
return result.Success;
109129
}
110130

111131
public Task<bool> Sync(TelescopePosition position) {
@@ -125,7 +145,7 @@ public async Task<bool> SetHome()
125145

126146
public async Task<bool> SetTracking(bool enabled) {
127147
var b = enabled ? 1 : 0;
128-
var status = await SendCommand($":MT{b}#");
148+
var status = await SendCommand($":MT{b}#,#");
129149
if (status.Success) {
130150
MountState.IsTracking = enabled;
131151
}
@@ -141,7 +161,7 @@ public async Task<bool> SetLocation(double lat, double lon, double altitudeInMet
141161
}
142162
int lonFront = (int)lon;
143163
int lonBack = (int)((lon - lonFront) * 100.0);
144-
var lonCmd = $":Sg{lonFront:000}*{lonBack:00}#";
164+
var lonCmd = $":Sg{lonFront:000}*{lonBack:00}#,#";
145165
var status = await SendCommand(lonCmd);
146166
if (!status.Success) return false;
147167

@@ -151,36 +171,39 @@ public async Task<bool> SetLocation(double lat, double lon, double altitudeInMet
151171
var absLat = Math.Abs(lat);
152172
int latFront = (int)absLat;
153173
int latBack = (int)((absLat - latFront) * 100.0);
154-
var latCmd = $":St{latSign}{latFront:00}*{latBack:00}#";
174+
var latCmd = $":St{latSign}{latFront:00}*{latBack:00}#,#";
155175
status = await SendCommand(latCmd);
156176
if (!status.Success) return false;
157177

158178

159179
// GMT Offset
160180
var offsetSign = DateTimeOffset.Now.Offset.TotalHours > 0 ? "+" : "-";
161181
var offset = Math.Abs(DateTimeOffset.Now.Offset.TotalHours);
162-
status = await SendCommand($":SG{offsetSign}{offset:00}#");
182+
status = await SendCommand($":SG{offsetSign}{offset:00}#,#");
163183
if (!status.Success) return false;
164184

165185

166186
// Local Time and Date
167187
var n = DateTime.Now;
168-
status = await SendCommand($":SL{n:HH:mm:ss}#");
188+
status = await SendCommand($":SL{n:HH:mm:ss}#,#");
169189
if (!status.Success) return false;
170-
status = await SendCommand($":SC{n:MM/dd/yy}#");
190+
status = await SendCommand($":SC{n:MM/dd/yy}#,#");
171191
return status.Success;
172192
}
173193

174194
public async Task<CommandResponse> SendCommand(string cmd) {
175195
if (!cmd.StartsWith(":")) {
176196
cmd = $":{cmd}";
177197
}
178-
179-
if (!cmd.EndsWith("#")) {
198+
199+
if (!cmd.EndsWith("#")) {
180200
cmd += "#";
181201
}
182-
183-
return await _commHandler.SendCommand(cmd);
202+
if (cmd.EndsWith("#,#"))
203+
{
204+
return await _commHandler.SendCommand(cmd.Substring(0,cmd.Length-2));
205+
}
206+
return await _commHandler.SendBlind(cmd);
184207
}
185208
}
186209
}

Software/OpenAstroTracker ASCOM/OATControl/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@
227227
<Button Margin="2,0,2,2" Style="{StaticResource AccentedSquareButtonStyle}" Command="{Binding StopSlewingCommand}" >Stop</Button>
228228
<StackPanel Grid.Column="2" Grid.Row="2" Orientation="Horizontal">
229229
<Button MinWidth="50" Margin="2,2" Style="{StaticResource AccentedSquareButtonStyle}" Command="{Binding HomeCommand}" >Home</Button>
230-
<Button MinWidth="50" Margin="2,2" Style="{StaticResource AccentedSquareButtonStyle}" Command="{Binding ParkCommand}" >Park</Button>
230+
<Button MinWidth="50" Margin="2,2" Style="{StaticResource AccentedSquareButtonStyle}" Command="{Binding ParkCommand}" Content="{Binding ParkCommandString}" />
231231
</StackPanel>
232232
<Button Height="26" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="0,2,2,0" Content="Set Home" Style="{StaticResource AccentedSquareButtonStyle}" Command="{Binding SetHomeCommand}" />
233233
</StackPanel>

0 commit comments

Comments
 (0)