From d2f162fa5eab5cfdf4332101b4a55aaac6d96290 Mon Sep 17 00:00:00 2001 From: Piotr Krysik Date: Thu, 30 Nov 2017 12:50:00 +0100 Subject: trx: Changes in the redio_if -added a part generated by GRC (radio_if_grc) form trx_radio_if example, -radio_if now only adds things that are missing in radio_if_grc --- python/trx/CMakeLists.txt | 4 +- python/trx/__init__.py | 1 + python/trx/radio_if.py | 231 ++++------------------------------------ python/trx/radio_if_grc.py | 259 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 283 insertions(+), 212 deletions(-) create mode 100755 python/trx/radio_if_grc.py diff --git a/python/trx/CMakeLists.txt b/python/trx/CMakeLists.txt index d8e9164..4049e54 100644 --- a/python/trx/CMakeLists.txt +++ b/python/trx/CMakeLists.txt @@ -24,5 +24,7 @@ GR_PYTHON_INSTALL( ctrl_if.py ctrl_if_bb.py fake_pm.py - radio_if.py DESTINATION ${GR_PYTHON_DIR}/grgsm/trx + radio_if.py + radio_if_grc.py + DESTINATION ${GR_PYTHON_DIR}/grgsm/trx ) diff --git a/python/trx/__init__.py b/python/trx/__init__.py index 81e569c..5d193b5 100644 --- a/python/trx/__init__.py +++ b/python/trx/__init__.py @@ -24,4 +24,5 @@ from udp_link import udp_link from ctrl_if import ctrl_if from ctrl_if_bb import ctrl_if_bb from fake_pm import fake_pm +from radio_if_grc import radio_if_grc from radio_if import radio_if diff --git a/python/trx/radio_if.py b/python/trx/radio_if.py index 689f074..9e97277 100644 --- a/python/trx/radio_if.py +++ b/python/trx/radio_if.py @@ -2,9 +2,10 @@ # -*- coding: utf-8 -*- # GR-GSM based transceiver -# Follow graph implementation +# Extending a GRC flowgraph gr-gsm/examples/trx_radio_if/radio_if_grc.grc # # (C) 2016-2017 by Vadim Yanitskiy +# (C) 2017 by Piotr Krysik # # All Rights Reserved # @@ -22,229 +23,37 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -import pmt -import time -import grgsm -import osmosdr - -from math import pi - -from gnuradio.filter import firdes -from gnuradio import digital -from gnuradio import blocks -from gnuradio import uhd -from gnuradio import gr - - -# HACK: should be implemented in C++! -import numpy as np - -class burst_type_filter(gr.sync_block): - def __init__(self, burst_types=[]): - gr.sync_block.__init__( - self, - name='Burst type filter', - in_sig=[], - out_sig=[] - ) - self.burst_types = burst_types - self.message_port_register_in(pmt.intern("bursts_in")) - self.message_port_register_out(pmt.intern("bursts_out")) - self.set_msg_handler(pmt.intern("bursts_in"), self.filter) - - def filter(self, msg): - burst_with_header = pmt.to_python(pmt.cdr(msg)) - burst = burst_with_header[-148:] - header = np.ndarray.tolist(burst_with_header[0:-148]) - burst_type = header[12] - if burst_type in self.burst_types: - self.message_port_pub(pmt.intern("bursts_out"), msg) - -class burst_to_fn_time(gr.basic_block): - def __init__(self): # only default arguments here - gr.basic_block.__init__( - self, - name='Burst to fn_time', - in_sig=[], - out_sig=[] - ) - self.message_port_register_in(pmt.intern("bursts_in")) - self.message_port_register_out(pmt.intern("fn_time_out")) - self.set_msg_handler(pmt.intern("bursts_in"), self.convert) - - def convert(self, msg): - fn_time = pmt.dict_ref(pmt.car(msg),pmt.intern("fn_time"),pmt.PMT_NIL) - fn_time_msg = pmt.dict_add(pmt.make_dict(), pmt.intern("fn_time"), fn_time) - if pmt.to_python(fn_time) is not None: - self.message_port_pub(pmt.intern("fn_time_out"), fn_time_msg) - -class radio_if(gr.top_block): - # PHY specific variables - rx_freq = 935e6 - tx_freq = 890e6 +from grgsm.trx import radio_if_grc +class radio_if(radio_if_grc): # Application state flags trx_started = False - # GSM timings - delay_correction = 285.616e-6 - ul_dl_shift = -(6.0/1625000*(156.25)*3) - def __init__(self, phy_args, phy_sample_rate, phy_rx_gain, phy_tx_gain, phy_ppm, phy_rx_antenna, phy_tx_antenna, - trx_remote_addr, trx_base_port): + trx_remote_addr, trx_base_port, + phy_tx_freq=938900000, + phy_rx_freq=938900000-45e6, + delay_correction=285.616e-6, + uplink_shift=-(6.0/1625000*(156.25)*3), + timing_advance=0): print("[i] Init Radio interface") - # PHY specific variables - self.rx_gain = phy_rx_gain - self.tx_gain = phy_tx_gain - - gr.top_block.__init__(self, "GR-GSM TRX") - - # TRX Burst Interface - self.trx_burst_if = grgsm.trx_burst_if( - trx_remote_addr, str(trx_base_port)) - - - # RX path definition - self.phy_src = uhd.usrp_source(phy_args, - uhd.stream_args(cpu_format="fc32", - channels=range(1))) - - self.phy_src.set_center_freq(self.rx_freq, 0) - self.phy_src.set_antenna(phy_rx_antenna, 0) - self.phy_src.set_samp_rate(phy_sample_rate) - self.phy_src.set_bandwidth(650e3, 0) - self.phy_src.set_gain(phy_rx_gain) - - self.gsm_input = grgsm.gsm_input( - ppm = phy_ppm - int(phy_ppm), osr = 4, - fc = self.rx_freq, samp_rate_in = phy_sample_rate) - - self.gsm_receiver = grgsm.receiver(4, ([0]), ([])) - - self.gsm_clck_ctrl = grgsm.clock_offset_control( - self.rx_freq, phy_sample_rate, osr = 4) - - self.ts_filter = grgsm.burst_timeslot_filter(0) - self.ts_filter.set_policy(grgsm.FILTER_POLICY_DROP_ALL) - - # Connections - self.connect( - (self.phy_src, 0), - (self.gsm_input, 0)) - - self.connect( - (self.gsm_input, 0), - (self.gsm_receiver, 0)) - - self.msg_connect( - (self.gsm_receiver, 'C0'), - (self.ts_filter, 'in')) - - self.msg_connect( - (self.ts_filter, 'out'), - (self.trx_burst_if, 'bursts')) - - self.msg_connect( - (self.gsm_receiver, 'measurements'), - (self.gsm_clck_ctrl, 'measurements')) - - self.msg_connect( - (self.gsm_clck_ctrl, 'ctrl'), - (self.gsm_input, 'ctrl_in')) - - - # TX Path Definition - self.phy_sink = uhd.usrp_sink(phy_args, - uhd.stream_args(cpu_format="fc32", - channels=range(1)), "packet_len") - - self.phy_sink.set_antenna(phy_tx_antenna, 0) - self.phy_sink.set_samp_rate(phy_sample_rate) - self.phy_sink.set_center_freq(self.tx_freq, 0) - self.phy_sink.set_gain(self.tx_gain) - - self.tx_time_setter = grgsm.txtime_setter( - 0xffffffff, 0, 0, 0, 0, 0, - self.delay_correction + self.ul_dl_shift) - - self.tx_burst_proc = grgsm.preprocess_tx_burst() - - self.pdu_to_tagged_stream = blocks.pdu_to_tagged_stream( - blocks.byte_t, 'packet_len') - - self.gmsk_mod = grgsm.gsm_gmsk_mod( - BT = 4, pulse_duration = 4, sps = 4) - - self.burst_shaper = digital.burst_shaper_cc( - (firdes.window(firdes.WIN_HANN, 16, 0)), - 0, 20, False, "packet_len") - - # Connections - self.msg_connect( - (self.trx_burst_if, 'bursts'), - (self.tx_time_setter, 'bursts_in')) - - self.msg_connect( - (self.tx_time_setter, 'bursts_out'), - (self.tx_burst_proc, 'bursts_in')) - - self.msg_connect( - (self.tx_burst_proc, 'bursts_out'), - (self.pdu_to_tagged_stream, 'pdus')) - - self.connect( - (self.pdu_to_tagged_stream, 0), - (self.gmsk_mod, 0)) - - self.connect( - (self.gmsk_mod, 0), - (self.burst_shaper, 0)) - - self.connect( - (self.burst_shaper, 0), - (self.phy_sink, 0)) - - - # RX & TX synchronization - self.bt_filter = burst_type_filter([3]) - self.burst_to_fn_time = burst_to_fn_time() - - # Connections - self.msg_connect( - (self.gsm_receiver, 'C0'), - (self.bt_filter, 'bursts_in')) - - self.msg_connect( - (self.bt_filter, 'bursts_out'), - (self.burst_to_fn_time, 'bursts_in')) - - self.msg_connect( - (self.burst_to_fn_time, 'fn_time_out'), - (self.tx_time_setter, 'fn_time')) + radio_if_grc.__init__(self, + samp_rate=phy_sample_rate, + tx_gain=phy_tx_gain, rx_gain=phy_rx_gain, + tx_freq=phy_tx_freq, rx_freq=phy_rx_freq, + osr=4, ppm=phy_ppm, + trx_base_port=str(trx_base_port), + trx_remote_addr=trx_remote_addr, + delay_correction=delay_correction, + uplink_shift=uplink_shift, + timing_advance=timing_advance) def shutdown(self): print("[i] Shutdown Radio interface") self.stop() self.wait() - def set_rx_freq(self, fc): - self.phy_src.set_center_freq(fc, 0) - self.gsm_clck_ctrl.set_fc(fc) - self.gsm_input.set_fc(fc) - self.rx_freq = fc - - def set_tx_freq(self, fc): - self.phy_sink.set_center_freq(fc, 0) - self.tx_freq = fc - - def set_rx_gain(self, gain): - self.phy_src.set_gain(gain, 0) - self.rx_gain = gain - - def set_tx_gain(self, gain): - self.phy_sink.set_gain(gain, 0) - self.tx_gain = gain diff --git a/python/trx/radio_if_grc.py b/python/trx/radio_if_grc.py new file mode 100755 index 0000000..26c1f22 --- /dev/null +++ b/python/trx/radio_if_grc.py @@ -0,0 +1,259 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- +################################################## +# GNU Radio Python Flow Graph +# Title: Trx radio interface +# Author: (C) Piotr Krysik 2017 +# Description: Alpha version of trx radio interface +# Generated: Thu Nov 30 12:45:52 2017 +################################################## + +from gnuradio import blocks +from gnuradio import digital +from gnuradio import eng_notation +from gnuradio import filter +from gnuradio import gr +from gnuradio import uhd +from gnuradio.eng_option import eng_option +from gnuradio.filter import firdes +from grgsm import gsm_gmsk_mod +from optparse import OptionParser +import grgsm +import math +import time + + +class radio_if_grc(gr.top_block): + + def __init__(self, delay_correction=285.616e-6, osr=4, ppm=-1, rx_freq=938600000, rx_gain=40, samp_rate=13e6/12.0, timing_advance=0, trx_base_port="5700", trx_remote_addr="127.0.0.1", tx_freq=938900000, tx_gain=30, uplink_shift=-(6.0/1625000*(156.25)*3)): + gr.top_block.__init__(self, "Trx radio interface") + + ################################################## + # Parameters + ################################################## + self.delay_correction = delay_correction + self.osr = osr + self.ppm = ppm + self.rx_freq = rx_freq + self.rx_gain = rx_gain + self.samp_rate = samp_rate + self.timing_advance = timing_advance + self.trx_base_port = trx_base_port + self.trx_remote_addr = trx_remote_addr + self.tx_freq = tx_freq + self.tx_gain = tx_gain + self.uplink_shift = uplink_shift + + ################################################## + # Blocks + ################################################## + self.uhd_usrp_source_0 = uhd.usrp_source( + ",".join(("", "")), + uhd.stream_args( + cpu_format="fc32", + channels=range(1), + ), + ) + self.uhd_usrp_source_0.set_clock_rate(26e6, uhd.ALL_MBOARDS) + self.uhd_usrp_source_0.set_samp_rate(samp_rate) + self.uhd_usrp_source_0.set_center_freq(rx_freq, 0) + self.uhd_usrp_source_0.set_gain(rx_gain, 0) + self.uhd_usrp_source_0.set_antenna("RX2", 0) + self.uhd_usrp_sink_0 = uhd.usrp_sink( + ",".join(("", "")), + uhd.stream_args( + cpu_format="fc32", + channels=range(1), + ), + "packet_len", + ) + self.uhd_usrp_sink_0.set_clock_rate(26e6, uhd.ALL_MBOARDS) + self.uhd_usrp_sink_0.set_subdev_spec("A:B", 0) + self.uhd_usrp_sink_0.set_samp_rate(samp_rate) + self.uhd_usrp_sink_0.set_center_freq(tx_freq, 0) + self.uhd_usrp_sink_0.set_gain(tx_gain, 0) + self.uhd_usrp_sink_0.set_antenna("TX/RX", 0) + self.low_pass_filter_0_0 = filter.fir_filter_ccf(1, firdes.low_pass( + 1, samp_rate, 125e3, 5e3, firdes.WIN_HAMMING, 6.76)) + self.gsm_txtime_setter_0 = grgsm.txtime_setter(None if (None is not None) else 0xffffffff, 0, 0, 0, 0, timing_advance, delay_correction+uplink_shift) + self.gsm_trx_burst_if_0 = grgsm.trx_burst_if(trx_remote_addr, trx_base_port) + self.gsm_receiver_0 = grgsm.receiver(4, ([0]), ([4]), False) + self.gsm_preprocess_tx_burst_0 = grgsm.preprocess_tx_burst() + self.gsm_msg_to_tag_0_0 = grgsm.msg_to_tag() + self.gsm_msg_to_tag_0 = grgsm.msg_to_tag() + self.gsm_gmsk_mod_0 = gsm_gmsk_mod( + BT=0.3, + pulse_duration=4, + sps=osr, + ) + self.gsm_controlled_rotator_cc_0_0 = grgsm.controlled_rotator_cc(-ppm/1.0e6*2*math.pi*tx_freq/samp_rate) + self.gsm_controlled_rotator_cc_0 = grgsm.controlled_rotator_cc(ppm/1.0e6*2*math.pi*rx_freq/samp_rate) + self.gsm_burst_type_filter_0 = grgsm.burst_type_filter(([3])) + self.gsm_burst_to_fn_time_0 = grgsm.burst_to_fn_time() + self.digital_burst_shaper_xx_0 = digital.burst_shaper_cc((firdes.window(firdes.WIN_HANN, 16, 0)), 0, 20, False, "packet_len") + self.blocks_pdu_to_tagged_stream_0_0 = blocks.pdu_to_tagged_stream(blocks.byte_t, "packet_len") + + ################################################## + # Connections + ################################################## + self.msg_connect((self.gsm_burst_to_fn_time_0, 'fn_time_out'), (self.gsm_txtime_setter_0, 'fn_time')) + self.msg_connect((self.gsm_burst_type_filter_0, 'bursts_out'), (self.gsm_burst_to_fn_time_0, 'bursts_in')) + self.msg_connect((self.gsm_preprocess_tx_burst_0, 'bursts_out'), (self.blocks_pdu_to_tagged_stream_0_0, 'pdus')) + self.msg_connect((self.gsm_receiver_0, 'C0'), (self.gsm_burst_type_filter_0, 'bursts_in')) + self.msg_connect((self.gsm_receiver_0, 'C0'), (self.gsm_trx_burst_if_0, 'bursts')) + self.msg_connect((self.gsm_trx_burst_if_0, 'bursts'), (self.gsm_txtime_setter_0, 'bursts_in')) + self.msg_connect((self.gsm_txtime_setter_0, 'bursts_out'), (self.gsm_preprocess_tx_burst_0, 'bursts_in')) + self.connect((self.blocks_pdu_to_tagged_stream_0_0, 0), (self.gsm_gmsk_mod_0, 0)) + self.connect((self.digital_burst_shaper_xx_0, 0), (self.gsm_msg_to_tag_0_0, 0)) + self.connect((self.gsm_controlled_rotator_cc_0, 0), (self.low_pass_filter_0_0, 0)) + self.connect((self.gsm_controlled_rotator_cc_0_0, 0), (self.uhd_usrp_sink_0, 0)) + self.connect((self.gsm_gmsk_mod_0, 0), (self.digital_burst_shaper_xx_0, 0)) + self.connect((self.gsm_msg_to_tag_0, 0), (self.gsm_controlled_rotator_cc_0, 0)) + self.connect((self.gsm_msg_to_tag_0_0, 0), (self.gsm_controlled_rotator_cc_0_0, 0)) + self.connect((self.low_pass_filter_0_0, 0), (self.gsm_receiver_0, 0)) + self.connect((self.uhd_usrp_source_0, 0), (self.gsm_msg_to_tag_0, 0)) + + def get_delay_correction(self): + return self.delay_correction + + def set_delay_correction(self, delay_correction): + self.delay_correction = delay_correction + self.gsm_txtime_setter_0.set_delay_correction(self.delay_correction+self.uplink_shift) + + def get_osr(self): + return self.osr + + def set_osr(self, osr): + self.osr = osr + self.gsm_gmsk_mod_0.set_sps(self.osr) + + def get_ppm(self): + return self.ppm + + def set_ppm(self, ppm): + self.ppm = ppm + self.gsm_controlled_rotator_cc_0.set_phase_inc(self.ppm/1.0e6*2*math.pi*self.rx_freq/self.samp_rate) + self.gsm_controlled_rotator_cc_0_0.set_phase_inc(-self.ppm/1.0e6*2*math.pi*self.tx_freq/self.samp_rate) + + def get_rx_freq(self): + return self.rx_freq + + def set_rx_freq(self, rx_freq): + self.rx_freq = rx_freq + self.gsm_controlled_rotator_cc_0.set_phase_inc(self.ppm/1.0e6*2*math.pi*self.rx_freq/self.samp_rate) + self.uhd_usrp_source_0.set_center_freq(self.rx_freq, 0) + + def get_rx_gain(self): + return self.rx_gain + + def set_rx_gain(self, rx_gain): + self.rx_gain = rx_gain + self.uhd_usrp_source_0.set_gain(self.rx_gain, 0) + + + def get_samp_rate(self): + return self.samp_rate + + def set_samp_rate(self, samp_rate): + self.samp_rate = samp_rate + self.gsm_controlled_rotator_cc_0.set_phase_inc(self.ppm/1.0e6*2*math.pi*self.rx_freq/self.samp_rate) + self.gsm_controlled_rotator_cc_0_0.set_phase_inc(-self.ppm/1.0e6*2*math.pi*self.tx_freq/self.samp_rate) + self.low_pass_filter_0_0.set_taps(firdes.low_pass(1, self.samp_rate, 125e3, 5e3, firdes.WIN_HAMMING, 6.76)) + self.uhd_usrp_sink_0.set_samp_rate(self.samp_rate) + self.uhd_usrp_source_0.set_samp_rate(self.samp_rate) + + def get_timing_advance(self): + return self.timing_advance + + def set_timing_advance(self, timing_advance): + self.timing_advance = timing_advance + self.gsm_txtime_setter_0.set_timing_advance(self.timing_advance) + + def get_trx_base_port(self): + return self.trx_base_port + + def set_trx_base_port(self, trx_base_port): + self.trx_base_port = trx_base_port + + def get_trx_remote_addr(self): + return self.trx_remote_addr + + def set_trx_remote_addr(self, trx_remote_addr): + self.trx_remote_addr = trx_remote_addr + + def get_tx_freq(self): + return self.tx_freq + + def set_tx_freq(self, tx_freq): + self.tx_freq = tx_freq + self.gsm_controlled_rotator_cc_0_0.set_phase_inc(-self.ppm/1.0e6*2*math.pi*self.tx_freq/self.samp_rate) + self.uhd_usrp_sink_0.set_center_freq(self.tx_freq, 0) + + def get_tx_gain(self): + return self.tx_gain + + def set_tx_gain(self, tx_gain): + self.tx_gain = tx_gain + self.uhd_usrp_sink_0.set_gain(self.tx_gain, 0) + + + def get_uplink_shift(self): + return self.uplink_shift + + def set_uplink_shift(self, uplink_shift): + self.uplink_shift = uplink_shift + self.gsm_txtime_setter_0.set_delay_correction(self.delay_correction+self.uplink_shift) + + +def argument_parser(): + parser = OptionParser(option_class=eng_option, usage="%prog: [options]") + parser.add_option( + "", "--delay-correction", dest="delay_correction", type="eng_float", default=eng_notation.num_to_str(285.616e-6), + help="Set delay_correction [default=%default]") + parser.add_option( + "", "--osr", dest="osr", type="intx", default=4, + help="Set OSR [default=%default]") + parser.add_option( + "", "--ppm", dest="ppm", type="eng_float", default=eng_notation.num_to_str(-1), + help="Set Clock offset correction [default=%default]") + parser.add_option( + "-d", "--rx-freq", dest="rx_freq", type="eng_float", default=eng_notation.num_to_str(938600000), + help="Set rx_freq [default=%default]") + parser.add_option( + "-g", "--rx-gain", dest="rx_gain", type="eng_float", default=eng_notation.num_to_str(40), + help="Set rx_gain [default=%default]") + parser.add_option( + "", "--samp-rate", dest="samp_rate", type="eng_float", default=eng_notation.num_to_str(13e6/12.0), + help="Set samp_rate [default=%default]") + parser.add_option( + "", "--timing-advance", dest="timing_advance", type="eng_float", default=eng_notation.num_to_str(0), + help="Set timing_advance [default=%default]") + parser.add_option( + "", "--trx-base-port", dest="trx_base_port", type="string", default="5700", + help="Set 5700 [default=%default]") + parser.add_option( + "", "--trx-remote-addr", dest="trx_remote_addr", type="string", default="127.0.0.1", + help="Set 127.0.0.1 [default=%default]") + parser.add_option( + "-u", "--tx-freq", dest="tx_freq", type="eng_float", default=eng_notation.num_to_str(938900000), + help="Set tx_freq [default=%default]") + parser.add_option( + "-r", "--tx-gain", dest="tx_gain", type="eng_float", default=eng_notation.num_to_str(30), + help="Set tx_gain [default=%default]") + parser.add_option( + "", "--uplink-shift", dest="uplink_shift", type="eng_float", default=eng_notation.num_to_str(-(6.0/1625000*(156.25)*3)), + help="Set uplink_shift [default=%default]") + return parser + + +def main(top_block_cls=radio_if_grc, options=None): + if options is None: + options, _ = argument_parser().parse_args() + + tb = top_block_cls(delay_correction=options.delay_correction, osr=options.osr, ppm=options.ppm, rx_freq=options.rx_freq, rx_gain=options.rx_gain, samp_rate=options.samp_rate, timing_advance=options.timing_advance, trx_base_port=options.trx_base_port, trx_remote_addr=options.trx_remote_addr, tx_freq=options.tx_freq, tx_gain=options.tx_gain, uplink_shift=options.uplink_shift) + tb.start() + tb.wait() + + +if __name__ == '__main__': + main() -- cgit v1.2.3