Couldn't figure out how to reply from the digest, so I'm making a new post. New 
to this.


Original message/context:

==============================

From: Tellrell White


I'm currently in the process of creating a block in python that does two 
things; takes in? a certain number of input items, and once it reaches a 
certain number of input items 2) it sends a command to the UHD USRP sink block 
to adjust its gain by a certain amount.

I have a few questions. 1) Is it even possible to create a single block that 
can accomplish both these tasks? 2) How exactly do I make this block issue the 
command to adjust the gain after it receives a certain number of values??Below 
is some code that I currently have constructed for this purpose. I'm pretty new 
to python so I'm sure this code is probably not the most efficient way but any 
suggestions are welcome.


===============================


Hey Tellrell,


1) I think it can be achieved using 1 block. The block will need to output a 
message with "gain" and the gain value whenever the input matches your 
condition. Connect the output message port to the USRP sink's command port. 
Message should be a pmt that looks something like ("gain", value).


More info on the command port: 
https://gnuradio.org/doc/doxygen/page_uhd.html#uhd_command_syntax



2) I went ahead and implemented something as an example. Increases gain upto a 
limit and then starts over again. See attached code, xml for block, rxed signal 
plot, and flowgraph pic.

I'm new to this too, so hopefully other will correct my mistakes.

Regards,

AB
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# 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.
#

import numpy as np
from gnuradio import gr
import pmt

class set_gain(gr.basic_block):
    """
    docstring for block set_gain
    """
    def __init__(self, num_values, max_gain, min_gain, def_gain):
        gr.basic_block.__init__(self,
            name="set_gain",
            in_sig=[np.complex64],
            out_sig=[])
        self.message_port_register_out(pmt.intern("gain_port"))  ##name must match
                                                            ##name in XML file, I think
        self.num_values = num_values
        self.max_gain = max_gain
        self.min_gain = min_gain
        self.cur_gain = def_gain
        self.seen = 0       #keeps a count of how many samples have been seen

    def forecast(self, noutput_items, ninput_items_required):
        #setup size of input_items[i] for work call
        for i in range(len(ninput_items_required)):
            ninput_items_required[i] = noutput_items

    def general_work(self, input_items, output_items):
        in0 = input_items[0]
        self.seen+=in0.shape[0]
        if self.seen>self.num_values:
            self.cur_gain = (self.cur_gain + 1)%self.max_gain
            self.cur_gain = max(self.min_gain, self.cur_gain)
            key = pmt.to_pmt("gain")
            value = pmt.to_pmt(self.cur_gain)
            self.message_port_pub(pmt.intern('gain_port'), pmt.cons(key, value))
            self.seen = 0

        self.consume_each(in0.shape[0]) #consume everything you've account for
                                    # tell system to move on to next samples

        return 0        ##return 0 samples generated
                        ## as the block doesn't have an output stream
<?xml version="1.0"?>
<block>
  <name>set_gain</name>
  <key>channel_set_gain</key>
  <category>[channel]</category>
  <import>import channel</import>
  <make>channel.set_gain($num_values, $max_gain, $min_gain, $def_gain)</make>
  <param>
    <name>num_values</name>
    <key>num_values</key>
    <type>int</type>
  </param>
  <param>
    <name>max_gain</name>
    <key>max_gain</key>
    <type>int</type>
  </param>
  <param>
    <name>min_gain</name>
    <key>min_gain</key>
    <type>int</type>
  </param>
  <param>
    <name>def_gain</name>
    <key>def_gain</key>
    <type>int</type>
  </param>

  <sink>
    <name>in</name>
    <type>complex</type>
  </sink>

  <source>
    <name>gain_port</name>
    <type>message</type>
  </source>
</block>

Attachment: flow
Description: flow

_______________________________________________
Discuss-gnuradio mailing list
Discuss-gnuradio@gnu.org
https://lists.gnu.org/mailman/listinfo/discuss-gnuradio

Reply via email to