Commit c900f24f authored by Tammo Jan Dijkema's avatar Tammo Jan Dijkema
Browse files

Add reading threads, remove getter functions

parent f1f596be
......@@ -7,6 +7,8 @@ from astropy import units as u
from configparser import ConfigParser
import os.path
import numpy as np
import threading
import select
config = ConfigParser()
config.readfp(open(os.path.join(os.path.dirname(__file__), 'telescope.ini')))
......@@ -23,10 +25,6 @@ class telescope():
raise Exception("Mode must be None, 'J2000' or 'AZEL', not " + mode)
self.setmode = setmode
self.readsocketj2000 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.readsocketj2000.connect((config.get('Console','HostName'),
config.getint('Console','Port_Read_J2000')))
self.outsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if self.setmode == 'J2000':
self.outsocket.connect((config.get('Console','HostName'),
......@@ -41,25 +39,50 @@ class telescope():
self.offsetsocket.connect((config.get('Console','HostName'),
config.getint('Console','Port_Write_Offset')))
# Initialize other traces
try:
self.tracesocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.tracesocket.connect((config.get('Trace2Port', 'Hostname'),
config.getint('Trace2Port', 'Port')))
except:
logging.info("trace2port not found running, some features won't work")
self.tracesocket = None
def getRaDec(self):
'''
Get the J200 coordinates of the telescope as an astropy SkyCoord
'''
msg = self.readsocketj2000.recv(1024)
statusData = msg.strip().split('\n')[-1].split()
ra = statusData[2]
dec = statusData[3]
return SkyCoord(ra, dec, frame='icrs')
if consoleHost is None:
consoleHost = config.get('Console','HostName')
self.j2000readthread = threading.Thread(target=self.readj2000, args=(
consoleHost,
config.getint('Console','Port_Read_J2000')))
self.j2000readthread.daemon = True
self.j2000readthread.start()
self.tracereadthread = threading.Thread(target=self.readtraces, args=(
config.get('Trace2Port', 'Hostname'),
config.getint('Trace2Port', 'Port')))
self.tracereadthread.daemon = True
self.tracereadthread.start()
def readj2000(self, consolehost, j2000_read_port):
'''
Poll the socket with the j2000 info, store its values in class members
'''
self.j2000socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.j2000socket.connect((consolehost, j2000_read_port))
while True:
readable, writable, exceptional = select.select(
[self.j2000socket], [], [])
msg = readable[0].recv(4096)
vals = msg.strip().split()
ra = vals[2]
dec = vals[3]
self.radec = SkyCoord(ra, dec, frame='icrs')
def readtraces(self, tracehost, traceport):
'''
Poll the socket with the traces, store their values in class members
'''
self.tracesocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.tracesocket.connect((tracehost, traceport))
while True:
readable, writable, exceptional = select.select(
[self.tracesocket], [], [])
msg = readable[0].recv(4096)
vals = msg.strip().split()
self.az = (float(vals[0])*u.rad).to(u.deg)
self.el = (float(vals[1])*u.rad).to(u.deg)
self.dist_az = (float(vals[2])*u.rad).to(u.deg)
self.dist_el = (float(vals[3])*u.rad).to(u.deg)
def setRaDec(self, setpoint):
'''
......@@ -86,7 +109,7 @@ class telescope():
Set the AzEl setpoint of the telescope.
Only works if the setmode is 'AZEL'
setpoint should be a tuple of radians or of astropy Quantity
setpoint should be a tuple of degrees or of astropy Quantity
'''
if self.setmode != 'AZEL':
raise Exception("Cannot set Az/El if mode is not AZEL")
......@@ -105,76 +128,32 @@ class telescope():
'''
Set the offset of the telescope in Az / El.
offset should be a tuple of radians or of astropy Quantity
offset should be a tuple of degrees or of astropy Quantity
'''
if self.setmode is None:
raise Exception("Cannot set offset if mode is None")
(off_az, off_el) = offset
if isinstance(off_az, u.Quantity):
off_az = off_az.to(u.rad).value
else:
off_az = np.deg2rad(off_az)
if isinstance(off_el, u.Quantity):
off_el = off_el.to(u.rad).value
else:
off_el = np.deg2rad(off_el)
cmd = '{:f}\t{:f}\n'.format(off_az, off_el)
logging.info('Offset sent to DT: {}'.format(cmd))
self.offsetsocket.send(cmd)
def getDistToSetpoint(self):
'''
Get the distance to the current setpoint, as a numpy array with astropy Quantity
This corresponds to the values in the upper right of the console window
Note that this value can be negative
If also other trace values are required, use getTraces()
'''
traces = self.getTraces()
return np.array([traces['azimuth_setpoint_error.difference'].value,
traces['elevation_setpoint_error.difference'].value])*traces['azimuth_setpoint_error.difference'].unit
def getAzEl(self):
'''
Get the current azimuth and elevation, as a numpy array with astropy Quantity
This corresponds to the values in the upper left of the console window
If also other trace values are required, use getTraces()
'''
traces = self.getTraces()
return np.array([traces['Azimuth_Position'].value,
traces['Elevation_Position'].value])*traces['Azimuth_Position'].unit
def getTraces(self):
'''
Get a dict with the traces of trace2port.
'''
if self.tracesocket is None:
raise Exception("getTraces only works when trace2port is running")
msg = self.tracesocket.recv(1024)
logging.debug(msg)
if not msg:
raise Exception("trace2port socket has gone away")
statusData = msg.strip().split('\n')[-1].split()
if len(statusData) < 4:
statusData = msg.strip().split('\n')[-2].split()
ret = dict()
for tracename in ['Azimuth_Position', 'Elevation_Position',
'azimuth_setpoint_error.difference', 'elevation_setpoint_error.difference']:
traceval = float(statusData[config.getint("Trace2Port", tracename)])
ret[tracename] = (traceval*u.rad).to(u.deg)
return ret
def waitUntilThere(self):
'''
Wait until distance to the setpoint gets within tolerance
'''
while not np.all(np.abs(self.getDistToSetpoint())<0.01*u.deg):
while abs(self.dist_el)>0.01*u.deg or abs(self.dist_az)>0.01*u.deg:
# No time.sleep here, because getDistToSetpoint already blocks with an interval
continue
# Socket poll, bitmask met filedesc van socket.open (poll is blocking)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment