-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtel
executable file
·285 lines (224 loc) · 9.24 KB
/
tel
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
#!/usr/bin/env python3
#
# This file is part of the Robotic Observatory Control Kit (rockit)
#
# rockit is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# rockit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with rockit. If not, see <http://www.gnu.org/licenses/>.
"""Commandline client for managing the telescope"""
import glob
import os
import sys
import Pyro4
from astropy.coordinates import Angle, SkyCoord
import astropy.units as u
from rockit.mount.planewave import CommandStatus, MountState, Config
from rockit.common import print
SCRIPT_NAME = os.path.basename(sys.argv[0])
sys.excepthook = Pyro4.util.excepthook
def run_command(command, args):
"""Runs a daemon command, handling cancellation and error messages"""
if 'MOUNTD_CONFIG_PATH' in os.environ:
config = Config(os.environ['MOUNTD_CONFIG_PATH'])
else:
# Load the config file defined in the MOUNTD_CONFIG_PATH environment variable or from the
# default system location (/etc/mountd/). Exit with an error if zero or multiple are found.
files = glob.glob("/etc/mountd/*.json")
if len(files) != 1:
print('error: failed to guess the default config file. ' +
'Run as MOUNTD_CONFIG_PATH=/path/to/config.json tel <command> \\[<args>]')
return 1
config = Config(files[0])
try:
ret = command(config, args)
except KeyboardInterrupt:
# ctrl-c terminates the running command
# pylint: disable=comparison-with-callable
if command != status:
ret = stop(config, args)
# pylint: enable=comparison-with-callable
# Report successful stop
if ret == 0:
ret = -100
except Pyro4.errors.CommunicationError:
ret = -101
# Print message associated with error codes, except for -1 (error handled locally)
if ret not in (0, -1):
print(CommandStatus.message(ret))
return ret
def park(config, args):
"""Slews the telescope to a named park position"""
if len(args) == 1:
if args[0] not in config.park_positions:
print(f'error: unknown park position \'{args[0]}\'')
return -1
ping_daemon(config)
with config.daemon.connect(timeout=0) as daemon:
return daemon.park(args[0])
print(f'usage: {SCRIPT_NAME} park <position>')
print()
for name, info in config.park_positions.items():
print(f' {name:6s} {info["desc"]}')
print()
return -1
def slew(config, args):
"""Slews the telescope to a specified J2000 RA,Dec position"""
if len(args) != 2:
print(f'usage: {SCRIPT_NAME} slew <HH:MM:SS.S> <DD:MM:SS.S>')
return -1
try:
coord = SkyCoord(ra=args[0], dec=args[1], unit=(u.hourangle, u.deg))
except ValueError as e:
print('error: ' + str(e))
return -1
ping_daemon(config)
with config.daemon.connect(timeout=0) as daemon:
return daemon.slew_radec(coord.ra.to_value(u.deg), coord.dec.to_value(u.deg))
def horizon(config, args):
"""Slews the telescope to a specified Alt,Az position"""
if len(args) != 2:
print(f'usage: {SCRIPT_NAME} horizon <DD:MM:SS.S> <DD:MM:SS.S>')
return -1
try:
coord = SkyCoord(alt=args[0], az=args[1], unit=u.deg, frame='altaz')
except ValueError as e:
print('error: ' + str(e))
return -1
ping_daemon(config)
with config.daemon.connect(timeout=0) as daemon:
return daemon.slew_altaz(coord.alt.to_value(u.deg), coord.az.to_value(u.deg))
def offset(config, args):
"""Offsets the telescope by a specified delta RA,Dec"""
if len(args) != 2:
print(f'usage: {SCRIPT_NAME} offset <HH:MM:SS.S> <DD:MM:SS.S>')
return -1
try:
offset_ra = Angle(args[0], unit=u.hourangle)
except ValueError:
print(f'error: failed to parse \'{args[0]}\' as a HH:MM:SS.S right ascension.')
return -1
try:
offset_dec = Angle(args[1], unit=u.deg)
except ValueError:
print(f'error: failed to parse \'{args[1]}\' as a DD:MM:SS.S declination.')
return -1
ping_daemon(config)
with config.daemon.connect(timeout=0) as daemon:
return daemon.offset_radec(offset_ra.to_value(u.deg), offset_dec.to_value(u.deg))
def track(config, args):
"""Slews the telescope to a specified J2000 RA,Dec position and begins tracking"""
if len(args) != 2:
print(f'usage: {SCRIPT_NAME} track <HH:MM:SS.S> <DD:MM:SS.S>')
return -1
try:
coord = SkyCoord(ra=args[0], dec=args[1], unit=(u.hourangle, u.deg))
except ValueError as e:
print('error: ' + str(e))
return -1
ping_daemon(config)
with config.daemon.connect(timeout=0) as daemon:
return daemon.track_radec(coord.ra.to_value(u.deg), coord.dec.to_value(u.deg))
def home(config, _):
"""Find the mount home position"""
ping_daemon(config)
with config.daemon.connect(timeout=0) as daemon:
return daemon.find_homes()
def status(config, _):
"""Reports the current mount status"""
with config.daemon.connect() as daemon:
data = daemon.report_status()
if data is None:
return 1
print(f' Telescope is {MountState.label(data["state"], formatting=True)}')
if data['state'] in [MountState.Disabled, MountState.NotHomed, MountState.Homing]:
return 0
coords = SkyCoord(ra=data['ra'], dec=data['dec'], unit=u.deg)
ra_desc = coords.ra.to(u.hourangle).to_string(sep=':', precision=2)
dec_desc = coords.dec.to(u.deg).to_string(sep=':', precision=2)
altaz = SkyCoord(alt=data['alt'], az=data['az'], unit=u.deg, frame='altaz')
alt_desc = altaz.alt.to(u.deg).to_string(sep=':', precision=2)
az_desc = altaz.az.to_string(sep=':', precision=2)
ra_offset_desc = ''
ra_offset = Angle(data['offset_ra'], unit=u.deg).to(u.hourangle)
if ra_offset != 0:
ra_offset_desc = f' with offset [b]{ra_offset.to_string(sep=":", precision=2)}[/b]'
dec_offset_desc = ''
dec_offset = Angle(data['offset_dec'], unit=u.deg)
if dec_offset != 0:
dec_offset_desc = f' with offset [b]{dec_offset.to_string(sep=":", precision=2)}[/b]'
print(f' RA is [b]{ra_desc}[/b]' + ra_offset_desc)
print(f' Dec is [b]{dec_desc}[/b]' + dec_offset_desc)
print(f' Altitude is [b]{alt_desc}[/b]')
print(f' Azimuth is [b]{az_desc}[/b]')
print(f' Moon separation is [b]{data["moon_separation"]:.0f}\u00B0[/b]')
print(f' Sun separation is [b]{data["sun_separation"]:.0f}\u00B0[/b]')
lst_desc = Angle(data['lst'], unit=u.deg).to(u.hourangle).to_string(sep=':', precision=2)
print(f' Local sidereal time is [b]{lst_desc}[/b]')
return 0
def stop(config, _):
"""Stops any active mount movement"""
with config.daemon.connect() as daemon:
return daemon.stop()
def initialize(config, _):
"""Connect to mount and enable motor power"""
with config.daemon.connect() as daemon:
return daemon.initialize()
def shutdown(config, _):
"""Disable motor power and disconnect from mount"""
with config.daemon.connect() as daemon:
return daemon.shutdown()
def list_parks(config, _):
"""List available park positions for bash command completion"""
print(' '.join(sorted(config.park_positions.keys())))
return 0
def ping_daemon(config):
"""Check that the daemon is alive before calling a long-timeout method"""
with config.daemon.connect() as daemon:
daemon.ping()
def print_usage():
"""Prints the utility help"""
print(f'usage: {SCRIPT_NAME} <command> \\[<args>]')
print()
print('general commands:')
print(' status print a human-readable summary of the telescope status')
print(' park park the telescope in a named position')
print()
print('observing commands:')
print(' slew slew the telescope to a given J2000 RA, Dec')
print(' horizon slew the telescope to a given Alt, Az')
print(' track slew the telescope to a given J2000 RA, Dec and track the target')
print(' offset offset the telescope by a given RA, Dec')
print(' stop immediately stop any mount movement')
print()
print('engineering commands:')
print(' init connect to mount and enable motor power')
print(' home find the mount home position')
print(' kill disable motor power and disconnect from mount')
print()
return 0
if __name__ == '__main__':
commands = {
'park': park,
'slew': slew,
'horizon': horizon,
'track': track,
'offset': offset,
'status': status,
'home': home,
'stop': stop,
'init': initialize,
'kill': shutdown,
'list-parks': list_parks
}
if len(sys.argv) >= 2 and sys.argv[1] in commands:
sys.exit(run_command(commands[sys.argv[1]], sys.argv[2:]))
sys.exit(print_usage())