Commit 4fd99e25 authored by Tammo Jan Dijkema's avatar Tammo Jan Dijkema
Browse files

Try to configure through ini-file (does not work yet)

parent 5ee3729b
from __future__ import print_function
import socket
import logging
from astropy.coordinates import SkyCoord, AltAz
from astropy import units as u
from configparser import ConfigParser
def create_socket(host, port):
s = None
for res in socket.getaddrinfo(host, port,\
socket.AF_UNSPEC,\
socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error, msg:
s = None
continue
try:
s.connect(sa)
except socket.error, msg:
s.close()
s = None
continue
break
return s
config = ConfigParser()
config.read('telescope.ini')
print(config.keys())
class telescope():
def __init__(self, lat, long, alt,\
listenOnly = True, connect = False,\
RDPORT = 11030, STPORT = 11031, HOST = 'console'):
self._lat = lat
self._long = long
self._alt = alt
self._connect = connect
if connect:
self._readsocket = create_socket(HOST, RDPORT)
if self._readsocket == None:
print "Failed to create read connection"
elif not(listenOnly):
self._setsocket = create_socket(HOST, STPORT)
if self._setsocket == None:
print "Failed to create set socket"
else:
self._setsocket = None
else:
self._readsocket = None
self._setsocket = None
def lat(self):
return self._lat
def long(self):
return self._long
def alt(self):
return self._alt
def readPos(self):
if self._readsocket != None:
return self._readsocket.recv(1024)
def __init__(self, setmode=None, consoleHost=None):
'''
Initializes a telescope instance. The mode for writing can be
'J2000' or 'AZEL'.
'''
if setmode not in ('J2000', 'AZEL', None):
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['Console']['HostName'],
config['Console']['Port_Read_J2000']))
self.outsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if self.setmode == 'J2000':
self.outsocket.connect((host, setport_j2000))
elif self.setmode == 'AZEL':
self.outsocket.connect((host, setport_azel))
def getRaDec(self):
'''
Get the J200 coordinates of the telescope as an astropy SkyCoord
'''
msg = self.readsocketj2000.recv(4096)
statusData = msg.split()
ra = statusData[2].decode("utf-8")
dec = statusData[3].decode("utf-8")
return SkyCoord(ra, dec, frame='icrs')
def setRaDec(self, setpoint):
'''
Set the J2000 setpoint of the telescope.
Only works if the setmode is 'J2000'.
setpoint can be an astropy SkyCoord or a tuple (ra,dec) in radians
'''
if self.setmode != 'J2000':
raise Exception("Cannot set Ra/Dec if mode is not J2000")
if not isinstance(setpoint, SkyCoord):
coord = SkyCoord(ra =setpoint[0] * u.radian,
dec=setpoint[1] * u.radian)
(ra, dec) = setpoint.to_string('hmsdms').split()
cmd = bytes(ra + "\t" + dec[1:] + "\n", 'UTF-8')
logging.info('J2000 setpoint sent to DT: {}'.format(cmd))
outsocket.send(cmd)
def setAzEl(self, setpoint):
'''
Set the AzEl setpoint of the telescope.
Only works if the setmode is 'AZEL'
setpoint can be an astropy AltAz or a tuple (az,el) in radians
'''
if self.setmode != 'AZEL':
raise Exception("Cannot set Az/El i mode is not AZEL")
if isinstance(setpoint, AltAz):
az = setpoint.az.radian
el = setpoint.alt.radian
else:
return None
def setPosRADec(self, RA, Dec):
if self._setsocket:
self._setsocket.send(str(RA)+" "+str(Dec)+"\n")
# The remote host
# Port for receiving setpoint position,
# current position in Ra, Dec, and UT Unix time in format
# "xxhxxmxx.x xxxdxxmxx.xs xxhxxmxx.x xxxdxxmxx.xs xxxxxxxxx"
# Port for setting setpoint position,
# new position in Ra, Dec
# "xxhxxmxx.x xxxdxxmxx.xs
(az, el) = setpoint
md = bytes(az + '\t' + el + '\n', 'UTF-8')
logging.info('AzEl setpoint sent to DT: {}'.format(cmd))
outsocket.send(cmd)
def setOffset(self, offset):
'''
Set the offset of the telescope in Az / El.
offset should be a tuple of radians or of astropy Quantity
'''
(off_az, off_el) = offset
if isinstance(off_az, u.Quantity):
off_az = off_az.to(u.rad).value
if isinstance(off_az, u.Quantity):
off_el = off_el.to(u.rad).value
md = bytes(off_az + '\t' + off_el + '\n', 'UTF-8')
logging.info('Offset sent to DT: {}'.format(cmd))
outsocket.send(cmd)
[Console]
HostName: consoledemo.dmz.camras.nl
Port_Read_J2000 : 11030
Port_Write_J2000 : 11031
Port_Write_AzEl : 11041
Port_Write_Offset: 11011
[Trace2PortHost]
HostName: localhost
Port: 11042
Az_idx: 1
El_idx: 2
Markdown is supported
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