dashboard.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/python3
  2. from CBD.CBD import CBD
  3. from CBD.lib.std import *
  4. from CBD.lib.endpoints import SignalCollectorBlock
  5. from CBD.realtime.plotting import PlotManager, LinePlot, follow
  6. from CBD.simulator import Simulator
  7. # import time
  8. import matplotlib.pyplot as plt
  9. import tkinter as tk
  10. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  11. DELTA_T = 0.1
  12. class SinGen(CBD):
  13. def __init__(self, block_name):
  14. CBD.__init__(self, block_name, input_ports=[], output_ports=[])
  15. # Create the Blocks
  16. self.addBlock(GenericBlock("sin", block_operator=("sin")))
  17. self.addBlock(TimeBlock("time"))
  18. self.addBlock(SignalCollectorBlock("plot"))
  19. self.addBlock(ConstantBlock("A", 1.0))
  20. self.addBlock(ConstantBlock("B", 1.0))
  21. self.addBlock(ProductBlock("amp"))
  22. self.addBlock(ProductBlock("per"))
  23. # Create the Connections
  24. self.addConnection("B", "per")
  25. self.addConnection("time", "per")
  26. self.addConnection("per", "sin")
  27. self.addConnection("A", "amp")
  28. self.addConnection("sin", "amp")
  29. self.addConnection("amp", "plot")
  30. fig = plt.figure(figsize=(15, 5), dpi=100)
  31. ax = fig.add_subplot(111)
  32. ax.set_ylim((-1, 1))
  33. cbd = SinGen("SinGen")
  34. root = tk.Tk()
  35. canvas = FigureCanvasTkAgg(fig, master=root) # A Tk DrawingArea
  36. canvas.draw()
  37. canvas.get_tk_widget().grid(column=1, row=1)
  38. manager = PlotManager()
  39. manager.register("sin", cbd.findBlock("plot")[0], (fig, ax), LinePlot())
  40. manager.connect('sin', 'update', lambda d, axis=ax: axis.set_xlim(follow(d[0], 10.0, lower_bound=0.0)))
  41. # manager.connect('sin', 'update', lambda d, axis=ax: axis.set_ylim((min(d[1]), max(d[1]))))
  42. manager.connect('sin', 'update', lambda d, axis=ax: axis.set_ylim(follow(d[1], lower_lim=-1.0, upper_lim=1.0)))
  43. label = tk.Label(root, text="y = 1.00 * sin(1.00 * t)")
  44. label.grid(column=1, row=2)
  45. def set_amplitude(val):
  46. cbd.findBlock("A")[0].setValue(float(val))
  47. update_label()
  48. def set_period(val):
  49. cbd.findBlock("B")[0].setValue(float(val))
  50. update_label()
  51. def update_label():
  52. label["text"] = "y = {:.2f} * sin({:.2f} * t)".format(cbd.findBlock("A")[0].getValue(),
  53. cbd.findBlock("B")[0].getValue())
  54. amplitude = tk.Scale(root, label="Amplitude", length=1200, orient=tk.HORIZONTAL, from_=0, to=5, resolution=0.1,
  55. command=set_amplitude)
  56. amplitude.set(1.0)
  57. amplitude.grid(column=1, row=3)
  58. period = tk.Scale(root, label="Period", length=1200, orient=tk.HORIZONTAL, from_=0, to=5, resolution=0.1,
  59. command=set_period)
  60. period.set(1.0)
  61. period.grid(column=1, row=4)
  62. # Run the Simulation
  63. # sim_time = 100.0
  64. sim = Simulator(cbd)
  65. sim.setRealTime()
  66. # sim.setProgressBar()
  67. sim.setDeltaT(DELTA_T)
  68. sim.setRealTimePlatformTk(root)
  69. sim.run()
  70. root.mainloop()