Tengo 2 programas:
- tracker.py: realiza los calculos necesarios para apuntar una antena en elevación y azimut a un satélite en base a los datos TLE de celestrak
- client.py: establece una conexion por socket con un servidor que le da las órdenes a los dos servomotores que apuntan a la antena
Para ello tengo que enviarle en tiempo real los calculos realizados por el tracker en un string especifico: str("P" + azimut + elevacion)
La duda que me surge es si ¿tengo que definirlos como funciones en un mismo programa o llamar las variables calculadas de un programa a otro?¿se puede hacer esto? Adjunto los códigos
tracker.py
import time, datetime, ephem
from math import *
ec1_tle = {"Norad": "ESTCUBE 1",
"Linea1": "1 39161U 13021C 21026.18622615 .00000188 00000-0 37342-4 0 9999",
"Linea2": "2 39161 97.9502 110.5937 0008490 244.1514 115.8810 14.72415311414813"}
Pontevedra = ("42.4200", "-8.640", "0")
class Tracker():
def __init__(self, satelite, estacion=("42.4200", "-8.640", "0")):
self.estacion = ephem.Observer()
self.estacion.lat = estacion[0]
self.estacion.lon = estacion[1]
self.estacion.elevation = int(estacion[2])
self.satelite = ephem.readtle(
satelite["Norad"], satelite["Linea1"], satelite["Linea2"])
def set_epoch(self, epoch=time.time()):
''' sets epoch when parameters are observed '''
self.estacion.date = datetime.datetime.utcfromtimestamp(epoch)
self.satelite.compute(self.estacion)
def azimuth(self):
''' returns satelite azimuth in degrees '''
return degrees(self.satelite.az)
def elevation(self):
''' returns satelite elevation in degrees '''
return degrees(self.satelite.alt)
def latitude(self):
''' returns satelite latitude in degrees '''
return degrees(self.satelite.sublat)
def longitude(self):
''' returns satelite longitude in degrees '''
return degrees(self.satelite.sublong)
def range(self):
''' returns satelite range in meters '''
return self.satelite.range
def ecef_coordinates(self):
''' returns satellite earth centered cartesian coordinates'''
x, y, z = self._aer2ecef(self.azimuth(), self.elevation(), self.range(), float(
self.estacion.lat), float(self.estacion.lon), self.estacion.elevation)
return x, y, z
def _aer2ecef(self, azimuthDeg, elevationDeg, slantRange, obs_lat, obs_long, obs_alt):
# site ecef in meters
sitex, sitey, sitez = llh2ecef(obs_lat, obs_long, obs_alt)
# some needed calculations
senlat = sin(radians(obs_lat))
senlon = sin(radians(obs_long))
coslat = cos(radians(obs_lat))
coslon = cos(radians(obs_long))
azRad = radians(azimuthDeg)
elRad = radians(elevationDeg)
# az,el,range to sez convertion
SUR = -slantRange * cos(elRad) * cos(azRad)
ESTE = slantRange * cos(elRad) * sin(azRad)
CENIT = slantRange * sin(elRad)
x = (senlat * coslon * SUR) + (-senlon * ESTE) + \
(coslat * coslon * CENIT) + sitex
y = (senlat * senlon * SUR) + (coslon * ESTE) + \
(coslat * senlon * zenith) + sitey
z = (-coslat * SUR) + (senlat * CENIT) + sitez
return x, y, z
tracker = Tracker(satelite=ec1_tle, estacion=Pontevedra)
while 1:
tracker.set_epoch(time.time())
print('Time:', time.ctime(time.time()))
print("Calculado desde: (42.395 N ; 8.708 W)\n")
print("A continuación se muestra en pantalla el Azimut:\n")
print(tracker.azimuth())
print("A continuación se muestra en pantalla la Elevación:\n")
print("elevación : %0.2f" % tracker.elevation())
AZ = str(tracker.azimuth())
EL = str(tracker.elevation())
mensaje = "P" + " " + AZ + " " + EL
print(mensaje)
time.sleep(2)
client.py
import socket, time, select
# local host IP '127.0.0.1'
host = '127.0.0.1'
# Define the port on which you want to connect
port = 4533
address = (host,port)
print("Se va a establecer la conexión...\n")
FORMAT = 'ascii'
#Creamos el socket y lo conectamos por el socket 4533 a localhost
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
result_of_check = s.connect_ex(address)
def Client():
while True:
print("Conexion establecida")
message = "P 180.0 0.0\r"
# message sent to server
s.send(message.encode(FORMAT))
infds, outfds, errfds = select.select([s], [s], [], 5)
if len(infds) != 0:
data = s.recv(1024)
if len(data) != 0:
print('Received from the server :',str(data.decode(FORMAT)))
if len(outfds) != 0:
print("Esperando...\n")
time.sleep(5)
# Close the connection
s.close()
print("Un saludo")
def Comprobacion():
if result_of_check == 0:
print("Se va a establecer conexión...\n")
Client()
else:
print("No es posible establecer conexion\n")
s.close()
Comprobacion()