/
telescope.py
175 lines (137 loc) · 5.04 KB
/
telescope.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
import serial
import time
class Telescope:
def __init__(self, debug=False):
self.debug = debug
self.mount = Mount(self)
self.focuser = Focuser(self)
self.clock = Clock(self)
def connect(self, device):
self.__serial_port = serial.Serial(
port=device,
baudrate=9600,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=5)
# Check the telescope is listening.
self.sendCommand(chr(0x06))
response = self.__getCharacterResponse()
if not response:
raise Exception('Telescope did not reply.')
def sendCommand(self, command, response_type='none'):
if self.debug:
print(command)
self.__serial_port.write(command.encode('ascii'))
if response_type == 'boolean':
response = self.__getBooleanResponse()
elif response_type == 'character':
response = self.__getCharacterResponse()
elif response_type == 'string':
response = self.__getStringResponse()
else:
return
return response
def findHome(self):
self.sendCommand(':hF#')
def queryHomeStatus(self):
home_status = self.sendCommand(':h?#', 'character')
if (home_status == '1'):
return 'found'
elif (home_status == '2'):
return 'in_progress'
else:
return 'failed'
def setLocation(self):
self.sendCommand(':W1#')
self.sendCommand(':St+51*04#', 'boolean') # Latitude
self.sendCommand(':Sg1*47#', 'boolean') # Longitude
def __getCharacterResponse(self):
character = self.__serial_port.read(1)
if len(character) != 1:
raise Exception('Timed out waiting for response.')
return character.decode(errors='ignore')
def __getBooleanResponse(self):
character = self.__getCharacterResponse()
if character == '1':
return True
else:
return False
def __getStringResponse(self):
result = '';
while True:
character = self.__getCharacterResponse()
if character == '#':
break
result += character
return result
class Clock:
def __init__(self, telescope):
self.telescope = telescope
def setTime(self):
self.telescope.sendCommand(':hI' + time.strftime('%y%m%d%H%M%S') + '#', 'boolean')
def getTime(self):
time24 = self.telescope.sendCommand(':GL#', 'string')
date = self.telescope.sendCommand(':GC#', 'string')
return time.strptime(time24 + ' ' + date, '%H:%M:%S %m/%d/%y')
class Focuser:
def __init__(self, telescope):
self.telescope = telescope
def focus(self, direction):
if direction == 'in':
self.telescope.sendCommand(':F+#')
elif direction == 'out':
self.telescope.sendCommand(':F-#')
else:
raise Exception('Unknown focus direction.')
def setSpeed(self, speed):
if speed == 'slowest' or speed == 1:
self.telescope.sendCommand(':F1#')
elif speed == 'slow' or speed == 2:
self.telescope.sendCommand(':F2#')
elif speed == 'fast' or speed == 3:
self.telescope.sendCommand(':F3#')
elif speed == 'fastest' or speed == 4:
self.telescope.sendCommand(':F4#')
else:
raise Exception('Unknown focus speed.')
def halt(self):
self.telescope.sendCommand(':FQ#')
class Mount:
def __init__(self, telescope):
self.telescope = telescope
def slew(self, direction):
if direction == 'up':
self.telescope.sendCommand(':Mn#')
elif direction == 'down':
self.telescope.sendCommand(':Ms#')
elif direction == 'left':
self.telescope.sendCommand(':Mw#')
elif direction == 'right':
self.telescope.sendCommand(':Me#')
else:
raise Exception('Unknown slew direction.')
def setSpeed(self, speed):
if speed == 'slowest' or speed == 1:
self.telescope.sendCommand(':RG#')
elif speed == 'slow' or speed == 2:
self.telescope.sendCommand(':RC#')
elif speed == 'fast' or speed == 3:
self.telescope.sendCommand(':RM#')
elif speed == 'fastest' or speed == 4:
self.telescope.sendCommand(':RS#')
else:
raise Exception('Unknown slew speed.')
def setHorizontalRate(self, rate):
self.telescope.sendCommand(':RA' + str(rate * 8) + '#')
def setVerticalRate(self, rate):
self.telescope.sendCommand(':RE' + str(rate * 8) + '#')
def halt(self, axis=None):
if axis == 'x':
self.telescope.sendCommand(':Qw#')
self.telescope.sendCommand(':Qe#')
elif axis == 'y':
self.telescope.sendCommand(':Qn#')
self.telescope.sendCommand(':Qs#')
else:
self.telescope.sendCommand(':Q#')