gnuradio 创建cos_source

C++教程

ys_linux@computer:~$ gr_modtool nm kcd
Creating out-of-tree module in ./gr-kcd... Done.
Use 'gr_modtool add' to add a new block to this currently empty module.
ys_linux@computer:~$ cd gr-kcd/
ys_linux@computer:~/gr-kcd$ ls
apps   CMakeLists.txt  examples  include  MANIFEST.md  swig
cmake  docs            grc       lib      python
ys_linux@computer:~/gr-kcd$ gr_modtool add my_qpsk_demod_cb
GNU Radio module name identified: tutorial
('sink', 'source', 'sync', 'decimator', 'interpolator', 'general', 'tagged_stream', 'hier', 'noblock')
Enter block type: general
Language (python/cpp): C++
Language (python/cpp): cpp
Language: C++
Block/code identifier: my_qpsk_demod_cb
Enter valid argument list, including default arguments: int freq ,float amp
Add Python QA code? [Y/n] 
Add C++ QA code? [y/N] Y
Adding file 'lib/my_qpsk_demod_cb_impl.h'...
Adding file 'lib/my_qpsk_demod_cb_impl.cc'...
Adding file 'include/tutorial/my_qpsk_demod_cb.h'...
Adding file 'lib/qa_my_qpsk_demod_cb.cc'...
Adding file 'lib/qa_my_qpsk_demod_cb.h'...
Editing swig/tutorial_swig.i...
Adding file 'python/qa_my_qpsk_demod_cb.py'...
Editing python/CMakeLists.txt...
Adding file 'grc/tutorial_my_qpsk_demod_cb.xml'...
Editing grc/CMakeLists.txt...

然后编辑代码

/* -*- c++ -*- */
/* 
 * Copyright 2017 <+YOU OR YOUR COMPANY+>.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

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

#include <gnuradio/io_signature.h>
#include "cos_source_impl.h"
#include "pmt/pmt.h"
using namespace std;


namespace gr {
  namespace kcd {

    cos_source::sptr
    cos_source::make(int freq ,float amp)
    {

      std::cout << "my_cout"<<endl;

      pmt::pmt_t P = pmt::from_long(23);
      std::cout << P << std::endl;
      pmt::pmt_t P2 = pmt::from_complex(gr_complex(0, 1)); // Alternatively: pmt::from_complex(0, 1)
      std::cout << P2 << std::endl;
      std::cout << pmt::is_complex(P2) << std::endl;

      return gnuradio::get_initial_sptr
        (new cos_source_impl(freq, amp));
    }

    /*
     * The private constructor
     */
    cos_source_impl::cos_source_impl(int freq ,float amp)
      : gr::sync_block("cos_source",
              gr::io_signature::make(0, 0, 0),
              gr::io_signature::make(1, 1, sizeof(float)))
    {}

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

    int
    cos_source_impl::work(int noutput_items,
        gr_vector_const_void_star &input_items,
        gr_vector_void_star &output_items)
    {
      // const <+ITYPE+> *in = (const <+ITYPE+> *) input_items[0];
      float *out = (float *) output_items[0];

        for (int i = 0;i<noutput_items;i++ )
        {
          // out[i] = kcd_source_sincos(i);
          if (num < 19) {
            num++;
          }
          else {
            num=0;
          }
            out[i]=cos(3.1415926535898*2*num/20);
          // out[i]=noutput_items;
        }
      // Do <+signal processing+>

      // Tell runtime system how many output items we produced.
      return noutput_items;
    }

  } /* namespace kcd */
} /* namespace gr */

头文件

/* -*- c++ -*- */
/* 
 * Copyright 2017 <+YOU OR YOUR COMPANY+>.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifndef INCLUDED_KCD_COS_SOURCE_IMPL_H
#define INCLUDED_KCD_COS_SOURCE_IMPL_H

#include <kcd/cos_source.h>

namespace gr {
  namespace kcd {

    class cos_source_impl : public cos_source
    {
     private:
      // Nothing to declare in this block.

     public:
      cos_source_impl(int freq ,float amp);
      ~cos_source_impl();
      int num=0;
      int number=0;
      // Where all the action really happens
      int work(int noutput_items,
         gr_vector_const_void_star &input_items,
         gr_vector_void_star &output_items);
    };

  } // namespace kcd
} // namespace gr

#endif /* INCLUDED_KCD_COS_SOURCE_IMPL_H */

修改xml

<?xml version="1.0"?>
<block>
  <name>cos_source</name>
  <key>kcd_cos_source</key>
  <category>[kcd]</category>
  <import>import kcd</import>
  <make>kcd.cos_source($freq, $amp)</make>
  <!-- Make one 'param' node for every Parameter you want settable from the GUI.
       Sub-nodes:
       * name
       * key (makes the value accessible as $keyname, e.g. in the make node)
       * type -->
  <param>
    <name>freq</name>
    <key>freq</key>
    <type>int</type>
  </param>
  
  <param>
    <name>amp</name>
    <key>amp</key>
    <type>float</type>
  </param>

  <!-- Make one 'sink' node per input. Sub-nodes:
       * name (an identifier for the GUI)
       * type
       * vlen
       * optional (set to 1 for optional inputs) -->
 <!--  <sink>
    <name>in</name>
    <type> e.g. int, float, complex, byte, short, xxx_vector, ...</type>
  </sink> -->

  <!-- Make one 'source' node per output. Sub-nodes:
       * name (an identifier for the GUI)
       * type
       * vlen
       * optional (set to 1 for optional inputs) -->
  <source>
    <name>out</name>
    <type>float<!-- e.g. int, float, complex, byte, short, xxx_vector, ...--></type>
  </source>
</block>
然后打包

ys_linux@computer:~/gr-tutorial$ cmake ../ $make $sudo make intall $sudo ldconfig

然后启动gnuradio

 

posted @ 2017-11-24 12:23  木心的木偶  阅读(562)  评论(0编辑  收藏  举报