-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_TCP_with_Stellarium.py
110 lines (92 loc) · 3.84 KB
/
simple_TCP_with_Stellarium.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
#Talking to Stellarium through a tcp socket.
#Install telescope control on Stellarium
#In config, choose:
# telescope controlled by external software
# name: SRT
# connection delay 1 s
# host: localhost
# port: 10002
# field of view 3
#
#this code stops (not prettily) when stellarium disconnects
#
from __future__ import print_function
import struct
import socket, select
import numpy as np
import serial
from astropy import units as u
from astropy.time import Time
from astropy.coordinates import SkyCoord, EarthLocation, AltAz
#from astropy.utils.iers import conf
#conf.auto_max_age = None
ser = serial.Serial('/dev/ttyACM1',19200, timeout=1)
def send(s): # send a command to qp
ser.write(s+'\n')
print("to qp: " + s)
return
def update_cursor(ra,dec): # sets the stellarium cursor, in radians
imy_ra = int(ra*0x80000000/np.pi)
imy_dec = int(dec*0x80000000/np.pi)
reply = struct.pack("3iIii", 24, 0, data[2], imy_ra, imy_dec, 0)
i.send(reply)
#Acre Road Obeservatory
acre_lat = 55.9024278*u.degree
acre_lon = -4.307582/180*u.degree
Acre_Road = EarthLocation(lat=acre_lat, lon=acre_lon, height=50*u.m)
# Home position
oaz = 22.0*u.degree
oalt = 3.0*u.degree
#initialise
send('O' + ' %.5f' % acre_lat.to(u.radian).value + ' %.5f' % acre_lon.to(u.radian).value + ' 0.0 0.0 '+' %.5f' % oaz.to(u.radian).value +' %.5f' % oalt.to(u.radian).value + ' 6.0')
send('gH')
home = False
while not home:
line = ser.readline()
if line.split(' ')[0] == '#home:':
home = True
print("telescope homed")
print("from qp: "+line)
open_sockets = []
listening_socket = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
listening_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listening_socket.bind( ("", 10002) ) # port number
listening_socket.listen(5)
time = Time.now()
home_pos = AltAz(oaz,oalt,obstime=time,location=Acre_Road)
goto_ra = SkyCoord(home_pos).icrs.ra
goto_dec = SkyCoord(home_pos).icrs.dec
c = SkyCoord(ra=goto_ra.to(u.radian), dec=goto_dec.to(u.radian), frame='icrs')
track = False
while True:
rlist, wlist, xlist = select.select( [listening_socket] + open_sockets, [], [],1 ) # get any goto data from stellarium, 1 sec timeout
for i in rlist:
if i is listening_socket:
new_socket, addr = listening_socket.accept()
open_sockets.append(new_socket)
else:
data = i.recv(1024)
if data == "":
open_sockets.remove(i)
print ("Connection closed")
else: # we have goto data from stellarium...
data = struct.unpack("3iIi", data)
goto_ra = data[3]*(np.pi/0x80000000)
goto_dec = data[4]*(np.pi/0x80000000)
print('from Stellarium: RA = {0:2.3f} Dec = {1:2.3f} radians'.format(goto_ra,goto_dec))
c = SkyCoord(ra=goto_ra*u.radian, dec=goto_dec*u.radian, frame='icrs')
track = True
if track:
time = Time.now()
c_altaz = c.transform_to(AltAz(obstime=time,location=Acre_Road))
# print("Alt = {0.alt:.5} Az = {0.az:.5}".format(c_altaz))
send('gh' + ' %.5f' % c_altaz.az.to(u.radian).value + ' %.5f' % c_altaz.alt.to(u.radian).value)
# see that qp has to say for itself
for line in ser.readlines():
print("from qp: "+line)
if (line.split()[0] == '>g') and (line.split()[1] == 'A'):
time = Time.now()
current_pos = AltAz(float(line.split()[2])*u.radian ,float(line.split()[3])*u.radian,obstime=time,location=Acre_Road)
my_ra = SkyCoord(current_pos).icrs.ra
my_dec = SkyCoord(current_pos).icrs.dec
update_cursor(my_ra.to(u.radian).value, my_dec.to(u.radian).value)