SinGen_experiment.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/python3
  2. # This file was automatically generated from drawio2cbd with the command:
  3. # /home/red/git/DrawioConvert/__main__.py SinGen.xml -fav -F CBD -e SinGen -E delta=0.1
  4. from CBD.realtime.plotting import PlotManager, LinePlot, follow
  5. from CBD.simulator import Simulator
  6. from SinGen import *
  7. import time
  8. import matplotlib.pyplot as plt
  9. from matplotlib import style
  10. style.use('ggplot')
  11. import tkinter as tk
  12. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  13. DELTA_T = 0.1
  14. fig = plt.figure(figsize=(15, 5), dpi=100)
  15. ax = fig.add_subplot(111)
  16. ax.set_ylim((-1, 1))
  17. cbd = SinGen("SinGen")
  18. root = tk.Tk()
  19. canvas = FigureCanvasTkAgg(fig, master=root) # A Tk DrawingArea
  20. canvas.draw()
  21. canvas.get_tk_widget().grid(column=1, row=1)
  22. manager = PlotManager()
  23. manager.register("sin", cbd.findBlock("plot")[0], (fig, ax), LinePlot())
  24. manager.connect('sin', 'update', lambda d, axis=ax: axis.set_xlim(follow(d[0], 10.0, lower_bound=0.0)))
  25. # plt.show(block=False)
  26. # def term(*_):
  27. # plt.draw()
  28. # plt.pause(0.01)
  29. # return not manager.is_opened()
  30. # Run the Simulation
  31. sim_time = 100.0
  32. sim = Simulator(cbd)
  33. sim.setRealTime()
  34. sim.setProgressBar()
  35. sim.setDeltaT(DELTA_T)
  36. sim.setRealTimePlatformTk(root)
  37. # sim.setTerminationCondition(term)
  38. sim.run(sim_time)
  39. root.mainloop()
  40. # while sim.is_running():
  41. # # plt.draw()
  42. # # plt.pause(0.01)
  43. #
  44. # # Game Loop (time managing must be done by the user):
  45. # before = time.time()
  46. # sim.realtime_gameloop_call()
  47. # time.sleep(DELTA_T - (before - time.time()))
  48. # PLOT THE DURATION LOG
  49. log = sim.getDurationLog()
  50. fig2 = plt.figure()
  51. ax2 = fig2.subplots()
  52. ax2.set_title("Block Computation [Plotting Manager + TkInter] (T = {:.2f}, dt = {:.2f})".format(sim_time, DELTA_T))
  53. ax2.set_xlabel("Iterations")
  54. ax2.set_ylabel("Time")
  55. # ax2.plot([-1, len(log)], [DELTA_T, DELTA_T], c='red')
  56. # ax2.plot([-1, len(log)], [0.01, 0.01], c='green')
  57. ax2.bar(range(len(log)), log)
  58. # plt.show()