-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenastroclient.py
296 lines (288 loc) · 9.28 KB
/
openastroclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import time, threading, io, sys
import PyIndi
import readline
import autopa
import calibrate
from os.path import split
from util import exec
cmdName = "Meade"
telescopeName = "LX200 OpenAstroTech"
# telescopeName = "ZWO CCD ASI183MC"
dec_offset = 15964
ra_offset = -2692
hostname = "stellarmate.local"
# hostname = "localhost"
ra_steps = "421.8" # 417.9
dec_steps = "421.8" # 418.9
port = 7624
#user = "astroberry"
ssh_cmd = "ssh mate"
class OpenAstroClient(PyIndi.BaseClient):
def __init__(self, hostname=hostname, port=port):
super(OpenAstroClient, self).__init__()
self.setServer(hostname,int(port))
self.blobEvent=threading.Event()
self.debug = True
self._hostname = hostname
if (not(self.connectServer())):
raise Exception(f"No indiserver running on {hostname}:{port} - Run server in Ekos first.")
self.telescope=self.getDevice(telescopeName)
while not(self.telescope):
time.sleep(0.5)
self.log(f"Waiting for {telescopeName}")
self.telescope=self.getDevice(telescopeName)
if True:
self.connect()
self.meadeProp = self.telescope.getText(cmdName)
while not(self.meadeProp):
self.log(f"Waiting for '{cmdName}'")
time.sleep(0.5)
self.meadeProp=self.telescope.getText(cmdName)
@property
def debug(self):
return self._debug
@property
def hostname(self):
return self._hostname
@debug.setter
def debug(self, val):
self._debug = val
def log(self,msg,end="\n"):
if self.debug:
print(msg,end=end)
def sendCommand(self,cmd):
self.meadeResultRead = False
self.meadeProp[0].text = cmd
self.log(f">> Sending: {cmd}")
self.sendNewText(self.meadeProp)
time.sleep(0.2)
return ("ok", "ok")
def sendCommandAndWait(self,cmd):
self.meadeResultRead = False
self.meadeProp[0].text = cmd
self.log(f">> Sending: {cmd}")
self.sendNewText(self.meadeProp)
while not(self.meadeResultRead):
self.log(f"\r<<Waiting for result: {cmd}", end="")
time.sleep(0.2)
self.log(f"<< Received: {self.meadeResult.s} {self.meadeResult.tp.text}")
return self.meadeResult.s,self.meadeResult.tp.text
def newDevice(self, d):
pass
def newProperty(self, p):
pass
def removeProperty(self, p):
pass
def newBLOB(self, bp):
pass
def newSwitch(self, svp):
pass
def newNumber(self, nvp):
pass
def newText(self, tvp):
if tvp.label == cmdName:
self.meadeResultRead = True
self.meadeResult = tvp
self.log(f">> newText=>{tvp.label}={tvp.tp.text}")
pass
def newLight(self, lvp):
pass
def newMessage(self, d, m):
pass
def serverConnected(self):
pass
def serverDisconnected(self, code):
pass
def toggleSwitch(self, switch, index):
for i in range(0,len(switch)):
state = PyIndi.ISS_ON if i == index else PyIndi.ISS_OFF
switch[i].setState(state)
self.sendNewSwitch(switch) # send this new value to the device
def getProp(self, device, propType, propName):
func = None
if propType.lower() == "switch":
func = self.telescope.getSwitch
elif propType.lower() == "number":
func = self.telescope.getNumber
elif propType.lower() == "text":
func = self.telescope.getText
result = None
if func != None:
result = func(propName)
while not(result):
time.sleep(0.5)
result = func(propName)
return result
def connect(self):
telescope_connect = self.getProp(self.telescope, "switch", "CONNECTION")
if not(self.telescope.isConnected()):
self.toggleSwitch(telescope_connect, 0)
def disconnect(self):
telescope_connect = self.getProp(self.telescope, "switch", "CONNECTION")
# if the telescope device is not connected, we do connect it
while self.telescope.isConnected():
self.toggleSwitch(telescope_connect, 1)
time.sleep(1)
class Settings:
def __init__(self):
self.dec_lower = None
self.dec_upper = None
self.ra_steps = None
self.dec_steps = None
self.dec_park = None
self.ra_offset = None
pass
def save(self):
pass
def load(self):
pass
def read(self):
if(self.dec_lower):
return
res = sendCommandAndWait(f"XGDLL")[1]
# res = res.split("|")
self.dec_lower = float(res)
res = sendCommandAndWait(f"XGDLU")[1]
self.dec_upper = float(res)
self.ra_steps = float(sendCommandAndWait(f"XGR")[1])
self.dec_steps = float(sendCommandAndWait(f"XGD")[1])
self.dec_park = int(sendCommandAndWait(f"XGDP")[1])
self.ra_offset = int(sendCommandAndWait(f"XGHR")[1])
def write(self):
pass
def print(self):
print(f"""DEC Limits:
Lower: {self.dec_lower}
Upper: {self.dec_upper}
Steps:
RA: {self.ra_steps}
DEC: {self.dec_steps}
Offsets:
DEC: {self.dec_park}
RA: {self.ra_offset}""")
if __name__ == '__main__':
c = OpenAstroClient()
s = Settings()
cal = calibrate.Calibrate(c)
def sendCommand(cmd):
res = c.sendCommand(f"{cmd}#")
print(f"# {cmd} -> {res}")
return res
def sendCommandAndWait(cmd):
res = c.sendCommandAndWait(f":{cmd}#")
print(f"# {cmd} -> {res}")
return res
def status():
res = sendCommandAndWait(f"GX")[1].split(",")[0]
return res
def pos():
res = sendCommandAndWait(f"GX")[1].split(",")[2:4]
return [int(res[0]),int(res[1])]
def print_settings():
print("# settings")
s.read()
s.print()
def shutdown():
print("# shutdown")
exec(f"{ssh_cmd} 'sudo shutdown now'")
def xdf():
print("# firmware display")
sendCommandAndWait(f"XGHR")
sendCommandAndWait(f"XGD")
sendCommandAndWait(f"XGR")
def xfr():
print("# firmware reset")
sendCommandAndWait("XFR")
sendCommand(f":XSHR{ra_offset}#")
sendCommand(f":XSD{dec_steps}#")
sendCommand(f":XSR{ra_steps}#")
print(f"XGD{sendCommandAndWait('XGD')[1]}\nXGR{sendCommandAndWait('XGR')[1]}")
def reboot():
print("# reboot")
exec(f"{ssh_cmd} 'sudo reboot'")
def waitFor(states):
while True:
res = status()
print(res)
if res in states:# == 'Tracking' or res == 'Parked':
break
def rest():
(ra_pos,dec_pos) = pos()
print(f"current pos: {(ra_pos,dec_pos)}")
sendCommandAndWait(f"MXr{-ra_pos}")
waitFor(['Tracking', 'Parked'])
sendCommandAndWait(f"MXd{- dec_pos - dec_offset}")
waitFor(['Tracking', 'Parked'])
def home():
# re-set steps
# disabled to see if this causes the speedup
# sendCommand(f"XSR{ra_steps}")
# stop motors
sendCommandAndWait(f"Q")
# xfr()
# get status for first display
sendCommandAndWait(f"GX")
# set slew speed to M
sendCommandAndWait(f"RC")
# NOTE: re-check when the sensor was bent
# find RA home offset (just to be safe)
# sendCommandAndWait(f"XSHR{ra_offset}")
# find RA home in 2 hours range
sendCommandAndWait(f"MHRR3")
waitFor(['Tracking', 'Parked'])
# set slew speed to highest again
sendCommandAndWait(f"RS")
# move to 90 deg
sendCommandAndWait(f"MXd{dec_offset}")
waitFor(['Tracking', 'Parked'])
# set home pos
sendCommandAndWait(f"SHP")
# stop motors
sendCommandAndWait(f"Q")
sendCommandAndWait(f"GX")
print("# homing done")
def pa():
pa = autopa.AutoPA(c)
pa.alignOnce()
def calibrate(cmd):
if cmd != '#cal':
ra = cmd.endswith("ra")
dec = cmd.endswith("dec")
cal.calibrate(ra=ra, dec=dec)
else:
cal.calibrate()
while True:
print(">> Command: ", end="")
string = input()
if len(string) < 1:
continue
if string[0] == '#':
parts = string.split()
cmd,args = (parts[0], parts[1:])
if cmd == '#sleep':
time.sleep(int(args[0]))
if cmd == '#temp':
time.sleep(int(args[0]))
if cmd == '#home':
home()
if cmd == '#rest':
rest()
if cmd == '#xfr':
xfr()
if cmd == '#shutdown':
shutdown()
if cmd == '#reboot':
reboot()
if cmd == '#prefs':
print_settings()
if cmd.startswith('#cal'):
calibrate(cmd)
if cmd == '#pa':
pa()
elif string[0] == '@':
print(f">>> Send: {string}#")
result = c.sendCommand(f"{string}#")
print(f">> Result: {result}")
else:
result = sendCommandAndWait(string)
print(f">> Result: {result}")