From 9f1489439f891a5c82b3d48999d70ac64a592488 Mon Sep 17 00:00:00 2001 From: manojgudi Date: Fri, 7 Feb 2014 16:23:36 +0530 Subject: Made Serial block as Hierarchical Block with Constant Source embedded --- gr-sbhs/python/gr_sbhs.py | 100 +++++++++++++++++++------------------- gr-serial/grc/serial.xml | 14 ++---- gr-serial/python/CMakeLists.txt | 8 +++ gr-serial/python/gr_serial.py | 57 +++++++--------------- gr-serial/python/ser_functions.py | 45 +++++++++++++++++ 5 files changed, 124 insertions(+), 100 deletions(-) create mode 100644 gr-serial/python/ser_functions.py diff --git a/gr-sbhs/python/gr_sbhs.py b/gr-sbhs/python/gr_sbhs.py index a3070962e..9f93d61b5 100644 --- a/gr-sbhs/python/gr_sbhs.py +++ b/gr-sbhs/python/gr_sbhs.py @@ -7,61 +7,61 @@ from scan_machines import * class gr_sbhs(gras.Block): - def __init__(self): - - gras.Block.__init__(self, - name="gr_sbhs", - in_sig=[numpy.float32, numpy.float32], - out_sig=[numpy.float32]) - - from scan_machines import * - print "Scanning Machines" - scan_machines() + def __init__(self): + + gras.Block.__init__(self, + name="gr_sbhs", + in_sig=[numpy.float32, numpy.float32], + out_sig=[numpy.float32]) + + from scan_machines import * + print "Scanning Machines" + scan_machines() - # SBHS init - self.new_device = Sbhs() - self.new_device.connect(1) - self.new_device.connect_device(0) + # SBHS init + self.new_device = Sbhs() + self.new_device.connect(1) + self.new_device.connect_device(0) - def set_parameters(self,window): - self.n = window - - # Check if value of window is integral of length of input source vector - # For cases like -> input = [3 , 4, 5 ,6] & window = 3 - def isIntegralWin(self, input_item, window): - if (len(input_item) % window ): - raise Exception("Value of Window should be an integral value of length of input items") - - - def work(self, input_items, output_items): - - # Assuming input_items[0] and input_items[1] have same LENGTH - for heat_items, fan_items in zip(input_items[0], input_items[1]): - - print "HEAT WRITTEN", heat_items - - # Set heat as 0 for negative values of heat - if heat_items < 0: - self.new_device.setHeat(0) - else: - self.new_device.setHeat(heat_items) + def set_parameters(self,window): + self.n = window + + # Check if value of window is integral of length of input source vector + # For cases like -> input = [3 , 4, 5 ,6] & window = 3 + def isIntegralWin(self, input_item, window): + if (len(input_item) % window ): + raise Exception("Value of Window should be an integral value of length of input items") + + + def work(self, input_items, output_items): + + # Assuming input_items[0] and input_items[1] have same LENGTH + for heat_items, fan_items in zip(input_items[0], input_items[1]): + + print "HEAT WRITTEN", heat_items + + # Set heat as 0 for negative values of heat + if heat_items < 0: + self.new_device.setHeat(0) + else: + self.new_device.setHeat(heat_items) - time.sleep(0.5) - self.new_device.setFan(fan_items) - time.sleep(0.5) - - # For Zero Temperatures - if not self.new_device.getTemp(): - raise Exception("Check SBHS connection, try re-plugging it and run scan_machines.py") + time.sleep(0.5) + self.new_device.setFan(fan_items) + time.sleep(0.5) + + # For Zero Temperatures + if not self.new_device.getTemp(): + raise Exception("Check SBHS connection, try re-plugging it and run scan_machines.py") - # Get temperature - output_items[0][:1] = self.new_device.getTemp() + # Get temperature + output_items[0][:1] = self.new_device.getTemp() - print "Temperature: ",output_items[0][:1] + print "Temperature: ",output_items[0][:1] - #Write a for loop for n_inputs - for i in range(len(input_items)): - self.consume(i,1) # Consume from port 0 input_items + #Write a for loop for n_inputs + for i in range(len(input_items)): + self.consume(i,1) # Consume from port 0 input_items - self.produce(0,self.n) # Produce from port 0 output_items + self.produce(0,self.n) # Produce from port 0 output_items diff --git a/gr-serial/grc/serial.xml b/gr-serial/grc/serial.xml index 239b4ae88..f63588d8e 100644 --- a/gr-serial/grc/serial.xml +++ b/gr-serial/grc/serial.xml @@ -10,9 +10,8 @@ gr_serial serial - from gnuradio import gr_ser - gr_ser.ser() -self.$(id).set_parameters($port, $baud, $bytesize, $parity, $stopbits) + from gnuradio.serial import gr_serial as gr_serial + gr_serial.HierBlock($port, $baud, $bytesize, $parity, $stopbits) IO Type @@ -83,14 +82,7 @@ self.$(id).set_parameters($port, $baud, $bytesize, $parity, $stopbits) --> $num_inputs > 0 $vlen > 0 - - - in - $(str($type).split('_')[0]) - $vlen - $num_inputs - - + out $(str($type).split('_')[1]) diff --git a/gr-serial/python/CMakeLists.txt b/gr-serial/python/CMakeLists.txt index e0f33d9b1..71f19c9c3 100644 --- a/gr-serial/python/CMakeLists.txt +++ b/gr-serial/python/CMakeLists.txt @@ -34,6 +34,14 @@ GR_PYTHON_INSTALL( COMPONENT "serial_python" ) +GR_PYTHON_INSTALL( + FILES + ser_functions.py + DESTINATION ${GR_PYTHON_DIR}/gnuradio/serial + COMPONENT "serial_python" +) + + ######################################################################## diff --git a/gr-serial/python/gr_serial.py b/gr-serial/python/gr_serial.py index 00b70c0d8..a7c60469e 100644 --- a/gr-serial/python/gr_serial.py +++ b/gr-serial/python/gr_serial.py @@ -1,45 +1,24 @@ -#!/usr/bin/python - import gras import numpy -# Serial is imported in __init__ -class ser(gras.Block): - +from gnuradio import gr +from gnuradio import blocks - def __init__(self): - gras.Block.__init__(self, - name="ser", - in_sig=[numpy.float32], - out_sig=[numpy.float32]) - self.n = 1 - def set_parameters(self, port, baud, bytesize, parity, stopbits): - - try: - print port - self.ser_obj = serial.Serial(port, baud, bytesize, parity, stopbits) - print("serial found on " + port ) - self.ser_obj.open() - except: - print "Couldn't Open Serial Port " + port + " Failed" - +# Source block1 import +import ser_functions +from gnuradio import blocks - def work(self, input_items, output_items): - - self.n = input_items[0][0] - out = output_items[0][:self.n] - # Input is size of output_items to be returned +class HierBlock(gr.hier_block2): + def __init__(self, port, baud, bytesize, parity, stopbits): + gr.hier_block2.__init__(self, "HierBlock", + gr.io_signature(1,1,gr.sizeof_float), + gr.io_signature(1,2,gr.sizeof_float)) - for i in range(self.n): - - # Try catch block to avoid Error - # ValueError: invalid literal for int() with base 10: '\xfe354\r\n' - try: - out[i] = int(self.ser_obj.readline()) - except: - pass - - print "OUT", out[:self.n] - - self.produce(0,len(out)) # Produce from port 0 output_items - self.consume(0,1) # Consume from port 0 input_items + # constant_block initialized + self.constant_block = gr.sig_source_f(0, gr.GR_CONST_WAVE,0,0,1) + # step_source block initialized + self.serial_source = ser_functions.ser() + self.serial_source.set_parameters(port, baud, bytesize, parity, stopbits) + + # Connect Block1 and Block2 + self.connect(self, (self.constant_block,0) , (self.serial_source,0), self) diff --git a/gr-serial/python/ser_functions.py b/gr-serial/python/ser_functions.py new file mode 100644 index 000000000..68ea18875 --- /dev/null +++ b/gr-serial/python/ser_functions.py @@ -0,0 +1,45 @@ +#!/usr/bin/python + +import gras +import numpy +import serial +class ser(gras.Block): + + + def __init__(self): + gras.Block.__init__(self, + name="ser", + in_sig=[numpy.float32], + out_sig=[numpy.float32]) + self.n = 1 + def set_parameters(self, port, baud, bytesize, parity, stopbits): + + try: + print port + self.ser_obj = serial.Serial(port, baud, bytesize, parity, stopbits) + print("serial found on " + port ) + self.ser_obj.open() + except: + print "Couldn't Open Serial Port " + port + " Failed" + + + def work(self, input_items, output_items): + + self.n = input_items[0][0] + out = output_items[0][:self.n] + # Input is size of output_items to be returned + + for i in range(self.n): + + # Try catch block to avoid Error + # ValueError: invalid literal for int() with base 10: '\xfe354\r\n' + try: + out[i] = int(self.ser_obj.readline()) + except: + pass + + print "OUT", out[:self.n] + + self.produce(0,len(out)) # Produce from port 0 output_items + self.consume(0,1) # Consume from port 0 input_items + -- cgit