experiment_tk.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright 2014 Modelling, Simulation and Design Lab (MSDL) at
  2. # McGill University and the University of Antwerp (http://msdl.cs.mcgill.ca/)
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from pypdevs.simulator import Simulator
  16. try:
  17. from Tkinter import *
  18. except ImportError:
  19. from tkinter import *
  20. from trafficLightModel import *
  21. isBlinking = None
  22. model = TrafficLight(name="trafficLight")
  23. refs = {"INTERRUPT": model.INTERRUPT}
  24. root = Tk()
  25. sim = Simulator(model)
  26. sim.setRealTime(True)
  27. sim.setRealTimeInputFile(None)
  28. sim.setRealTimePorts(refs)
  29. sim.setVerbose(None)
  30. sim.setRealTimePlatformTk(root)
  31. def toManual():
  32. global isBlinking
  33. isBlinking = False
  34. sim.realtime_interrupt("INTERRUPT toManual")
  35. def toAutonomous():
  36. global isBlinking
  37. isBlinking = None
  38. sim.realtime_interrupt("INTERRUPT toAutonomous")
  39. size = 50
  40. xbase = 10
  41. ybase = 10
  42. frame = Frame(root)
  43. canvas = Canvas(frame)
  44. canvas.create_oval(xbase, ybase, xbase+size, ybase+size, fill="black", tags="red_light")
  45. canvas.create_oval(xbase, ybase+size, xbase+size, ybase+2*size, fill="black", tags="yellow_light")
  46. canvas.create_oval(xbase, ybase+2*size, xbase+size, ybase+3*size, fill="black", tags="green_light")
  47. canvas.pack()
  48. frame.pack()
  49. def updateLights():
  50. state = model.state.get()
  51. if state == "red":
  52. canvas.itemconfig("red_light", fill="red")
  53. canvas.itemconfig("yellow_light", fill="black")
  54. canvas.itemconfig("green_light", fill="black")
  55. elif state == "yellow":
  56. canvas.itemconfig("red_light", fill="black")
  57. canvas.itemconfig("yellow_light", fill="yellow")
  58. canvas.itemconfig("green_light", fill="black")
  59. elif state == "green":
  60. canvas.itemconfig("red_light", fill="black")
  61. canvas.itemconfig("yellow_light", fill="black")
  62. canvas.itemconfig("green_light", fill="green")
  63. elif state == "manual":
  64. canvas.itemconfig("red_light", fill="black")
  65. global isBlinking
  66. if isBlinking:
  67. canvas.itemconfig("yellow_light", fill="yellow")
  68. isBlinking = False
  69. else:
  70. canvas.itemconfig("yellow_light", fill="black")
  71. isBlinking = True
  72. canvas.itemconfig("green_light", fill="black")
  73. root.after(500, updateLights)
  74. b = Button(root, text="toManual", command=toManual)
  75. b.pack()
  76. c = Button(root, text="toAutonomous", command=toAutonomous)
  77. c.pack()
  78. root.after(100, updateLights)
  79. sim.simulate()
  80. root.mainloop()