2016-02-26 55 views
1

我想爲我自己的通用塊編寫一個輸入和一個輸出,用於C++中的GNU無線電。我使用gr_modtool按照gnuradio.org中的步驟操作。它可以很好地工作。但是當我用相同的源連接其他塊(範圍sink2)時,其中沒有輸出。GNU無線電從其他塊與我自己的OOT塊並行輸出

我連接流圖作爲:

      /-> Scope Sink2 
Signal Source -> Throttle -| 
          \-> my own block -> Scope Sink1 

我使用GNU無線電播v3.7.6.1-65-g500517ac

我所創建的塊 'energy_de'。這個創建其他四個文件中: energy_de.h

#ifndef INCLUDED_CPP_ENERGY_DE_H 
#define INCLUDED_CPP_ENERGY_DE_H 

#include <cpp/api.h> 
#include <gnuradio/block.h> 

namespace gr { 
    namespace cpp { 

    /*! 
    * \brief <+description of block+> 
    * \ingroup cpp 
    * 
    */ 
    class CPP_API energy_de : virtual public gr::block 
    { 
    public: 
     typedef boost::shared_ptr<energy_de> sptr; 

     /*! 
     * \brief Return a shared_ptr to a new instance of cpp::energy_de. 
     * 
     * To avoid accidental use of raw pointers, cpp::energy_de's 
     * constructor is in a private implementation 
     * class. cpp::energy_de::make is the public interface for 
     * creating new instances. 
     */ 
     static sptr make(float makenoise); 

     virtual float noise() const = 0; 
     virtual void set_noise (float noise) = 0; 
    }; 

    } // namespace cpp 
} // namespace gr 

energy_de_impl.h

#ifndef INCLUDED_CPP_ENERGY_DE_IMPL_H 
#define INCLUDED_CPP_ENERGY_DE_IMPL_H 

#include <cpp/energy_de.h> 

namespace gr { 
    namespace cpp { 

    class energy_de_impl : public energy_de 
    { 
    private: 
     float d_noise; 

    public: 
     energy_de_impl(float noise); 
     ~energy_de_impl(); 

     // Where all the action really happens 
     void forecast (int noutput_items, gr_vector_int &ninput_items_required); 
     float noise() const { return d_noise; } 
     void set_noise(float noise) { d_noise = noise; } 

     int general_work(int noutput_items, 
       gr_vector_int &ninput_items, 
       gr_vector_const_void_star &input_items, 
       gr_vector_void_star &output_items); 
    }; 

    } // namespace cpp 
} // namespace gr 

energy_de_impl.cc

#ifdef HAVE_CONFIG_H 
#include "config.h" 
#endif 

#include <gnuradio/io_signature.h> 
#include "energy_de_impl.h" 

namespace gr { 
    namespace cpp { 

    energy_de::sptr 
    energy_de::make(float noise) 
    { 
     return gnuradio::get_initial_sptr 
     (new energy_de_impl(noise)); 
    } 

    /* 
    * The private constructor 
    */ 
    energy_de_impl::energy_de_impl(float noise) 
     : gr::block("energy_de", 
       gr::io_signature::make(1, 1, sizeof(float)), 
       gr::io_signature::make(1, 1, sizeof(float))), 
     d_noise(noise) 
    { 
    } 

    /* 
    * Our virtual destructor. 
    */ 
    energy_de_impl::~energy_de_impl() 
    { 
    } 

    void 
    energy_de_impl::forecast (int noutput_items, gr_vector_int &ninput_items_required) 
    { 
     ninput_items_required[0] = noutput_items; 
    } 

    int 
    energy_de_impl::general_work (int noutput_items, 
         gr_vector_int &ninput_items, 
         gr_vector_const_void_star &input_items, 
         gr_vector_void_star &output_items) 
    { 
     const float *in = (const float *) input_items[0]; 
     float *out = (float *) output_items[0]; 

     for(int i = 0; i < noutput_items; i++){ 
      if (in[i]*in[i] > d_noise){ 
       out[i] = 1.0; 
      } 
      else{ 
       out[i] = 0.0; 
      } 
     } 

     return noutput_items; 
    } 

    } /* namespace cpp */ 
} /* namespace gr */ 

cpp_energy_de.xml

<?xml version="1.0"?> 
<block> 
    <name>energy_de</name> 
    <key>cpp_energy_de</key> 
    <category>cpp</category> 
    <import>import cpp</import> 
    <make>cpp.energy_de($noise)</make> 
    <callback>set_niose($noise)</callback> 

    <param> 
    <name>noise</name> 
    <key>noise</key> 
    <type>float</type> 
    </param> 


    <sink> 
    <name>in</name> 
    <type>float</type> 
    </sink> 

    <source> 
    <name>out</name> 
    <type>float</type> 
    </source> 
</block> 

爲什麼我C注意從Scope Sink2獲取輸出?我忘了在四個文件中寫什麼?這是關於我的塊的input_items緩衝區的問題嗎?

回答

1

當使用普通block而非sync_block,你general_work必須呼叫consume,說明你有多少項目看,否則你自己的塊的輸入緩衝區(==油門的輸出緩衝)迅速填滿,油門可以不要把新的樣品放入它,你的流程圖停止。此時,您的範圍匯可能根本沒有足夠的輸入來顯示任何內容。

我認爲你的使用案例,只使用sync_block會容易得多,因此,這是正確的方法。

我想指您一個mail我今天寫到discuss GNU Radio mailing list。它解釋了這背後的緩衝空間概念。

   /->A->Null Sink 
File Source -| 
       \->B->File Sink 

[...]

所以下面的機理是:文件源的輸出緩衝器A的 輸入緩衝器和B的輸入緩衝區中沒有記憶的重複 這裏。

文件源有一個帶有寫指針的緩衝區寫入程序,而A和B有它們自己的指向該緩衝區的讀指針。

當文件源產生的N個項目,由N.寫指針前進

類似地,當消耗m個,A的由M.讀指針前進

致電時(general_)工作時,input_items緩衝區(s)實際上只是一個指針(start_of_buffer +讀指針) 。同樣, output_items緩衝區(s)實際上只是指向寫入 指針。

只允許文件源產生如此多的項目,使得寫指針不會超出最小讀指針,因爲在那種情況下,它將覆蓋下游塊未被消耗的採樣。

+0

非常感謝!使用'消耗'後,我收到範圍sink2的信號。 – Betty