Module ProcessView
[hide private]
[frames] | no frames]

Source Code for Module ProcessView

  1  # -*- coding: iso-8859-1 -*- 
  2   
  3  import Tkinter 
  4   
  5  #: Global variable which stores a reference to an existing top-level window 
  6  _SingleInstance = None 
  7   
8 -def Open(system):
9 """Open as top-level window.""" 10 global _SingleInstance 11 if _SingleInstance is None: 12 _SingleInstance = Tkinter.Toplevel() 13 _SingleInstance.title("Process View") 14 _SingleInstance.bind("<Destroy>", _set_None) 15 _Window(_SingleInstance,system) 16 else: 17 _SingleInstance.deiconify()
18
19 -def Close():
20 """Close top-level window.""" 21 global _SingleInstance 22 if _SingleInstance is not None: 23 _SingleInstance.destroy()
24
25 -def _set_None(e=None):
26 """If used as top-level window, this is called at "destroy" 27 and sets our global variable to None.""" 28 global _SingleInstance 29 _SingleInstance = None
30 31
32 -def AddAsSubwidget(root,system):
33 """Add the process view as subwidget into root.""" 34 _Window(root,system)
35 36 37 import math 38
39 -class _Window(object):
40 """Shows current process state.""" 41 42 _update_time = 50 #: ms 43 _width = 3 #: radius 44 _scale_x = 50 45 _scale_length = 50 46 _canvas_width = 530 47 _canvas_height = 400 48
49 - def __init__(self,root,system):
50 self.system = system 51 self.root = root 52 self.canvas = Tkinter.Canvas(self.root,width=self._canvas_width,height=self._canvas_height) 53 l = self.system.process.l*self._scale_length 54 x = 0 55 y = 0 56 x_end = l*math.cos(math.pi/2.0) 57 y_end = l*math.sin(math.pi/2.0) 58 h = self._canvas_height 59 w = self._canvas_width 60 r = self._width 61 self.yzeroaxis = self.canvas.create_line((0,h/2,w,h/2),width=1) 62 self.xzeroaxis = self.canvas.create_line((w/2,0,w/2,h),width=1,fill="red") 63 self.pole = self.canvas.create_line((x,y,x_end,y_end),width=r*2.0,capstyle="round") 64 self.origin = self.canvas.create_oval((x-r,y-r,x+r,y+r),fill="white") 65 self.canvas.pack() 66 67 self.output = Tkinter.Text(self.root,width=80,height=1) 68 self.output.pack() 69 self.update()
70
71 - def update(self):
72 """regularly called update of visualization.""" 73 # base params of visualization 74 h = self._canvas_height 75 w = self._canvas_width 76 r = self._width 77 78 # get values 79 v = self.system.process.getStateValues() 80 x = v['X']*self._scale_x+w/2 81 phi = math.radians(v['Phi']) 82 l = self.system.process.l*self._scale_length 83 84 # graphical output 85 # xzeroline 86 if x >= w: 87 self.canvas.coords(self.xzeroaxis,1,0,1,h) 88 elif x < 0: 89 self.canvas.coords(self.xzeroaxis,w-1,0,w-1,h) 90 else: 91 self.canvas.coords(self.xzeroaxis,w/2,0,w/2,h) 92 93 # position of pendulum 94 x = x % w 95 y = h/2 96 x_end = x+l*math.cos(phi) 97 y_end = y-l*math.sin(phi) 98 self.canvas.coords(self.pole,x,y,x_end,y_end) 99 self.canvas.coords(self.origin,x-r,y-r,x+r,y+r) 100 101 # text output 102 text = "Phi:%5.1f | dPhi_dT:%6.1f | X:%6.1f | dX_dT:%6.3f\n" % (v['Phi'],v['dPhi_dT'],v['X'],v['dX_dT']) 103 self.output.insert("1.0",text) 104 self.output.delete("2.0","3.0") 105 106 # recall self after some time 107 self.root.after(self._update_time,self.update)
108