I have an gnuradio generated python code that I am using a Flask webserver to run it through. In this webserver I have some roundSliders widgets that I am using to control the variable values of my main code. I had some issues with this program before (for more info, see this thread). But after getting some recommendations and suggestion from valued members here I think I got it working now. BUT, I can't see the light flickering/flashing of the transmitter on the USRP device, also I can't hear any audio output (I suppose to hear some static noise).
from __future__ import print_function
from gnuradio import analog
from gnuradio import audio
from gnuradio import blocks
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.filter import firdes
from optparse import OptionParser
from gnuradio import uhd
import time
class top_block_22(gr.top_block): # PEP8: CamelCaseName for classes
def __init__(self, sample_rate=32000, amplitude=0, frequency=0, bandwidth=0, gain=0, center_frequency=0, usrpsamp_rate=0):
gr.top_block.__init__(self, "Top Block 22")
##################################################
# Variables
##################################################
self.sample_rate = sample_rate
print('[top_block_22] __init__: sample_rate:', self.sample_rate)
self.amplitude = amplitude
print('[top_block_22] __init__: amplitude:', self.amplitude)
self.frequency = frequency
print('[top_block_22] __init__: frequency:', self.frequency)
self.bandwidth = bandwidth
print('[top_block_22] __init__: bandwidth:', self.bandwidth)
self.usrpsamp_rate = usrpsamp_rate
print('[top_block_22] __init__: usrpsamp_rate:', self.usrpsamp_rate)
self.gain = gain
print('[top_block_22] __init__: gain:', self.gain)
self.center_frequency = center_frequency
print('[top_block_22] __init__: center_frequency:', self.center_frequency)
##################################################
# Blocks
##################################################
self.blocks_add_xx_0 = blocks.add_vff(1)
self.audio_sink_0 = audio.sink(sample_rate, '', True)
self.analog_sig_source_x_0 = analog.sig_source_f(sample_rate, analog.GR_COS_WAVE, frequency, amplitude, 0)
self.analog_noise_source_x_0 = analog.noise_source_f(analog.GR_GAUSSIAN, 1, 0)
##################################################
# Connections
##################################################
self.connect((self.analog_noise_source_x_0, 0), (self.blocks_add_xx_0, 1))
self.connect((self.analog_sig_source_x_0, 0), (self.blocks_add_xx_0, 0))
self.connect((self.blocks_add_xx_0, 0), (self.audio_sink_0, 0))
def change_sample_rate(self, value=None):
if value is not None:
self.sample_rate = value
print('[top_block_22] change: sample_rate:', value)
self.analog_sig_source_x_0.set_sampling_freq(value)
def change_amplitude(self, value=None):
if value is not None:
value /= 10000.
self.amplitude = value
print('[top_block_22] change: amplitude:', value)
self.analog_sig_source_x_0.set_amplitude(value)
self.analog_noise_source_x_0.set_amplitude(value)
def change_frequency(self, value=None):
if value is not None:
self.frequency = value
print('[top_block_22] change: frequency:', value)
self.analog_sig_source_x_0.set_frequency(value)
#TODO: change some values
def change_bandwidth(self, value=None):
if value is not None:
self.bandwidth = value
print('[top_block_22] change: bandwidth:', value)
self.uhd_usrp_sink_0 = uhd.usrp_sink(
",".join(("type=b200", "")),
uhd.stream_args(
cpu_format="sc16",
channels=range(1),
),
)
self.uhd_usrp_sink_0.set_bandwidth(value)
self.uhd_usrp_sink_0.set_antenna('TX/RX', 0)
self.connect((self.blocks_add_xx_0, 0), (self.uhd_usrp_sink_0, 0))
def change_gain(self, value=None):
if value is not None:
self.gain = value
print('[top_block_22] change: gain:', value)
self.uhd_usrp_sink_0 = uhd.usrp_sink(
",".join(("type=b200", "")),
uhd.stream_args(
cpu_format="sc16",
channels=range(1),
),
)
self.uhd_usrp_sink_0.set_gain(value)
self.uhd_usrp_sink_0.set_antenna('TX/RX', 0)
self.connect((self.blocks_add_xx_0, 0), (self.uhd_usrp_sink_0, 0))
def change_center_frequency(self, value=None):
if value is not None:
self.center_frequency = value
print('[top_block_22] change: center_frequency:', value)
self.uhd_usrp_sink_0 = uhd.usrp_sink(
",".join(("type=b200", "")),
uhd.stream_args(
cpu_format="sc16",
channels=range(1),
),
)
self.uhd_usrp_sink_0.set_center_freq(value)
self.uhd_usrp_sink_0.set_antenna('TX/RX', 0)
self.connect((self.blocks_add_xx_0, 0), (self.uhd_usrp_sink_0, 0))
def change_usrpsamp_rate(self, value=None):
if value is not None:
self.usrpsamp_rate = value
print('[top_block_22] change: usrpsamp_rate:', value)
self.uhd_usrp_sink_0 = uhd.usrp_sink(
",".join(("type=b200", "")),
uhd.stream_args(
cpu_format="sc16",
channels=range(1),
),
)
self.uhd_usrp_sink_0.set_samp_rate(value)
self.uhd_usrp_sink_0.set_antenna('TX/RX', 0)
self.connect((self.blocks_add_xx_0, 0), (self.uhd_usrp_sink_0, 0))
def change(self, sample_rate=None, amplitude=None, frequency=None, bandwidth=None, gain=None, center_frequency=None, usrpsamp_rate=None):
if sample_rate is not None:
self.change_sample_rate(sample_rate)
if amplitude is not None:
self.change_amplitude(amplitude)
if frequency is not None:
self.change_frequency(frequency)
if bandwidth is not None:
self.change_bandwidth(bandwidth)
if gain is not None:
self.change_gain(gain)
if center_frequency is not None:
self.change_center_frequency(center_frequency)
if usrpsamp_rate is not None:
self.change_usrpsamp_rate(usrpsamp_rate)
def turn_off(self):
self.change(0, 0, 0, 0, 0, 0, 0)
# -----------------------------------------------------------------------------
from flask import Flask, request, jsonify
app = Flask(__name__)
# start with default values
#tb = top_block_22()
tb = top_block_22(32000, 0, 0, 0, 0, 0, 2e6)
tb.start(True)
@app.route('/')
def index():
return '''<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>GNURadio Slider Example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/roundSlider/1.3.2/roundslider.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/roundSlider/1.3.2/roundslider.min.js"></script>
<style>
.slider {
position: absolute;
align:center;
}
.row1 {top:100px;}
.row2 {top:450px;}
.row3 {top:750px;}
.col1 {left:75px;}
.col2 {left:470px;}
.col3 {left:870px;}
.col1 {left:75px;}
</style>
</head>
<body>
<div id="slider1" class='slider row1 col1'></div>
<!-- <p>Sample Rate Slider</p> -->
<div id="slider2" class='slider row1 col2'></div>
<!-- <p>Amplitude Slider2</p> -->
<div id="slider3" class='slider row1 col3'></div>
<!-- <p>Frequency Slider3</p> -->
<div id="slider4" class='slider row2 col1'></div>
<!-- <p>Bandwidth Slider4</p> -->
<div id="slider5" class='slider row2 col2'></div>
<!-- <p>Gain Slider5</p> -->
<div id="slider6" class='slider row2 col3'></div>
<!-- <p>Center Frequency Slider6</p> -->
<div id="slider7" class='slider row3 col1'></div>
<!-- <p>USRP Sample Rate Slider7</p> -->
<button id="turn_off_button">TURN OFF</button>
<script>
// create sliders
$("#slider1").roundSlider({
//sliderType: "min-range",
radius: 150,
min: 0,
max: 1000000000000,
value: 20000, // default value at start
//change: function(event) { $.getJSON('/valueofslider', {slide1_val: event.value}); }
change: function(event) { $.getJSON('/set_sample_rate/' + event.value); }
});
$("#slider2").roundSlider({
//sliderType: "min-range",
radius: 150,
min: 0,
max: 1000,
value: 444, // default value at start
//change: function(event) { $.getJSON('/valueofslider', {slide2_val: event.value}); }
change: function(event) { $.getJSON('/set_amplitude/' + event.value); }
});
$("#slider3").roundSlider({
radius: 150,
min: 0,
max: 10000000000,
value: 350, // default value at start
//change: function(event) { $.getJSON('/valueofslider', {slide3_val: event.value}); }
change: function(event) { $.getJSON('/set_frequency/' + event.value); }
});
$("#slider4").roundSlider({
radius: 150,
min: 0,
max: 10000,
value: 75000, // default value at start
//change: function(event) { $.getJSON('/valueofslider', {slide4_val: event.value}); }
change: function(event) { $.getJSON('/set_bandwidth/' + event.value); }
});
$("#slider5").roundSlider({
radius: 150,
min: 0,
max: 10000000000,
value: 50, // default value at start
//change: function(event) { $.getJSON('/valueofslider', {slide5_val: event.value}); }
change: function(event) { $.getJSON('/set_gain/' + event.value); }
});
$("#slider6").roundSlider({
radius: 150,
min: 0,
max: 10000000000,
value: 880000000, // default value at start
//change: function(event) { $.getJSON('/valueofslider', {slide6_val: event.value}); }
change: function(event) { $.getJSON('/set_center_frequency/' + event.value); }
});
$("#slider7").roundSlider({
radius: 150,
min: 0,
max: 10000000000,
value: 16000000, // default value at start
//change: function(event) { $.getJSON('/valueofslider', {slide7_val: event.value}); }
change: function(event) { $.getJSON('/set_usrpsamp_rate/' + event.value); }
});
$("#turn_off_button").click(function(){
// set sliders
$("#slider1").data("roundSlider").setValue(0);
$("#slider2").data("roundSlider").setValue(0);
$("#slider3").data("roundSlider").setValue(0);
$("#slider4").data("roundSlider").setValue(0);
$("#slider5").data("roundSlider").setValue(0);
$("#slider6").data("roundSlider").setValue(0);
$("#slider7").data("roundSlider").setValue(0);
// send to server
$.getJSON('/valueofslider', {
slide1_val: 0,
slide2_val: 0,
slide3_val: 0,
slide4_val: 0,
slide5_val: 0,
slide6_val: 0,
slide7_val: 0,
});
});
</script>
</body>
</html>'''
@app.route('/off')
def off():
"""Turn off sound."""
tb.turn_off()
#return jsonify({'val': 0})
return 'off'
@app.route('/set_sample_rate/<int:value>')
def set_sample_rate(value):
#sound(sample_rate=value)
tb.change_sample_rate(value)
#return jsonify({'sample_rate': value})
return str(value)
@app.route('/set_amplitude/<int:value>')
def set_amplitude(value):
#sound(amplitude=value)
tb.change_amplitude(value)
#return jsonify({'amplitude': value})
return str(value)
@app.route('/set_frequency/<int:value>')
def set_frequency(value):
#sound(frequency=value)
tb.change_frequency(value)
#return jsonify({'frequency': value})
return str(value)
@app.route('/set_bandwidth/<int:value>')
def set_bandwidth(value):
#sound(frequency=value)
tb.change_bandwidth(value)
#return jsonify({'frequency': value})
return str(value)
@app.route('/set_gain/<int:value>')
def set_gain(value):
#sound(frequency=value)
tb.change_gain(value)
#return jsonify({'frequency': value})
return str(value)
@app.route('/set_center_frequency/<int:value>')
def set_center_frequency(value):
#sound(frequency=value)
tb.change_center_frequency(value)
#return jsonify({'frequency': value})
return str(value)
@app.route('/set_usrpsamp_rate/<int:value>')
def set_usrpsamp_rate(value):
#sound(frequency=value)
tb.change_usrpsamp_rate(value)
#return jsonify({'frequency': value})
return str(value)
@app.route('/valueofslider')
def slide():
sample_rate = request.args.get('slide1_val', None)
amplitude = request.args.get('slide2_val', None)
frequency = request.args.get('slide3_val', None)
bandwidth = request.args.get('slide4_val', None)
gain = request.args.get('slide5_val', None)
center_frequency = request.args.get('slide6_val', None)
usrpsamp_rate = request.args.get('slide7_val', None)
sound(
sample_rate=sample_rate,
amplitude=amplitude,
frequency=frequency,
bandwidth=bandwidth,
gain=gain,
center_frequency=center_frequency,
usrpsamp_rate=usrpsamp_rate,
)
#return jsonify({
# 'sample_rate': sample_rate,
# 'amplitude': amplitude,
# 'frequency ': frequency ,
# 'bandwidth': bandwidth,
# 'gain': gain,
# 'center_frequency ': center_frequency ,
# 'usrpsamp_rate ': usrpsamp_rate ,
# })
return 'sample_rate: {}, amplitude: {}, frequency: {}, bandwidth: {}, gain: {}, center_frequency: {}, usrpsamp_rate: {}'.format(sample_rate, amplitude, frequency, bandwidth )
def sound(sample_rate=None, amplitude=None, frequency=None, bandwidth=None, gain=None, center_frequency=None, usrpsamp_rate=None):
"""version which uses `change()`"""
if sample_rate is not None:
sample_rate = int(sample_rate)
tb.change_sample_rate(sample_rate)
if amplitude is not None:
amplitude = int(amplitude)
tb.change_amplitude(amplitude)
if frequency is not None:
frequency = int(frequency )
tb.change_frequency(frequency )
if bandwidth is not None:
bandwidth = int(bandwidth )
tb.change_bandwidth(bandwidth )
if gain is not None:
gain = int(gain )
tb.change_gain(gain )
if center_frequency is not None:
center_frequency = int(center_frequency )
tb.change_center_frequency(center_frequency )
if usrpsamp_rate is not None:
usrpsamp_rate = int(usrpsamp_rate )
tb.change_usrpsamp_rate(usrpsamp_rate )
print('[sound] sample_rate:', sample_rate)
print('[sound] amplitude:', amplitude)
print('[sound] frequency:', frequency)
print('[sound] bandwidth:', bandwidth)
print('[sound] gain:', gain)
print('[sound] center_frequency:', center_frequency)
print('[sound] center_frequency:', usrpsamp_rate)
#if tb: # if tb is not None
# tb.change(sample_rate, amplitude, frequency, bandwidth, gain, center_frequency, usrpsamp_rate)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
And here is the output report:
linux; GNU C++ version 7.3.0; Boost_106501; UHD_003.010.003.000-0-unknown
[top_block_22] __init__: sample_rate: 32000
[top_block_22] __init__: amplitude: 0
[top_block_22] __init__: frequency: 0
[top_block_22] __init__: bandwidth: 0
[top_block_22] __init__: usrpsamp_rate: 2000000.0
[top_block_22] __init__: gain: 0
[top_block_22] __init__: center_frequency: 0
gr::log :INFO: audio source - Audio sink arch: alsa
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
linux; GNU C++ version 7.3.0; Boost_106501; UHD_003.010.003.000-0-unknown
[top_block_22] __init__: sample_rate: 32000
[top_block_22] __init__: amplitude: 0
[top_block_22] __init__: frequency: 0
[top_block_22] __init__: bandwidth: 0
[top_block_22] __init__: usrpsamp_rate: 2000000.0
[top_block_22] __init__: gain: 0
[top_block_22] __init__: center_frequency: 0
gr::log :INFO: audio source - Audio sink arch: alsa
* Debugger is active!
* Debugger PIN: 138-727-058
127.0.0.1 - - [12/Aug/2019 16:35:59] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [12/Aug/2019 16:36:07] "GET /set_sample_rate/33333 HTTP/1.1" 200 -
127.0.0.1 - - [12/Aug/2019 16:36:12] "GET /set_amplitude/433 HTTP/1.1" 200 -
127.0.0.1 - - [12/Aug/2019 16:36:17] "GET /set_frequency/355 HTTP/1.1" 200 -
[top_block_22] change: sample_rate: 33333
[top_block_22] change: amplitude: 0.0433
[top_block_22] change: frequency: 355
[top_block_22] change: bandwidth: 1000
-- Detected Device: B200mini
-- Operating over USB 2.
-- Initialize CODEC control...
-- Initialize Radio control...
-- Performing register loopback test... pass
-- Performing CODEC loopback test... pass
-- Setting master clock rate selection to 'automatic'.
-- Asking for clock rate 16.000000 MHz...
-- Actually got clock rate 16.000000 MHz.
-- Performing timer loopback test... pass
127.0.0.1 - - [12/Aug/2019 16:36:33] "GET /set_bandwidth/1000 HTTP/1.1" 200 -
127.0.0.1 - - [12/Aug/2019 16:36:38] "GET /set_gain/55 HTTP/1.1" 200 -
127.0.0.1 - - [12/Aug/2019 16:36:59] "GET /set_center_frequency/870000000 HTTP/1.1" 200 -
[top_block_22] change: gain: 55
[top_block_22] change: center_frequency: 870000000
[top_block_22] change: usrpsamp_rate: 15900000
-- Asking for clock rate 15.900000 MHz...
-- Actually got clock rate 15.900000 MHz.
-- Performing timer loopback test... pass
127.0.0.1 - - [12/Aug/2019 16:37:09] "GET /set_usrpsamp_rate/15900000 HTTP/1.1" 200 -