experiment_tk.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. from Tkinter import *
  17. from trafficLightModel import *
  18. isBlinking = None
  19. model = TrafficLight(name="trafficLight")
  20. refs = {"INTERRUPT": model.INTERRUPT}
  21. root = Tk()
  22. sim = Simulator(model)
  23. sim.setRealTime(True)
  24. sim.setRealTimeInputFile(None)
  25. sim.setRealTimePorts(refs)
  26. sim.setVerbose(None)
  27. sim.setRealTimePlatformTk(root)
  28. def toManual():
  29. global isBlinking
  30. isBlinking = False
  31. sim.realtime_interrupt("INTERRUPT toManual")
  32. def toAutonomous():
  33. global isBlinking
  34. isBlinking = None
  35. sim.realtime_interrupt("INTERRUPT toAutonomous")
  36. size = 50
  37. xbase = 10
  38. ybase = 10
  39. frame = Frame(root)
  40. canvas = Canvas(frame)
  41. canvas.create_oval(xbase, ybase, xbase+size, ybase+size, fill="black", tags="red_light")
  42. canvas.create_oval(xbase, ybase+size, xbase+size, ybase+2*size, fill="black", tags="yellow_light")
  43. canvas.create_oval(xbase, ybase+2*size, xbase+size, ybase+3*size, fill="black", tags="green_light")
  44. canvas.pack()
  45. frame.pack()
  46. def updateLights():
  47. state = model.state.get()
  48. if state == "red":
  49. canvas.itemconfig("red_light", fill="red")
  50. canvas.itemconfig("yellow_light", fill="black")
  51. canvas.itemconfig("green_light", fill="black")
  52. elif state == "yellow":
  53. canvas.itemconfig("red_light", fill="black")
  54. canvas.itemconfig("yellow_light", fill="yellow")
  55. canvas.itemconfig("green_light", fill="black")
  56. elif state == "green":
  57. canvas.itemconfig("red_light", fill="black")
  58. canvas.itemconfig("yellow_light", fill="black")
  59. canvas.itemconfig("green_light", fill="green")
  60. elif state == "manual":
  61. canvas.itemconfig("red_light", fill="black")
  62. global isBlinking
  63. if isBlinking:
  64. canvas.itemconfig("yellow_light", fill="yellow")
  65. isBlinking = False
  66. else:
  67. canvas.itemconfig("yellow_light", fill="black")
  68. isBlinking = True
  69. canvas.itemconfig("green_light", fill="black")
  70. root.after(500, updateLights)
  71. b = Button(root, text="toManual", command=toManual)
  72. b.pack()
  73. c = Button(root, text="toAutonomous", command=toAutonomous)
  74. c.pack()
  75. root.after(100, updateLights)
  76. sim.simulate()
  77. root.mainloop()