ValueError:至少需要两个终结点才能断开连接

时间:2019-09-09 14:26:56

标签: python gnuradio-companion

我有一个烧瓶Web服务器中托管的gnuradio python代码。该程序由许多产生信号或噪声的块/源组成...该程序可以正常工作,直到我向其中添加了一些代码并开始了标题中提到的错误。之所以添加新的代码行,是因为最初我的程序一旦运行,则所有信号源和噪声源将同时工作。但是我想控制一个源是打开的,哪个源是关闭的。因此,在有关编码的基础非常薄弱的​​基础上,再加上有价值的成员的一些建议,我尽力了,并在python区域内编写了连接线,然后在JS中添加了带有函数的按钮,并写了一些我认为可以称为在烧瓶中起作用。

现在我运行全部代码时,出现ValueError: At least two endpoints required for disconnect错误。

说实话,我不确定我添加的行是否有助于实现我的目标。

因此,我发布此线程是希望你们能引导我解决问题。

程序代码:

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


class TopBlock22(gr.top_block): # PEP8: CamelCaseName for classes

    def __init__(self, sample_rate=32000, amplitude=400, frequency=350):

        gr.top_block.__init__(self, "Top Block 22")
        ##################################################
        # Variables
        ##################################################
        self.sample_rate = sample_rate
        print('[TopBlock22] __init__: sample_rate:', self.sample_rate)

        self.amplitude = amplitude
        print('[TopBlock22] __init__: amplitude:', self.amplitude)

        self.frequency = frequency
        print('[TopBlock22] __init__: frequency:', self.frequency)


        self.analog_sig_source_x_0 = analog_sig_source_x_0 = False

        self.analog_noise_source_x_0 = analog_noise_source_x_0 = False


        if self.analog_sig_source_x_0:
         self.connect(analog_sig_source_x_0)

        else:

            self.disconnect(analog_sig_source_x_0)


        if self.analog_noise_source_x_0:
         self.connect(analog_noise_source_x_0)

        else:

            self.disconnect(analog_noise_source_x_0)

        ##################################################
        # Blocks
        ##################################################
        self.blocks_add_xx = blocks.add_vff(1)
        self.audio_sink = audio.sink(32000, '', True)
        self.analog_sig_source_x_1 = analog.sig_source_f(sample_rate, analog.GR_COS_WAVE, 440,  amplitude, 0)
        self.analog_sig_source_x_0 = analog.sig_source_f(sample_rate, analog.GR_COS_WAVE, 350, amplitude, 0)
        self.analog_noise_source_x_0 = analog.noise_source_f(analog.GR_GAUSSIAN,  amplitude, -42)

        ##################################################
        # Connections
        ##################################################
        self.connect((self.analog_noise_source_x_0, 0), (self.blocks_add_xx, 2))
        self.connect((self.analog_sig_source_x_0, 0), (self.blocks_add_xx, 0))
        self.connect((self.analog_sig_source_x_1, 0), (self.blocks_add_xx, 1))
        self.connect((self.blocks_add_xx, 0), (self.audio_sink, 0))

    def change_sample_rate(self, value=None):
        if value is not None:
            self.sample_rate = value
            print('[TopBlock22] change: sample_rate:', value)
            self.analog_sig_source_x_1.set_sampling_freq(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('[TopBlock22] change: amplitude:', value)
            self.analog_sig_source_x_1.set_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('[TopBlock22] change: frequency:', value)
            #TODO: change some values

    def change(self, sample_rate=None, amplitude=None, frequency=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)

    def turn_off(self):
        self.change(0, 0, 0, False, False)



# -----------------------------------------------------------------------------

from flask import Flask, request, jsonify

app = Flask(__name__)

tb = TopBlock22(0, 0, 0)
tb.start()


@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;}

.col1 {left:75px;}
.col2 {left:470px;}
.col3 {left:870px;}

</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>Slider4</p>  -->

<div id="slider5" class='slider row2 col2'></div>
<!-- <p>Slider5</p>  -->

<div id="slider6" class='slider row2 col3'></div>
<!-- <p>Slider6</p>  -->

<button id="turn_on_button1">Turn On Signal Source</button>
<button id="turn_on_button2">TURN On Noise Source</button>

