# Variable doesnt work cycle by cycle

**URL:** <https://www.ruby-forum.com/t/variable-doesnt-work-cycle-by-cycle/232209>\
**Category:** GNU Radio\
**Created:** [September 25, 2014, 4:45am UTC](https://www.ruby-forum.com/t/variable-doesnt-work-cycle-by-cycle/232209 "2014-09-25T04:45:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [September 25, 2014, 4:45am UTC](https://www.ruby-forum.com/t/variable-doesnt-work-cycle-by-cycle/232209/1 "2014-09-25T04:45:17Z")

</div>

I’m having problem with my first created OutOfTree block in gnuradio  
with the command “gr\_modtool.py add -t general -l python”. As a first  
trial, I wanted to have an output signal that toggle every cycle. In the  
gnu radio companion, I have a signal source of 1K with a sample rate of  
20K. It is connected to a throttle with the same sample rate. Then it  
goes into my block my\_trigger2, which then goes to channel 1 of a QT GUI  
Time Sink. The channel 0 being connected at the output of the throttle.

On the QT GUI, I can see the 1 kHz signal as expected. The expected  
behavior of my signal would have been a signal that toggle every cycle  
(value 0.25 and 0.5) so 10 kHz signal. Instead, I get a signal that  
toggles every second or so, which I don’t understand why.

I also tried to generate a block as a sync\_block with the same behavior.  
If I connect the input\_items on output\_items, then I get the expected 1  
kHz signal. The problem seem to be around the pulse2 variable but I  
tried different coding tricks without any success.

Any idea what is the problem?

Here’s my python code:

#!/usr/bin/env python

# -_- coding: utf-8 -_-

# 

import numpy  
from gnuradio import gr

class my\_trigger2(gr.basic\_block):  
pulse2 = 0

```
def __init__ (self):
    gr.basic_block. __init__ (self,
        name="my_trigger2",
        in_sig=[numpy.float32],
        out_sig=[numpy.float32])

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]
    out0 = output_items[0]

    if my_trigger2.pulse2 == 0.25:
       my_trigger2.pulse2 = 0.5
    else:
       my_trigger2.pulse2 = 0.25

    output_items[0][:] = my_trigger2.pulse2
    self.consume(0, len(input_items[0]))
    return len(output_items[0])

```

The xml code is:

\<?xml version="1.0"?\> my\_trigger2 my\_blocks\_my\_trigger2 my\_blocks import my\_blocks my\_blocks.my\_trigger2() in float out float
