-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrig_control.py
More file actions
432 lines (368 loc) · 16.4 KB
/
rig_control.py
File metadata and controls
432 lines (368 loc) · 16.4 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
################################################################################
#
# RigControl.py - Rev 1.0
# Copyright (C) 2021-6 by Joseph B. Attili, joe DOT aa2il AT gmail DOT com
#
# Rig Control class for satellite predictions.
#
################################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
################################################################################
import sys
import numpy as np
from widgets_qt import QTLIB
exec('from '+QTLIB+' import QtCore')
import time
from datetime import timedelta,datetime
from rig_io.ft_tables import SATELLITE_LIST,SAT_PL_TONES
from rotor import *
from utilities import freq2band, error_trap
################################################################################
# Rig control called every sec seconds
class RigControl:
def __init__(self,P,sec):
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.Updater)
self.timer.start(1000*sec)
self.P = P
self.gui = P.gui
self.frqA = 0
self.frqB = 0
self.fdown = None
self.fdop1 = None
self.fdop2 = None
self.az = None
self.el = None
self.sat_map_cntr=0
if P.PLATFORM=='Windows':
tmpfile="satellites.log"
else:
tmpfile="/tmp/satellites.log"
self.fp_log = open(tmpfile, "w")
row=['Time Stamp','Source','Selected',
'Inverting','dn1','dn2','up1','up2','Mode',
'fup','fdown','df','fdop1','fdop2',
'frqA','frqB','RIT','XIT',
'az','el','pos[0]','pos[1]','new_pos[0]','new_pos[1]','daz','de',
'flipper','rig_engaged','rotor_engaged','rotor_updated']
for item in row:
self.fp_log.write(str(item)+',')
self.fp_log.write('\n')
self.fp_log.flush()
def Updater(self):
P=self.P
gui=P.gui
VERBOSITY=0
if VERBOSITY>0:
print('\nRIG CONTROL UPDATER ...')
if P.USE_LOCK:
acq=gui.rigLock.acquire(timeout=0.5)
if not acq:
error_trap('UPDATER: Unable to acquire lock - giving up :-(')
return None
need_update=gui.refresh.isSet()
if need_update:
print('UPDATER: Needs and update!',gui.Selected)
gui.refresh.clear()
engaged = gui.rig_engaged or gui.rotor_engaged
if (need_update or engaged or self.fdown==None) and gui.Selected:
# Tune to middle of transponder BW if we've selected a new sat
if gui.New_Sat_Selection:
print('\nNew Sat Selected:',gui.Selected)
gui.setFocus()
P.satellite = gui.Satellites[gui.Selected]
P.SIMPLEX=False
if P.SSTV and hasattr(P.satellite,'sstv'):
P.transp = P.satellite.transponders[P.satellite.sstv]
P.SIMPLEX = True
print('UPDATER: sstv =',P.satellite.sstv)
print('UPDATER: transp=',P.transp)
elif P.satellite.main:
P.transp = P.satellite.transponders[P.satellite.main]
print('UPDATER: main=',P.satellite.main)
print('UPDATER: transp=',P.transp)
else:
print('RIG_CONTROL->UPDATER: Hmmmm - no transponder for this sat')
gui.New_Sat_Selection=False
return
# Put rig into sat mode
if P.sock.rig_type2=='FT991a':
print('Putting FT991a into SPLIT mode ...')
P.sock.split_mode(1,VERBOSITY=1)
P.SAT_MODE = True
self.vfos=['A','B']
elif P.sock.rig_type2=='IC9700':
if P.satellite.name in ['Moon','IO-117'] or P.SIMPLEX:
print('Putting IC9700 into Regular (non-SAT) mode ...')
P.sock.sat_mode(0,VERBOSITY=1)
P.SAT_MODE = False
self.vfos=['A','B']
else:
print('Putting IC9700 into SAT mode ...')
P.sock.sat_mode(1,VERBOSITY=1)
P.SAT_MODE = True
self.vfos=['M','S']
self.check_ic9700_bands(P)
elif P.sock.rig_type2=='pySDR':
self.vfos=['A']
elif P.sock.rig_type2==None or P.sock.rig_type2=='None' \
or P.sock.rig_type2=='Dummy':
self.vfos=['A','B']
else:
print('UPDATER: Unknown rig',P.sock.rig_type2,' - Aborting')
sys.exit(0)
# Set proper mode on both VFOs
mode = gui.ModeSelect(USE_LOCK=False)
# Set PL Tone for FM sats
if mode=='FM':
name = P.satellite.name
print('UPDATER: FM Sat ',name,'\tmode=',mode,
'\ttones=',SAT_PL_TONES)
if name in SAT_PL_TONES:
tone = SAT_PL_TONES[name]
print('UPDATER: FM Sat ',name,' - Setting PL Tone to ',tone,' Hz')
P.sock.set_PLtone(tone,VERBOSITY=1)
tone2=P.sock.get_PLtone(VERBOSITY=1)
print('\tread back=',tone2,' Hz')
gui.status_bar.setText('PL Tone = '+str(tone2)+' Hz',APPEND=True)
# Set downlink freq to center of transp passband - uplink will follow
self.fdown = 0.5*(P.transp['fdn1']+P.transp['fdn2'])
self.track_freqs(tag='Selection')
gui.New_Sat_Selection=False
# Tell keyer name of new sat
if self.P.UDP_CLIENT:
self.P.udp_client.Send('Sat:'+gui.Selected)
# Set RIT & XIT for this sat
try:
OFFSETS=self.P.SETTINGS['OFFSETS']
#print('\toffsets=',OFFSETS,'\tSelected=',gui.Selected)
print('\toffsets=',OFFSETS[gui.Selected])
gui.rit=OFFSETS[gui.Selected][0]
gui.xit=OFFSETS[gui.Selected][1]
except:
error_trap('RIG CONTROL->UPDATER: Unable to set RIT/XIT')
print('\tsat=',gui.Selected)
gui.rit=0
gui.xit=0
gui.txt11.setText(str(gui.rit))
gui.txt13.setText(str(gui.xit))
else:
# Check if op has spun main dial - if so, compute new downlink freq
frq = int( P.sock.get_freq(VFO=self.vfos[0]) )
#print('++++++++++++++++++++++++++--------------- frq=',frq,'\tfrqA=',self.frqA)
if frq>0 and frq!=self.frqA:
#print('========================================================================')
#print('=============================== Rig freq change: old frq=',self.frqA,'\tnew frq=',frq)
#print('========================================================================')
# Compute new downlink freq at the sat
self.fdown = frq - gui.rit - self.fdop1
# Don't do anything until op stops spinning the dial
self.frqA = frq
nan=np.nan
self.save_diagnostics('Spin',nan,[nan,nan],[nan,nan],nan,nan,nan)
return
# Update up and down link freqs - WAS 2 TABS IN
self.track_freqs(tag='Update')
else:
self.track_freqs(tag='Update')
# Update AOS/LOS indicator and other gui items that may have changed
self.update_aos_los()
self.P.SatModeAct.setChecked(self.P.SAT_MODE)
if P.USE_LOCK:
gui.rigLock.release()
# Routine to check VFO bands - the IC9700 is quirky if the bands are reversed
def check_ic9700_bands(self,P):
frq1 = int( P.sock.get_freq(VFO=self.vfos[0]) )
#band1 = P.sock.get_band(1e-6*frq1)
band1 = freq2band(1e-6*frq1)
print('frq1=',frq1,band1)
frq2 = P.transp['fdn1']
#band2 = P.sock.get_band(1e-6*frq2)
band2 = freq2band(1e-6*frq2)
print('frq2=',frq2,band2)
if band1!=band2:
print('Flipping VFOs')
P.sock.select_vfo('X')
# Routine to set rig mode on both VFOs
def set_rig_mode(self,mode,bw=None):
P=self.P
print('=== RIG_SET_MODE: mode=',mode,'\tbw=',bw,'\tvfos=',self.vfos,
'\tinverting=',P.transp['Inverting'],'===')
if mode=='SSTV':
mode='FM'
if bw==None:
filter='Wide'
else:
filter=bw
P.sock.set_mode(mode,VFO=self.vfos[0],Filter=filter,VERBOSITY=1)
if len(self.vfos)>1:
if P.transp['Inverting']:
if mode=='FM':
P.sock.set_mode('FM',VFO=self.vfos[1])
elif mode=='USB':
P.sock.set_mode('LSB',VFO=self.vfos[1])
elif mode=='LSB':
P.sock.set_mode('USB',VFO=self.vfos[1])
elif mode=='CW':
P.sock.set_mode('CW-R',VFO=self.vfos[1],Filter=filter)
elif mode=='CW-R':
P.sock.set_mode('CW',VFO=self.vfos[1],Filter=filter)
else:
P.sock.set_mode(mode,VFO=self.vfos[1],Filter=filter)
print('================== RIG_SET_MODE Done ================================')
# Function to set up & downlink freqs on rig
def track_freqs(self,Force=False,tag='Track'):
P=self.P
gui=self.gui
#print('\nTRACK_FREQS: P.transp=',P.transp)
# Compute uplink freq corresponding to downlink
try:
df = self.fdown - P.transp['fdn1']
if P.transp['fup1']==0:
self.fup = 0
elif P.transp['Inverting']:
self.fup = P.transp['fup2'] - df
#print('Inv:',P.transp['fup2'],df,self.fup)
else:
self.fup = P.transp['fup1'] + df
#print('Non:',P.transp['fup1'],df,self.fup)
except:
error_trap('RIG CONTROL -> TRACK FREQS - Whoops!')
print('\tsat=',gui.Selected)
return
# Compute Doppler shifts for up and down links
[self.fdop1,self.fdop2,self.az,self.el,rng,lat,lon,footprint] = \
P.satellite.Doppler_Shifts(self.fdown,self.fup,P.my_qth)
# Set up link freq
if len(self.vfos)>1 and self.fup>0:
self.frqB = int(self.fup+self.fdop2 + gui.xit)
if gui.rig_engaged or Force:
P.sock.set_freq(1e-3*self.frqB,VFO=self.vfos[1],VERBOSITY=0)
# Compute downlink freq at rig = frq at sat + Doppler
self.frqA = int(self.fdown+self.fdop1 + gui.rit)
if gui.rig_engaged or Force:
print('\nTRACK FREQS: VFO A=',self.frqA,'\tVFO B=',self.frqB)
P.sock.set_freq(1e-3*self.frqA,VFO=self.vfos[0],VERBOSITY=0)
if P.USE_SDR:
#print('Setting SDR freq to:',1e-3*self.frqA)
P.sock3.set_freq(1e-3*self.frqA)
#print(self.frqA,self.frqB)
# Form new rotor position
if 1:
rotor_updated,pos,daz,de,new_pos = \
rotor_positioning(gui,self.az,self.el,Force)
"""
try:
except:
error_trap('RIG CONTROL -> TRACK_FREQS: Unable to update rotor position')
print(self.az,self.el)
rotor_updated=False
pos=[np.nan,np.nan]
new_pos=[np.nan,np.nan]
daz=np.nan
de=np.nan
"""
# Update sky track & sat map
gui.plot_position(self.az,self.el,pos)
engaged = gui.rig_engaged or gui.rotor_engaged
if self.P.SHOW_MAP and engaged:
self.sat_map_cntr+=1
if self.sat_map_cntr>=10:
name = P.satellite.name
print('\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Updating footprint ...',name)
self.sat_map_cntr=0
gui.MapWin.DrawSatFootprint(name,lon,lat,footprint)
# Toggle voice recorder (if available)
if self.el>0:
#print('=== ',gui.Selected,' is VISIBLE ===',az,el)
on_off=P.sock.recorder(True)
else:
#print('=== ',gui.Selected,' is BELOW THE HORIZON ===',az,el)
on_off=P.sock.recorder(False)
#print('=== buf=',on_off)
# Update gui
gui.txt1.setText("{:,}".format(int(self.fdown)))
gui.txt2.setText("{:,}".format(int(self.fup)))
gui.txt3.setText("{:,}".format(int(self.frqA)))
gui.txt4.setText("{:,}".format(int(self.frqB)))
#print('=== new_pos=',new_pos)
if not np.isnan(new_pos[0]):
gui.txt5.setText("Az: {: 3d}".format(int(new_pos[0])))
else:
gui.txt6.setText("El: {: f}".format(new_pos[0]))
if not np.isnan(new_pos[1]):
gui.txt6.setText("El: {: 3d}".format(int(new_pos[1])))
else:
gui.txt6.setText("El: {: f}".format(new_pos[1]))
if gui.flipper:
gui.txt7.setText('Flip-a-roo-ski!')
else:
gui.txt7.setText('Not flipped')
#self.update_aos_los()
gui.SRng.setText( '%d miles' % rng )
# Save log file to assist in further development
self.save_diagnostics(tag,df,pos,new_pos,daz,de,rotor_updated)
# Save log file to assist in further development
def save_diagnostics(self,source,df,pos,new_pos,daz,de,rotor_updated):
P=self.P
gui=self.gui
now = datetime.now()
row=[now,source,gui.Selected,
P.transp['Inverting'],P.transp['fdn1'],P.transp['fdn2'],
P.transp['fup1'],P.transp['fup2'],P.transp['mode'],
self.fup,self.fdown,df,self.fdop1,self.fdop2,
self.frqA,self.frqB,gui.rit,gui.xit,
self.az,self.el,pos[0],pos[1],new_pos[0],new_pos[1],daz,de,
gui.flipper,gui.rig_engaged,gui.rotor_engaged,rotor_updated]
for item in row:
self.fp_log.write(str(item)+',')
self.fp_log.write('\n')
self.fp_log.flush()
def hms(self,dt):
hrs = int( dt/3600. )
dt2 = dt-3600*hrs
mins = int( dt2/60. )
secs = int( dt2-60*mins )
txt = "{:02d}:{:02d}:{:02d}".format(hrs,mins,secs)
#print(dt,dt2,dt3,'\t',hrs,mins,secs,'\t',txt)
return txt
# Function to update portion of gui related to AOS/LOS
def update_aos_los(self):
gui=self.P.gui
now = time.mktime( datetime.now().timetuple() )
#print('UPDATE AOS-LOS: Now=',now,type(now),
# '\taos=',gui.aos,type(gui.aos),
# '\tlos=',gui.los,type(gui.los))
try:
daos=gui.aos-now
dlos=gui.los-now
#print('UPDATE AOS-LOS: d-aos=',daos,'\td-los=',dlos)
except:
daos=0
dlos=0
#print('UPDATE AOS-LOS: Whooops!')
if daos>0:
gui.txt9.setText("AOS in\t"+self.hms(daos))
gui.event_type = 1
elif dlos>0:
gui.txt9.setText("LOS in\t"+self.hms(dlos))
gui.event_type = 0
else:
gui.txt9.setText("Past Event")
gui.event_type = -1
if False:
screen = QDesktopWidget().screenGeometry()
print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ screen=',screen)
widget = gui.win.geometry()
print('win=',widget)
print('hint=',gui.win.sizeHint())