<script>

  // create sliders

  $("#slider1").roundSlider({
    radius: 150,
    min: 0,
    max: 10000,
    value: 0, // default value at start
    //change: function(event) { $.getJSON('/valueofslider', {slide1_val: event.value}); }
    change: function(event) { $.getJSON('/set_sample_rate/' + event.value); }
  });

  $("#slider2").roundSlider({
    radius: 150,
    min: 0,
    max: 10000,
    value: 0, // 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: 0, // 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: 10000000000,
    value: 0, // default value at start
    change: function(event) { $.getJSON('/valueofslider', {slide4_val: event.value}); }
  });

  $("#slider5").roundSlider({
    radius: 150,
    min: 0,
    max: 10000000000,
    value: 0, // default value at start
    change: function(event) { $.getJSON('/valueofslider', {slide5_val: event.value}); }
  });

  $("#slider6").roundSlider({
    radius: 150,
    min: 0,
    max: 10000000000,
    value: 0, // default value at start
    change: function (event) { $.getJSON('/valueofslider', {slide6_val: event.value}); }
  });

  $("#enable_button1").click(function(){

      $("#turn_on_button1").data("Sources_Control").setAnalog_sig_source_x_0(False);

      // send to server
      $.getJSON('/Sources_Control', {
        analog_sig_source_x_0 = True,

      });
    $("#enable_button2").click(function(){

      $("#turn_on_button2").data("Sources_Control").setAnalog_noise_source_x_0(False);

      // send to server
      $.getJSON('/Sources_Control', {
        analog_noise_source: True,

      });  
  });

</script>

</body>
</html>'''

@app.route('/off')
def off():
    tb.turn_off()
    return 'off'

@app.route('/set_sample_rate/<int:value>')
def set_sample_rate(value):
    tb.change_sample_rate(value)
    return str(value)

@app.route('/set_amplitude/<int:value>')
def set_amplitude(value):
    tb.change_amplitude(value)
    return str(value)

@app.route('/set_frequency/<int:value>')
def set_frequency(value):
    tb.change_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)

    sound(
        sample_rate=sample_rate, 
        amplitude=amplitude, 
        frequency=frequency,
    )

    return 'sample_rate: {}, amplitude: {}, frequency : {}'.format(sample_rate, amplitude, frequency )

@app.route('/Sources_Control')
def sources():
    if enable_button1 == True:
        analog_noise_source_x_0 = self.connect(analog_sig_source_x_0)
        print("signal source is connected")

    if enable_button2 == True:
        analog_noise_source_x_0 = self.connect(analog_noise_source_x_0)
        print("analog noise source is connected")


def sound(sample_rate=None, amplitude=None, frequency=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 )

    print('[sound] sample_rate:', sample_rate)
    print('[sound] amplitude:', amplitude)
    print('[sound] frequency:', frequency)


if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

错误回溯:

[TopBlock22] __init__: sample_rate: 0
[TopBlock22] __init__: amplitude: 0
[TopBlock22] __init__: frequency: 0
Traceback (most recent call last):
  File "/home/fit-pc/Desktop/calling function practice/app-D.py", line 111, in <module>
    tb = TopBlock22(0, 0, 0)
  File "/home/fit-pc/Desktop/calling function practice/app-D.py", line 40, in __init__
    self.disconnect(analog_sig_source_x_0)
  File "/usr/lib/python2.7/dist-packages/gnuradio/gr/hier_block2.py", line 37, in wrapped
    raise ValueError("At least two endpoints required for " + func.__name__)
ValueError: At least two endpoints required for disconnect
[Finished in 0.7s with exit code 1]
[shell_cmd: python -u "/home/fit-pc/Desktop/calling function practice/app-D.py"]
[dir: /home/fit-pc/Desktop/calling function practice]
[path: /home/fit-pc/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]

错误位置:

error image-1

至于新添加的代码行是:

此处:

added code-1

在那里:

added code-2

更新

我对代码的连接/断开线做了一些更改,如下所示,然后开始出现另一个错误:

self.analog_sig_source_x_0 = analog_sig_source_x_0 = False

self.analog_noise_source_x_0 = analog_noise_source_x_0 = False

self.blocks_add_xx = blocks_add_xx  = True

self.blocks_add_xx = blocks_add_xx = True


if self.analog_sig_source_x_0 == True:
 self.connect((self.analog_sig_source_x_0, 0), (self.blocks_add_xx, 0))

else:

    self.disconnect((self.analog_sig_source_x_0, 0), (self.blocks_add_xx, 0))


if self.analog_noise_source_x_0 == True:
 self.connect((self.analog_noise_source_x_0, 0), (self.blocks_add_xx, 1))

else:

    self.disconnect((self.analog_noise_source_x_0, 0), (self.blocks_add_xx, 1))

##################################################
# Blocks
##################################################
self.blocks_add_xx = blocks.add_vff(1)
self.audio_sink = audio.sink(32000, '', True)
self.analog_sig_source_x_0 = analog.sig_source_f(sample_rate, analog.GR_COS_WAVE, 350, amplitude, 0)
self.analog_noise_source_x_0 = analog.noise_source_f(analog.GR_GAUSSIAN,  amplitude, -42)

##################################################
# Connections
##################################################
self.connect((self.analog_noise_source_x_0, 0), (self.blocks_add_xx, 1))
self.connect((self.analog_sig_source_x_0, 0), (self.blocks_add_xx, 0))
self.connect((self.blocks_add_xx, 0), (self.audio_sink, 0))

新错误为ValueError: Unable to coerce endpoints: 'bool' object has no attribute 'to_basic_block'

[TopBlock22] __init__: sample_rate: 0
[TopBlock22] __init__: amplitude: 0
[TopBlock22] __init__: frequency: 0
Traceback (most recent call last):
  File "/home/fit-pc/Desktop/calling function practice/app-D.py", line 113, in <module>
    tb = TopBlock22(0, 0, 0)
  File "/home/fit-pc/Desktop/calling function practice/app-D.py", line 44, in __init__
    self.disconnect((self.analog_sig_source_x_0, 0), (self.blocks_add_xx, 0))
  File "/usr/lib/python2.7/dist-packages/gnuradio/gr/hier_block2.py", line 44, in wrapped
    raise ValueError("Unable to coerce endpoints: " + str(err))
ValueError: Unable to coerce endpoints: 'bool' object has no attribute 'to_basic_block'
[Finished in 0.7s with exit code 1]
[shell_cmd: python -u "/home/fit-pc/Desktop/calling function practice/app-D.py"]
[dir: /home/fit-pc/Desktop/calling function practice]
[path: /home/fit-pc/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]

新错误的地方:

image of new error location

0 个答案:

没有答案