Skip to content

Commit

Permalink
some optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
baskiton committed Jul 29, 2024
1 parent b563695 commit 9e24ae5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
6 changes: 1 addition & 5 deletions sats_receiver/gr_modules/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ def __init__(self,

self.resamp_gcd = resamp_gcd = math.gcd(bandwidth, samp_rate)

self.blocks_copy = gr.blocks.copy(gr.gr.sizeof_gr_complex)
self.blocks_copy.set_enabled(self.enabled)
self.freqshifter = gr.blocks.rotator_cc(2 * math.pi * (main_tune - frequency) / samp_rate)
self.resampler = gr.filter.rational_resampler_ccc(
interpolation=bandwidth // resamp_gcd,
Expand All @@ -50,15 +48,13 @@ def __init__(self,

self.connect(
self,
self.blocks_copy,
self.freqshifter,
self.resampler,
self,
)

def set_enabled(self, enabled):
self.enabled = enabled
self.blocks_copy.set_enabled(enabled)

def set_freq_offset(self, new_freq: Union[int, float]):
self.freqshifter.set_phase_inc(2 * math.pi * (self.main_tune - new_freq) / self.samp_rate)
Expand Down Expand Up @@ -291,6 +287,7 @@ def start(self, observation_key: str):
for decoder in self.decoders:
decoder.set_observation_key(observation_key)
decoder.start()
self.radio.set_enabled(1)

def stop(self):
if self.is_runned:
Expand Down Expand Up @@ -539,7 +536,6 @@ def start(self):

for r in self.recorders:
r.start(observation_key)
r.radio.set_enabled(1)

def stop(self):
if self.is_runned:
Expand Down
66 changes: 46 additions & 20 deletions sats_receiver/gr_modules/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ def __init__(self, up, config: Mapping):
self.ltz = dateutil.tz.tzlocal()

self.flow = []
self.signal_src = gr.blocks.null_source(gr.gr.sizeof_gr_complex)
self.connector = self.signal_src = gr.blocks.null_source(gr.gr.sizeof_gr_complex)
self.flow.append(self.signal_src)

if self.dc_block:
self.blocks_correctiq = gr.blocks.correctiq()
self.connector = self.blocks_correctiq = gr.blocks.correctiq()
self.flow.append(self.blocks_correctiq)

if self.decimation > 1:
self.decim = gr.filter.rational_resampler_ccc(interpolation=1, decimation=self.decimation, taps=[])
self.connector = self.decim = gr.filter.rational_resampler_ccc(interpolation=1, decimation=self.decimation, taps=[])
self.flow.append(self.decim)

self.src_null_sink = gr.blocks.null_sink(gr.gr.sizeof_gr_complex)
Expand Down Expand Up @@ -116,9 +116,9 @@ def update_config(self, config: Mapping, force=False):
for sat_name in to_remove_sats:
sat = self.satellites[sat_name]
self.up.scheduler.cancel(*sat.events)
if sat.is_runned:
self.disconnect(self.connector, sat)
sat.stop()
if self.is_runned:
self.disconnect(self.flow[-2], sat)
del self.satellites[sat_name]

for sat_name in to_create_sats:
Expand All @@ -140,9 +140,9 @@ def update_config(self, config: Mapping, force=False):
continue

if self.calculate_pass(sat):
if self.is_runned:
self.connect(self.flow[-2], sat)
self.satellites[sat.name] = sat
if sat.is_runned:
self.connect(self.connector, sat)

if to_remove_sats or to_create_sats:
self.unlock()
Expand Down Expand Up @@ -266,16 +266,20 @@ def is_active(self) -> bool:

return any(x.is_runned for x in self.satellites.values())

def set_source(self, src):
if self.connector == self.signal_src:
self.connector = src
self.signal_src = self.flow[0] = src

def start(self, max_noutput_items=10000000):
if self.enabled and not self.is_runned:
self.log.info('START tune=%sHz samp_rate=%sHz gain=%s biast=%s',
utils.num_disp(self.tune, 3), utils.num_disp(self.samp_rate, 3),
self.gain, self.biast)

try:
self.signal_src = gr.soapy.source(f'driver={self.source}{self.serial and f",serial={self.serial}"}',
'fc32', 1, '', '', [''], [''])
self.flow[0] = self.signal_src
self.set_source(gr.soapy.source(f'driver={self.source}{self.serial and f",serial={self.serial}"}',
'fc32', 1, '', '', [''], ['']))
except RuntimeError as e:
self.log.error('cannot start: %s', e)

Expand All @@ -296,9 +300,6 @@ def start(self, max_noutput_items=10000000):

self.connect(*self.flow)

for sat in self.satellites.values():
self.connect(self.flow[-2], sat)

super(SatsReceiver, self).start(max_noutput_items)
self.is_runned = True

Expand All @@ -314,19 +315,44 @@ def stop(self, sched_clear=True):
super(SatsReceiver, self).stop()

self.disconnect(*self.flow)
from_disc = self.flow[-2]
self.signal_src = gr.blocks.null_source(gr.gr.sizeof_gr_complex)
self.flow[0] = self.signal_src
self.set_source(gr.blocks.null_source(gr.gr.sizeof_gr_complex))

for sat in self.satellites.values():
if sched_clear:
self.up.scheduler.cancel(*sat.events)
if self.is_runned and sat.is_runned:
try:
self.disconnect(self.connector, sat)
sat.log.warning('stop %s', self.connector.name())
except ValueError as e:
sat.log.warning('stop %s fail: %s', self.connector.name(), e)
sat.stop()
if self.is_runned:
self.disconnect(from_disc, sat)

self.is_runned = False

def sat_attach(self, sat: modules.Satellite):
if self.is_runned:
self.lock()
self.connect(self.connector, sat)
sat.log.warning('attach %s', self.connector.name())
sat.start()
if self.is_runned:
self.unlock()

def sat_detach(self, sat: modules.Satellite):
try:
if self.is_runned:
self.lock()
if sat.is_runned:
self.disconnect(self.connector, sat)
sat.log.warning('detach %s', self.connector.name())
except ValueError as e:
sat.log.warning('detach %s fail: %s', self.connector.name(), e)
finally:
if self.is_runned:
self.unlock()
sat.stop()

def action(self):
if self.is_active and not self.start():
for sat in self.satellites.values():
Expand Down Expand Up @@ -368,8 +394,8 @@ def calculate_pass(self, sat: modules.Satellite):
if set_t < rise_t:
rise_t = t
sat.events = [
None if sat.is_runned else self.up.scheduler.plan(rise_t, sat.start),
self.up.scheduler.plan(set_t, sat.stop),
None if sat.is_runned else self.up.scheduler.plan(rise_t, self.sat_attach, sat),
self.up.scheduler.plan(set_t, self.sat_detach, sat),
self.up.scheduler.plan(set_tt, self.calculate_pass, sat)
]
self.log.info('Sat `%s` planned on %s <-> %s',
Expand Down

0 comments on commit 9e24ae5

Please sign in to comment.