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 socket
import logging
from astropy.coordinates import SkyCoord, AltAz
from astropy import units as u
from configparser import ConfigParser
def create_socket(host, port): config = ConfigParser()
s = None config.read('telescope.ini')
for res in socket.getaddrinfo(host, port,\ print(config.keys())
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
class telescope(): class telescope():
def __init__(self, lat, long, alt,\ def __init__(self, setmode=None, consoleHost=None):
listenOnly = True, connect = False,\ '''
RDPORT = 11030, STPORT = 11031, HOST = 'console'): Initializes a telescope instance. The mode for writing can be
self._lat = lat 'J2000' or 'AZEL'.
self._long = long '''
self._alt = alt if setmode not in ('J2000', 'AZEL', None):
self._connect = connect raise Exception("Mode must be None, 'J2000' or 'AZEL', not " + mode)
if connect: self.setmode = setmode
self._readsocket = create_socket(HOST, RDPORT)
if self._readsocket == None: self.readsocketj2000 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Failed to create read connection" self.readsocketj2000.connect((config['Console']['HostName'],
elif not(listenOnly): config['Console']['Port_Read_J2000']))
self._setsocket = create_socket(HOST, STPORT)
if self._setsocket == None: self.outsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Failed to create set socket" if self.setmode == 'J2000':
else: self.outsocket.connect((host, setport_j2000))
self._setsocket = None elif self.setmode == 'AZEL':
else: self.outsocket.connect((host, setport_azel))
self._readsocket = None
self._setsocket = None def getRaDec(self):
'''
def lat(self): Get the J200 coordinates of the telescope as an astropy SkyCoord
return self._lat '''
def long(self): msg = self.readsocketj2000.recv(4096)
return self._long statusData = msg.split()
def alt(self): ra = statusData[2].decode("utf-8")
return self._alt dec = statusData[3].decode("utf-8")
return SkyCoord(ra, dec, frame='icrs')
def readPos(self):
if self._readsocket != None: def setRaDec(self, setpoint):
return self._readsocket.recv(1024) '''
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: else:
return None (az, el) = setpoint
def setPosRADec(self, RA, Dec): md = bytes(az + '\t' + el + '\n', 'UTF-8')
if self._setsocket: logging.info('AzEl setpoint sent to DT: {}'.format(cmd))
self._setsocket.send(str(RA)+" "+str(Dec)+"\n") outsocket.send(cmd)
# The remote host def setOffset(self, offset):
# Port for receiving setpoint position, '''
# current position in Ra, Dec, and UT Unix time in format Set the offset of the telescope in Az / El.
# "xxhxxmxx.x xxxdxxmxx.xs xxhxxmxx.x xxxdxxmxx.xs xxxxxxxxx"
# Port for setting setpoint position, offset should be a tuple of radians or of astropy Quantity
# new position in Ra, Dec '''
# "xxhxxmxx.x xxxdxxmxx.xs (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
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