-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitradio
285 lines (265 loc) · 9.04 KB
/
initradio
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
#!/usr/bin/env python3
import syslog
import os
import serial
import json
import argparse
import datetime
import codecs
import ephem
from calendar import timegm
from math import degrees
from random import uniform
import time
from time import time, ctime, mktime, sleep
syslog.openlog("initradio", facility=syslog.LOG_DAEMON)
latitude = 41.4118
longitude = -71.2398
altitude = 20
#os.system('sudo mkdir /dev/shm/TQ')
#os.system('sudo mount -a')
def initradio(tx,rx,wide):
ser = serial.Serial('/dev/serial0',9600,timeout=2)
syslog.syslog(syslog.LOG_INFO, 'Initializing radio connection')
attempts = 0
while attempts < 3:
try:
success = False
ser.write(b'AT+DMOCONNECT\r\n')
resp = str(codecs.decode(ser.readline()))[0:13]
#syslog.syslog(syslog.LOG_INFO, resp)
if resp == '+DMOCONNECT:0':
success = True
except:
attempts += 1
syslog.syslog(syslog.LOG_ERR, 'Initialization failed, retrying...: ' + resp)
#add power cycle radio routine here
continue
else:
if success:
syslog.syslog(syslog.LOG_INFO, 'Link to radio established.')
break
finally:
if not success:
syslog.syslog(syslog.LOG_ERR, 'Radio non responsive! Shutting down: ' + resp)
os.system('sudo systemctl stop direwolf')
#add power off radio routine here
exit()
attempts = 0
success = False
ser.write(b'AT+VERSION\r\n')
resp = str(codecs.decode(ser.readline()))[0:21]
syslog.syslog(syslog.LOG_INFO, resp)
while attempts < 3:
try:
success = False
syslog.syslog(syslog.LOG_INFO, 'Setting radio to TX ' + str(tx) +' mhz and RX ' + str(rx) + ' mhz')
setgroup = str.encode('AT+DMOSETGROUP=' + wide + ',' + tx + ',' + rx + ',0000,1,0000\r\n')
ser.write(setgroup)
resp = str(codecs.decode(ser.readline()))[0:14]
#syslog.syslog(syslog.LOG_INFO, resp)
if resp == '+DMOSETGROUP:0':
success = True
except:
attempts += 1
syslog.syslog(syslog.LOG_ERR, 'Tuning to frequency failed, retrying...: ' + resp)
#add power cycle radio routine here
continue
else:
if success:
syslog.syslog(syslog.LOG_INFO, 'Radio successfully tuned to TX ' + str(tx) + ' Mhz / RX ' + str(rx) + ' Mhz')
break
finally:
if not success:
syslog.syslog(syslog.LOG_ERR, 'Tuning frequency failed! Shutting down: ' + resp)
os.system('sudo systemctl stop direwolf')
exit()
attempts = 0
success = False
while attempts < 3:
try:
success = False
ser.write(b'AT+SETFILTER=1,1,1\r\n')
#with filters 1 = off, 0 = on per DRA-818v datasheet
resp = str(codecs.decode(ser.readline()))[0:15]
#syslog.syslog(syslog.LOG_INFO, resp)
if resp == '+DMOSETFILTER:0':
success = True
except:
attempts += 1
syslog.syslog(syslog.LOG_ERR, 'Set audio filters failed, retrying...: ' + resp)
continue
else:
if success:
syslog.syslog(syslog.LOG_INFO, 'Turned off pre/de-emphasis, highpass, and lowpass filters')
break
finally:
if not success:
syslog.syslog(syslog.LOG_ERR, 'Set audio filters failed. Ignoring: ' + resp)
attempts = 0
while attempts < 3:
try:
success = False
ser.write(b'AT+DMOSETVOLUME=3\r\n')
resp = str(codecs.decode(ser.readline()))[0:15]
#syslog.syslog(syslog.LOG_INFO, resp)
if resp == '+DMOSETVOLUME:0':
success = True
except:
attempts += 1
syslog.syslog(syslog.LOG_ERR, 'Failed to set volume, retrying...:' + resp)
continue
else:
if success:
syslog.syslog(syslog.LOG_INFO, 'Set volume level to 3')
break
finally:
if not success:
syslog.syslog(syslog.LOG_ERR, 'Failed to set volume. Ignoring: ' + resp)
ser.close()
return()
def waitforit(eventtime):
current = mktime(datetime.datetime.now().timetuple())
if eventtime <= current:
delay = 0
syslog.syslog(syslog.LOG_INFO, "The ISS is passing overhead now, proceeding to acquire target")
else:
delta = eventtime - current
delay = max(0, delta - 5)
seconds = delay % (24 * 3600)
hours = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
syslog.syslog(syslog.LOG_INFO, "Waiting " + str(int(hours)) + " hours " + str(int(minutes)) + " minutes " + str(int(seconds)) + " seconds to acquire target on " + ctime(eventtime))
sleep(delay)
syslog.syslog(syslog.LOG_INFO, "The ISS is passing overhead now, proceeding to acquire target")
return()
def profile(mode):
os.system ('sudo mount -o remount,rw /')
os.system('sudo cp /etc/direwolf.' + mode + ' /etc/direwolf.conf')
if mode == 'aprs':
initradio('144.3900','144.3900','0')
#As I understand it APRS is narrow 12.5khz bandwidth
if mode == 'iss':
initradio('syslog.syslog(syslog.LOG_INFO, 'ISS Profile loaded')
#But the ISS uses wide band 25 khz bandwidth
##Set the radio off frequency for the ISS to account for the doppler shift
#at the beginning of the pass as the ISS is moving towards us
os.system('sudo mount -o remount,ro /')
return()
def direwolf(mode):
syslog.syslog(syslog.LOG_INFO, 'Restarting DireWolf to activate the ' + mode + ' profile')
os.system('sudo systemctl restart direwolf')
return()
def get_segments(secs,begin,middle,end)
seg1 = secs * begin
seg2 = secs * middle
seg3 = secs * end
return (seg1, seg2, seg3)
def listen(secs):
seconds = secs % (24 * 3600)
hours = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
syslog.syslog(syslog.LOG_INFO, 'Listening for the ISS for ' + str(int(minutes)) + ' minutes ' + str(int(seconds)) + ' seconds')
if secs <= 364: #less than 10%
doppler = int(secs)
if secs <=240: #Low Pass
segments = get_segments(secs,.10,.80,.10)
elif secs <=360: #Medium Pass
segments = get_segments(secs,.20,.60,.20)
else: #secs > 360 high pass
segments = get_segments(secs,.25,.50,.25)
#For a rudimentary adjustment for doppler shift, we take the time of the total pass
#and split into 3 segments, for a low pass, 10-80-10, for a medium pass 20-60-20, and for a high pass 25-50-25
#For simplicity, we'll use 5 segments, but the shift will vary so for a low pass +1,0,0,0,-1 for a medium pass +2,+1,0,-1,-2 for a high pass +3,+2,+1,0,-1,-2,-3
#shifting +3, +2, +1, 0, -1, -2, -3 khz, with the 24% segment being simplex
initradio('145.8220','145.8280','0')
sleep(segments[0])
syslog.syslog(syslog.LOG_INFO, 'adjusting for doppler shift')
initradio('145.8250','145.8250','0')
sleep(segments[1])
syslog.syslog(syslog.LOG_INFO, 'adjusting for doppler shift')
initradio('145.8280','145.8220','0')
sleep(segments[2])
syslog.syslog(syslog.LOG_INFO, 'Returning to regular APRS profile')
return()
def get_passes(lon, lat, alt, n):
"""Compute n number of passes of the ISS for a location"""
# Get latest TLE of the ISS from NASA
iss_tle = urlopen('https://tle.ivanstanojevic.me/api/tle/25544').read()
tle = json.loads(iss_tle)
iss = ephem.readtle(str(tle[0]), str(tle[1]), str(tle[2]))
# Set location
location = ephem.Observer()
location.lat = str(latitude)
location.long = str(longitude)
location.elevation = altitude
# Override refration calculation
location.pressure = 0
location.horizon = '10:00'
# Set time now
now = datetime.datetime.utcnow()
location.date = now
# Predict passes
passes = []
for p in xrange(n):
tr, azr, tt, altt, ts, azs = location.next_pass(iss)
duration = int((ts - tr) * 60 * 60 * 24)
year, month, day, hour, minute, second = tr.tuple()
dt = datetime.datetime(year, month, day, hour, minute, int(second))
if duration > 60:
passes.append({"risetime": timegm(dt.timetuple()), "duration": duration})
# Increase the time by more than a pass and less than an orbit
location.date = tr + 25*ephem.minute
# Return object
obj = {"request": {
"datetime": timegm(now.timetuple()),
"latitude": lat,
"longitude": lon,
"altitude": alt,
"passes": n,
},
"response": passes,
}
return obj
def getnextpass(lat,lon,alt):
while True:
attempts = 0
while attempts < 3:
try:
issinfo = ISS_Info.iss_passes(lat,lon,alt,2)['response']
except:
syslog.syslog(syslog.LOG_ERR, 'Failed to retrieve an ISS pass')
attempts += 1
continue
else:
timenow = mktime(datetime.datetime.now().timetuple())
resp = dict(issinfo[0])
starttime = resp['risetime']
duration = resp['duration']
syslog.syslog(syslog.LOG_INFO, 'The International Space Station is passing overhead on ' + ctime(starttime))
if starttime < timenow:
syslog.syslog(syslog.LOG_INFO, 'Current pass is in progress or just occurred. Using next pass')
dict.clear(resp)
resp = dict(issinfo[1])
starttime = resp['risetime']
duration = resp['duration']
syslog.syslog(syslog.LOG_INFO, 'The International Space Station is passing overhead next on ' + ctime(starttime))
waitforit(starttime)
syslog.syslog(syslog.LOG_INFO, 'Setting DireWolf to use ISS profile')
profile('iss')
direwolf('iss')
listen(duration)
profile('aprs')
direwolf('aprs')
dict.clear(resp)
break
finally:
list.clear(issinfo)
return()
profile('aprs')
direwolf('aprs')
getnextpass(latitude,longitude,altitude)