summaryrefslogtreecommitdiff
path: root/gr-sbhs/python/gr_controller.py
blob: 5269148d9d08b0740280d594e6d1a9f26aebf377 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import gras
import numpy
from que import *

class sbhs_controller(gras.Block):

	def __init__(self):
		gras.Block.__init__(self,
			name="sbhs_controller",
			in_sig=[numpy.float32],
			out_sig=[numpy.float32])	
		
		# Initializing  FIFO Queues
		# Remember Total_Number(Q_Push) should be equal to Total_Number(Q_Pull)

		self.q1 = Queue(3) # Queue for Error t_0 .. t_2
		self.q2 = Queue(2) # Queue for output o_0 o_1
		
		# Initial queue values
		self.t_1 = 0
		self.o_0 = 0

	def set_parameters(self,p,i,d,a,b,f):
		self.proportional = p 
		self.integtime = i
		self.derivtime = d
		self.delt = a #del_t
		self.setpt = b #set_point
		self.n = f #window
	
	def work(self, input_items, output_items):
		
		in0 = input_items[0][0]
		out = output_items[0]
		
		# Queue structure
		#  --> |0|0|0| =>  |in0|t_1|0|
		self.q1.push(self.t_1)
		self.q1.push(in0) 
		
		# Queue Structure
		# --> |0|0|  => |self.o_0|0|
		self.q2.push(self.o_0)

		# Popping
		self.t_0 = in0
		self.t_1 = self.q1.pop()
		self.t_2 = self.q1.pop()
		
		self.o_1 = self.q2.pop()
		
		# Error propogation 
		print "E(t=t)", self.t_0
		print "E(t=t-1)", self.t_1
		print "E(t=t-2)", self.t_2
		print "-------------------"
		
		# Difference Equation
		self.o_0 = ((self.proportional * (in0 - self.t_1)
                	+(self.delt/self.integtime)*in0
			+(self.derivtime/self.delt)*(in0 - 2*self.t_1
			+self.t_2 )) + self.o_1 ) * 0.92
		
		out[:1] = self.o_0
			
		self.consume(0,1) # Consumuption from port 0 input_items
		self.consume(1,1) 
	
		self.produce(0,1) # Produce from port 0 output_items